Write a text-based, 2-person, tic-tac-toe game. This game will combine most of w
ID: 3574698 • Letter: W
Question
Write a text-based, 2-person, tic-tac-toe game. This game will combine most of what you have learned so far. You’ll use methods and a 2-D array in this game. A general description of how to proceed and what is expected follows below. The flow of your game should be roughly as follows:
Display the instructions for the game Assume X always goes first Create the empty board (initialize your 2D array) Display the board While there isn’t a winner or a tie Get the player’s move (collect a row and a column) While the player’s move is not legal Get the player’s move Update the board with the move Display the updated board Switch turns Congratulate the winner or declare a tie
The main method declares the 2D array, keeps track of whose turn it is, takes care of collecting the player’s move (a row and a column) and executes the flow of the program as described above by calling the necessary methods. Here are the methods that I will be looking for in your program: public static void displayInstructions( ): This is a void method that takes no arguments. It’s job is simply to display the instructions for the game (see below). public char[][] createBoard( ): This method takes no arguments but returns a 3x3 array of characters with each character set to ‘ ‘ (space). public static void displayBoard(char[][] board): This method takes the current game board as an argument and displays it to the screen. public static boolean isWinner(char[][] board): This method takes the current game board as an argument and determines if there is a winner. If there is, it returns true else return false. public static boolean isTie(char[][] board): This method takes the current game board as an argument and determines if there is a tie. If there is, it returns true else return false. public static boolean legalMove(int row, int column, char[][] board): This method checks to see if the user’s move is legal. A legal move is inside the board to a space that isn’t currently occupied. It returns true or false accordingly. public static void updateBoard(int row, int column, char player, char[][] board): This method updates the board with the player’s move.
Sample output from two separate games is shown below.
Instructions: Fill in a complete row, column or
diagonal with either X's or O's to win
Recall that array indices start at 0!
| |
----------
| |
----------
| |
Player X--> Enter a row and column: 1
1
| |
----------
| X |
----------
| |
Player O--> Enter a row and column: 0 0
O | |
----------
| X |
----------
| |
Player X--> Enter a row and column: 1 1
Player X--> Enter a row and column: -1 -1
Player X--> Enter a row and column: 1 5
Player X--> Enter a row and column: 0 2
O | | X
----------
| X |
----------
| |
Player O--> Enter a row and column: 2 0
O | | X
----------
| X |
----------
O | |
Player X--> Enter a row and column: 1 0
O | | X
----------
X | X |
----------
O | |
Player O--> Enter a row and column: 2 2
O | | X
----------
X | X |
----------
O | | O
Player X--> Enter a row and column: 1 2
O | | X
----------
X | X | X
----------
O | | O
Game over!
Congratulations player X, you won!!!
Instructions: Fill in a complete row, column or
diagonal with either X's or O's to win
Recall that array indices start at 0!
| |
----------
| |
----------
| |
Player X--> Enter a row and column: 1 1
| |
----------
| X |
----------
| |
Player O--> Enter a row and column: 0 0
O | |
----------
| X |
----------
| |
Player X--> Enter a row and column: 2 2
O | |
----------
| X |
----------
| | X
Player O--> Enter a row and column: 0 2
O | | O
----------
| X |
----------
| | X
Player X--> Enter a row and column: 0 1
O | X | O
----------
| X |
----------
| | X
Player O--> Enter a row and column: 2 1
O | X | O
----------
| X |
----------
| O | X
Player X--> Enter a row and column: 1 0
O | X | O
----------
X | X |
----------
| O | X
Player O--> Enter a row and column: 1 2
O | X | O
----------
X | X | O
----------
| O | X
Player X--> Enter a row and column: 2 0
O | X | O
----------
X | X | O
----------
X | O | X
Game over!
There was a tie
Explanation / Answer
Please find below the TicTacToe game in java:
import java.io.*; //decleration
import java.util.*;
public class TiTaTo{ //public class TiTaTo for TicTacToe Game
private char[][] board; //datatypes declaration
private int whoseturn;
private String[] players;
int movesmade;
final static private char[] pieces = {'X','O'};
// This will Initialize a Tic Tac Toe object.
public TicTacToe(String player1, String player2) {
board = new char[3][3]; //board details
for (int i=0;i < 3; i++)
for (int j=0; j < 3; j++)
board[i][j] = '_';
whoseturn = 0;
movesmade = 0;
players = new String[2];
players[0] = player1;
players[1] = player2;
}
// The below tries to make a move at row, column. If it really is valid, then the move is made
// and true will be returned. Else nothing will be done and false will be returned.
public boolean Move(int row, int column) {
if ( (board[row][column] == '_') && inbounds(row,column) ) { //if condition
board[row][column] = pieces[whoseturn];
movesmade++;
return true;
}
else
return false;
}
// The below returns true if indexes passed to the method are inbounds.
public boolean inbounds(int row, int column) {
if ((row < 0) || (column < 0))
return false;
if ((row > 2) || (column > 2))
return false;
return true;
}
// The below changes whose turn it is.
public void changeturn() {
whoseturn = (whoseturn + 1)%2;
}
// The below returns the current player's name.
public String getCurrentPlayer() {
return players[whoseturn];
}
// This will Prints out the playing board.
public void printboard() {
System.out.println(" 0 1 2"); //print part
for (int i=0; i<3; i++) {
System.out.print(i+" ");
for (int j=0; j<3; j++)
System.out.print(board[i][j]+" ");
System.out.println();
}
}
// The below returns a character indicating the winner.
public char winner() {
// The below Checks for three X's or O's in a row.
for (int i=0; i<3; i++) //for loop
if (SameArray(board[i]) && board[i][0] != '_') //if condition
return board[i][0];
// The below Check for three X's or O's in a column.
for (int i=0; i<3; i++) //for loop
if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && board[0][i] != '_') //if condition
return board[0][i];
// The below Checks forward diagonal. Here, we know if there are three blanks, no one has one. //if condition
if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]))
return board[0][0];
// The below Checks backward diagonal. Here, we know if there are three blanks, no one has one.
if ((board[2][0] == board[1][1]) && (board[1][1] == board[0][2])) //if condition
return board[2][0];
if (movesmade == 9) //if condition
return 'T';
return '_';
}
// Checks to see if all the characters in a single character array are
// the same.
private static boolean SameArray(char[] word) {
char check = word[0];
for (int i=1; i<word.length; i++)
if (check != word[i]) //if condition
return false;
return true;
}
// Returns the player who's playing piece is the character x.
public String whosePiece(char x) {
for (int i=0; i<2; i++)
if (x == pieces[i]) //if condition
return players[i];
return "Dummy";
}
public static void main(String[] args) throws IOException { //for exception
Scanner stdin = new Scanner(System.in); //input taken
// To Read in players' names.
System.out.println("Player #1, enter your name.");
String name1 = stdin.next();
System.out.println("Player #2, enter your name.");
String name2 = stdin.next();
// To Create the TiTaTo object.
TiTaTomygame = new TicTacToe(name1, name2);
// To Play as long as there is no winner or tie.
while (mygame.winner() == '_') {
int r,c;
boolean done = false;
// To Read in a move & check if it's valid.
do {
mygame.printboard();
System.out.print(mygame.getCurrentPlayer());
System.out.print(", Enter the row(0-2) and column(0-2) ");
System.out.println("of your move.");
r = stdin.nextInt();
c = stdin.nextInt();
if (!mygame.inbounds(r,c)) //if condition
System.out.println("Sorry, those are invalid entries.");
else {
if (!mygame.Move(r,c))
System.out.println("Sorry, that square is taken.");
else
done = true;
}
} while (!done); //while loop
// This Changes who's turn it is.
mygame.changeturn(); //changeturn() method defined here
}
// The below Prints out a message with the winner.
mygame.printboard();
char win = mygame.winner();
if (win == 'T') //if condition
System.out.println("Both of you played to a tie."); //print section
else {
System.out.print("Congratulations, " + mygame.whosePiece(win));
System.out.println(", you have won the game.");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.