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 += " | | \n";
result += " " + board[0][0] + " | " + board[0][1] + " | " +
board[0][2] + "\n";
result += "_____|_____|_____\n";
result += " | | \n";
result += " " + board[1][0] + " | " + board[1][1] + " | " +
board[1][2] + "\n";
result += "_____|_____|_____\n";
result += " | | \n";
result += " " + board[2][0] + " | " + board[2][1] + " | " +
board[2][2] + "\n";
result += " | | \n";
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;
}
}
////creating class and extending board class
//TicTacToe.java
/*The java class TicTacToe that do not extends
* any Board class and aloows user to enter
* row and column numbers and allows to view
* board and allows to exit from the program.*/
import java.util.Scanner;
public class TicTacToe
{
//Modificaiton :
//include string array
//creating board
private String board[][]=new String[3][3];
//declaring variable
private boolean flag=false;
private static boolean plyr=true;
//Modificaiton
//create a constructor of TicTacToe
//to initialize the board array
/*construction to fill the board with null string
"-"*/
public TicTacToe()
{
for(int i=0;i<3;i++)
for(int
j=0;j<3;j++)
board[i][j]="-";
}
//Modification
//Align the board neatly to show
//board values
//printing the board
public String toString()
{
String result = "";
result += " " + board[0][0] + " | "
+ board[0][1] + " | " + board[0][2] + "\n";
result += " " + board[1][0] + " | "
+ board[1][1] + " | " + board[1][2] + "\n";
result += " " + board[2][0] + " | "
+ board[2][1] + " | " + board[2][2] + "\n";
return result;
}
//creating main function
public static void main(String args[]){
//declaring variable
int row,col,choice;
//Initializing 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 symbol 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;
}
}
Sample Output:
1.Enter the X or O
2.View Board
0.Exit
Select an option(0,1,2): 1
Player 1 turn
Enter row no: 0
Enter column no: 0
1.Enter the X or O
2.View Board
0.Exit
Select an option(0,1,2): 2
X | - | -
- | - | -
- | - | -
1.Enter the X or O
2.View Board
0.Exit
Select an option(0,1,2): 1
Player 2 turn
Enter row no: 0
Enter column no: 1
1.Enter the X or O
2.View Board
0.Exit
Select an option(0,1,2): 2
X | O | -
- | - | -
- | - | -
1.Enter the X or O
2.View Board
0.Exit
Select an option(0,1,2): 1
Player 1 turn
Enter row no: 1
Enter column no: 0
1.Enter the X or O
2.View Board
0.Exit
Select an option(0,1,2): 2
X | O | -
X | - | -
- | - | -
1.Enter the X or O
2.View Board
0.Exit
Select an option(0,1,2): 1
Player 2 turn
Enter row no: 2
Enter column no: 2
1.Enter the X or O
2.View Board
0.Exit
Select an option(0,1,2): 2
X | O | -
X | - | -
- | - | O
1.Enter the X or O
2.View Board
0.Exit
Select an option(0,1,2): 1
Player 1 turn
Enter row no: 2
Enter column no: 0
WON!!!
Please modify the following code to NOT have "TicTacToe extends Board" (I don't want any extending)....
This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe { private char currentPlayer; private char[][] board; private char winner; /** * Default constructor * Initializes the board to be 3 by 3 and...
JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe { private final int BOARDSIZE = 3; // size of the board private enum Status { WIN, DRAW, CONTINUE }; // game states private char[][] board; // board representation private boolean firstPlayer; // whether it's player 1's move private boolean gameOver; // whether...
This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...
Please I need your help I have the code below but I need to add one thing, after player choose "No" to not play the game again, Add a HELP button or page that displays your name, the rules of the game and the month/day/year when you finished the implementation. import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; import java.util.Random; public class TicTacToe extends JFrame implements ChangeListener, ActionListener { private JSlider slider; private JButton oButton, xButton; private Board...
Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...
Make the Sudoku algorithm for checking for duplicates to be Big -O of N, instead of N-squared. I.E. No nested for loops in rowIsLatin and colIsLatin. Only nested loop allowed in goodSubsquare. public class Sudoku { public String[][] makeSudoku(String s) { int SIZE = 9; int k = 0; String[][] x = new String[SIZE][SIZE]; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++)...
JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 The program will assign X to Player 1, and O to Player The program will ask Player 1, to...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...
I have already finished most of this assignment. I just need some help with the canMove and main methods; pseudocode is also acceptable. Also, the programming language is java. Crickets and Grasshoppers is a simple two player game played on a strip of n spaces. Each space can be empty or have a piece. The first player plays as the cricket pieces and the other plays as grasshoppers and the turns alternate. We’ll represent crickets as C and grasshoppers as...