Create a Tic Tac Toe Program Sample output from your program should look like th
ID: 3689955 • Letter: C
Question
Create a Tic Tac Toe Program Sample output from your program should look like the following: ===============================e ---a------b------c--- | | | | ---d------e------f--- | | | | ---g------h------i--- | | | | --------------------- Entering input for: player1: O enter in cell [a, b, ... i]e ===============================e ---a------b------c--- | | | | ---d------e------f--- | | O | | ---g------h------i--- | | | | --------------------- Entering input for: player2: X enter in cell [a, b, ... i]b ===============================e ---a------b------c--- | | X | | ---d------e------f--- | | O | | ---g------h------i--- | | | | --------------------- Entering input for: player1: O enter in cell [a, b, ... i]i ===============================e ---a------b------c--- | | X | | ---d------e------f--- | | O | | ---g------h------i--- | | | O | --------------------- Entering input for: player2: X enter in cell [a, b, ... i]a ===============================e ---a------b------c--- | X | X | | ---d------e------f--- | | O | | ---g------h------i--- | | | O | --------------------- Entering input for: player1: O enter in cell [a, b, ... i]c ===============================e ---a------b------c--- | X | X | O | ---d------e------f--- | | O | | ---g------h------i--- | | | O | --------------------- Entering input for: player2: X enter in cell [a, b, ... i]f ===============================e ---a------b------c--- | X | X | O | ---d------e------f--- | | O | X | ---g------h------i--- | | | O | --------------------- Entering input for: player1: O enter in cell [a, b, ... i]g ******** We have a winner: player1: O ===============================e ---a------b------c--- | X | X | O | ---d------e------f--- | | O | X | ---g------h------i--- | O | | O | --------------------- Do you want to play Tic-Tac-Toe (y/n)?y ===============================e ---a------b------c--- | | | | ---d------e------f--- | | | | ---g------h------i--- | | | | --------------------- Entering input for: player1: O enter in cell [a, b, ... i]e ===============================e ---a------b------c--- | | | | ---d------e------f--- | | O | | ---g------h------i--- | | | | --------------------- Entering input for: player2: X enter in cell [a, b, ... i]a ===============================e ---a------b------c--- | X | | | ---d------e------f--- | | O | | ---g------h------i--- | | | | --------------------- Entering input for: player1: O enter in cell [a, b, ... i]c ===============================e ---a------b------c--- | X | | O | ---d------e------f--- | | O | | ---g------h------i--- | | | | --------------------- Entering input for: player2: X enter in cell [a, b, ... i]g ===============================e ---a------b------c--- | X | | O | ---d------e------f--- | | O | | ---g------h------i--- | X | | | --------------------- Entering input for: player1: O enter in cell [a, b, ... i]d ===============================e ---a------b------c--- | X | | O | ---d------e------f--- | O | O | | ---g------h------i--- | X | | | --------------------- Entering input for: player2: X enter in cell [a, b, ... i]f ===============================e ---a------b------c--- | X | | O | ---d------e------f--- | O | O | X | ---g------h------i--- | X | | | --------------------- Entering input for: player1: O enter in cell [a, b, ... i]h ===============================e ---a------b------c--- | X | | O | ---d------e------f--- | O | O | X | ---g------h------i--- | X | O | | --------------------- Entering input for: player2: X enter in cell [a, b, ... i]b ===============================e ---a------b------c--- | X | X | O | ---d------e------f--- | O | O | X | ---g------h------i--- | X | O | | --------------------- Entering input for: player1: O enter in cell [a, b, ... i]i ===============================e ---a------b------c--- | X | X | O | ---d------e------f--- | O | O | X | ---g------h------i--- | X | O | O | --------------------- ******** Game Ends in Draw Do you want to play Tic-Tac-Toe (y/n)?n Bye ************************************************************ In your JH7_worksheet.txt file, I want you to paste in at least 2 games played. One should have a winner, and another should be a draw. At the end, you should terminate tic_tac_toe. A starting point for your program is provided (if you feel overly constrained by this and have a better idea on structuring this game you can create your own structure -- main should not have very much code):
package tictactoe; import java.util.*; class TicTacToe { char ttt[][] = new char[3][3]; static final char player1 = 'O'; static final char player2 = 'X'; Scanner scan =new Scanner(System.in); String playerID(char player) { // Prints the identity of the player // For example: // player2: X if (player == player1) return "player1: "+player; else return "player2: "+ player; } void getPlayerInput(char player) { // ******* Details to fill in ************ // Prompt the user for a cell input. Make sure its a legal // cell designator. Also make sure the cell doesn't already // have a player in it. Prompt the user again in the case // of any problem. Once a valid spot has been found, // you will issue a statement like: ttt[row][col]=player; } boolean gameIsDraw() { // ******* Details to fill in ************ // If all ttt entries have been taken return true // otherwise return false } boolean winner(char player) { // ******* Details to fill in ************ // Check to see if the parameter player has won // Winning means that player shows up in a line of // three. The line can be a column, row or a diagonal // Return true if player is a winner, otherwise return false. } void displayBoard() { // ******* Details to fill in ************ // If you want to skip the fun of this, I have a candidate displayBoard down below } void newgame() { char currPlayer = player1; for(int i=0; i<3; i++) for(int j=0; j<3; j++) ttt[i][j] =' '; boolean continueFlag = true; while (continueFlag) { displayBoard(); if (gameIsDraw()) { System.out.println("Game Ends in Draw"); continueFlag = false; } else { getPlayerInput(currPlayer); if (winner(currPlayer)) { System.out.println("We have a winner: " + playerID(currPlayer)); displayBoard(); continueFlag = false; } else { if (currPlayer == player1) currPlayer = player2; else currPlayer = player1; } } } } public static void main(String[] args) { TicTacToe game = new TicTacToe(); String str; do { game.newgame(); System.out.println("Do you want to play Tic-Tac-Toe (y/n)?"); str= game.scan.next(); } while ("y".equals(str)); System.out.println("Bye"); } } By the way, I'm going to give a version of displayBoard(). This routine is a bit of a pain and I'm not sure how much learning happens by going through the pain. Also, I'm not real happy with my version, but it at least works. So... if you want to use my version, here it is: void displayBoard() { System.out.println("********************************"); System.out.println(" ---a------b------c---"); for (int i=0; i<3; i++) { for (int j=0; j< 3; j++) { if (j == 0) System.out.print(" | "); System.out.print(ttt[i][j]); if (j < 2) System.out.print( " | "); if (j==2) System.out.print(" |"); } System.out.println(); switch (i) { case 0: System.out.println(" ---d------e------f---"); break; case 1: System.out.println(" ---g------h------i---"); break; case 2: System.out.println(" ---------------------"); break; } } }
Explanation / Answer
package Help;
import java.util.Scanner;
/**
* Tic-Tac-Toe: Two-player console, non-graphics, non-OO version.
* All variables/methods are declared as static (belong to the class)
* in the non-OO version.
*/
public class TTTConsoleNonOO2P {
// Name-constants to represent the seeds and cell contents
public static final int EMPTY = 0;
public static final int CROSS = 1;
public static final int NOUGHT = 2;
// Name-constants to represent the various states of the game
public static final int PLAYING = 0;
public static final int DRAW = 1;
public static final int CROSS_WON = 2;
public static final int NOUGHT_WON = 3;
// The game board and the game status
public static final int ROWS = 3, COLS = 3; // number of rows and columns
public static int[][] board = new int[ROWS][COLS]; // game board in 2D array
// containing (EMPTY, CROSS, NOUGHT)
public static int currentState; // the current state of the game
// (PLAYING, DRAW, CROSS_WON, NOUGHT_WON)
public static int currentPlayer; // the current player (CROSS or NOUGHT)
public static int currntRow, currentCol; // current seed's row and column
public static Scanner in = new Scanner(System.in); // the input Scanner
/** The entry main method (the program starts here) */
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
// Initialize the game-board and current status
initGame();
// Play the game once
do {
playerMove (currentPlayer); // update currentRow and currentCol
updateGame(currentPlayer, currntRow, currentCol); // update currentState
printBoard();
// Print message if game-over
if (currentState == CROSS_WON) {
System.out.println("'X' won! Bye!");
System.out.println("Do you want to play Tic-Tac-Toe (y/n)?");
String str =sc.next();
if ("y".equals(str)){
str= initGame();
}
else
break;
} else if (currentState == NOUGHT_WON) {
System.out.println("'O' won! Bye!");
System.out.println("Do you want to play Tic-Tac-Toe (y/n)?");
String str =sc.next();
if ("y".equals(str)){
str= initGame();
}
else
break;
} else if (currentState == DRAW) {
System.out.println("It's a Draw! Bye!");
System.out.println("Do you want to play Tic-Tac-Toe (y/n)?");
String str =sc.next();
if ("y".equals(str)){
str= initGame();
}
else
break;
}
// Switch player
currentPlayer = (currentPlayer == CROSS) ? NOUGHT : CROSS;
} while (currentState == PLAYING); // repeat if not game-over
}
/** Initialize the game-board contents and the current states
* @return */
public static String initGame() {
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLS; ++col) {
board[row][col] = EMPTY; // all cells empty
}
}
currentState = PLAYING; // ready to play
currentPlayer = CROSS; // cross plays first
return null;
}
/** Player with the "theSeed" makes one move, with input validation.
Update global variables "currentRow" and "currentCol". */
public static void playerMove(int theSeed) {
boolean validInput = false; // for input validation
do {
if (theSeed == CROSS) {
System.out.println("Player who want to enter 'X', enter your move for choose your row 1 to 3 & column 1 to 3: ");
} else {
System.out.println("Player who want to enter 'O', enter your move for choose your row 1 to 3 & column 1 to 3:");
}
int row = in.nextInt() - 1; // array index starts at 0 instead of 1
int col = in.nextInt() - 1;
if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY) {
currntRow = row;
currentCol = col;
board[currntRow][currentCol] = theSeed; // update game-board content
validInput = true; // input okay, exit loop
} else {
System.out.println("This move at (" + (row + 1) + "," + (col + 1)
+ ") is not valid. Try again...");
}
} while (!validInput); // repeat until input is valid
}
/** Update the "currentState" after the player with "theSeed" has placed on
(currentRow, currentCol). */
public static void updateGame(int theSeed, int currentRow, int currentCol) {
if (hasWon(theSeed, currentRow, currentCol)) { // check if winning move
currentState = (theSeed == CROSS) ? CROSS_WON : NOUGHT_WON;
} else if (isDraw()) { // check for draw
currentState = DRAW;
}
// Otherwise, no change to currentState (still PLAYING).
}
/** Return true if it is a draw (no more empty cell) */
// TODO: Shall declare draw if no player can "possibly" win
public static boolean isDraw() {
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLS; ++col) {
if (board[row][col] == EMPTY) {
return false; // an empty cell found, not draw, exit
}
}
}
return true; // no empty cell, it's a draw
}
/** Return true if the player with "theSeed" has won after placing at
(currentRow, currentCol) */
public static boolean hasWon(int theSeed, int currentRow, int currentCol) {
return (board[currentRow][0] == theSeed // 3-in-the-row
&& board[currentRow][1] == theSeed
&& board[currentRow][2] == theSeed
|| board[0][currentCol] == theSeed // 3-in-the-column
&& board[1][currentCol] == theSeed
&& board[2][currentCol] == theSeed
|| currentRow == currentCol // 3-in-the-diagonal
&& board[0][0] == theSeed
&& board[1][1] == theSeed
&& board[2][2] == theSeed
|| currentRow + currentCol == 2 // 3-in-the-opposite-diagonal
&& board[0][2] == theSeed
&& board[1][1] == theSeed
&& board[2][0] == theSeed);
}
/** Print the game board */
public static void printBoard() {
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLS; ++col) {
printCell(board[row][col]); // print each of the cells
if (col != COLS - 1) {
System.out.print("|"); // print vertical partition
}
}
System.out.println();
if (row != ROWS - 1) {
System.out.println("-----------"); // print horizontal partition
}
}
System.out.println();
}
/** Print a cell with the specified "content" */
public static void printCell(int content) {
switch (content) {
case EMPTY: System.out.print(" "); break;
case NOUGHT: System.out.print(" O "); break;
case CROSS: System.out.print(" X "); break;
}
}
}
OUTPUT ::
Player who want to enter 'X', enter your move for choose your row 1 to 3 & column 1 to 3:
1
1
X | |
-----------
| |
-----------
| |
Player who want to enter 'O', enter your move for choose your row 1 to 3 & column 1 to 3:
2
2
X | |
-----------
| O |
-----------
| |
Player who want to enter 'X', enter your move for choose your row 1 to 3 & column 1 to 3:
21
1
This move at (21,1) is not valid. Try again...
Player who want to enter 'X', enter your move for choose your row 1 to 3 & column 1 to 3:
2
1
X | |
-----------
X | O |
-----------
| |
Player who want to enter 'O', enter your move for choose your row 1 to 3 & column 1 to 3:
33
3
This move at (33,3) is not valid. Try again...
Player who want to enter 'O', enter your move for choose your row 1 to 3 & column 1 to 3:
3
2
X | |
-----------
X | O |
-----------
| O |
Player who want to enter 'X', enter your move for choose your row 1 to 3 & column 1 to 3:
1
3
X | | X
-----------
X | O |
-----------
| O |
Player who want to enter 'O', enter your move for choose your row 1 to 3 & column 1 to 3:
3
3
X | | X
-----------
X | O |
-----------
| O | O
Player who want to enter 'X', enter your move for choose your row 1 to 3 & column 1 to 3:
1
3
This move at (1,3) is not valid. Try again...
Player who want to enter 'X', enter your move for choose your row 1 to 3 & column 1 to 3:
3
1
X | | X
-----------
X | O |
-----------
X | O | O
'X' won! Bye!
Do you want to play Tic-Tac-Toe (y/n)?
y
Player who want to enter 'O', enter your move for choose your row 1 to 3 & column 1 to 3:
1
1
O | |
-----------
| |
-----------
| |
Player who want to enter 'X', enter your move for choose your row 1 to 3 & column 1 to 3:
21
2
This move at (21,2) is not valid. Try again...
Player who want to enter 'X', enter your move for choose your row 1 to 3 & column 1 to 3:
1
3
O | | X
-----------
| |
-----------
| |
Player who want to enter 'O', enter your move for choose your row 1 to 3 & column 1 to 3:
2
1
O | | X
-----------
O | |
-----------
| |
Player who want to enter 'X', enter your move for choose your row 1 to 3 & column 1 to 3:
2
2
O | | X
-----------
O | X |
-----------
| |
Player who want to enter 'O', enter your move for choose your row 1 to 3 & column 1 to 3:
3
1
O | | X
-----------
O | X |
-----------
O | |
'O' won! Bye!
Do you want to play Tic-Tac-Toe (y/n)?
n