-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameEngine.java
More file actions
127 lines (119 loc) · 3.11 KB
/
GameEngine.java
File metadata and controls
127 lines (119 loc) · 3.11 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.util.*;
public class GameEngine{
private Player[] players;
private GameBoard board;
public GameEngine(){
board = new GameBoard();
players = new Player[2];
}
public void start(){
Scanner sc = new Scanner(System.in);
boolean flag = true;
int play = 0;
while(flag){
flag = false;
System.out.println("1. Play Human vs. Human\n2. Play Human vs AI\n3. Play AI vs AI");
play = sc.nextInt();
if(play < 1 || play > 3){
System.out.println("Please enter a valid number");
sc.nextLine();
flag = true;
}
}
if(play == 1){
this.initHumanVSHuman();
}
else if(play == 2){
this.initHumanVSAI();
} else{
this.initAIVSAI();
}
}
private void initHumanVSHuman(){
Scanner sc = new Scanner(System.in);
System.out.println("Player 1");
System.out.print("Name: ");
String name = sc.nextLine();
Player player1 = new HumanPlayer(name, 1);
System.out.println("Player 2");
System.out.print("Name: ");
name = sc.nextLine();
Player player2 = new HumanPlayer(name, 2);
players[0] = player1;
players[1] = player2;
this.playGame();
}
private void initHumanVSAI(){
Scanner sc = new Scanner(System.in);
System.out.println("Player 1");
System.out.print("Name: ");
String name = sc.nextLine();
Player player1 = new HumanPlayer(name, 1);
Player AI = this.createAI(2);
players[0] = player1;
players[1] = AI;
this.playGame();
}
private void initAIVSAI(){
Player AI1 = this.createAI(1);
Player AI2 = this.createAI(2);
players[0] = AI1;
players[1] = AI2;
this.playGame();
}
private Player createAI(int theNum){
Scanner sc = new Scanner(System.in);
boolean flag = true;
int aiType = 0;
Player ai;
System.out.println("\nWhat AI would you like?");
while(flag){
flag = false;
System.out.println("1. Dumb AI\n2. Basic AI\n3. Advanced AI");
aiType = sc.nextInt();
if(aiType < 1 || aiType > 3){
System.out.println("Please enter a valid AI number number");
sc.nextLine();
flag = true;
}
}
if(aiType == 1){
ai = new DumbAIPlayer(theNum);
}
else if(aiType == 2){
ai = new BasicAIPlayer(theNum);
}
else{
ai = new AdvancedAIPlayer(theNum);
}
return ai;
}
private void playGame(){
board.printBoard();
while(board.getWinner() == -1 && !board.isFull()){
int c = players[0].playChip(board);
int r = board.getRowForCol(c);
board.changeSlot(r,c,1);
board.printBoard();
if(board.getWinner() != -1 || board.isFull()){
break;
}
c = players[1].playChip(board);
r = board.getRowForCol(c);
board.changeSlot(r,c,2);
board.printBoard();
}
if(board.isFull()){
System.out.println("Nobody won :(");
}
else if(board.getWinner() != -1){
int win = board.getWinner();
System.out.println(players[win-1].getName() + " won!");
}
}
public void playGame(GameBoard gb){
board = gb;
this.initAIVSAI();
this.playGame();
}
}