A configuration of the game is a 6 times 7 integer array boa
Solution
import java.util.*;
class Connect4{
private int nrows;
private int ncols;
private int[][] board;
public Connect4(int[][] _board){
nrows = 6;
ncols = 7;
board = new int[nrows][ncols];
for (int i = 0; i < nrows; i ++ ) {
for (int j = 0 ; j < ncols ; j ++ ) {
board[i][j] = _board[i][j];
}
}
}
public char decode(int a){
if( a == 1) return \'G\';
if( a == 1) return \'B\';
return \'N\';
}
public char winner(){
// will start from 0
// will only check right, down
for (int i = 0; i < nrows; i ++ ) {
for (int j = 0 ; j < ncols ; j ++ ) {
if(board[i][j] != 0){
// check down
if(i+4 < nrows){
if(board[i][j] == board[i+1][j] && board[i][j] == board[i+2][j] && board[i][j] == board[i+3][j] && board[i][j] == board[i+4][j] ){
return decode(board[i][j]);
}
}
if(j+4 < ncols){
if(board[i][j] == board[i][j+1] && board[i][j] == board[i][j+2] && board[i][j] == board[i][j+3] && board[i][j] == board[i][j+4] ){
return decode(board[i][j]);
}
}
}
}
}
return \'N\';
}
public static void main(String[] args) {
int[][] multi = new int[][]{
{ 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 2, 0, 0, 0, 0},
{ 0, 2, 2, 1, 0, 2, 0},
{ 0, 1, 2, 1, 1, 1, 0}
};
Connect4 conn = new Connect4(multi);
System.out.println(conn.winner() + \" \");
}
}
![A configuration of the game is a 6 times 7 integer array, board, such that board[i][j] = = 1 indicates that a green chip occupies position (i, j); board[i][j] A configuration of the game is a 6 times 7 integer array, board, such that board[i][j] = = 1 indicates that a green chip occupies position (i, j); board[i][j]](/WebImages/31/a-configuration-of-the-game-is-a-6-times-7-integer-array-boa-1087197-1761571857-0.webp)
![A configuration of the game is a 6 times 7 integer array, board, such that board[i][j] = = 1 indicates that a green chip occupies position (i, j); board[i][j] A configuration of the game is a 6 times 7 integer array, board, such that board[i][j] = = 1 indicates that a green chip occupies position (i, j); board[i][j]](/WebImages/31/a-configuration-of-the-game-is-a-6-times-7-integer-array-boa-1087197-1761571857-1.webp)