Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can i add an AI to my tic tac toe game? So I have 4 classes, IGame.java which is an interface, I have a

How can i add an AI to my tic tac toe game? So I have 4 classes, IGame.java which is an interface, I have a Main.java class to run the program and i have two games: TicTacToe.java and ConnectFour.java. I was wondering how I can add an interface and/or superclass for player, and how to make an AI for both of the games?

TicTacToe.java

public class TicTacToe implements IGame{ private char[][] board; private char currentPlayerSign; /** * Constructor. * Properly initialized board, sets which player will play first. * @param x * @param y */ public TicTacToe() { board = new char[3][3]; currentPlayerSign = 'x'; clearBoard(); } /** * Gives access to the CurrentPlayerSign */ public char getCurrentPlayerSign() { return currentPlayerSign; } @Override /** * Return the board as strings. */ public void printBoard() { System.out.println("--------"); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { System.out.print(board[i][j] + " | "); } System.out.println(); } } @Override /** * Empty board/values. Looping through rows and columns. * Set each space to "-". */ public void clearBoard() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board[i][j] = '-'; } } } @Override /** * If there is none "-" then the board is full and return true. */ public boolean fullBoard() { boolean fullBoard = true; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == '-') { fullBoard = false; } } } return fullBoard; } @Override /** * Main method to test rows, columns and diagonals for win * since these have different conditions. * return the three methods for rows, columns or diagonal win. */ public boolean ifWinner() { return rowsForWin() || colForWin() || diagForWin(); } @Override /** * Checks if there is three identical in a row */ public boolean rowsForWin() { for (int i = 0; i < 3; i++) { if (checkRowColumns(board[i][0], board[i][1], board[i][2]) == true) { return true; } } return false; } @Override /** * Checks if there is three identical in the columns */ public boolean colForWin() { for (int i = 0; i < 3; i++) { if (checkRowColumns(board[0][i], board[1][i], board[2][i]) == true) { return true; } } return false; } @Override /** * Checks if there is three identical diagonal from each corner of the board */ public boolean diagForWin() { return ((checkRowColumns(board[0][0], board[1][1], board[2][2]) == true) || (checkRowColumns(board[0][2], board[1][1], board[2][0]) == true)); } /** * Will check the specified characters if three X or O and compare them * @return */ boolean checkRowColumns(char c1, char c2, char c3) { return ((c1 != '-') && (c1 == c2) && (c2 == c3)); } @Override /** * Change turns from player 1 to 2 and back */ public void turn() { if(currentPlayerSign == 'x') { currentPlayerSign = 'o'; } else { currentPlayerSign = 'x'; } } @Override /** * Places the sign on the board */ public boolean placeSign(int row, int column) { if ((row >= 0) && (row < 3)) { if ((column >= 0) && (column < 3)) { if (board[row][column] == '-') { board[row][column] = currentPlayerSign; return true; } } } return false; } } 

ConnectFour.java

 public class ConnectFour implements IGame { private char[][] board; private char currentPlayerSign; /** * * Constructor. * * Properly initialized board, sets which player will play first. * * @param x * * @param y * */ public ConnectFour() { board = new char[6][7]; currentPlayerSign = 'x'; clearBoard(); } public char getCurrentPlayerSign() { return currentPlayerSign; } @Override public void clearBoard() { for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { board[i][j] = '-'; } } } @Override public void printBoard() { System.out.println(" 1 2 3 4 5 6 7"); for (int i = 0; i < 6; i++) { System.out.print("|"); for (int j = 0; j < 7; j++) { System.out.print(board[i][j] + "|"); } System.out.println(); } } @Override public boolean fullBoard() { boolean fullBoard = true; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { if (board[i][j] == '-') { fullBoard = false; } } } return fullBoard; } @Override public boolean ifWinner() { // TODO Auto-generated method stub return false; } @Override public boolean rowsForWin() { return false; } @Override public boolean colForWin() { // TODO Auto-generated method stub return false; } @Override public boolean diagForWin() { // TODO Auto-generated method stub return false; } @Override public void turn() { if (currentPlayerSign == 'X') { currentPlayerSign = 'O'; } else currentPlayerSign = 'X'; } @Override public boolean placeSign(int row, int column) { if ((row >= 0) && (row < 6)) { if ((column >= 0) && (column < 7)) { if (board[row][column] == '-') { board[row][column] = currentPlayerSign; return true; } } } return false; } } 

Main.java

 import java.util.Scanner; public class Main { /** * * @param args */ public static void main(String[] args) { Scanner s = new Scanner(System.in); ConnectFour CF = new ConnectFour(); TicTacToe TTT = new TicTacToe(); CF.clearBoard(); System.out.println("Tic Tac Toe"); do { System.out.println("Board layout:"); CF.printBoard(); int row; int column; do { System.out.println("Player " + CF.getCurrentPlayerSign() + " enter empty row and column to play"); row = s.nextInt()-1; column = s.nextInt()-1; } while (!CF.placeSign(row, column)); CF.turn(); } while (!CF.ifWinner() && !CF.fullBoard()); if (CF.fullBoard() && !CF.ifWinner()) { System.out.println("Game result: Draw"); } else { System.out.println("Board layout:"); CF.printBoard(); CF.turn(); System.out.println(Character.toUpperCase(CF.getCurrentPlayerSign()) + "Wins!"); } } } 

IGame.java

public interface IGame { /** * Empty board. */ void clearBoard(); /** * Print the board to output. */ void printBoard(); /** * Check if board is full, return true if full, return false if not full. * @return */ boolean fullBoard(); /** * Checks if anyone has won the game. If someone has won, return true. * If no one has won, return false. * @return */ boolean ifWinner(); /** * Will check the rows for a win. True if win. * @return */ boolean rowsForWin(); /** * Will check the columns for a win. True if win. * @return */ boolean colForWin(); /** * Will check the diagonals for a win. True if win. * @return */ boolean diagForWin(); /** * change turns from player 1 to 2 and back. */ void turn(); /** * Place the correct letter to correct row x column on the board * @param row * @param column * @return */ boolean placeSign(int row, int column); } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

4 Explain how companies analyse and use marketing information

Answered: 1 week ago