Program Description: Create a game to play 3x3 Tic-Tac-Toe. It should be a 2 pla
ID: 440597 • Letter: P
Question
Program Description: Create a game to play 3x3 Tic-Tac-Toe. It should be a 2 player game, alternating between two symbols. The symbols should be X and O. The X player goes first. The game continues until one player wins (gets 3 in a row; across, down, or diagonally) or until all places are marked which is a stalemate. Game Specifications: 1. Tic-Tac-Toe is a turn based game. On each turn, it should display the current status of the game, prompt for the next user input, and advise of any error conditions. 2. On each turn, the game should ask the current player which row and column to mark their symbol. 3. The game should determine when the game ends and if there is a winner. 4. The game should provide a means to quit before the official end of game. 5. When the game is over, the output should indicate who won and offer the user the option to play again or quit. 6. Your program should be able to handle all user input without crashing. 7. Your program must allow for all possible outcomes for the game. Program Specifications: 1. All user input and output should be done using System.out and System.in. 2. You must use object-oriented programming; you must have at least 3 meaningful classes! 3. You should start designing the game with UML diagrams and pseudocode. You must submit a complete UML for all your implemented classes and pseudocode for at least one major algorithm in your program. These should either be in PowerPoint or Word and submitted with your program. 4. You should use good programming style and must document your program completely with javadoc markup syntax. a. Each class should include: i. Description of the class ii. @author tag b. Each method should include: i. Description of method ii. @param tags to describe your input parameters iii. @return tag to describe your return typeExplanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TicTacToe {
private char[][] board = new char[3][3];
private String player1;
private String player2;
private int currentPlayer;
private char marker1;
private char marker2;
private int plays;
private BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
protected void init() {
int counter = 0;
for (int i = 0; i < 3; i++) {
for (int i1 = 0; i1 < 3; i1++) {
board[i][i1] = Character.forDigit(++counter, 10);
}
}
currentPlayer = 1;
plays = 0;
}
protected void switchPlayers() {
if (getCurrentPlayer() == 1) {
setCurrentPlayer(2);
} else {
setCurrentPlayer(1);
}
setPlays(getPlays() + 1);
}
protected boolean placeMarker(int play) {
for (int i = 0; i < 3; i++) {
for (int i1 = 0; i1 < 3; i1++) {
if (board[i][i1] == Character.forDigit(play, 10)) {
board[i][i1] = (getCurrentPlayer() == 1) ? getMarker1() : getMarker2();
return true;
}
}
}
return false;
}
protected boolean winner() {
//Checking rows
char current = ' ';
for (int i = 0; i < 3; i++) {
int i1 = 0;
for (i1 = 0; i1 < 3; i1++) {
if (!Character.isLetter(board[i][i1])) {
break;
}
if (i1 == 0) {
current = board[i][i1];
} else if (current != board[i][i1]) {
break;
}
if (i1 == 2) {
//Found winner
return true;
}
}
}
//Checking columns
for (int i = 0; i < 3; i++) {
current = ' ';
int i1 = 0;
for (i1 = 0; i1 < 3; i1++) {
if (!Character.isLetter(board[i1][i])) {
break;
}
if (i1 == 0) {
current = board[i1][i];
} else if (current != board[i1][i]) {
break;
}
if (i1 == 2) {
//Found winner
return true;
}
}
}
//Checking diagonals
current = board[0][0];
if (Character.isLetter(current) && board[1][1] == current && board[2][2] == current) {
return true;
}
current = board[2][0];
if (Character.isLetter(current) && board[1][1] == current && board[0][2] == current) {
return true;
}
return false;
}
protected String getRules() {
StringBuilder builder = new StringBuilder();
builder.append("Players take turns marking a square. Only squares ");
builder.append("not already marked can be picked. Once a player has ");
builder.append("marked three squares in a row, they win! If all squares ");
builder.append("are marked and no three squares are the same, a tied game is declared. ");
builder.append("Have Fun! ");
return builder.toString();
}
protected String getPrompt() {
String prompt = "";
try {
prompt = reader.readLine();
} catch (IOException ex) {
ex.printStackTrace();
}
return prompt;
}
protected String drawBoard() {
StringBuilder builder = new StringBuilder("Game board: ");
for (int i = 0; i < 3; i++) {
for (int i1 = 0; i1 < 3; i1++) {
builder.append("[" + board[i][i1] + "]");
}
builder.append(" ");
}
return builder.toString();
}
public int getCurrentPlayer() {
return currentPlayer;
}
public void setCurrentPlayer(int currentPlayer) {
this.currentPlayer = currentPlayer;
}
public char getMarker1() {
return marker1;
}
public void setMarker1(char marker1) {
this.marker1 = marker1;
}
public char getMarker2() {
return marker2;
}
public void setMarker2(char marker2) {
this.marker2 = marker2;
}
public int getPlays() {
return plays;
}
public void setPlays(int plays) {
this.plays = plays;
}
public String getPlayer1() {
return player1;
}
public void setPlayer1(String player1) {
this.player1 = player1;
}
public String getPlayer2() {
return player2;
}
public void setPlayer2(String player2) {
this.player2 = player2;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.