For my computer science class, we were asked to write a BASIC java program that
ID: 3532302 • Letter: F
Question
For my computer science class, we were asked to write a BASIC java program that allows 2 players to play Tic Tac Toe. It is not supposed to use arrays, graphics, AI, or anything else. Just methods, statements, branches, loops!
here is the skeleton code we are supposed to work with:
import java.util.*;
public class TicTacToeSkeleton {
public static final String EMPTY = " ";
public static final String X = "X";
public static final String O = "O";
public static String square1;
public static String square2;
public static String square3;
public static String square4;
public static String square5;
public static String square6;
public static String square7;
public static String square8;
public static String square9;
public static void main(String[] args) {
initialize();
playGame();
}
/**
* Step 2
* Sets up anything that needs to happen before the game
* begins and starts the game.
*/
public static void initialize() {
// Add your code here
}
/**
* Step 3
* Runs the main loop that contains all the instructions for
* one turn during the game.
*
* @return true if player 1 won, false otherwise.
*/
public static boolean playGame() {
// Add your code here
return false;
}
// Add methods for steps 3a and 3b here
/**
* Step 3c
* Puts a mark in the square specified
*
* @param square The user's input
* @param mark The X or O that should be placed in the location corresponding
* to the specified square.
*/
public static void markSquare(String square, String mark) {
// Add your code here
}
// Add a method for step 3d here
}
Explanation / Answer
import java.util.Scanner;
public class TicTac
{
//Constants
public final static int HEIGHT = 3;
public final static int WIDTH = 3;
public final static boolean CONTINUE = true;
public final static boolean END = false;
public final static String PLAYER1 = "O";
public final static String PLAYER2= "X";
//Atributes
public String[][] grid;
public boolean status;
public String player;
//Constructors
public TicTac()
{
player = PLAYER1;
status = CONTINUE;
grid = new String[HEIGHT][WIDTH];
for(int i = 0; i < HEIGHT; i++)
{
for(int j = 0; j < WIDTH; j++)
{
grid[i][j] = "-" ;
}
}
}
public boolean isEmpty(int pY, int pX)
{
if(grid [pY][pX].equals("-"))
{
return true;
}
return false;
}
public boolean placeMove(String player, int pX, int pY)
{
if(isEmpty(pY, pX))
{
grid[pY][pX] = player;
return true;
}
return false;
}
public String getGrid()
{
String grid1 = " " + grid[0][0] + " " + grid[0][1] + " " + grid[0][2] + " ";
String grid2 = " " + grid[1][0] + " " + grid[1][1] + " " + grid[1][2] + " ";
String grid3 = " " + grid[2][0] + " " + grid[2][1] + " " + grid[2][2] + " ";
return grid1 +grid2 + grid3;
}
public void changePlayer()
{
if (player.equals(PLAYER1))
{
player = PLAYER2;
}
else
{
player = PLAYER1;
}
}
public void hasWon()
{
if( grid[0][0].equals( grid[0][1]) && grid[0][2].equals(grid[0][0]) && grid[0][0].equals("O"))
{
status = END;
}
else if( grid[1][0].equals( grid[1][1]) && grid[1][2].equals(grid[1][0]) && grid[1][0].equals("O"))
{
status = END;
}
else if( grid[2][0].equals( grid[2][1]) && grid[2][2].equals(grid[2][0]) && grid[2][0].equals("O"))
{
status = END;
}
else if( grid[0][0].equals( grid[1][1]) && grid[2][2].equals(grid[0][0]) && grid[0][0].equals("O"))
{
status = END;
}
else if( grid[0][2].equals( grid[1][1]) && grid[2][0].equals(grid[0][2]) && grid[0][2].equals("O") )
{
status = END;
}
}
public static void main (String [] args)
{
TicTac tt = new TicTac();
Scanner keyboard = new Scanner(System.in);
int py = -1;
int px = -1;
while(tt.status)
{
if(tt.player.equals(PLAYER1))
{
System.out.println("Player 1, in wich row are you going to play");
py = keyboard.nextInt() - 1;
System.out.println("Player 1, in wich column are you going to play");
px = keyboard.nextInt() - 1;
}
else if(tt.player.equals(PLAYER2))
{
System.out.println("Player 2, in wich row are you going to play");
py = keyboard.nextInt() - 1;
System.out.println("Player 2, in wich column are you going to play");
px = keyboard.nextInt() - 1;
}
while(!tt.placeMove(tt.player, px, py))
{
if(tt.player.equals(PLAYER1))
{
System.out.println("Player 1, in wich row are you going to play");
py = keyboard.nextInt() - 1;
System.out.println("Player 1, in wich column are you going to play");
px = keyboard.nextInt() - 1;
}
else if(tt.player.equals(PLAYER2))
{
System.out.println("Player 2, in wich row are you going to play");
py = keyboard.nextInt() - 1;
System.out.println("Player 2, in wich column are you going to play");
px = keyboard.nextInt() - 1;
}
tt.placeMove(tt.player, px, py);
System.out.print(tt.getGrid());
tt.hasWon();
tt.changePlayer();
}
System.out.print(tt.getGrid());
tt.hasWon();
tt.changePlayer();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.