Please help in Java I need to make a tic tac toe game in Jav
Please help in Java:
I need to make a tic tac toe game in Java using a 2d array and I am having trouble switching back and forth for the X and O. The first player is automatically an X so the second player is an O. Both are human. I am stuck and cannot get it to work, this is what I have so far.
public static void main(String[] args) {
//board is 11x11
char [] [] board = new char [11] [11];
//method to create board
createBoard(board);
//method to print board
printBoard(board);
//method to ask where to put token
token(board);
//check if full
isBoardFull(board);
//check rows
checkRowsForWin(board);
//checl columns
checkColumnsForWin(board);
//check diagonal
checkDiagonalsForWin(board);
}
public static void createBoard(char[][] board) {
for(int i = 0; i<11;++i){
for(int j = 0; j<11; ++j){
board[i][j]=\' \';
}
}
for(int i = 3; i<11;i= i+4){
for(int j = 0; j<11; ++j){
board[i][j]=\'-\';
}
}
for (int i = 0; i<11; ++i){
for (int j = 3; j<11; j=j+4)
board[i][j]=\'|\';
}
board[1][1]=\'1\';
board[1][5]=\'2\';
board[1][9]=\'3\';
board[5][1]=\'4\';
board[5][5]=\'5\';
board[5][9]=\'6\';
board[9][1]=\'7\';
board[9][5]=\'8\';
board[9][9]=\'9\';
}
public static void printBoard(char[][] board) {
for (int i = 0; i < 11; i++) {
//columns
for (int j = 0; j < 11; j++) {
if(j%11==0){
System.out.println(\"\");
}
System.out.print(board[i][j]);
}
}
}
public static void token(char[][]board){
Scanner scnr =new Scanner (System.in);
char k = \'X\';
char h = \'O\';
while (k < 9){
System.out.println(\"Where do you want to place the X?\");
k = scnr.next().charAt(0);
}
while (h < 9){
System.out.println(\"Where do you want to place the O?\");
h = scnr.next().charAt(0);
}
}
public static boolean isBoardFull(char [][] board) {
boolean isFull = true;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] == \'-\') {
isFull = false;
}
}
}
return isFull;
}
public static boolean checkRowCol(char c1, char c2, char c3) {
// check to see if all three values are the same
return ((c1 != \'-\') && (c1 == c2) && (c2 == c3));
}
public static boolean checkRowsForWin(char [][] board) {
//check rows for winner
for (int i = 0; i < 3; i++) {
if (checkRowCol(board[i][0], board[i][1], board[i][2]) == true) {
return true;
}
}
return false;
}
public static boolean checkColumnsForWin(char [][] board) {
//check columns for winner
for (int i = 0; i < 3; i++) {
if (checkRowCol(board[0][i], board[1][i], board[2][i]) == true) {
return true;
}
}
return false;
}
public static boolean checkDiagonalsForWin(char[][] board) {
//check diagonals for winner
return ((checkRowCol(board[0][0], board[1][1], board[2][2]) == true)
|| (checkRowCol(board[0][2], board[1][1], board[2][0]) == true));
}
}
Solution
Code:
import java.util.Scanner;
public class Main {
// Name-constants to represent the seeds and cell contents
public static final int EMPTY = 0;
public static final int CROSS = 1;
public static final int NOUGHT = 2;
// Name-constants to represent the various states of the game
public static final int PLAYING = 0;
public static final int DRAW = 1;
public static final int CROSS_WON = 2;
public static final int NOUGHT_WON = 3;
// The game board and the game status
public static final int ROWS = 3, COLS = 3; // number of rows and columns
public static int[][] board = new int[ROWS][COLS]; // game board in 2D array
// containing (EMPTY, CROSS, NOUGHT)
public static int currentState; // the current state of the game
// (PLAYING, DRAW, CROSS_WON, NOUGHT_WON)
public static int currentPlayer; // the current player (CROSS or NOUGHT)
public static int currntRow, currentCol; // current seed\'s row and column
public static Scanner in = new Scanner(System.in); // the input Scanner
/** The entry main method (the program starts here) */
public static void main(String[] args) {
// Initialize the game-board and current status
initGame();
// Play the game once
do {
playerMove(currentPlayer); // update currentRow and currentCol
updateGame(currentPlayer, currntRow, currentCol); // update currentState
printBoard();
// Print message if game-over
if (currentState == CROSS_WON) {
System.out.println(\"\'X\' won! Bye!\");
} else if (currentState == NOUGHT_WON) {
System.out.println(\"\'O\' won! Bye!\");
} else if (currentState == DRAW) {
System.out.println(\"It\'s a Draw! Bye!\");
}
// Switch player
currentPlayer = (currentPlayer == CROSS) ? NOUGHT : CROSS;
} while (currentState == PLAYING); // repeat if not game-over
}
/** Initialize the game-board contents and the current states */
public static void initGame() {
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLS; ++col) {
board[row][col] = EMPTY; // all cells empty
}
}
currentState = PLAYING; // ready to play
currentPlayer = CROSS; // cross plays first
}
/** Player with the \"theSeed\" makes one move, with input validation.
Update global variables \"currentRow\" and \"currentCol\". */
public static void playerMove(int theSeed) {
boolean validInput = false; // for input validation
do {
if (theSeed == CROSS) {
System.out.print(\"Player \'X\', enter your move (row[1-3] column[1-3]): \");
} else {
System.out.print(\"Player \'O\', enter your move (row[1-3] column[1-3]): \");
}
int row = in.nextInt() - 1; // array index starts at 0 instead of 1
int col = in.nextInt() - 1;
if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY) {
currntRow = row;
currentCol = col;
board[currntRow][currentCol] = theSeed; // update game-board content
validInput = true; // input okay, exit loop
} else {
System.out.println(\"This move at (\" + (row + 1) + \",\" + (col + 1)
+ \") is not valid. Try again...\");
}
} while (!validInput); // repeat until input is valid
}
/** Update the \"currentState\" after the player with \"theSeed\" has placed on
(currentRow, currentCol). */
public static void updateGame(int theSeed, int currentRow, int currentCol) {
if (hasWon(theSeed, currentRow, currentCol)) { // check if winning move
currentState = (theSeed == CROSS) ? CROSS_WON : NOUGHT_WON;
} else if (isDraw()) { // check for draw
currentState = DRAW;
}
// Otherwise, no change to currentState (still PLAYING).
}
/** Return true if it is a draw (no more empty cell) */
// TODO: Shall declare draw if no player can \"possibly\" win
public static boolean isDraw() {
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLS; ++col) {
if (board[row][col] == EMPTY) {
return false; // an empty cell found, not draw, exit
}
}
}
return true; // no empty cell, it\'s a draw
}
/** Return true if the player with \"theSeed\" has won after placing at
(currentRow, currentCol) */
public static boolean hasWon(int theSeed, int currentRow, int currentCol) {
return (board[currentRow][0] == theSeed // 3-in-the-row
&& board[currentRow][1] == theSeed
&& board[currentRow][2] == theSeed
|| board[0][currentCol] == theSeed // 3-in-the-column
&& board[1][currentCol] == theSeed
&& board[2][currentCol] == theSeed
|| currentRow == currentCol // 3-in-the-diagonal
&& board[0][0] == theSeed
&& board[1][1] == theSeed
&& board[2][2] == theSeed
|| currentRow + currentCol == 2 // 3-in-the-opposite-diagonal
&& board[0][2] == theSeed
&& board[1][1] == theSeed
&& board[2][0] == theSeed);
}
/** Print the game board */
public static void printBoard() {
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLS; ++col) {
printCell(board[row][col]); // print each of the cells
if (col != COLS - 1) {
System.out.print(\"|\"); // print vertical partition
}
}
System.out.println();
if (row != ROWS - 1) {
System.out.println(\"-----------\"); // print horizontal partition
}
}
System.out.println();
}
/** Print a cell with the specified \"content\" */
public static void printCell(int content) {
switch (content) {
case EMPTY: System.out.print(\" \"); break;
case NOUGHT: System.out.print(\" O \"); break;
case CROSS: System.out.print(\" X \"); break;
}
}
}




