-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxconnect4.java
165 lines (140 loc) · 5.58 KB
/
maxconnect4.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
/*H****************************************************************
* FILENAME : maxconnect4.java
*
* DESCRIPTION :
* Controls the flow of the Max Connect 4 game
*
* NOTES :
* There are two game modes;
* interactive and one-move.
*
* Copyright 2019, Jacob Wilkins. All rights reserved.
*
* AUTHOR : Jacob Wilkins START DATE : 4 Mar 19
*
*H*/
import java.io.*;
import java.util.Scanner;
public class maxconnect4 {
public static void main(String[] args) {
if (args.length != 4) {
System.out.println("Four command-line arguments are needed:\n" + "Usage: java [program name] interactive [input_file] [computer-next / human-next] [depth]\n" + " or: java [program name] one-move [input_file] [output_file] [depth]\n");
exit_function(0);
}
String game_mode = args[0].toString();
String input = args[1].toString();
String nextPlayer = args[2].toString();
int maxDepth = Integer.parseInt(args[3]);
GameBoard currentGame = new GameBoard(input);
AiPlayer calculon = new AiPlayer();
int playColumn = 99;
boolean playMade = false;
int computerNum;
if (nextPlayer.equalsIgnoreCase("computer-next")) {
computerNum = 1;
} else {
computerNum = 2;
}
/// interactive mode ///
if (game_mode.equalsIgnoreCase("interactive")) {
System.out.print("\nMaxConnect-4 game\n");
System.out.print("game state before move:\n");
currentGame.printGameBoard();
System.out.println("Score: Player 1 = " + currentGame.getScore(1) + ", Player 2 = " + currentGame.getScore(2) + "\n");
while (currentGame.getPieceCount() < 42) {
if (nextPlayer.equalsIgnoreCase("computer-next")) {
int current_player = currentGame.getCurrentTurn();
playColumn = calculon.findBestPlay(currentGame, maxDepth, computerNum);
currentGame.playPiece(playColumn);
nextPlayer = "human-next";
System.out.println("move " + currentGame.getPieceCount() + ": Player " + current_player + ", column " + playColumn);
System.out.print("game state after move:\n");
currentGame.printGameBoard();
System.out.println("Score: Player 1 = " + currentGame.getScore(1) + ", Player 2 = " + currentGame.getScore(2) + "\n ");
currentGame.printGameBoardToFile("computer.txt");
}
if (nextPlayer.equalsIgnoreCase("human-next")) {
int current_player = currentGame.getCurrentTurn();
Scanner reader = new Scanner(System.in);
System.out.print("Enter a column (1-7): ");
playColumn = reader.nextInt();
if ((playColumn > 0) && (playColumn < 8)) {
if (currentGame.isValidPlay(playColumn - 1)) {
currentGame.playPiece(playColumn - 1);
nextPlayer = "computer-next";
System.out.println("move " + currentGame.getPieceCount() + ": Player " + current_player + ", column " + playColumn);
System.out.print("game state after move:\n");
currentGame.printGameBoard();
System.out.println("Score: Player 1 = " + currentGame.getScore(1) + ", Player 2 = " + currentGame.getScore(2) + "\n ");
currentGame.printGameBoardToFile("human.txt");
} else {
System.out.println("\nThe moved entered is not valid.\nTry again.\n");
}
} else {
System.out.println("\nThe moved entered is not valid.\nTry again.\n");
}
}
}
System.out.println("\nI can't play.\nThe Board is Full");
if (currentGame.getScore(1) == currentGame.getScore(2)) {
System.out.println("\nYou Tied!\n");
} else if (currentGame.getScore(1) > currentGame.getScore(2)) {
if (computerNum == 1) {
System.out.println("\nComputer Won!\n");
} else {
System.out.println("\nYou Won!\n");
}
} else {
if (computerNum == 2) {
System.out.println("\nComputer Won!\n");
} else {
System.out.println("\nYou Won!\n");
}
}
System.out.println("Game Over\n");
return;
} else if (!game_mode.equalsIgnoreCase("one-move")) {
System.out.println("\n" + game_mode + " is an unrecognized game mode \ntry again. \n");
return;
}
/// one-move mode ///
String output = args[2].toString();
System.out.print("\nMaxConnect-4 game\n");
System.out.print("game state before move:\n");
currentGame.printGameBoard();
System.out.println("Score: Player 1 = " + currentGame.getScore(1) + ", Player 2 = " + currentGame.getScore(2) + "\n");
if (currentGame.getPieceCount() < 42) {
int current_player = currentGame.getCurrentTurn();
playColumn = calculon.findBestPlay(currentGame, maxDepth, computerNum);
currentGame.playPiece(playColumn);
System.out.println("move " + currentGame.getPieceCount() + ": Player " + current_player + ", column " + playColumn);
System.out.print("game state after move:\n");
currentGame.printGameBoard();
System.out.println("Score: Player 1 = " + currentGame.getScore(1) + ", Player 2 = " + currentGame.getScore(2) + "\n ");
currentGame.printGameBoardToFile(output);
} else {
System.out.println("\nI can't play.\nThe Board is Full");
if (currentGame.getScore(1) == currentGame.getScore(2)) {
System.out.println("\nYou Tied!\n");
} else if (currentGame.getScore(1) > currentGame.getScore(2)) {
if (computerNum == 1) {
System.out.println("\nComputer Won!\n");
} else {
System.out.println("\nYou Won!\n");
}
} else {
if (computerNum == 2) {
System.out.println("\nComputer Won!\n");
} else {
System.out.println("\nYou Won!\n");
}
}
System.out.println("Game Over\n");
}
return;
}
private static void exit_function(int value) {
System.out.println("exiting from MaxConnectFour.java!\n\n");
System.exit(value);
}
}