Java Complete (15 points) Part 3 - Connect Four Game! http://foureyes.github.io/
ID: 3681179 • Letter: J
Question
Java
Complete (15 points) Part 3 - Connect Four Game!
http://foureyes.github.io/csci-ua.0101-spring2016-004/assignments/hw06.html
using the below method
public class FourInARow{
//main method
public static int checkFour(int[][] board, int piece){
if (check_vertical(board, piece) == true || check_horizontal(board, piece) == true || check_diagonal(board, piece) == true) {
return 1;
} else {
return -1;
}
}
//check vertical
public static boolean check_vertical(int[][] board,int piece){
for (int i = 0; i<board.length; i++){
for(int t=0; t<board[0].length-3;t++){
if (piece == board[i][t] && piece ==board[i][t+1] && board[i][t+1]==board[i][t+2] && board[i][t+2]==board[i][t+3]){
return true;
}
}
}
return false;
}
//check horizontal
public static boolean check_horizontal(int[][] board, int piece){
for (int i =0; i<board.length; i++){
for(int t=0; t<board[0].length-3;t++){
if (piece == board[t][i]&& piece ==board[t+1][i] && board[t+1][i]==board[t+2][i] && board[t+2][i]==board[t+3][i]){
return true;
}
}
}
return false;
}
//check diagonal
public static boolean check_diagonal(int[][] board, int piece){
//check down
for (int t =0; t<board.length-3; t++){
for(int i=0; i<board[0].length-3;i++){
if (piece == board[i][t]&& piece==board[i+1][t+1] && board[i+1][t+1]==board[i+2][t+2] && board[i+2][t+2]==board[i+3][t+3]){
return true;
}
}
}
//check up
for (int t = board.length-1; t>2; t--){
for(int i=0; i<board[0].length-3;i++){
if (piece == board[t][i]&& piece==board[t-1][i+1] && board[t-1][i+1]==board[t-2][i+2] && board[t-2][i+2]==board[t-3][i+3]){
return true;
}
}
}
return false;
}
Explanation / Answer
//check down
for (int t = board.length-1; t>2; t--){
for(int i=0; i<board[0].length-3;i++){
if (piece == board[t][i]&& piece==board[t-1][i+1] && board[t-1][i+1]==board[t-2][i+2] && board[t-2][i+2]==board[t-3][i+3]){
return true;
}
}
}
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.