Write a java tic-tack-toe program for a 13x13 size board with the following cons
ID: 3714328 • Letter: W
Question
Write a java tic-tack-toe program for a 13x13 size board with the following constraints:
First write a java command line program called Player (with a main method) that can play a game of tick tack toe via sockets. Selecting optimal moves at each turn is not important, but that the program can take a turn (wait for a message to go) and then select and its move (communicate a move), finally exiting when the game is over (someone has won or no moves are left by returning a system code 0 or 1 exit status).
Write a java class Spawn that implements the Runnable interface and can create a process that calls a Player program, some form board representation will need to be built.
Write the main method that can start Spawn threads and will act as the Server to talk to the resulting processes that will play the game.
Visualize this complex interaction by showing what is happening with the sockets, threads or processes.
Explanation / Answer
Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
TTT game = new TTT();
game.initializeBoard();
System.out.println("Tic-Tac-Toe!");
do
{
System.out.println("Current board layout:");
game.printBoard();
int row;
int col;
do
{
System.out.println("Player " + game.getCurrentPlayerMark() + ", enter an empty row and column to place your mark!");
row = scan.nextInt()-1;
col = scan.nextInt()-1;
}
while (!game.placeMark(row, col));
game.changePlayer();
}
while(!game.checkForWin() && !game.isBoardFull());
if (game.isBoardFull() && !game.checkForWin())
{
System.out.println("The game was a tie!");
}
else
{
System.out.println("Current board layout:");
game.printBoard();
game.changePlayer();
System.out.println(Character.toUpperCase(game.getCurrentPlayerMark()) + " Wins!");
}
}
}
TTT.java
public class TTT {
private char[][] board;
private char currentPlayerMark;
public TTT() {
board = new char[3][3];
currentPlayerMark = 'x';
initializeBoard();
}
//Gives us access to currentPlayerMark
public char getCurrentPlayerMark()
{
return currentPlayerMark;
}
// Set/Reset the board back to all empty values.
public void initializeBoard() {
// Loop through rows
for (int i = 0; i < 3; i++) {
// Loop through columns
for (int j = 0; j < 3; j++) {
board[i][j] = '-';
}
}
}
// Print the current board (may be replaced by GUI implementation later)
public void printBoard() {
System.out.println("-------------");
for (int i = 0; i < 3; i++) {
System.out.print("| ");
for (int j = 0; j < 3; j++) {
System.out.print(board[i][j] + " | ");
}
System.out.println();
System.out.println("-------------");
}
}
// Loop through all cells of the board and if one is found to be empty (contains char '-') then return false.
// Otherwise the board is full.
public boolean isBoardFull() {
boolean isFull = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '-') {
isFull = false;
}
}
}
return isFull;
}
// Returns true if there is a win, false otherwise.
// This calls our other win check functions to check the entire board.
public boolean checkForWin() {
return (checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin());
}
// Loop through rows and see if any are winners.
private boolean checkRowsForWin() {
for (int i = 0; i < 3; i++) {
if (checkRowCol(board[i][0], board[i][1], board[i][2]) == true) {
return true;
}
}
return false;
}
// Loop through columns and see if any are winners.
private boolean checkColumnsForWin() {
for (int i = 0; i < 3; i++) {
if (checkRowCol(board[0][i], board[1][i], board[2][i]) == true) {
return true;
}
}
return false;
}
// Check the two diagonals to see if either is a win. Return true if either wins.
private boolean checkDiagonalsForWin() {
return ((checkRowCol(board[0][0], board[1][1], board[2][2]) == true) || (checkRowCol(board[0][2], board[1][1], board[2][0]) == true));
}
// Check to see if all three values are the same (and not empty) indicating a win.
private boolean checkRowCol(char c1, char c2, char c3) {
return ((c1 != '-') && (c1 == c2) && (c2 == c3));
}
// Change player marks back and forth.
public void changePlayer() {
if (currentPlayerMark == 'x') {
currentPlayerMark = 'o';
}
else {
currentPlayerMark = 'x';
}
}
// Places a mark at the cell specified by row and col with the mark of the current player.
public boolean placeMark(int row, int col) {
// Make sure that row and column are in bounds of the board.
if ((row >= 0) && (row < 3)) {
if ((col >= 0) && (col < 3)) {
if (board[row][col] == '-') {
board[row][col] = currentPlayerMark;
return true;
}
}
}
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.