Lab 01
Tic Tac Toe
Objective:
Write a class called TicTacToeBoard which is used by a driver to play a game of Tic Tac Toe. Download the driver which contains the game loop and the user input. DO NOT MODIFY THE DRIVER!
The class TicTacToeBoard needs to have the following:
Instance Variables
A 2D 3x3 matrix of Strings called spaces. This will be used to select the where the player put their marker
Class Variables (these need to be public and constant)
A String called EMPTY_SPACE that
Program Code:
//TicTacToeBoard class: that creates the board and contains the logic to
//dispaly the board while playing and displays the winners
public class TicTacToeBoard
{
//2-D String array
String [][]spaces=new String[3][3];
//static variables
String EMPTY_SPACE="_";
static String X_SPACE="X";
static String O_SPACE="O";
//constructor
public TicTacToeBoard()
{
initilaizeBoard();
}
//initialises the board to the initial state
public void initilaizeBoard()
{
for(int i=0;i<spaces.length;i++)
for(int j=0;j<spaces[i].length;j++)
spaces[i][j]=EMPTY_SPACE;
}
//checks the cells in the board
private boolean checkHorizontalVertical(String c1, String c2, String c3)
{
return ((!c1.equals("_")) && (c1.equals(c2)) && (c2.equals(c3)));
}
//clears the board: re-initializes to its original state
public void clearBoard()
{
initilaizeBoard();
}
//drawBoard method will displays the board
public void drawBoard()
{
for (int i = 0; i < 3; i++)
{
System.out.print("| ");
for (int j = 0; j < 3; j++)
{
System.out.print(spaces[i][j] + " | ");
}
System.out.println();
}
}
//this method checks for the row wise cells whether they are filled
//and are filled with the same values
public boolean checkHorizontalVictory()
{
for (int i = 0; i < 3; i++)
{
if (checkHorizontalVertical(spaces[i][0], spaces[i][1], spaces[i][2]) == true)
{
return true;
}
}
return false;
}
//this method checks for the column wise cells whether they are filled
//and are filled with the same values
public boolean checkVerticalVictory()
{
for (int i = 0; i < 3; i++) {
if (checkHorizontalVertical(spaces[0][i], spaces[1][i], spaces[2][i]) == true) {
return true;
}
}
return false;
}
//this method checks for the diagonal wise cells whether they are filled
//and are filled with the same values
public boolean checkDiagonalVictory()
{
return ((checkHorizontalVertical(spaces[0][0], spaces[1][1], spaces[2][2]) == true) || (checkHorizontalVertical(spaces[0][2], spaces[1][1], spaces[2][0]) == true));
}
//this method will declares the winner by looking
//over all the cell in the board
public boolean checkVictory()
{
return (checkHorizontalVictory() || checkVerticalVictory() || checkDiagonalVictory());
}
//this method checks whether the board is filled or not
public boolean checkFilledBoard()
{
boolean isFull = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (spaces[i][j].equals("_")) {
isFull = false;
}
}
}
return isFull;
}
//this method will fill the cells in the board depending
//on the user input coordinates
public boolean setSpace(int x, int y, String marker )
{
if ((x >= 0) && (x < 3))
{
if ((y >= 0) && (y<3))
{
if (spaces[x][y].equals("_"))
{
spaces[x][y] = marker;
return true;
}
}
}
return false;
}
}
------------------------------------------------------------------------------------------------
//TicTacToeGame.java
import java.util.Scanner;
public class TicTacToeGame
{
public static void main(String[] args)
{
//Constructs the tic tac toe board
TicTacToeBoard board = new TicTacToeBoard();
//Sets up the scanner for user input
Scanner keyboard = new Scanner(System.in);
//These hold the coordinates that the user will enter to put their marker
//X's are player 1 and O's are player two
int xVal = 0;
int yVal = 0;
//Runs the game loop
boolean gameOver = false;
//Determines if it is player 1 or player 2
boolean player1Turn = true;
System.out.println("Welcome to Tic Tac Toe");
while(gameOver == false)//Game loop
{
board.drawBoard();
System.out.println("Enter coordinates player "+(player1Turn?"1":"2"));
xVal = keyboard.nextInt();
yVal = keyboard.nextInt();
//Using the x and y val it attempts to set that space to an X or O based on whose turn it is.
if(board.setSpace(xVal, yVal, player1Turn?TicTacToeBoard.X_SPACE:TicTacToeBoard.O_SPACE))
{
//After the marker is placed a winning configuration is searched
if(board.checkVictory())
{
board.drawBoard();
//Depending who put the marker down determines the winner
System.out.println("Player "+ (player1Turn?"1":"2")+ " wins!");
//Determines whether or not to repeat the game
System.out.println("Would you like to play again? Enter \"yes\" or \"no\"");
String decision = keyboard.next();
if(decision.equalsIgnoreCase("no"))//ends the game
{
System.out.println("Goodbye");
break;
}
//Clears the board and starts over with player 1
board.clearBoard();
player1Turn = true;
}
//Checks if the board is completely filled
else if(board.checkFilledBoard())
{
System.out.println("Board is filled!");
//Determines whether or not to repeat the game
System.out.println("Would you like to play again? Enter \"yes\" or \"no\"");
String decision = keyboard.next();
if(decision.equalsIgnoreCase("no"))//ends the game
{
System.out.println("Goodbye");
break;
}
//Clears the board and starts over with player 1
board.clearBoard();
player1Turn = true;
}
else
{
//Changes the player's turn
player1Turn = !player1Turn;
}
}
else
{
//If a space wasn't empty or was out of the board range then prompt with an error
System.out.println("That's an invalid space. Try again.");
}
}
}
}
------------------------------------------------------------------------------------------------
Sample output:
Welcome to Tic Tac Toe
| _ | _ | _ |
| _ | _ | _ |
| _ | _ | _ |
Enter coordinates player 1
0
2
| _ | _ | X |
| _ | _ | _ |
| _ | _ | _ |
Enter coordinates player 2
1
0
| _ | _ | X |
| O | _ | _ |
| _ | _ | _ |
Enter coordinates player 1
0 0
| X | _ | X |
| O | _ | _ |
| _ | _ | _ |
Enter coordinates player 2
2 0
| X | _ | X |
| O | _ | _ |
| O | _ | _ |
Enter coordinates player 1
0 1
| X | X | X |
| O | _ | _ |
| O | _ | _ |
Player 1 wins!
Would you like to play again? Enter "yes" or "no"
no
Goodbye
Program Code:
//TicTacToeBoard class: that creates the board and contains the logic to
//dispaly the board while playing and displays the winners
public class TicTacToeBoard
{
//2-D String array
String [][]spaces=new String[3][3];
//static variables
String EMPTY_SPACE="_";
static String X_SPACE="X";
static String O_SPACE="O";
//constructor
public TicTacToeBoard()
{
initilaizeBoard();
}
//initialises the board to the initial state
public void initilaizeBoard()
{
for(int i=0;i<spaces.length;i++)
for(int j=0;j<spaces[i].length;j++)
spaces[i][j]=EMPTY_SPACE;
}
//checks the cells in the board
private boolean checkHorizontalVertical(String c1, String c2, String c3)
{
return ((!c1.equals("_")) && (c1.equals(c2)) && (c2.equals(c3)));
}
//clears the board: re-initializes to its original state
public void clearBoard()
{
initilaizeBoard();
}
//drawBoard method will displays the board
public void drawBoard()
{
for (int i = 0; i < 3; i++)
{
System.out.print("| ");
for (int j = 0; j < 3; j++)
{
System.out.print(spaces[i][j] + " | ");
}
System.out.println();
}
}
//this method checks for the row wise cells whether they are filled
//and are filled with the same values
public boolean checkHorizontalVictory()
{
for (int i = 0; i < 3; i++)
{
if (checkHorizontalVertical(spaces[i][0], spaces[i][1], spaces[i][2]) == true)
{
return true;
}
}
return false;
}
//this method checks for the column wise cells whether they are filled
//and are filled with the same values
public boolean checkVerticalVictory()
{
for (int i = 0; i < 3; i++) {
if (checkHorizontalVertical(spaces[0][i], spaces[1][i], spaces[2][i]) == true) {
return true;
}
}
return false;
}
//this method checks for the diagonal wise cells whether they are filled
//and are filled with the same values
public boolean checkDiagonalVictory()
{
return ((checkHorizontalVertical(spaces[0][0], spaces[1][1], spaces[2][2]) == true) || (checkHorizontalVertical(spaces[0][2], spaces[1][1], spaces[2][0]) == true));
}
//this method will declares the winner by looking
//over all the cell in the board
public boolean checkVictory()
{
return (checkHorizontalVictory() || checkVerticalVictory() || checkDiagonalVictory());
}
//this method checks whether the board is filled or not
public boolean checkFilledBoard()
{
boolean isFull = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (spaces[i][j].equals("_")) {
isFull = false;
}
}
}
return isFull;
}
//this method will fill the cells in the board depending
//on the user input coordinates
public boolean setSpace(int x, int y, String marker )
{
if ((x >= 0) && (x < 3))
{
if ((y >= 0) && (y<3))
{
if (spaces[x][y].equals("_"))
{
spaces[x][y] = marker;
return true;
}
}
}
return false;
}
}
------------------------------------------------------------------------------------------------
//TicTacToeGame.java
import java.util.Scanner;
public class TicTacToeGame
{
public static void main(String[] args)
{
//Constructs the tic tac toe board
TicTacToeBoard board = new TicTacToeBoard();
//Sets up the scanner for user input
Scanner keyboard = new Scanner(System.in);
//These hold the coordinates that the user will enter to put their marker
//X's are player 1 and O's are player two
int xVal = 0;
int yVal = 0;
//Runs the game loop
boolean gameOver = false;
//Determines if it is player 1 or player 2
boolean player1Turn = true;
System.out.println("Welcome to Tic Tac Toe");
while(gameOver == false)//Game loop
{
board.drawBoard();
System.out.println("Enter coordinates player "+(player1Turn?"1":"2"));
xVal = keyboard.nextInt();
yVal = keyboard.nextInt();
//Using the x and y val it attempts to set that space to an X or O based on whose turn it is.
if(board.setSpace(xVal, yVal, player1Turn?TicTacToeBoard.X_SPACE:TicTacToeBoard.O_SPACE))
{
//After the marker is placed a winning configuration is searched
if(board.checkVictory())
{
board.drawBoard();
//Depending who put the marker down determines the winner
System.out.println("Player "+ (player1Turn?"1":"2")+ " wins!");
//Determines whether or not to repeat the game
System.out.println("Would you like to play again? Enter \"yes\" or \"no\"");
String decision = keyboard.next();
if(decision.equalsIgnoreCase("no"))//ends the game
{
System.out.println("Goodbye");
break;
}
//Clears the board and starts over with player 1
board.clearBoard();
player1Turn = true;
}
//Checks if the board is completely filled
else if(board.checkFilledBoard())
{
System.out.println("Board is filled!");
//Determines whether or not to repeat the game
System.out.println("Would you like to play again? Enter \"yes\" or \"no\"");
String decision = keyboard.next();
if(decision.equalsIgnoreCase("no"))//ends the game
{
System.out.println("Goodbye");
break;
}
//Clears the board and starts over with player 1
board.clearBoard();
player1Turn = true;
}
else
{
//Changes the player's turn
player1Turn = !player1Turn;
}
}
else
{
//If a space wasn't empty or was out of the board range then prompt with an error
System.out.println("That's an invalid space. Try again.");
}
}
}
}
------------------------------------------------------------------------------------------------
Sample output:
Welcome to Tic Tac Toe
| _ | _ | _ |
| _ | _ | _ |
| _ | _ | _ |
Enter coordinates player 1
0
2
| _ | _ | X |
| _ | _ | _ |
| _ | _ | _ |
Enter coordinates player 2
1
0
| _ | _ | X |
| O | _ | _ |
| _ | _ | _ |
Enter coordinates player 1
0 0
| X | _ | X |
| O | _ | _ |
| _ | _ | _ |
Enter coordinates player 2
2 0
| X | _ | X |
| O | _ | _ |
| O | _ | _ |
Enter coordinates player 1
0 1
| X | X | X |
| O | _ | _ |
| O | _ | _ |
Player 1 wins!
Would you like to play again? Enter "yes" or "no"
no
Goodbye
Lab 01 Tic Tac Toe Objective: Write a class called TicTacToeBoard which is used by a...
(Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...
Please make this into one class. JAVA Please: Tic Tac Toe class - Write a fifth game program that can play Tic Tac Toe. This game displays the lines of the Tic Tac Toe game and prompts the player to choose a move. The move is recorded on the screen and the computer picks his move from those that are left. The player then picks his next move. The program should allow only legal moves. The program should indicate when...
(Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array of integers. The constructor should initialize the empty board to all 0s. Allow two human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has...
(C++) Please create a tic tac toe game. Must write a Player class to store each players’ information (name and score) and to update their information (increase their score if appropriate) and to retrieve their information (name and score).The players must be called by name during the game. The turns alternate starting with player 1 in the first move of round 1. Whoever plays last in a round will play second in the next round (assuming there is one).The rows...
18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...
1. Use Turtle Graphics to create a tic tac toe game in Python. Write a Python program that allows for one player vs computer to play tic tac toe game, without using turtle.turtle
PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1. Create the data structure – Nine slots that can each contain an X, an O, or a blank. – To represent the board with a dictionary, you can assign each slot a string-value key. – String values in the key-value pair to represent what’s in each slot on the board: ■ 'X' ■ 'O' ■ ‘ ‘ 2. Create a function to print the...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...