Java Complete (15 points) Part 3 - Connect Four Game! http://foureyes.github.io/
ID: 3681234 • 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
/* This program implements the 'Connect four' game (exercise 7.20 in the book, 8th edition) */ import java.util.*; public class ConnectFour { // This method attempts to put the disk of the given color in the given column. // It returns true if successful and false if the column is filled and we cannot // put a disk. public static boolean putDisk(char[][] field, int column, char color) { // If the first disk is there, the column is filled, returning false. if (field[0][column] != ' ') return false; // Check all elements in the column. for (int row = 0; row < 7; ++row) { // If we found something, which means if the character is not // zero character if (field[row][column] != ' ') { // Put the disk on top of the current one. field[row-1][column] = color; return true; } } // If no other disks found, we place this diak at the bottom. field[6][column] = color; return true; } // Check rows, if there are 4 or more disks of the same color - return winner color private static char getWinnerInRows(char[][] field) { // Check rows and see if there are 4 disks of the same color for (int row = 0; row < 7; ++row) { int count = 0; // We will compare current element with the previous for (int column = 1; column < 7; ++column) { if (field[row][column] != ' ' && field[row][column] == field[row][column-1]) ++count; else count = 1; // Check if there are 4 in a row. if (count >= 4) { // Return color of the winner return field[row][column]; } } } // Otherwise return character, which means nobody win in rows. return ' '; } // Check columns, if there are 4 or more disks of the same color - return winner color private static char getWinnerInColumns(char[][] field) { // Check rows and see if there are 4 disks of the same color for (int column = 0; column < 7; ++column) { int count = 0; // We will compare current element with the previous for (int row = 1; row < 7; ++row) { if (field[row][column] != ' ' && field[row][column] == field[row-1][column]) ++count; else count = 1; // Check if there are 4 in a row. if (count >= 4) { // Return color of the winner return field[row][column]; } } } // Otherwise return character, which means nobody win in rows. return ' '; } // Check diagonals, if there are 4 or more disks of the same color - return winner color private static char getWinnerInDiagonals(char[][] field) { // There are 2 kinds of diagonals, let's check those that go from top-left to bottom right // There are diagonals, that starts on top of each column, let's check them for (int column = 0; column < 7; ++column) { int count = 0; // Traverse diagonal that starts at [0][column], we start with the first row, // because we will compare elements with the previous one, similar to how // we did this for rows and columns for (int row = 1; row < 7; ++row) { // Coordinates an the diagonal change as [row + i][column + i], // so we stop when column can get outside of the range if (column + row >= 7) break; if (field[row][column+row] != ' ' && field[row-1][column + row - 1] == field[row][column+row]) ++count; else count = 1; if (count >= 4) return field[row][column+row]; } } // There are diagonals, that starts on left of each row, let's check them for (int row = 0; row < 7; ++row) { int count = 0; // Traverse diagonal that starts at [row][0], we start with the first column, // because we will compare elements with the previous one, similar to how // we did this for rows and columns for (int column = 1; column < 7; ++column) { // Coordinates an the diagonal change as [row + i][column + i], // so we stop when column can get outside of the range if (column + row >= 7) break; if (field[row + column][column] != ' ' && field[row+column - 1][column - 1] == field[row + column][column]) ++count; else count = 1; if (count >= 4) return field[row + column][column]; } } // Now we need to do the same for diagonals that go from top-right to bottom-left // There are diagonals, that starts on top of each column, let's check them for (int column = 0; column < 7; ++column) { int count = 0; // Traverse diagonal that starts at [0][column], we start with the first row, // because we will compare elements with the previous one, similar to how // we did this for rows and columns for (int row = 1; row < 7; ++row) { // Coordinates an the diagonal change as [row + i][column + i], // so we stop when column can get outside of the range if (column - row < 0) break; if (field[row][column-row] != ' ' && field[row - 1][column - row + 1] == field[row][column-row]) ++count; else count = 1; if (count >= 4) return field[row][column-row]; } } // There are diagonals, that starts on left of each row, let's check them for (int row = 0; row < 7; ++row) { int count = 0; // Traverse diagonal that starts at [row][0], we start with the first column, // because we will compare elements with the previous one, similar to how // we did this for rows and columns for (int column = 5; column >= 0; --column) { // Coordinates an the diagonal change as [row + i][column + i], // so we stop when column can get outside of the range if (column - row < 0) break; if (field[column - row][column] != ' ' && field[column - row - 1][column + 1] == field[column - row][column]) ++count; else count = 1; if (count >= 4) return field[column - row][column]; } } // Otherwise return character, which means nobody win in rows. return ' '; } public static char getWinner(char[][] field) { char winner = getWinnerInRows(field); if (winner != ' ') return winner; winner = getWinnerInColumns(field); if (winner != ' ') return winner; winner = getWinnerInDiagonals(field); if (winner != ' ') return winner; // Now we need to check if there are empty positions, otherwise it is a draw for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.