-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameDriver.java
More file actions
105 lines (91 loc) · 3.95 KB
/
GameDriver.java
File metadata and controls
105 lines (91 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/******************************************************************************
* Written by: Tyler Moroni, George Gherardi *
* Code for group project *
******************************************************************************/
import java.io.*;
import java.util.*;
public class GameDriver {
public static void main(String[] args) {
Scanner scanner;
int choice;
scanner= new Scanner(System.in);
System.out.println("Welcome to the Game Center!");
boolean playAgain;
do {
// Asks what game would like to be played
System.out.println("Select a game to play:");
System.out.println("1. Connect Four (by Tyler)");
System.out.println("2. Reversi (by George)");
System.out.println("3. Another Game (To Be Implemented)");
choice = scanner.nextInt();
// Choices for games go here
switch (choice) {
case 1:
playConnectFour();
break;
case 2:
playReversi();
break;
case 3:
System.out.println("Sorry, the third game is not" +
" implemented yet.");
break;
default:
System.out.println("Invalid choice. Please select a" +
" valid game.");
}
// When game is done ask if you want to change to another game
System.out.print("Do you want to play another game? (yes/no): ");
String response = scanner.next().toLowerCase();
playAgain = response.equals("yes");
} while (playAgain);
System.out.println("Thanks for playing at the Game Center!");
}
// Code for game 1 (written by Tyler)
public static void playConnectFour() {
Scanner scanner;
String player1Name, player2Name;
scanner = new Scanner(System.in);
System.out.println("Welcome to Connect Four!");
// Asks for player name in order to correctly show whos turn it is
player1Name = getPlayerName(scanner, "Player 1");
player2Name = getPlayerName(scanner, "Player 2");
// Running code to see if the player would like to play again
boolean playAgain;
do {
ConnectFourGame.playConnectFour(player1Name, player2Name);
System.out.print("Do you want to play Connect Four again?" +
"(yes/no): ");
String response = scanner.next().toLowerCase();
playAgain = response.equals("yes");
} while (playAgain);
}
public static String getPlayerName(Scanner scanner, String playerLabel) {
System.out.print(playerLabel + ", enter your name: ");
return scanner.next();
}
// Code for game 2 (Reversi) Written by George
public static void playReversi() {
Scanner scanner = new Scanner(System.in);
String player1, player2;
System.out.println("Welcome to Reversi!");
player1 = getPlayerName(scanner, "Player 1");
player2 = getPlayerName(scanner, "Player 2");
ReversiGame game = new ReversiGame(player1, player2);
while (!game.isGameOver()) {
game.printBoard();
System.out.println("Current Player: " + game.currentPlayer.getName());
System.out.print("Enter your move (row column, e.g., 1 2): ");
int row = scanner.nextInt() - 1;
int col = scanner.nextInt() - 1;
game.makeMove(row, col);
}
game.printBoard();
char winner = game.getWinner();
if (winner == ' ') {
System.out.println("It's a tie!");
} else {
System.out.println("Player " + winner + " wins!");
}
}
}