-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmainClass.java
More file actions
218 lines (212 loc) · 7.37 KB
/
mainClass.java
File metadata and controls
218 lines (212 loc) · 7.37 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class mainClass {
private int[][] gameBoard = new int[7][6];
public static void main (String[] args){
mainClass main = new mainClass(50907);
}
public mainClass(int port) {
try{
Socket socket = new Socket("127.0.0.1", 9500, InetAddress.getByName("127.0.0.1"), port);
System.out.println(socket.getInetAddress() + ":" + socket.getLocalPort());
String response = " ";
response = input(socket);
System.out.println("received existing game");
if(response.equals("Y")) {
System.out.println("Do you want to continue your saved game? Y/N");
Scanner scanner = new Scanner(System.in);
response = scanner.nextLine();
output(socket, response);
}
int counter;
counter = Integer.parseInt(input(socket));
System.out.println(counter + "Counter value");
if(counter == 2){
int[][] board;
board = boardToArray(input(socket));
setGameBoard(board);
printBoard(board);
}
boolean gameContinues = true;
while (gameContinues) {
int[][] board = getGameBoard();
int move = getMove(counter);
if(move == 10){
System.out.println("Not a valid column number try again");
}else {
int row = getRow(move, board);
if (row == 10) {
System.out.println("Column is full");
} else {
board[move][row] = counter;
setGameBoard(board);
printBoard(board);
gameContinues = checkWinConditionHV(move, row, counter, board);
if(gameContinues){
gameContinues = checkWinConditionD(move, row, counter, board);
}
}
}
output(socket, boardToString(getGameBoard()));
setGameBoard(boardToArray(input(socket)));
printBoard(getGameBoard());
}
}catch(Exception e){
System.out.println("Unable to make connection");
e.printStackTrace();
}
}
private int[][] getGameBoard() {
return gameBoard;
}
private void setGameBoard(int[][] gameBoard) {
this.gameBoard = gameBoard;
}
private int getMove(int counter){
int move;
Scanner scanner = new Scanner(System.in);
System.out.println("It's player " + counter + "'s turn. " + "Please enter the the column number you wish to play in, columns go from 0 to 6 ");
try {
move = Integer.parseInt(scanner.next());
if (move > 6 || move < 0) {
return 10;
} else {
return move;
}
}catch(Exception e){
return 10;
}
}
private int getRow(int column, int[][] board){
int row;
for(int y = 0; y < 6; y++){
if(board[column][y] == 0){
row = y;
return row;
}
}
row = 10;
return row;
}
private void printBoard(int[][] board){
for(int y = 5; y >= 0; y--){
System.out.println();
for(int x = 0; x < 7; x++){
System.out.print(board[x][y] + " ");
}
}
System.out.println();
}
private boolean checkWinConditionHV(int moveX, int moveY, int counter, int[][] board){
int piecesInLine = 0;
for(int y = 0; y < 6; y++){
if(board[moveX][y] == counter){
piecesInLine = piecesInLine + 1;
if(piecesInLine == 4){
System.out.println("Player " + counter + " wins");
return false;
}
}else{
piecesInLine = 0;
}
}
for(int x = 0; x < 7; x++){
if(board[x][moveY] == counter){
piecesInLine = piecesInLine + 1;
if(piecesInLine == 4){
System.out.println("Player " + counter + " wins");
return false;
}
}else {
piecesInLine = 0;
}
}
return true;
}
private boolean checkWinConditionD(int moveX, int moveY, int counter, int[][] board){
int x = moveX;
int y = moveY;
int piecesInLine = 0;
boolean edgeNotFound = true;
while(edgeNotFound){
if(y == 0 || x == 0){
edgeNotFound = false;
}else {
x = x - 1;
y = y - 1;
}
}
while((x < 7 && x > -1) && (y < 6 && y > -1)) {
if (board[x][y] == counter) {
piecesInLine = piecesInLine + 1;
if (piecesInLine == 4) {
System.out.println("Player " + counter + " wins");
return false;
}
} else {
piecesInLine = 0;
}
x = x + 1;
y = y + 1;
}
x = moveX;
y = moveY;
edgeNotFound = true;
piecesInLine = 0;
while(edgeNotFound){
if(y == 5 || x == 0){
edgeNotFound = false;
}else {
x = x - 1;
y = y + 1;
}
}
while((x < 7 && x > -1) && (y < 6 && y > -1)) {
if (board[x][y] == counter) {
piecesInLine = piecesInLine + 1;
if (piecesInLine == 4) {
System.out.println("Player " + counter + " wins");
return false;
}
} else {
piecesInLine = 0;
}
x = x + 1;
y = y - 1;
}
return true;
}
private String input(Socket ss) throws IOException {
InputStream input = ss.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String data = reader.readLine();
return data;
}
private int[][] boardToArray(String board){
int[][] gameBoard = new int[7][6];
int counter = 0;
for(int y = 0; y < 6; y++){
for(int x = 0; x < 7; x++) {
gameBoard[x][y] = Integer.parseInt(String.valueOf(board.charAt(counter)));
counter++;
}
}
return gameBoard;
}
private String boardToString(int[][] board){
StringBuilder gameBoard = new StringBuilder();
for(int y = 0; y < 6; y++){
for(int x = 0; x < 7; x++) {
gameBoard.append(board[x][y]);
}
}
return gameBoard.toString();
}
private void output(Socket s, String board) throws IOException{
OutputStream output = s.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
writer.println(board);
}
}