Question: Objectives: Understand multi-class programs Understand random number generators Use of 2D arrays Interactive I/O String Processing Description: Many computer games, such as strategy games, simulation
Objectives: Understand multi-class programs Understand random number generators Use of 2D arrays Interactive I/O String Processing
Description: Many computer games, such as strategy games, simulation games etc uses game boards that can be implemented as two dimensional arrays. In Java (and in many other languages) 2D arrays can be defined as an array of 1D arrays. For example a 2D matrix of characters (with dimensions 8x10) can be defined as follows.
char[][] board = new char[8][10]
We can think of board as a table with 8 rows (indexed from 0..7) and 10 columns (indexed from 0...9).
Tic Tac Toe is played on a 3x3 board with two players alternating between Xs and Os, alternatively placing their respective marks on the board. First player succeeding in getting three marks in a row, column or diagonally wins the game.
How do we design the classes to implement a tic tac toe game? Let us think of the objects in a tic tac toe game environment. We can think of four types of objects, TicTacToe Board, HumanPlayer, RandomPlayer and AIPlayer. In this program we will only implement human player and random player. As an extra credit assignment you are encouraged to write a smart player that can analyze the board and make the best move. However that is not necessary. Let us begin with the following classes and methods.
Class Methods and Constructors TicTacToe clearBoard(), winner(), printBoard(),putMark(int,int), Board Board(int width, int height) Board(char[][] board) TicTacToeRunner main() HumanPlayer makeMove(TicTacToe b) RandomPlayer makeMove(TicTacToe b) AIPlayer (extra credit) makeMove(TicTacToe b) TicTacToeGUI (extra credit) displayBoard(TicTacToe b)
The description of each method can be found in the source code. Your assignment is to complete the incomplete methods from TicTacToe, HumanPlayer and RandomPlayer classes.
Creating Your Program You may use any kind of IDE such as jGRAsP, Dr. Java or IntelliJ or Eclipse or repl.it to test your program.
Human vs Human First, define two human players and play them against each other. You can do this by initializing two HumanPlayers as follows. TicTacToe t0 = new TicTacToe(); HumanPlayer player1 = new HumanPlayer(t0); HumanPlayer player2 = new HumanPlayer(t0);
The program should prompt each player to enter position as a pair of valid integers. For example, two players taking turns can be shown as follows: Player X: 0 0 X-- --- ---
Player O: 2 2 X-- --- --O
Game should continue until one player wins or the game is a draw. The program should correctly display the end of the game. For Example: Player X: 0 2 XXX --- O-O
Player X wins!
Or for a tie: Player X: 1 2 XOX OOX XXO
TIE!
Human vs Random Initialize a human player and a random player as follows. HumanPlayer Player1 = new HumanPlayer(t0); RandomPlayer Player2 = new RandomPlayer(t0); RandomPlayer will always make a random move. Therefore, human player should be able to usually beat the random player.
Random vs Random First define two random players and play them against each other. You can do this by initializing two RandomPlayers as follows. RandomPlayer player1 = new RandomPlayer(t0); RandomPlayer player2 = new RandomPlayer(t0);
It would be interesting to see which random player wins and to see if starting random player has any advantage over the other one.
Human vs Computer (extra credit only) For 10 extra points, you can complete the computerPlayer class. A computerPlayer in most cases must be able to defeat the randomplayer. ComputerPlayer must be smart, analyzing the entire board to make a move.
RandomPlayer Player1 = new randomPlayer(t0); ComputerPlayer Player2 = new ComputerPlayer(t0);
Getting Started https://repl.it/@nfolwell/TicTacToeLab - Fork a copy!
Grading Programming Components TicTacToe class 3 points Board class - 2 points HumanPlayer class 1 point RandomPlayer class 1 point TicTacToeRunner class 2 points Documentation and Indentation - 1 point
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
