A Java-based tic-tac-toe game that runs on the command line interface. The game utilizes 2D arrays and ArrayLists to implement the game logic and provide an enjoyable gaming experience.
This is an implementation of the classic game "Tic Tac Toe" in Java. It creates a game board as a 2-D array of characters and allows the player to enter their placement, followed by the computer's placement, on the board using a while loop that continues until a winner is declared or the board is filled. The program uses a series of conditions to check if there is a winner, using lists to represent the possible winning combinations. If a winner is declared, the program breaks out of the while loop and prints the result.
The code starts by importing the java.util.*
package, which includes several utility classes such as Scanner
and Random
. It then defines two static ArrayLists, playerPositions
and cpuPositions
, which keep track of the positions chosen by the player and the computer, respectively.
The main
function creates the game board and prints it using the printGameBoard
method. It then enters a while loop that runs until a winner is declared or the board is filled. Inside the loop, the player is prompted to enter their placement using a Scanner
, and their choice is checked against the playerPositions
and cpuPositions
ArrayLists to make sure they have not already chosen that position. If the position is valid, the program uses the placePiece
method to update the game board and add the position to the playerPositions
ArrayList. It then checks if there is a winner using the checkWinner
method and prints the result if necessary.
Next, the program generates a random cpuPosition
using the Random
class, and checks it against the playerPositions
and cpuPositions
ArrayLists to ensure it has not already been chosen. The placePiece
method is then called again to update the game board and add the position to the cpuPositions
ArrayList. The printGameBoard
method is called again to print the updated board, and the program checks if there is a winner using the checkWinner
method and prints the result if necessary.
The printGameBoard
method is a simple nested for loop that iterates over the 2-D game board array and prints each character symbol in each row.
The placePiece
method takes the game board, position, and user as arguments. It uses a switch statement to update the game board based on the position chosen by the player or the computer. It also adds the position to the appropriate ArrayList (playerPositions
for the player and cpuPositions
for the computer).
The checkWinner
method checks if the playerPositions
or cpuPositions
ArrayList contains any of the possible winning combinations stored in List objects. If one of the combinations is found in playerPositions
, the program returns "Congratulations You Won!". If one of the combinations is found in cpuPositions
, the program returns "CPU Wins! Sorry :( ". If there are no winning combinations and the board is filled, the program returns "Tie Game!".
Submitted by Pallav Goswami (pallavgoswami)
Download packets of source code on Coders Packet
Comments