Java Console Games Tangibles
Completing the Java Console Games Hacks.
import java.util.Scanner;
class TickTackToe {
// Class to represent the Tic Tac Toe game
private Scanner scTTT; // Scanner object to read input
private String[] board; // Array to represent the Tic Tac Toe board
private String currentPlayer; // Current player (X or O)
private int turn; // Number of turns played
1
public void play() {
// Method to start the game
initializeGame(); // Initialize the game variables
System.out.println("Do you want to play against a friend or the computer?");
System.out.println("Type 1 for friend, 2 for computer");
int choice = scTTT.nextInt(); // Read user's choice
if (choice == 1) {
playAgainstFriend(); // Play against a friend
} else if (choice == 2) {
playAgainstComputer(); // Play against the computer
}
scTTT.close(); // Close the scanner
}
private void initializeGame() {
// Method to initialize game variables
scTTT = new Scanner(System.in); // Initialize the scanner
board = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9"}; // Initialize the board
currentPlayer = "X"; // Start with player X
turn = 0; // No turns played yet
}
private void displayBoard() {
// Method to display the current state of the board
for (int i = 0; i < 9; i += 3) {
System.out.println(board[i] + " | " + board[i + 1] + " | " + board[i + 2]);
}
// Display the board in the format of a Tic Tac Toe grid
}
private void switchPlayer() {
// Method to switch the current player (X <-> O)
currentPlayer = (currentPlayer.equals("X")) ? "O" : "X";
// If currentPlayer is X, switch to O, and vice versa
}
private boolean isSquareTaken(int move) {
// Method to check if a square on the board is already taken
return board[move - 1].equals("X") || board[move - 1].equals("O");
// Check if the chosen square is already marked by X or O
}
private void announceWinner() {
// Method to announce the winner of the game
System.out.println("Player " + currentPlayer + " wins!");
// Print the winning player (X or O)
}
private boolean checkWinCondition() {
// Method to check if there's a winning condition on the board
// Check rows for a winning line
for (int i = 0; i < 9; i += 3) {
if (board[i].equals(currentPlayer) && board[i + 1].equals(currentPlayer) && board[i + 2].equals(currentPlayer)) {
return true; // Return true if there's a winning line
}
}
// Check columns for a winning line
for (int i = 0; i < 3; i++) {
if (board[i].equals(currentPlayer) && board[i + 3].equals(currentPlayer) && board[i + 6].equals(currentPlayer)) {
return true; // Return true if there's a winning line
}
}
// Check diagonals for a winning line
if ((board[0].equals(currentPlayer) && board[4].equals(currentPlayer) && board[8].equals(currentPlayer)) ||
(board[2].equals(currentPlayer) && board[4].equals(currentPlayer) && board[6].equals(currentPlayer))) {
return true; // Return true if there's a winning line
}
return false; // No winning line found
}
private boolean isBoardFull() {
// Method to check if the board is completely filled
for (String square : board) {
if (!square.equals("X") && !square.equals("O")) {
return false; // Return false if there's an empty square
}
}
return true; // All squares are filled
}
private void playAgainstFriend() {
// Method to play against a friend
System.out.println("Type the number of the square you want to place your piece in");
boolean quit = false; // Variable to control the game loop
while (!quit) {
displayBoard(); // Display the current state of the board
System.out.println("Player " + currentPlayer + "'s turn");
int move = scTTT.nextInt(); // Read the player's move
if (move < 1 || move > 9 || isSquareTaken(move)) {
System.out.println("Invalid move, try again");
} else {
board[move - 1] = currentPlayer; // Mark the chosen square
turn++; // Increment the turn counter
if (checkWinCondition()) {
displayBoard(); // Display the final board
announceWinner(); // Announce the winner
quit = true; // Exit the game loop
} else if (isBoardFull()) {
displayBoard(); // Display the final board
System.out.println("It's a tie!"); // Announce a tie
quit = true; // Exit the game loop
} else {
switchPlayer(); // Switch to the other player
}
}
}
}
private void playAgainstComputer() {
// Method to play against the computer
System.out.println("Type the number of the square you want to place your piece in");
boolean quit = false; // Variable to control the game loop
while (!quit) {
displayBoard(); // Display the current state of the board
if (currentPlayer.equals("X")) {
System.out.println("Player 1's turn");
int move = scTTT.nextInt(); // Read the player's move
if (move < 1 || move > 9 || isSquareTaken(move)) {
System.out.println("Invalid move, try again");
} else {
board[move - 1] = currentPlayer; // Mark the chosen square
turn++; // Increment the turn counter
if (checkWinCondition()) {
displayBoard(); // Display the final board
announceWinner(); // Announce the winner
quit = true; // Exit the game loop
} else if (isBoardFull()) {
displayBoard(); // Display the final board
System.out.println("It's a tie!"); // Announce a tie
quit = true; // Exit the game loop
} else {
switchPlayer(); // Switch to the other player
}
}
} else {
System.out.println("Computer's turn");
int move = (int) (Math.random() * 9) + 1; // Generate a random move
while (isSquareTaken(move)) {
move = (int) (Math.random() * 9) + 1; // Find an available random move
}
board[move - 1] = currentPlayer; // Mark the computer's move
turn++; // Increment the turn counter
if (checkWinCondition()) {
displayBoard(); // Display the final board
System.out.println("Computer wins!"); // Announce the computer's win
quit = true; // Exit the game loop
} else if (isBoardFull()) {
displayBoard(); // Display the final board
System.out.println("It's a tie!"); // Announce a tie
quit = true; // Exit the game loop
} else {
switchPlayer(); // Switch to the other player
}
}
}
}
public static void main(String[] args) {
// Main method to start the game
TicTacToeGame game = new TicTacToeGame(); // Create a new game instance
game.play(); // Start the game
}
}
class HigherOrLower {
public void horl() {
System.out.println("Higher or Lower");
System.out.println("You have three guesses to guess the number I am thinking of between 1-8.");
System.out.println("If you guess the number correctly, you win!");
Scanner scHL = new Scanner(System.in);
int randomG = (int) (Math.random() * 8) + 1;
int guess = scHL.nextInt();
for (int i = 3; i > 0; i--) {
if (guess == randomG) {
System.out.println("You win!");
break;
} else if (guess > randomG) {
System.out.println("The number is lower");
} else if (guess < randomG) {
System.out.println("The number is higher");
}
guess = scHL.nextInt();
}
System.out.println("Game over.");
scHL.close();
}
}
class RockPaperScissors {
public void rps() {
System.out.println("Rock Paper Scissors");
System.out.println("Type r for rock, p for paper, or s for scissors");
Scanner scRPS = new Scanner(System.in);
String userChoice = scRPS.nextLine().toLowerCase();
Boolean quit = false;
int random = (int) (Math.random() * 3);
while (quit == false) {
if (userChoice.equals("r")) {
if (random == 1) {
System.out.println("You chose rock \nThe computer chose paper \nYou lose!");
} else if (random == 2) {
System.out.println("You chose rock \nThe computer chose scissors \nYou win!");
} else {
System.out.println("You chose rock \nThe computer chose rock \nIt's a tie!");
}
quit = true;
} else if (userChoice.equals("p")) {
if (random == 1) {
System.out.println("You chose paper \nThe computer chose paper \nIt's a tie!");
} else if (random == 2) {
System.out.println("You chose paper \nThe computer chose scissors \nYou lose!");
} else {
System.out.println("You chose paper \nThe computer chose rock \nYou win!");
}
quit = true;
} else if (userChoice.equals("s")) {
if (random == 1) {
System.out.println("You chose scissors \nThe computer chose paper \nYou win!");
} else if (random == 2) {
System.out.println("You chose scissors \nThe computer chose scissors \nIt's a tie!");
} else {
System.out.println("You chose scissors \nThe computer chose rock \nYou lose!");
}
quit = true;
} else {
System.out.println("Invalid input, try again");
userChoice = scRPS.nextLine();
}
}
scRPS.close();
}
}
import java.util.Scanner;
public static void displayMenu() {
System.out.println("Type 1 for Tic Tac Toe, 2 for Higher or Lower, 3 for Rock Paper Scissors, or 4 to exit");
}
public static void selectGame(Scanner sc) {
int choice = sc.nextInt();
switch (choice) {
case 1:
TicTacToeGame ttt = new TicTacToeGame();
ttt.play();
break;
case 2:
HigherOrLower hol = new HigherOrLower();
hol.horl();
break;
case 3:
RockPaperScissors rps = new RockPaperScissors();
rps.rps();
break;
case 4:
// Exit case
System.out.println("Exiting the game. Goodbye!");
break;
default:
System.out.println("Invalid input, try again");
selectGame(sc);
}
}
Scanner sc = new Scanner(System.in);
displayMenu();
selectGame(sc);
sc.close();
Type 1 for Tic Tac Toe, 2 for Higher or Lower, 3 for Rock Paper Scissors, or 4 to exit
Do you want to play against a friend or the computer?
Type 1 for friend, 2 for computer
Type the number of the square you want to place your piece in
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
Player 1's turn
X | 2 | 3
4 | 5 | 6
7 | 8 | 9
Computer's turn
X | 2 | 3
4 | 5 | O
7 | 8 | 9
Player 1's turn
X | X | 3
4 | 5 | O
7 | 8 | 9
Computer's turn
X | X | 3
4 | 5 | O
7 | 8 | O
Player 1's turn
X | X | X
4 | 5 | O
7 | 8 | O
Player X wins!