Question
I need to add a Computer player to my Tic Tac Toe game. I also need to have an interactive game with one human player
I need to add a Computer player to my Tic Tac Toe game.
I also need to have an interactive game with one human player and one computer player and having a demonstration game with two computer players.
Here are my two classes.
TTT.java
public class TTT { private char[][] board; private char currentPlayerMark; public TTT() { board = new char[3][3]; currentPlayerMark = 'x'; initializeBoard(); } //Set/Reset the board back to all empty values public void initializeBoard() { //loop through rows for (int i=0;i<3;i++) { //loop through columns for(int j=0;j<3;j++) { board[i][j]='-'; } } } public void printBoard() { System.out.println("-------------"); for(int i=0;i<3;i++) { System.out.print("|"); for (int j=0;j<3;j++) { System.out.print(board[i][j]+"|"); } System.out.println(); System.out.println("-------------"); } } public boolean isBoardFull() { boolean isFull=true; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { if(board[i][j]=='-') { isFull=false; } } } return isFull; } public boolean checkForWin() { return checkRowsForWin()||checkColumnsForWin()||checkDiagonalsForWin(); } private boolean checkRowsForWin() { for(int i=0;i<3;i++) { if(checkRowCol(board[i][0],board[i][1],board[i][2])==true) { return true; } }return false; } private boolean checkColumnsForWin() { for(int i=0;i<3;i++) { if(checkRowCol(board[0][i],board[1][i],board[2][i])==true) { return true; } }return false; } private boolean checkDiagonalsForWin() { return checkRowCol(board[0][0],board[1][1],board[2][2])==true||checkRowCol(board[0][2],board[1][1],board[2][0])==true; } private boolean checkRowCol(char c1, char c2, char c3) { return c1 !='-'&&c1==c2&&c2==c3; } public void changePlayer() { if (currentPlayerMark=='x') { currentPlayerMark='o'; } else { currentPlayerMark='x'; } } public boolean placeMark(int row, int col) { if((row>=0)&&(row<3)) { if((col>=0)&&(col<3)) { if(board[row][col]=='-') { board[row][col]=currentPlayerMark; return true; } } } return false;
} public char getCurrentPlayerMark() { return currentPlayerMark; }
}
Main.java
import java.util.*; public class Main {
public static void main(String[] args) { // TODO Auto-generated method stub
Scanner scan = new Scanner(System.in); TTT game= new TTT(); game.initializeBoard(); System.out.println("Tic-Tac-Toe!"); do { System.out.println("Current board layout:"); game.printBoard(); int row; int col; do { System.out.println("Player "+game.getCurrentPlayerMark()+", enter an empty row and column to place your mark!"); row = scan.nextInt()-1; col = scan.nextInt()-1; } while (!game.placeMark(row, col)); game.changePlayer(); } while(!game.checkForWin() && !game.isBoardFull()); if (game.isBoardFull() && !game.checkForWin()) { System.out.println("The game was a tie!"); }else { System.out.println("Current board layout:"); game.printBoard(); game.changePlayer(); System.out.println(Character.toUpperCase(game.getCurrentPlayerMark())+" Wins!"); } }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started