-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyConnectFour.java
196 lines (189 loc) · 6.72 KB
/
MyConnectFour.java
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import java.io.*;
import java.util.*;
public class MyConnectFour {
BufferedReader settings = new BufferedReader(new InputStreamReader(System.in));
InputHandler input = new InputHandler();
ArrayList<Player> players = new ArrayList<Player>();
Board boardToPlayOn;
public MyConnectFour() {
try {
cleanConsole();
System.out.println(" Welcome to connect-N game. Play against other players or the computer!\n\n");
while (true) {
System.out.println("1-> Start a new game with default settings (7x6 board, connect-4)");
System.out.println("2-> New Custom game");
System.out.println("3-> Exit the game");
switch(input.ReadLine()){
case 3:
System.exit(1);
case 2:
input.setMax(99); // allow user to write any int
boardToPlayOn = setupBoard();
addPlayers();
playGame();
break;
case 1:
boardToPlayOn = new Board(7, 6, 4);
addPlayers();
playGame();
break;
default:
System.out.println("Please choose 1,2 or 3");
break;
}
}
} catch (Exception e) {
System.out.println("Error in loading game : " + e);
}
}
public void cleanConsole(){
System.out.print("\033[H\033[2J"); //clean screen - this works well in VSCode + repl.it
System.out.flush();
}
public Board setupBoard() {
input.setMax(999);
System.out.println("Warning : Boards with either dimension > 10 will likely look weird");
System.out.println("Enter x dimension of board");
int x = input.ReadLine();
System.out.println("Enter y dimension of board");
int y = input.ReadLine();
System.out.println("Connect how many?");
int n = input.ReadLine();
Board newBoard = new Board(x, y, n);
input.setMax(10);
return newBoard;
}
private void playGame() {
try {
input.setMax(boardToPlayOn.getBoardX());
exchangePlayerCharacters();
cleanConsole();
System.out.println(players.get(0).getName() + " goes first.");
boardToPlayOn.printBoard();
boolean win = false , draw = false;
String winner = "";
while (!win && !draw) {
for (Player currentPlayer : players) {
if (boardToPlayOn.didMatchDraw() == true){ draw = true; break;}
currentPlayer.makeMove(input);
win = currentPlayer.haveIWon();
if (win == true) { winner = currentPlayer.getName(); break;}
}
}
if(win==true){
System.out.println(winner + " has won!!! \n");
}
if(draw==true){
System.out.println("Match was a draw\n");
}
players.clear();
} catch (Exception e) {
System.out.println("Error in running game : " + e);
}
}
private void exchangePlayerCharacters() { //get everyones characters and give them to possible bots
try{
ArrayList<Character> allPlayerCharacters = new ArrayList<Character>();
for(Player thisPlayer : players){
allPlayerCharacters.add(thisPlayer.getChar());
}
for(Player thisPlayer : players){
thisPlayer.setEnemyCharacter(allPlayerCharacters);
}
}catch (Exception e){
System.out.println("Error when trying to set enemy characters: " + e);
}
}
private void addPlayers() {
/*method to add AI or Human players. Asks user for input of a name, whether player is human or bot, and a character
Will check if the character is unique AND if it is valid i.e. not '\0' (nothing) or a whitespace ' '
*/
input.setMax(10);
System.out.println("Enter number of players (2-10).");
int numberOfPlayers = input.ReadLine();
try {
while (numberOfPlayers <= 1 || numberOfPlayers > 10) {
System.out.println("Enter number of players (2-10)");
numberOfPlayers = input.ReadLine();
}
for (int i = 0; i < numberOfPlayers; i++) {
Player newPlayer;
int z = 1 + i;
System.out.println("Enter name of player " + z);
String newPlayerName = settings.readLine();
Boolean humanHelper = false, human = true, charHelper = false;
System.out.println("Is this player human? (Y or N)");
String playerHuman = settings.readLine();
while (!humanHelper) {
switch (playerHuman) {
case "y":
human = true;
humanHelper = true;
break;
case "Y":
human = true;
humanHelper = true;
break;
case "N":
human = false;
humanHelper = true;
case "n":
human = false;
humanHelper = true;
break;
default:
System.out.println("Enter Y or N only");
playerHuman = settings.readLine();
break;
}
}
boolean validCharacter = false;
System.out.println("Enter a character for this player");
char[] characters = settings.readLine().toCharArray();
char newPlayerCharacter = '\0';
while (!charHelper) { //is char unique?
while(!validCharacter){ // is char valid?
switch(characters.length){
case 0:
System.out.println("Enter a character for this player");
characters = settings.readLine().toCharArray();
break;
default:
if(characters[0] != ' '){//not empty or a space
validCharacter = true;
newPlayerCharacter = characters[0];
break;
}
System.out.println("Enter a character for this player");
characters = settings.readLine().toCharArray();
break;
}
}
Boolean isCharUnique = true;
for (Player checkPlayer : players) {
if (checkPlayer.getChar() == newPlayerCharacter) {
isCharUnique = false;
validCharacter = false;
}
}
if (isCharUnique != true) {
System.out.println("Character \"" + characters[0] + "\" already used, please choose another one");
characters = settings.readLine().toCharArray();
} else if (isCharUnique == true) {
System.out.println("Character \"" + characters[0] + "\" chosen successfuly");
charHelper = true;
}
}
if (human == true) {
newPlayer = new Player(newPlayerName, newPlayerCharacter, boardToPlayOn, human);
} else {
newPlayer = new AI(newPlayerName, newPlayerCharacter, boardToPlayOn);
}
players.add(newPlayer);
}
cleanConsole();
} catch (Exception e) {
System.out.println("Error when trying to add players : " + e);
}
}
}