import java.util.Scanner;
public class fourPieces {
public static void main(String[] args) {
char[][] checkerboard = initial();
int[] record = new int[7];
show(checkerboard);
while (isInArray(checkerboard, ' ')) {
input(checkerboard, record, "red");
input(checkerboard, record, "yellow");
}
}
public static void input(char[][] checkerboard, int[] record, String color) {
Scanner input = new Scanner(System.in);
System.out.print("Drop a " + color + " disk at column (0-6): ");
int n = input.nextInt();
while (n > 6 || record[n] > 5 ) {
System.out.print("It's out of range. please enter another number: ");
n = input.nextInt();
}
char letter = color.equals("red") ? 'R' : 'Y';
checkerboard[5 - record[n]][n] = letter;
record[n]++;
show(checkerboard);
if (isConsecutiveFour(checkerboard, letter)) {
System.out.println("The " + color + " player won");
System.exit(0);
}
if (!isInArray(checkerboard, ' ')) {
System.out.println("draw");
System.exit(0);
}
}
public static boolean isConsecutiveFour(char[][] checkerboard, char ch) {
for (int i = 0; i < checkerboard.length; i++)
for (int j = 0; j < checkerboard[i].length; j++)
if (row(checkerboard, i, j, ch) || column(checkerboard, i, j, ch) ||
oblique(checkerboard, i, j, ch) || oblique2(checkerboard, i, j, ch))
return true;
return false;
}
public static boolean row(char[][] checkerboard, int i, int j, char ch) {
for (int count = 0; count < 4; count++)
if (j + count + 1 > checkerboard[i].length || checkerboard[i][j + count] != ch)
return false;
return true;
}
public static boolean column(char[][] checkerboard, int i, int j, char ch) {
for (int count = 0; count < 4; count++)
if (i + count + 1 > checkerboard.length || checkerboard[i + count][j] != ch)
return false;
return true;
}
public static boolean oblique(char[][] checkerboard, int i, int j, char ch) {
for (int count = 0; count < 4; count++)
if (i + count + 1 > checkerboard.length || j + count + 1 > checkerboard[i].length ||
checkerboard[i + count][j + count] != ch)
return false;
return true;
}
public static boolean oblique2(char[][] checkerboard, int i, int j, char ch) {
for (int count = 0; count < 4; count++)
if (i - count < 0 || j + count + 1 > checkerboard[i].length ||
checkerboard[i - count][j + count] != ch)
return false;
return true;
}
public static void show(char[][] checkerboard) {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
System.out.print("|");
System.out.print(checkerboard[i][j]);
}
System.out.println("|");
}
System.out.println("---------------");
}
public static char[][] initial() {
char[][] checkerboard = new char[6][7];
for (int i = 0; i < checkerboard.length; i++)
for (int j = 0; j < checkerboard[0].length; j++)
checkerboard[i][j] = ' ';
return checkerboard;
}
public static boolean isInArray(char[][] checkerboard, char ch) {
for (char[] cArray: checkerboard)
for (char c: cArray)
if (c == ch)
return true;
return false;
}
}