Question: Java code for 3 by 3 tic tac toe board. need help with ai class A public method namedPlayer Class Since we will have two
Java code for by tic tac toe board. need help with ai class
A public method namedPlayer Class
Since we will have two different kinds of players human and AI the Player class will be an abstract class which both subclasses will extend. This class only has two components:
A public method named chooseMove which takes a single TicTacToe argument and returns one of the possibleMoves. How this move is chosen will depend on the kind of player, so this must be an abstract method which subclasses must override.
A public method named boardValue which takes a single TicTacToe argument and returns a double value representing how good the given board is for this player. The default implementation for this method should be to return if this player has won this board, if this player has lost this board, and otherwise see checkWin and checkLose in the TicTacToe class Since this default implementation is common to all subclasses, this should not be an abstract method.
AIPlayer Class
With simple games, there are techniques for developing AI players which perform very well. For general games this can be a very challenging task to accomplish efficiently, but for very simple games like tictactoe even a simple recursive solution is efficient enough. This class will model this recursive solution. The AIPlayer class includes the following components:
It should extend the Player class.
It should have one private String object with the name name.
It should have one private Player object with the name opponent.
It should have a getter and setter for the opponent field.
It should override the toString method to return the name field concatenated with the string AIso if the name field was "Player the return value should be "Player AI
It should have one public constructor that takes one String and one Player as arguments as initial values for these two fields.
It should have two mutually recursive methods, minValue and maxValue which work in the following way:
Both methods are public, take a TicTacToe instance as their single argument, and return a double value.The base cases for both methods are the same: if the argument represents a tictactoe game where this AIPlayer has won the game the value is If the argument is a game where this player has lost the game the value is If the argument is a draw game, the value is If no one has won and the game is not a draw, the value of the board must be computed recursively.The recursive cases are similar to each other, but slightly different. The maxValue method is used to determine what value this player could get by choosing optimally among the options provided by possibleMoves for the current game board. In order to compute this, the AIPlayer assumes that its opponent is going to try and choose the move option on the next board which minimizes its return. Therefore, maxValue should compute the minValue of each of the possible moves for this player, and then choose the option with the highest value.Similarly, the recursive case for the minValue method looks at the possibleMoves for the opponent, computes the maxValue of each option, and returns the value of the option which is lowest.
With the above two recursive methods completed, the AIPlayer can now override the chooseMove method. This method should get all of the possibleMoves for the given board, calculate each of the possible moves' minValue, and then return the move with the highest value.
Since the AIPlayer has a mechanism for computing the value of a game board that is more sophisticated than the default provided in Player, we should override the boardValue method as well, so that it returns the maxValue of the given board. This might be helpful when debugging your recursive implementations. For example, consider the following board:
OXXOX
The current player in this situation is O but regardless of which option they choose, the X player can win on the next turn. Therefore, even though though this board isn't complete yet, the boardValue for the X player should be and for the O player It can be helpful when debugging your recursive solution to construct several different boards that are one, two, or three moves away from a winninglosing or draw board, and then trace the recursion step by step.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
