[35m___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|[0m Menu! [35m|
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower |
| 3 - Tic Tac Toe |
|_________________________| [0m
Choose an option.
1: Rock Paper Scissors
Type r for rock, p for paper, or s for scissors
You chose rock
The computer chose scissors
You win!
[0m
[35m___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|[0m Menu! [35m|
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower |
| 3 - Tic Tac Toe |
|_________________________| [0m
Choose an option.
2: Higher or Lower
You have three guesses to guess the number I am thinking of between 1-8.
If you guess the number correctly, you win!
java.util.InputMismatchException: Not a number, try again.
[35m___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|[0m Menu! [35m|
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower |
| 3 - Tic Tac Toe |
|_________________________| [0m
Choose an option.
java.util.InputMismatchException: Not a number, try again.
[35m___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|[0m Menu! [35m|
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower |
| 3 - Tic Tac Toe |
|_________________________| [0m
Choose an option.
24234214: Unexpected choice, try again.[0m
[35m___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|[0m Menu! [35m|
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower |
| 3 - Tic Tac Toe |
|_________________________| [0m
Choose an option.
public void ticTacToe(){
System.out.println("Tic Tac Toe");
Scanner scTTT = new Scanner(System.in);
String[] board = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
String player = "X";
String player2 = "O";
int turn = 0;
Boolean quit = false;
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();
//make tic tac toe using player1 and player2
if(choice == 1){
System.out.println("Type the number of the square you want to place your piece in");
while(quit == false){
System.out.println("Player 1's turn (X)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move = scTTT.nextInt();
if(board[move - 1].equals("X") || board[move - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move - 1] = player;
turn++;
if(board[0].equals("X") && board[1].equals("X") && board[2].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[3].equals("X") && board[4].equals("X") && board[5].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[6].equals("X") && board[7].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[3].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[1].equals("X") && board[4].equals("X") && board[7].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[5].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[4].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[4].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(turn == 9){
System.out.println("It's a tie!");
quit = true;
}
else{
System.out.println("Player 2's turn (O)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move2 = scTTT.nextInt();
if(board[move2 - 1].equals("X") || board[move2 - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move2 - 1] = player2;
turn++;
if(board[0].equals("O") && board[1].equals("O") && board[2].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[3].equals("O") && board[4].equals("O") && board[5].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[6].equals("O") && board[7].equals("O") && board[8].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[0].equals("O") && board[3].equals("O") && board[6].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[1].equals("O") && board[4].equals("O") && board[7].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[2].equals("O") && board[5].equals("O") && board[8].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
}
}
}
}
}
if(choice == 2){
String computer = "O";
System.out.println("Type the number of the square you want to place your piece in");
while(quit == false){
System.out.println("Player 1's turn (X)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move = scTTT.nextInt();
if(board[move - 1].equals("X") || board[move - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move - 1] = player;
turn++;
if(board[0].equals("X") && board[1].equals("X") && board[2].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[3].equals("X") && board[4].equals("X") && board[5].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[6].equals("X") && board[7].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[3].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[1].equals("X") && board[4].equals("X") && board[7].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[5].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[4].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[4].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(turn == 9){
System.out.println("It's a tie!");
quit = true;
}
else{
System.out.println("Computer's turn (O)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move2 = (int)(Math.random() * 9) + 1;
if(board[move2 - 1].equals("X") || board[move2 - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move2 - 1] = computer;
turn++;
if(board[0].equals("O") && board[1].equals("O") && board[2].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[3].equals("O") && board[4].equals("O") && board[5].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[6].equals("O") && board[7].equals("O") && board[8].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[0].equals("O") && board[3].equals("O") && board[6].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[1].equals("O") && board[4].equals("O") && board[7].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[2].equals("O") && board[5].equals("O") && board[8].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
}
}
}
}
}
scTTT.close();
}
import java.util.Scanner;
import java.util.Random;
public class ConsoleGame {
private Scanner scanner = new Scanner(System.in);
private Random random = new Random();
public static void main(String[] args) {
new ConsoleGame().run();
}
private void run() {
boolean quit = false;
while (!quit) {
printMenu();
int choice = readIntInput("Choose an option: ");
quit = performAction(choice);
}
scanner.close();
}
private void printMenu() {
System.out.println(
"___________________________\n" +
"|~~~~~~~~~~~~~~~~~~~~~~~~~|\n" +
"| Menu! |\n" +
"|~~~~~~~~~~~~~~~~~~~~~~~~~|\n" +
"| 0 - Exit |\n" +
"| 1 - Rock Paper Scissors |\n" +
"| 2 - Higher or Lower |\n" +
"| 3 - Tic Tac Toe |\n" +
"|_________________________|"
);
}
private int readIntInput(String message) {
System.out.print(message);
return scanner.nextInt();
}
private boolean performAction(int choice) {
switch (choice) {
case 0:
System.out.println("Goodbye, World!");
return true;
case 1:
playRPS();
break;
case 2:
playHigherOrLower();
break;
case 3:
playTicTacToe();
break;
default:
System.out.println("Unexpected choice, try again.");
}
return false;
}
private void playRPS() {
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();
}
private void playHigherOrLower() {
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();
}
private void playTicTacToe() {
System.out.println("Tic Tac Toe");
Scanner scTTT = new Scanner(System.in);
String[] board = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
String player = "X";
String player2 = "O";
int turn = 0;
Boolean quit = false;
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();
//make tic tac toe using player1 and player2
if(choice == 1){
System.out.println("Type the number of the square you want to place your piece in");
while(quit == false){
System.out.println("Player 1's turn (X)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move = scTTT.nextInt();
if(board[move - 1].equals("X") || board[move - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move - 1] = player;
turn++;
if(board[0].equals("X") && board[1].equals("X") && board[2].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[3].equals("X") && board[4].equals("X") && board[5].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[6].equals("X") && board[7].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[3].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[1].equals("X") && board[4].equals("X") && board[7].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[5].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[4].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[4].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(turn == 9){
System.out.println("It's a tie!");
quit = true;
}
else{
System.out.println("Player 2's turn (O)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move2 = scTTT.nextInt();
if(board[move2 - 1].equals("X") || board[move2 - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move2 - 1] = player2;
turn++;
if(board[0].equals("O") && board[1].equals("O") && board[2].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[3].equals("O") && board[4].equals("O") && board[5].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[6].equals("O") && board[7].equals("O") && board[8].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[0].equals("O") && board[3].equals("O") && board[6].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[1].equals("O") && board[4].equals("O") && board[7].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[2].equals("O") && board[5].equals("O") && board[8].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
}
}
}
}
}
if(choice == 2){
String computer = "O";
System.out.println("Type the number of the square you want to place your piece in");
while(quit == false){
System.out.println("Player 1's turn (X)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move = scTTT.nextInt();
if(board[move - 1].equals("X") || board[move - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move - 1] = player;
turn++;
if(board[0].equals("X") && board[1].equals("X") && board[2].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[3].equals("X") && board[4].equals("X") && board[5].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[6].equals("X") && board[7].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[3].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[1].equals("X") && board[4].equals("X") && board[7].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[5].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[4].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[4].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(turn == 9){
System.out.println("It's a tie!");
quit = true;
}
else{
System.out.println("Computer's turn (O)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move2 = (int)(Math.random() * 9) + 1;
if(board[move2 - 1].equals("X") || board[move2 - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move2 - 1] = computer;
turn++;
if(board[0].equals("O") && board[1].equals("O") && board[2].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[3].equals("O") && board[4].equals("O") && board[5].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[6].equals("O") && board[7].equals("O") && board[8].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[0].equals("O") && board[3].equals("O") && board[6].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[1].equals("O") && board[4].equals("O") && board[7].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[2].equals("O") && board[5].equals("O") && board[8].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
}
}
}
}
}
scTTT.close();
}
}