Skip to content

Commit 84e7e4b

Browse files
committed
update
1 parent f7ed97c commit 84e7e4b

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

dev_projects/TddPlayGround/src/main/java/com/yen/TddPlayGround/Connect4/Connect4.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ public class Connect4 {
2020

2121
private String[][] board = new String[ROWS][COLUMNS];
2222

23+
private static final String RED = "R";
24+
25+
private static final String GREEN = "G";
26+
27+
private String currentPlayer = RED;
28+
2329
// private final int maxDiscsInColumn = 6;
2430
// private int discNum;
2531

@@ -30,6 +36,21 @@ public class Connect4 {
3036
}
3137
}
3238

39+
40+
public String getCurrentPlayer(){
41+
42+
return this.currentPlayer;
43+
}
44+
45+
public void SwitchPlayer(){
46+
47+
if (RED.equals(this.currentPlayer)){
48+
this.currentPlayer = GREEN;
49+
}else{
50+
this.currentPlayer = RED;
51+
}
52+
}
53+
3354
public int getNumberOfDiscs() {
3455

3556
//return this.discNum; //0;
@@ -53,7 +74,11 @@ public int putDiscInColumn(int column) {
5374
this.checkPositionToInsert(row, column);
5475
board[row][column] = "X";
5576
System.out.println(">>> putDiscInColumn");
56-
return column; //row;
77+
78+
// switch player
79+
SwitchPlayer();
80+
81+
return column; //row; // TODO : verify should return column or row
5782
}
5883

5984
private void checkPositionToInsert(int row, int column) {

dev_projects/TddPlayGround/src/test/java/com/yen/TddPlayGround/Connect4/Connect4TDDSpec.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,21 @@ public void whenNoMoreRoomInColumnThenRuntimeException(){
110110
System.out.println(exception);
111111
}
112112

113+
// book p.96
114+
@Test
115+
public void whenFirstPlayerPlaysThenDiscColorIsRed(){
116+
117+
Connect4 tested = new Connect4();
118+
assertEquals(tested.getCurrentPlayer(), "R");
119+
}
120+
121+
@Test
122+
public void whenSecondPlayerPlaysThenDiscColorIsRed(){
123+
124+
Connect4 tested = new Connect4();
125+
int column = 1;
126+
tested.putDiscInColumn(column);
127+
assertEquals(tested.getCurrentPlayer(), "G");
128+
}
129+
113130
}

0 commit comments

Comments
 (0)