Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

the basis of the game is already given, you just have to implement a stupid AI that uses randomness. so you only need to fill

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
the basis of the game is already given, you just have to implement a stupid AI that uses randomness. so you only need to fill out Player, HumanPlayer, ComputerRandomPlayer, TicTacToe. thx:)
We are now ready to program our solution. We will reuse the implementation of the class Tic Tac ToeGame from assignment 1. A class Utils has been provided to get a simple access to a few constants and global variables. Player Player is an interface. It defines only one method, the method play. Play is void and has one input parameter, a reference to a Tic Tac ToeGame. HumanPlayer Human Player is a class which implements the interface Player. In its implementation of the method play, it first checks that the game is indeed playable (and prints out an error message is that is not the case), and then queries the user for a valid input, reusing the code that was in the main of the class Tic Tac Toe of assignment 1. Once such an input has been provided, it plays in on the game and returns. ComputerRandom Player Computer RandomPlayer is a class which also implements the interface Player. In its implementation of the method play, it first checks that the game is indeed playable (and prints out an error message is that is not the case), and then chose randomly the next move and plays it on the game and returns. All the possible next moves have an equal chance of being played. Tic Tac Toe This class implements playing the game. You are provided with the initial part very similar to the one from as signment 1. The entire game is played in the main method. A local variable players, a reference to an array of two players, is used to store the human and the computer player. You must use that array to store your Player references You need to finish the implementation of the main to obtain the specified behaviour. You need to ensure that the first player is initially chosen randomly, and that the first move alternate between both players in subsequent games. Below is another sample run, this time on a 4x4 grid with a win length of 2. The human players makes a series of input mistakes along the way. C U U Tube Hee Unhne UR-C random Player 2's turn Player l's turn. XI O to play: Here, player 2 (the computer) was selected to start for the first game. As can be seen, the computer player doesn't print out anything when it plays, it just makes its move silently. Then, it is player I's turn (human). Follow we did in assignment 1, the HumanPlayer object first prints the game (here, we can see that the computer played cell 4) and then prompts the actual human (us, the user) for a move. Below, we see that the human has selected cell 1. The computer will then play (silently) and the human will be prompt again. It continues until the game finishes: 0 to play: 1 Player 2's turn. Player 1's turn. 01 | X XI O to play: 5 Player 2's turn. Player 1's turn. 01 | X X01 1 x 0 to play: 6 Player 2's turn. Player l's turn. 0 X X X100 II X O to play: 7 Player l's turn. X to play: 2 Player 2's turn. Player 1's turn. IXIT T 01 TIT X to play: 2 This cell has already been played 1 X 101 X to play: -1 The value should be between 1 and 16 1 X 1 10 Open - Edit History Select all : Select none Invert selection Properties Open Select PM PM CellValue - Notepad File Edit Format View Help /* * An enum class that defines the * values EMPTY, x * and . * @author Guy-Vincent Jourdan, University of Ottawa public enum Cellvalue { EMPTY, Unix (LF) In 1, Col 1 Open. Edit Properties History Open Select all 88 Select none Invert selection Select - 0 GameState - Notepad File Edit Format View Help the * An enum class that defines the * values PLAYING, DRAW, * XWIN and OWIN * @author Guy-Vincent Jourdan, University of Ottawa public enum GameState{ PLAYING, DRAW, XWIN, OWIN Unix (LF) Ln 1. Col 1 100% TicTacToe - Notepad File Edit Format View Help public class TicTac Toe - main of the application. Creates the instance of GameController - and starts the game. If the parameters line and column - are passed, they are used. Otherwise, a default value is used. Defaults values are also - used if the parters are too small (less than 2). paras args command line parameters public static void main(String[] args) { Student Info.display(); TicTac ToeGame game; int lines - 3; int columns - 3; int win - 3; try! if (args.length) 2) { lines - Integer.parseInt(args[]); if(lines 3) win - Integer.parseInt(args[2]): if(winc2) System.out.println("Invalid argument, using default..."); win = 3; if (args.length > 3){ System.out.println("Too many arguments. Only the first 3 are used."); } catch (Number FormatException e) { System.out.println("Invalid argument, using default...") lines - 3; columns - 3; win - 3; Player players // YOUR CODE HERE DEAS. WP O Type here to search TicTacToeGame - Notepad File Edit Format View Help The class TicTac ToeGame is the * class that implements the Tic Tac Toe Game. * It contains the grid and tracks its progress. * It automatically maintain the current state of * the game as players are making moves. * @author Guy-Vincent Jourdan, University of Ottawa public class Tic Tac ToeGame { The board of the game, stored as a single array. private CellValue[] board; * level records the number of rounds that have been * played so far. Starts at e. private int level; * gameState records the current state of the game. private GameState gameState; * lines is the number of lines in the grid public final int lines; * columns is the number of columns in the grid public final int columns; * sizeWin is the number of cell of the same type + that must be aligned to win the game public final int sizeWin; * default constructor, for a game of 3x3, which must * align 3 cells Tic Tac ToeGame - Notepad File Edit Format View Help public Tic Tac ToeGame({ this(3,3,3); * constructor allowing to specify the number of lines and the number of columns for the game. 3 cells must be aligned. param lines * the number of lines in the game * @param columns the number of columns in the game public Tic Tac ToeGame(int lines, int columns) { this(lines, columns, 3); * constructor allowing to specify the number of lines and the number of columns for the game, as well as - the number of cells that must be aligned to win. * @param lines * the number of lines in the game * @param columns * the number of columns in the game * @param sizeWin * the number of cells that must be aligned to win. public Tic Tac ToeGame(int lines, int columns, int sizeWin){ this.lines = lines; this.columns = columns; this.sizeWin - sizeWin; board - new CellValue(lines*columns); for(int i = 0; i = lines* columns) throw new Illegal ArgumentException ("Illegal position: " + i); return board[i]; * This method is call by the next player to play * at the cell at index i. If the index is invalid, an error message is * printed out. The behaviour is then unspecified * If the chosen cell is not empty, an error message is * printed out. The behaviour is then unspecified Type here to search 0 Bt SWPS Blog We are now ready to program our solution. We will reuse the implementation of the class Tic Tac ToeGame from assignment 1. A class Utils has been provided to get a simple access to a few constants and global variables. Player Player is an interface. It defines only one method, the method play. Play is void and has one input parameter, a reference to a Tic Tac ToeGame. HumanPlayer Human Player is a class which implements the interface Player. In its implementation of the method play, it first checks that the game is indeed playable (and prints out an error message is that is not the case), and then queries the user for a valid input, reusing the code that was in the main of the class Tic Tac Toe of assignment 1. Once such an input has been provided, it plays in on the game and returns. ComputerRandom Player Computer RandomPlayer is a class which also implements the interface Player. In its implementation of the method play, it first checks that the game is indeed playable (and prints out an error message is that is not the case), and then chose randomly the next move and plays it on the game and returns. All the possible next moves have an equal chance of being played. Tic Tac Toe This class implements playing the game. You are provided with the initial part very similar to the one from as signment 1. The entire game is played in the main method. A local variable players, a reference to an array of two players, is used to store the human and the computer player. You must use that array to store your Player references You need to finish the implementation of the main to obtain the specified behaviour. You need to ensure that the first player is initially chosen randomly, and that the first move alternate between both players in subsequent games. Below is another sample run, this time on a 4x4 grid with a win length of 2. The human players makes a series of input mistakes along the way. C U U Tube Hee Unhne UR-C random Player 2's turn Player l's turn. XI O to play: Here, player 2 (the computer) was selected to start for the first game. As can be seen, the computer player doesn't print out anything when it plays, it just makes its move silently. Then, it is player I's turn (human). Follow we did in assignment 1, the HumanPlayer object first prints the game (here, we can see that the computer played cell 4) and then prompts the actual human (us, the user) for a move. Below, we see that the human has selected cell 1. The computer will then play (silently) and the human will be prompt again. It continues until the game finishes: 0 to play: 1 Player 2's turn. Player 1's turn. 01 | X XI O to play: 5 Player 2's turn. Player 1's turn. 01 | X X01 1 x 0 to play: 6 Player 2's turn. Player l's turn. 0 X X X100 II X O to play: 7 Player l's turn. X to play: 2 Player 2's turn. Player 1's turn. IXIT T 01 TIT X to play: 2 This cell has already been played 1 X 101 X to play: -1 The value should be between 1 and 16 1 X 1 10 Open - Edit History Select all : Select none Invert selection Properties Open Select PM PM CellValue - Notepad File Edit Format View Help /* * An enum class that defines the * values EMPTY, x * and . * @author Guy-Vincent Jourdan, University of Ottawa public enum Cellvalue { EMPTY, Unix (LF) In 1, Col 1 Open. Edit Properties History Open Select all 88 Select none Invert selection Select - 0 GameState - Notepad File Edit Format View Help the * An enum class that defines the * values PLAYING, DRAW, * XWIN and OWIN * @author Guy-Vincent Jourdan, University of Ottawa public enum GameState{ PLAYING, DRAW, XWIN, OWIN Unix (LF) Ln 1. Col 1 100% TicTacToe - Notepad File Edit Format View Help public class TicTac Toe - main of the application. Creates the instance of GameController - and starts the game. If the parameters line and column - are passed, they are used. Otherwise, a default value is used. Defaults values are also - used if the parters are too small (less than 2). paras args command line parameters public static void main(String[] args) { Student Info.display(); TicTac ToeGame game; int lines - 3; int columns - 3; int win - 3; try! if (args.length) 2) { lines - Integer.parseInt(args[]); if(lines 3) win - Integer.parseInt(args[2]): if(winc2) System.out.println("Invalid argument, using default..."); win = 3; if (args.length > 3){ System.out.println("Too many arguments. Only the first 3 are used."); } catch (Number FormatException e) { System.out.println("Invalid argument, using default...") lines - 3; columns - 3; win - 3; Player players // YOUR CODE HERE DEAS. WP O Type here to search TicTacToeGame - Notepad File Edit Format View Help The class TicTac ToeGame is the * class that implements the Tic Tac Toe Game. * It contains the grid and tracks its progress. * It automatically maintain the current state of * the game as players are making moves. * @author Guy-Vincent Jourdan, University of Ottawa public class Tic Tac ToeGame { The board of the game, stored as a single array. private CellValue[] board; * level records the number of rounds that have been * played so far. Starts at e. private int level; * gameState records the current state of the game. private GameState gameState; * lines is the number of lines in the grid public final int lines; * columns is the number of columns in the grid public final int columns; * sizeWin is the number of cell of the same type + that must be aligned to win the game public final int sizeWin; * default constructor, for a game of 3x3, which must * align 3 cells Tic Tac ToeGame - Notepad File Edit Format View Help public Tic Tac ToeGame({ this(3,3,3); * constructor allowing to specify the number of lines and the number of columns for the game. 3 cells must be aligned. param lines * the number of lines in the game * @param columns the number of columns in the game public Tic Tac ToeGame(int lines, int columns) { this(lines, columns, 3); * constructor allowing to specify the number of lines and the number of columns for the game, as well as - the number of cells that must be aligned to win. * @param lines * the number of lines in the game * @param columns * the number of columns in the game * @param sizeWin * the number of cells that must be aligned to win. public Tic Tac ToeGame(int lines, int columns, int sizeWin){ this.lines = lines; this.columns = columns; this.sizeWin - sizeWin; board - new CellValue(lines*columns); for(int i = 0; i = lines* columns) throw new Illegal ArgumentException ("Illegal position: " + i); return board[i]; * This method is call by the next player to play * at the cell at index i. If the index is invalid, an error message is * printed out. The behaviour is then unspecified * If the chosen cell is not empty, an error message is * printed out. The behaviour is then unspecified Type here to search 0 Bt SWPS Blog

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

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions

Question

Does it matter to policy makers how people form expectations?

Answered: 1 week ago