Skip to content

Instantly share code, notes, and snippets.

@Saren-Arterius
Created November 13, 2016 09:21
Show Gist options
  • Save Saren-Arterius/5c9fb3a48e62768fbafc8412855ec74b to your computer and use it in GitHub Desktop.
Save Saren-Arterius/5c9fb3a48e62768fbafc8412855ec74b to your computer and use it in GitHub Desktop.
ITP3914 Four-in-a-Line
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class FourInALine {
public static final Scanner scanner = new Scanner(System.in);
public static final int WIN_MIN_LENGTH = 4;
public static final int ROW_COUNT = 6;
public static final int COLUMN_COUNT = 7;
private final List<int[]> slashPoints;
private final List<int[]> backslashPoints;
private int[][] gameGrid;
private int currentTurn = 1;
public FourInALine() {
gameGrid = new int[ROW_COUNT][COLUMN_COUNT];
slashPoints = new ArrayList<>();
for (int startColumn = 0; startColumn < (COLUMN_COUNT - WIN_MIN_LENGTH) + 1; startColumn++) {
slashPoints.add(new int[]{ROW_COUNT - 1, startColumn});
}
for (int startRow = WIN_MIN_LENGTH - 1; startRow < ROW_COUNT - 1; startRow++) {
slashPoints.add(new int[]{startRow, 0});
}
backslashPoints = new ArrayList<>();
for (int startColumn = 0; startColumn < (COLUMN_COUNT - WIN_MIN_LENGTH) + 1; startColumn++) {
backslashPoints.add(new int[]{0, startColumn});
}
for (int startRow = 1; startRow < (ROW_COUNT - WIN_MIN_LENGTH) + 1; startRow++) {
backslashPoints.add(new int[]{startRow, 0});
}
}
private int findWhoWin() {
// H scan
for (int row = 0; row < ROW_COUNT; row++) {
int[] maxLengths = {0, 0};
for (int column = 0; column < COLUMN_COUNT; column++) {
int result = updateLengths(gameGrid[row][column], maxLengths);
if (result != 0) {
return result;
}
}
}
// V scan
for (int column = 0; column < COLUMN_COUNT; column++) {
int[] maxLengths = {0, 0};
for (int row = 0; row < ROW_COUNT; row++) {
int result = updateLengths(gameGrid[row][column], maxLengths);
if (result != 0) {
return result;
}
}
}
// Slash scan
for (int[] startPoint : slashPoints) {
int row = startPoint[0];
int column = startPoint[1];
int[] maxLengths = {0, 0};
do {
int result = updateLengths(gameGrid[row][column], maxLengths);
if (result != 0) {
return result;
}
row--;
column++;
} while (row >= 0 && column <= COLUMN_COUNT - 1);
}
// Backslash scan
for (int[] startPoint : backslashPoints) {
int row = startPoint[0];
int column = startPoint[1];
int[] maxLengths = {0, 0};
do {
int result = updateLengths(gameGrid[row][column], maxLengths);
if (result != 0) {
return result;
}
row++;
column++;
} while (row <= ROW_COUNT - 1 && column <= COLUMN_COUNT - 1);
}
return 0;
}
private void drawGrid() {
for (int row = ROW_COUNT - 1; row >= 0; row--) {
System.out.print(row + " |");
for (int column = 0; column < COLUMN_COUNT; column++) {
System.out.print(" " + gameGrid[row][column]);
}
System.out.println();
}
System.out.print(repeatChar(" ", 4));
System.out.println(repeatChar("-", (COLUMN_COUNT * 2) - 1));
System.out.print(repeatChar(" ", 3));
for (int i = 0; i < COLUMN_COUNT; i++) {
System.out.print(" " + i);
}
System.out.println();
}
public static int updateLengths(int cell, int[] maxLengths) {
switch (cell) {
case 0:
maxLengths[0] = 0;
maxLengths[1] = 0;
break;
case 1:
maxLengths[0] += 1;
maxLengths[1] = 0;
break;
case 2:
maxLengths[0] = 0;
maxLengths[1] += 1;
break;
}
if (maxLengths[0] == WIN_MIN_LENGTH) {
return 1;
}
if (maxLengths[1] == WIN_MIN_LENGTH) {
return 2;
}
return 0;
}
private void dropDiscTo(int column) {
int row = 0;
while (true) {
if (gameGrid[row][column] == 0) {
gameGrid[row][column] = currentTurn;
break;
}
row++;
}
}
private void start() {
while (true) {
drawGrid();
int winner = findWhoWin();
if (winner != 0) {
System.out.println("Player " + winner + " wins this game!");
break;
}
int input;
while (true) {
System.out.print("Player " + currentTurn + " type a column (0-6) or 9 to quit current game: ");
String inputMsg = scanner.nextLine();
try {
input = Integer.parseInt(inputMsg.trim());
} catch (NumberFormatException e) {
System.out.println("Invalid input!");
continue;
}
String errorMsg = getError(input);
if (errorMsg == null) {
break;
}
System.out.println(errorMsg);
}
if (input == 9) {
System.out.println("Bye Bye!");
break;
}
dropDiscTo(input);
if (currentTurn == 1) {
currentTurn = 2;
} else {
currentTurn = 1;
}
}
}
private String getError(int input) {
if (input == 9) {
return null;
}
if (input < 0 || input >= COLUMN_COUNT) {
return "Range of column should be 0 to " + (COLUMN_COUNT - 1) + "!";
}
if (gameGrid[ROW_COUNT - 1][input] != 0) {
return "Column " + input + " is full!";
}
return null;
}
private static String repeatChar(String c, int count) {
return new String(new char[count]).replace("\0", c);
}
public static void main(String[] args) {
FourInALine game = new FourInALine();
game.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment