Tic Tac Toe Game java using only methods and 2d arrays

java

Solution

You just have to do this:

board[rowNumber][columnNumber] = player;

Of course you would have to check beforehand that cell is not already occupied. If yes, then ask again for user input. I guess that wouldn't be that tough.

Apart from that, I would suggest you some improvements to your code:

Rather than having two players as `char` types, use an `enum Player`, with two constants - `X` and `O`. And use a `Player[]` instead.

enum Player {
    X, O;
}

No need to initialize your array with `'1', '2', ...`. Now that they will be `null` by default.

Rather than having `board` as local variable, and passing it in all the methods, make it a field in your class.

Currently your code is making just one move. Why? Also, you're not even using the return value of `hasWon()` and `boardFull()` method.

You can divide the `hasWon` method into 3 methods - `hasWonHorizontal()`, `hasWonVertical()`, `hasWonDiagonal()`. This will avoid that long `if` condition in the same method. And then call these 3 methods in sequence from `hasWon()` method.

Problem

Still cannot figure out what im doing wrong. I have no idea how to get the CPU choice into the board. I did get the board to re display the updated board with the row and column the player selected. I need to have 4 methods and id like them to do exactly only this: 1) displayBoard (takes single passed parameter of 2d array representing current tic tac toe board. 2.) makeAMove (takes 2 passed parameters: Two dimensional array representing tictactoe board and player character value ('X' or 'O'). Updates array with valid row and column selected by player character. This method does NOT return anything, it simply updates the board array. 3.) hasWon (Takes 2 passed parameters, 2 dimensional array representing TicTacToe board and player character ('X' or 'O'). Return TRUE if player character has won, FALSE otherwise. 4.) boardFull (Takes single passed parameter of two dimensional array representing TicTacToe board and return TRUE if all cells are occupied, false otherewise. These area ll the methods, some of which i have done in an ineffiecient way (i know) but im trying to teach myself just the logic of it first without using any classes. Anyone that can either comment here it would greatly help as i feel stuck at this point. ``` import java.util.Scanner; public class TicTacToe { public static void main(String[] args) { char[][] board = {{'1','2','3'}, {'4','5','6'}, {'7', '8', '9'}}; // assign player to char value of X's only int play; char player = 'X'; char cpu = 'O'; int rowNumber; int columnNumber; int playerORow; int playerOcolumn; System.out.println("Welcome to tic, tac, toe!\n"); System.out.println("Do you wish to play? 1 for yes, 2 for no "); Scanner input = new Scanner(System.in); play = input.nextInt(); if(play != 1) { System.out.print("Invalid input, game will now EXIT thanks for playing!"); System.exit(0); } // end if displayBoard(board); System.out.println("You are limited to X's only, good luck!"); System.out.println("Please enter row (0-3) of your move: "); rowNumber = input.nextInt(); System.out.println("Please enter column (1-3); of your move: "); columnNumber = input.nextInt(); System.out.println("Please enter row (0-3) for player O: "); playerORow = input.nextInt(); System.out.println("Please enter column (1-3); of your move: "); playerOcolumn = input.nextInt(); if(board[rowNumber][columnNumber] != 'X' && board[rowNumber][columnNumber] != 'O') { board[rowNumber][columnNumber] = player; } // end if else { } makeAMove(board, player); hasWon(board, player); boardFull(board); } // end main method // displays only the tic tac toe board public static void displayBoard(char[][] board) { // loop for each row System.out.println(board[0][0] + " | " + board[0][1] + " | " + board[0][2] + "\n---------"); System.out.println(board[1][0] + " | " + board[1][1] + " | " + board[1][2] + "\n---------"); System.out.println(board[2][0] + " | " + board[2][1] + " | " + board[2][2] + "\n"); } // end display board method // takes board array of values and updates it with valid row and column selected by player..does not return anything public static void makeAMove(char[][] board, char player) { displayBoard(board); } // end makeAMove method // compare each element in board to see if the char value of 'X' exists // if exists then then return true, else return false public static boolean hasWon(char[][] board, char player) { // Check if the player has won by checking winning conditions. if (board[0][0] == player && board[0][1] == player && board[0][2] == player || // 1st row board[1][0] == player && board[1][1] == player && board[1][2] == player || // 2nd row board[2][0] == player && board[2][1] == player && board[2][2] == player || // 3rd row board[0][0] == player && board[1][0] == player && board[2][0] == player || // 1st col. board[0][1] == player && board[1][1] == player && board[2][1] == player || // 2nd col. board[0][2] == player && board[1][2] == player && board[2][2] == player || // 3rd col. board[0][0] == player && board[1][1] == player && board[2][2] == player || // Diagonal \ board[2][0] == player && board[1][1] == player && board[0][2] == player) // Diagonal / return true; else { return false; } } // end hasWon method public static boolean boardFull(char [][] board) { if (board[0][0] != '1' && board[0][1] != '2' && board[0][2] != '3' && board[1][0] != '4' && board[1][1] != '5' && board[1][2] != '6' && board[2][0] != '7' && board[2][1] != '8' && board[2][2] != '9') return true; else { return false; } // end else } // end boardFull method ``` } // end class

Original source