Simple Tic Tac Toe (two players) game development with Java programming language.
Introduction
The game which I've built using Java programming language is played between two players and it's fairly simple. It has two classes - Main.java and GameBoard.java.
The class, GameBoard.java contains the necessary constructor and methods and in the Main.java class, a new instance of the GameBoard.java class is created to start the game.
There is a video at the end of this blog where you can see how the game would look like in the end.
The class, GameBoard.java contains the necessary constructor and methods and in the Main.java class, a new instance of the GameBoard.java class is created to start the game.
There is a video at the end of this blog where you can see how the game would look like in the end.
Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class GameBoard { | |
private char[][] gameBoard; | |
private boolean gameActive; | |
// The constructor GameBaord() creates a two dimensional array containing white space(s). | |
public GameBoard() { | |
gameBoard = new char[3][3]; | |
gameActive = true; | |
for (int i = 0; i < 3; i++) { | |
Arrays.fill (gameBoard[i], ' '); | |
} | |
} | |
// It sets the boolean variable to either true or false. | |
void setGameActive(boolean input) { | |
gameActive = input; | |
} | |
// It returns the boolean variable, 'gameActive'. | |
boolean gameOngoing() { | |
return gameActive; | |
} | |
// This method displays the board. | |
void displayBoard() { | |
System.out.println("\n"); | |
for (int i = 0; i < 3; i++) { | |
for (int j = 0; j < 3; j++) { | |
System.out.print(gameBoard[i][j]); | |
if (j == 0 || j == 1) { | |
System.out.print(" | "); | |
} | |
} | |
if (i == 0 || i == 1) { | |
System.out.println("\n------------"); | |
} | |
} | |
System.out.println("\n"); | |
} | |
// This method resets the game board. | |
void resetBoard() { | |
setGameActive(true); | |
for (int i = 0; i < 3; i++) { | |
Arrays.fill(gameBoard[i], ' '); | |
} | |
} | |
/** | |
* This method returns true if certain box in the game board is empty. | |
* Otherwise, it returns false. | |
**/ | |
boolean isEmpty(int row, int col) { | |
if (gameBoard[row-1][col-1] == ' ') { | |
return true; | |
} else { | |
System.out.println("\nThis place is already taken."); | |
return false; | |
} | |
} | |
// This method returns true if a player makes any invalid move. | |
boolean notValid(int row, int col) { | |
if (row > 3 || row < 1 || col < 1 || col > 3 || !isEmpty(row, col)) { | |
System.out.println("\nInvalid move!"); | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* This method assigns certain character (i.e, 'X' or 'O') | |
* to certain location in the two dimensional array, gameBoard. | |
**/ | |
void makeMove(int row, int col, char player) { | |
gameBoard[row-1][col-1] = player; | |
} | |
// Asks player to make a move. | |
void askPlayer(char player) { | |
int row, col; | |
Scanner sc = new Scanner(System.in); | |
do { | |
System.out.printf("\n Player %c, enter a row (1-3): ", player); | |
row = sc.nextInt(); | |
System.out.printf("\n Player %c, enter a column (1-3): ", player); | |
col = sc.nextInt(); | |
} while (notValid(row, col)); | |
makeMove(row, col, player); | |
} | |
// Checks for winner and sets the boolean variablee gameActive, false. | |
void checkForWinner() { | |
for (int i = 0; i < 3; i++) { | |
if (gameBoard[i][0] == gameBoard[i][1] && gameBoard[i][0] == | |
gameBoard[i][2] && gameOngoing() && gameBoard[i][0] != ' ') { | |
System.out.printf("\n\tThe winner is player %c. Congratulations!\n", gameBoard[i][0]); | |
setGameActive(false); | |
} | |
if (gameBoard[0][i] == gameBoard[1][i] && gameBoard[0][i] == | |
gameBoard[2][i] && gameOngoing() && gameBoard[0][i] != ' ') { | |
System.out.printf("\n\tThe winner is player %c. Congratulations!\n", gameBoard[0][i]); | |
setGameActive(false); | |
} | |
} | |
if (gameBoard[0][0] == gameBoard[1][1] && gameBoard[0][0] == | |
gameBoard[2][2] && gameOngoing() && gameBoard[0][0] != ' ') { | |
System.out.printf("\n\tThe winner is player %c. Congratulations!\n", gameBoard[0][0]); | |
setGameActive(false); | |
} | |
if (gameBoard[0][2] == gameBoard[1][1] && gameBoard[0][2] == | |
gameBoard[2][0] && gameOngoing() && gameBoard[0][2] != ' ') { | |
System.out.printf("\n\tThe winner is player %c. Congratulations!\n", gameBoard[0][0]); | |
setGameActive(false); | |
} | |
} | |
// Starts the game. | |
void startGame() { | |
displayBoard(); | |
System.out.println("\n\tPlayer X will make the first move. Good luck!"); | |
int counter = 0; | |
while(gameActive) { | |
if (counter % 2 == 0) { | |
askPlayer('X'); | |
} else { | |
askPlayer('O'); | |
} | |
displayBoard(); | |
checkForWinner(); | |
counter++; | |
if (counter == 9) { | |
if (gameActive) { | |
System.out.println("\nThe match is a tie."); | |
setGameActive(false); | |
} | |
} | |
} | |
while (!gameActive) { | |
restart(); | |
} | |
} | |
// This method restarts the game. | |
void restart() { | |
if (!gameActive) { | |
System.out.println("\nDo you want to play again? (y/n)"); | |
Scanner sc = new Scanner(System.in); | |
String yn = sc.nextLine(); | |
if (yn.equals("y")) { | |
resetBoard(); | |
startGame(); | |
} else if (yn.equals("n")) { | |
System.out.println("\n\tExiting game..."); | |
System.exit(0); | |
} else { | |
System.out.println("\nThe computer is unable to understand the input you've provided. Try again."); | |
restart(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class Main { | |
public static void main(String[] args) { | |
GameBoard g = new GameBoard(); | |
g.startGame(); | |
} | |
} |
This is how in the end the game would look like...
It's not much. But, I believe building such small projects may help those who are getting started with object-oriented programming to grasp the major concepts of it.
HAPPY CODING!
HAPPY CODING!
Comments
Post a Comment