Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please modify the following code to NOT have TicTacToe extends Board (I don't want any extending). Please ensure the resulting code completes EACH part of

Please modify the following code to NOT have "TicTacToe extends Board" (I don't want any extending). Please ensure the resulting code completes EACH part of the following prompt:

"Your task is to create a game of Tic-Tac-Toe using a 2-Dimensional String array as the game board. Start by creating a Board class that holds the array. The constructor should use a traditional for-loop to fill the array with "blank" Strings (eg. "-"). You may want to include other instance data... Include the follwoing methods: public void setSpace(int row, int col, String s) will change a specific space to have either "X" or "O", but only if the space is available. public boolean hasThree(String s) will check to see if there are 3 in a row, column, or diagonal. public String toString() will generate a String that displays the Board with appropriate formatting. Create a driver class called TicTacToe that instantiates a Board object and allows 2 players to go head to head in a the game."

public class Board { //creating board String board[][]=new String[3][3]; //construction to fill the board with null string "-" public Board() { for(int i=0;i<3;i++) for(int j=0;j<3;j++) board[i][j]="-"; } }

//creating class and extending board class public class TicTacToe extends Board{ //declaring variable boolean flag=false; static boolean plyr=true; //printing the board public String toString() { String result = "";

result += " | | "; result += " " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " "; result += "_____|_____|_____ "; result += " | | "; result += " " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " "; result += "_____|_____|_____ "; result += " | | "; result += " " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " "; result += " | | ";

return result; } //creating main function public static void main(String args[]){ //declaring variable int row,col,choice; //initilizing empty symbol String symb=""; //scanner object to get input Scanner in=new Scanner(System.in); //creating tictactoe object TicTacToe ttt=new TicTacToe(); //loop to get the input from the players do{ //printing option for the player System.out.println("1.Enter the X or O"); System.out.println("2.View Board"); System.out.println("0.Exit"); System.out.print("Select an option(0,1,2): "); //geting player response choice=in.nextInt(); if(choice==1){ //checking if the board is full then print msg and terminate the program if(!ttt.isFull()){ System.out.println("Board full"); break; } //Checking which player is about to play if(plyr==true){ System.out.println("Player 1 turn"); //assigning symbol for player 1 symb="X"; //changing the player for next turn plyr=!plyr; } else{ System.out.println("Player 2 turn"); //assigning symbol for player 1 symb="O"; //changing the player for next turn plyr=!plyr; } //asking player for location to insert symbol System.out.print("Enter row no: "); row=in.nextInt(); System.out.print("Enter column no: "); col=in.nextInt(); //inserting the symbol in the board ttt.setSpace(row,col,symb); } else if(choice==2){ //printing the board System.out.println(ttt.toString()); } else{ //informing user that selected option is invalid System.out.println("Selected option is invalid"); } //checking for winning situation if(ttt.hasThree(symb)==true){ System.out.println("WON!!!"); break; } //if choice is 0 then cancel the game }while(choice!=0); } //function to check that board is full boolean isFull(){ for(int i=0;i<3;i++) for(int j=0;j<3;j++) if(board[i][j].equals("-")) flag=true; return flag; } //inserting the sybol to the board void setSpace(int row, int col, String s){ //insert if the location in board is empty or show the massage to player if(board[row][col].equals("-")) board[row][col]=s; else{ System.out.println("The space is already filled choose another"); plyr=!plyr; } } //checking for winning situation boolean hasThree(String s){ boolean win=false; //checking for rows winning situation if((board[0][0].equals(s)) && (board[0][1].equals(s)) && (board[0][2].equals(s))) win=true; else if((board[1][0].equals(s)) && (board[1][1].equals(s)) && (board[1][2].equals(s))) win=true; else if((board[2][0].equals(s)) && (board[2][1].equals(s)) && (board[2][2].equals(s))) win=true; //checking for column winning situation else if((board[0][0].equals(s)) && (board[1][0].equals(s)) && (board[2][0].equals(s))) win=true; else if((board[0][1].equals(s)) && (board[1][1].equals(s)) && (board[2][1].equals(s))) win=true; else if((board[0][2].equals(s)) && (board[1][2].equals(s)) && (board[2][2].equals(s))) win=true; //checking for diagonal winning situation else if((board[0][0].equals(s)) && (board[1][1].equals(s)) && (board[2][2].equals(s))) win=true; else if((board[0][2].equals(s)) && (board[1][1].equals(s)) && (board[2][0].equals(s))) win=true; else win=false; return win; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

10. Describe the relationship between communication and power.

Answered: 1 week ago