@@ -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 ("\n ERROR: 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}
0 commit comments