Skip to content

Commit 125bba1

Browse files
author
Joe O'Regan
committed
win function edit, and highlight winning move on client
1 parent 838b0f8 commit 125bba1

File tree

2 files changed

+53
-72
lines changed

2 files changed

+53
-72
lines changed

src/com/sock/connect5_txt_v2/Board.java

Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,45 @@ public void setBoard(int[][] board) {
1313

1414
public Board(int rows, int cols) {
1515
this.board = new int[rows][cols];
16-
17-
init(); // Initialise the board with all 0's after creating
16+
init(); // Initialise the board with all 0's after creating
1817
}
19-
18+
2019
public void init() {
2120
for (int row = 0; row < Game.ROWS; row++) {
2221
for (int col = 0; col < Game.COLS; col++) {
2322
this.board[row][col] = 0;
2423
}
2524
}
2625
}
27-
26+
2827
public void setCol(int col, int type) {
2928
for (int i = Game.ROWS - 1; i >= 0; i--) {
3029
if (board[i][col] == 0) {
3130
board[i][col] = type;
31+
draw();
3232
break;
3333
}
3434
}
3535
}
36-
36+
3737
public boolean checkCol(int col, int type) {
3838
System.out.println("Checking Column: " + col + " for Player " + type);
3939
if (topRowFull(col)) {
4040
return false;
4141
}
42-
42+
4343
return true;
4444
}
45-
45+
4646
public boolean topRowFull(int col) {
4747
if (board[0][col] != 0) { // if top row is not 0, col is full
4848
System.out.println("\nERROR: Column " + col + " is full\n");
4949
return true;
5050
}
51-
51+
5252
return false;
5353
}
54-
54+
5555
public boolean full() {
5656
for (int row = 0; row < Game.ROWS; row++) {
5757
for (int col = 0; col < Game.COLS; col++) {
@@ -62,7 +62,7 @@ public boolean full() {
6262

6363
return true;
6464
}
65-
65+
6666
public void draw() {
6767
System.out.println();
6868

@@ -79,81 +79,58 @@ public void draw() {
7979
System.out.println();
8080
}
8181
}
82-
83-
public void showWin(int[] fiveInARow) {
84-
for (int i = 0; i < 10; i += 2) {
85-
board[fiveInARow[i]][fiveInARow[i + 1]] = 3; // Highlight winning line
86-
}
8782

88-
System.out.println("Winning Move");
89-
90-
draw();
91-
}
92-
9383
public boolean win() {
94-
boolean gameOver = false;
95-
int[] fiveInARow = new int[10];
96-
97-
for (int row = 0; row <= Game.ROWS - Game.CONNECT; row++) {
84+
for (int r = 0; r <= Game.ROWS - Game.CONNECT; r++) {
9885
// Diagonal /
99-
for (int col = Game.CONNECT - 1; col < Game.COLS; col++) {
100-
if (board[row][col] != 0
101-
&& board[row + 1][col - 1] == board[row][col]
102-
&& board[row + 2][col - 2] == board[row][col]
103-
&& board[row + 3][col - 3] == board[row][col]
104-
&& board[row + 4][col - 4] == board[row][col]) {
105-
gameOver = true;
106-
fiveInARow = new int[] { row, col, row + 1, col - 1, row + 2, col - 2, row + 3, col - 3, row + 4, col - 4 };
107-
break;
86+
for (int c = Game.CONNECT - 1; c < Game.COLS; c++) {
87+
if (winningLine(new int[] { r, c, r + 1, c - 1, r + 2, c - 2, r + 3, c - 3, r + 4, c - 4 })) {
88+
return true;
10889
}
10990
}
11091

11192
// Diagonal \
11293
for (int col = 0; col <= Game.COLS - Game.CONNECT; col++) {
113-
if (board[row][col] != 0
114-
&& board[row + 1][col + 1] == board[row][col]
115-
&& board[row + 2][col + 2] == board[row][col]
116-
&& board[row + 3][col + 3] == board[row][col]
117-
&& board[row + 4][col + 4] == board[row][col]) {
118-
gameOver = true;
119-
fiveInARow = new int[] { row, col, row + 1, col + 1, row + 2, col + 2, row + 3, col + 3, row + 4, col + 4 };
120-
break;
94+
if (winningLine(new int[] { r, col, r + 1, col + 1, r + 2, col + 2, r + 3, col + 3, r + 4, col + 4 })) {
95+
return true;
12196
}
12297
}
12398
}
12499

125100
// Check Rows
126101
for (int row = 0; row < Game.ROWS; row++) {
127102
for (int col = 0; col <= Game.COLS - Game.CONNECT; col++) {
128-
if (board[row][col] != 0
129-
&& board[row][col + 1] == board[row][col]
130-
&& board[row][col + 2] == board[row][col]
131-
&& board[row][col + 3] == board[row][col]
132-
&& board[row][col + 4] == board[row][col]) {
133-
gameOver = true;
134-
fiveInARow = new int[] { row, col, row, col + 1, row, col + 2, row, col + 3, row, col + 4 };
135-
break;
103+
if (winningLine(new int[] { row, col, row, col + 1, row, col + 2, row, col + 3, row, col + 4 })) {
104+
return true;
136105
}
137106
}
138107
}
139108

140109
// Check Columns
141110
for (int row = 0; row <= Game.ROWS - Game.CONNECT; row++) {
142111
for (int col = 0; col < Game.COLS; col++) {
143-
if (board[row][col] != 0 && board[row + 1][col] == board[row][col]
144-
&& board[row + 2][col] == board[row][col] && board[row + 3][col] == board[row][col]
145-
&& board[row + 4][col] == board[row][col]) {
146-
gameOver = true;
147-
fiveInARow = new int[] { row, col, row + 1, col, row + 2, col, row + 3, col, row + 4, col };
148-
break;
112+
if (winningLine(new int[] { row, col, row + 1, col, row + 2, col, row + 3, col, row + 4, col })) {
113+
return true;
149114
}
150115
}
151116
}
152117

153-
if (gameOver) {
154-
showWin(fiveInARow);
118+
return false;
119+
}
120+
121+
private boolean winningLine(int[] b) {
122+
if (board[b[0]][b[1]] != 0 && board[b[2]][b[3]] == board[b[0]][b[1]] && board[b[4]][b[5]] == board[b[0]][b[1]]
123+
&& board[b[6]][b[7]] == board[b[0]][b[1]] && board[b[8]][b[9]] == board[b[0]][b[1]]) {
124+
for (int i = 0; i < 10; i += 2) {
125+
board[b[i]][b[i + 1]] = 3; // Highlight winning line
126+
}
127+
128+
System.out.println("Winning Move");
129+
draw();
130+
131+
return true;
155132
}
156133

157-
return gameOver;
134+
return false;
158135
}
159136
}

src/com/sock/connect5_txt_v2/Connect5Client.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
public class Connect5Client {
1515
private Board board = new Board(Game.ROWS, Game.COLS);
1616
private Scanner sc = new Scanner(System.in);
17-
private int playerIcon, opponentIcon, validMove = 0;
17+
private int playerIcon, opponentIcon, validMove = 0, player;
1818
private static int PORT = 8000;
1919
private Socket socket;
2020
private BufferedReader in;
@@ -34,39 +34,43 @@ public void play() throws Exception {
3434
response = in.readLine();
3535

3636
if (response.startsWith("WELCOME")) {
37-
int mark = Integer.parseInt(String.valueOf(response.charAt(8))); // Return PLAYER_1 or PLAYER_2
38-
playerIcon = (Integer.parseInt(String.valueOf(mark)) == Game.PLAYER_1) ? Game.PLAYER_1 : Game.PLAYER_2;
37+
player = Integer.parseInt(String.valueOf(response.charAt(8))); // Return PLAYER_1 or PLAYER_2
38+
playerIcon = (Integer.parseInt(String.valueOf(player)) == Game.PLAYER_1) ? Game.PLAYER_1
39+
: Game.PLAYER_2;
3940
opponentIcon = (playerIcon == Game.PLAYER_1) ? Game.PLAYER_2 : Game.PLAYER_1; // opposite of player
40-
System.out.println("Connect 5 - Player " + mark);
41+
System.out.println("Connect 5 - Player " + player);
4142
}
4243

4344
while (true) {
4445
response = in.readLine();
45-
46+
4647
if (response.startsWith("VALID_MOVE")) {
47-
board.setCol(validMove, playerIcon);
48-
board.draw();
49-
System.out.println("Valid move, please wait");
48+
//System.out.flush();
49+
//Runtime.getRuntime().exec("cls");
50+
board.setCol(validMove, playerIcon); // Player moves
51+
System.out.println("\nValid move, please wait");
5052
} else if (response.startsWith("OPPONENT_MOVED")) {
53+
//System.out.flush();
54+
//Runtime.getRuntime().exec("cls");
5155
int loc = Integer.parseInt(response.substring(15));
52-
board.setCol(loc, opponentIcon);
53-
board.draw();
54-
System.out.println("Opponent moved, your turn");
56+
board.setCol(loc, opponentIcon); // Opponent moves
57+
System.out.println("\nPlayer " + ((player == 1) ? Game.PLAYER_2 : Game.PLAYER_1) + " Moved");
5558
} else if (response.startsWith("VICTORY")) {
5659
System.out.println("You win");
60+
board.win();
5761
break;
5862
} else if (response.startsWith("DEFEAT")) {
5963
System.out.println("You lose");
64+
board.win();
6065
break;
6166
} else if (response.startsWith("TIE")) {
62-
System.out.println("You tied");
6367
break;
6468
} else if (response.startsWith("MESSAGE")) {
6569
System.out.println(response.substring(8));
6670
}
6771

6872
if (response.startsWith("CONTINUE") || response.toLowerCase().contains("your move")
69-
|| response.toLowerCase().contains("?")) { // player move, or returned invalid move
73+
|| response.toLowerCase().contains("?")) { // start game, player move, or returned invalid move
7074
playerMove();
7175
}
7276
}
@@ -79,7 +83,7 @@ public void play() throws Exception {
7983

8084
private void playerMove() {
8185
board.draw();
82-
System.out.print("Select a column (0 - 8): ");
86+
System.out.print("\nSelect a column (0 - 8): ");
8387
int move = sc.nextInt();
8488
validMove = move;
8589
out.println("MOVE " + (move));

0 commit comments

Comments
 (0)