patternjavaMinor
Tic-Tac-Toe Game (Java)
Viewed 0 times
toeticjavatacgame
Problem
This program runs the game tic-tac-toe within the console. The players, X and O, take turns placing their character onto the 3x3 grid. When a player successfully makes 3 in a row, either vertically, horizontally, or diagonally, they win. If no winner is decided upon all squares being filled, it is a stalemate.
I'm wondering how efficient this code is. Are there any redundancies, formatting issues, legibility issues, etc.?
TicTacToe.java
GameBoard.java
I'm wondering how efficient this code is. Are there any redundancies, formatting issues, legibility issues, etc.?
TicTacToe.java
package tictactoe;
public class TicTacToe {
public static void main(String[] args) {
GameBoard myGame = new GameBoard();
myGame.displayBoard();
int counter = 1;
while (myGame.gameActive() && counter < 10) {
if (counter % 2 == 0)
myGame.askPlayer('O');
else
myGame.askPlayer('X');
counter++;
System.out.println("\n");
myGame.displayBoard();
myGame.checkForWinner();
if (counter == 10 && myGame.gameOnGoing() == true)
System.out.print("Stalemate!\n");
}
}
}GameBoard.java
package tictactoe;
import java.util.Arrays;
import java.util.Scanner;
public class GameBoard {
private char[][] gameBoard;
private boolean gameOnGoing = true;
//This is the constructor for the GameBoard class.
public GameBoard() {
gameBoard = new char[3][3];
for (int row=0; row 3 || row 3 || col =0 && row =0 && col <=2) {
if (gameBoard[row][col] != ' ')
return false;
else {
gameBoard[row][col] = player;
return true;
}
}
else
return false;
} //end of method makeMove
}Solution
Below are just some quick points. I didn't get into the meat of the
TicTacToe.java
-
You're using a Magic Number here for your counter. This should be extracted as a constant.
-
The stalemate conditional can be placed outside of the while block, as you already know that
-
I would also extract the
-
There's rarely a point in having a
-
Regarding comments, you should use proper JavaDoc comments for your methods, and there's no need to comment the end of every method. So this,
should be this:
GameBoard class.TicTacToe.java
-
You're using a Magic Number here for your counter. This should be extracted as a constant.
-
The stalemate conditional can be placed outside of the while block, as you already know that
counter = 10.-
I would also extract the
X and O chars as constants. I'm curious about what will happen when an unexpected char is passed to askPlayer.-
There's rarely a point in having a
\n in a print or println statement. In your finaly line, you can just have System.out.println("Stalemate!");
-
You have all this game logic in a main method, when it should properly encapsulated into its own separate method.
GameBoard.java
-
Minor point, but I wouldn't initialize gameOnGoing to true by default. Rather, I'd have it set after everything was initialized in the constructor.
-
Extract the gameboard size, 3` as a constant.-
Regarding comments, you should use proper JavaDoc comments for your methods, and there's no need to comment the end of every method. So this,
//This is the constructor for the GameBoard class.
public GameBoard() {
gameBoard = new char[3][3];
for (int row=0; row < gameBoard.length; row++) {
Arrays.fill(gameBoard[row], ' ');
}
} //end of constructorshould be this:
/**
* This is the constructor for the GameBoard class.
*/
public GameBoard() {
gameBoard = new char[3][3];
for (int row=0; row < gameBoard.length; row++) {
Arrays.fill(gameBoard[row], ' ');
}
}Code Snippets
//This is the constructor for the GameBoard class.
public GameBoard() {
gameBoard = new char[3][3];
for (int row=0; row < gameBoard.length; row++) {
Arrays.fill(gameBoard[row], ' ');
}
} //end of constructor/**
* This is the constructor for the GameBoard class.
*/
public GameBoard() {
gameBoard = new char[3][3];
for (int row=0; row < gameBoard.length; row++) {
Arrays.fill(gameBoard[row], ' ');
}
}Context
StackExchange Code Review Q#107328, answer score: 3
Revisions (0)
No revisions yet.