Hello every one i have ma assignment for ma class is to make tictactoe game using arryas in java..i don't know if neeed two codes or no..i did big part so far..but i need help.
and ma prof. needs only one player and da computer be the other one.... in 5x5 tictactoe :
two codes
__________________________________________________import java.util.Scanner; public class TTTGame { public static void main(String[] args) { TTTBoard myGame = new TTTBoard(); Scanner input = new Scanner(System.in); int row; int col; while(myGame.determineWinner() == 0 && !myGame.isBoardComplete()) { myGame.displayBoard(); System.out.println("Player " + myGame.getCurrentPlayer()); System.out.println("Make your move."); System.out.print("Row please (1-5):"); row = input.nextInt(); while(row < 1 || row > 5) { System.out.println("Invalid Row."); System.out.print("Try again (1-5):"); row = input.nextInt(); } System.out.print("Col please (1-5):"); col = input.nextInt(); // error trap for valid col while(col < 1 || col > 5) { System.out.println("Invalid Col."); System.out.print("Try again (1-5):"); col = input.nextInt(); } while(!myGame.makeMove(row, col)) { System.out.println("Invalid Move... Try Again."); System.out.print("Row please (1-5):"); row = input.nextInt(); while(row < 1 || row > 5) { System.out.println("Invalid Row."); System.out.print("Try again (1-5):"); row = input.nextInt(); } System.out.print("Col please (1-5):"); col = input.nextInt(); while(col < 1 || col > 5) { System.out.println("Invalid Col."); System.out.print("Try again (1-5):"); col = input.nextInt(); } } } if (myGame.determineWinner() == 0) { System.out.println("Sorry - Cat's Game"); } else { System.out.print("The Winner is Player "); if (myGame.getCurrentPlayer() == 1) { System.out.println("2"); } else { System.out.println("1"); } } } }
public class TTTBoard { private int _player; private int _board[][]; public TTTBoard() { _player = 1; _board = new int[5][5]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { _board[i][j] = 0; } } } public boolean makeMove(int row, int col) { boolean move; if (_board[row][col] == 0) { _board[row][col] = _player; return true; } else { move = false; } return move; } public boolean isBoardComplete() { boolean complete = true; for (int i = 0; i < 5; i++) { for(int j =0; j < 5; j++) { if (_board[i][j] == 0) { complete = false; } } } return complete; } public int determineWinner() { int winner = 0; if (_board[0][0] == _board[0][1] && _board[0][0] == _board[0][2] && _board[0][0] != 0) { winner = _board[0][0]; } if (_board[1][0] == _board[1][1] && _board[1][0] == _board[1][2] && _board[1][0] != 0) { winner = _board[1][0]; } if (_board[2][0] == _board[2][1] && _board[2][0] == _board[2][2] && _board[2][0] != 0) { winner = _board[2][0]; } // Check for winner in col 1 if (_board[0][0] == _board[1][0] && _board[0][0] == _board[2][0] && _board[0][0] != 0) { winner = _board[0][0]; } // Check for winner in col 2 if (_board[0][1] == _board[1][1] && _board[0][1] == _board[2][1] && _board[0][1] != 0) { winner = _board[0][1]; } // Check for winner in col 3 if (_board[0][2] == _board[1][2] && _board[0][2] == _board[2][2] && _board[0][2] != 0) { winner = _board[0][2]; } // Check for winner in first diagonal if (_board[0][0] == _board[1][1] && _board[0][0] == _board[2][2] && _board[0][0] != 0) { winner = _board[0][0]; } // Check for winner in 2nd diagonal if (_board[2][0] == _board[1][1] && _board[2][0] == _board[0][2] && _board[2][0] != 0) { winner = _board[2][0]; } return winner; } public void displayBoard() { for ( int i = 0; i < 5; i++) { System.out.println(" "); for( int j = 0; j < 5; j++) { System.out.print(_board[i][j] + " "); } System.out.println("\n-|-|-|-|-\n"); } } public int getCurrentPlayer() { return _player; } }