Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- class ConnectFour {
- char winner;
- int move;
- // four adjacent points
- String one;
- String two;
- String three;
- String four;
- public ConnectFour(List<String> moves) {
- char[][] board = new char[6][7];
- int move = 0;
- for (String m : moves) {
- move++;
- board = set(board, m.charAt(0));
- if (isWinner(board)) {
- this.winner = 'X';
- break;
- }
- board = set(board, m.charAt(1));
- if (isWinner(board)) {
- this.winner = 'O';
- break; // double check to account for a situation where both players would win in the same turn
- }
- }
- this.move = move;
- if(winner == 0) {
- System.out.println(printboard(board) + "\n\nNo Winner\n");
- return;
- }
- System.out.println(printboard(board) + "\n");
- System.out.print(String.format("%s won at move %d (with %s %s %s %s)",
- this.winner, this.move, this.one, this.two, this.three, this.four));
- }
- private static char[][] set(char[][] board, char c) {
- char player = Character.isUpperCase(c) ? 'X' : 'O';
- int col = 5;
- int row = (int) Character.toUpperCase(c) - 65;
- while (board[col][row] != 0) {
- col--;
- }
- board[col][row] = player;
- return board;
- }
- private static boolean bingo(char a, char b, char c, char d) {
- return a != 0 && b != 0 && c != 0 && d != 0
- && a == b && b == c && c == d;
- }
- private boolean isWinner(char[][] board) {
- // horizontally
- for (int i = 0; i < 6; i++) {
- for (int j = 0; j < 4; j++) {
- char one = board[i][j];
- char two = board[i][j + 1];
- char three = board[i][j + 2];
- char four = board[i][j + 3];
- if (bingo(one, two, three, four)) {
- this.one = coordToPoint(i, j);
- this.two = coordToPoint(i, j + 1);
- this.three = coordToPoint(i, j + 2);
- this.four = coordToPoint(i, j + 3);
- return true;
- }
- }
- }
- // vertically
- for (int i = 5; i > 2; i--) {
- for (int j = 0; j < 7; j++) {
- char one = board[i][j];
- char two = board[i - 1][j];
- char three = board[i - 2][j];
- char four = board[i - 3][j];
- if (bingo(one, two, three, four)) {
- this.one = coordToPoint(i, j);
- this.two = coordToPoint(i - 1, j);
- this.three = coordToPoint(i - 2, j);
- this.four = coordToPoint(i - 3, j);
- return true;
- }
- }
- }
- // diagonally top left -> bottom right
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 4; j++) {
- char one = board[i][j];
- char two = board[i + 1][j + 1];
- char three = board[i + 2][j + 2];
- char four = board[i + 3][j + 3];
- if (bingo(one, two, three, four)) {
- this.one = coordToPoint(i, j);
- this.two = coordToPoint(i + 1, j + 1);
- this.three = coordToPoint(i + 2, j + 2);
- this.four = coordToPoint(i + 3, j + 3);
- return true;
- }
- }
- }
- // diagonally top right -> bottom left
- for (int i = 5; i > 2; i--) {
- for (int j = 0; j < 4; j++) {
- char one = board[i][j];
- char two = board[i - 1][j + 1];
- char three = board[i - 2][j + 2];
- char four = board[i - 3][j + 3];
- if (bingo(one, two, three, four)) {
- this.one = coordToPoint(i, j);
- this.two = coordToPoint(i - 1, j + 1);
- this.three = coordToPoint(i - 2, j + 2);
- this.four = coordToPoint(i - 3, j + 3);
- return true;
- }
- }
- }
- return false;
- }
- private static String coordToPoint(int i, int j) {
- return (char) (j + 65) + String.valueOf((6 - i));
- }
- private static String printboard(char[][] board) {
- String strBoard = "\n A B C D E F G\n";
- for (int i = 0; i < board.length; i++) {
- if (i != 0) {
- strBoard += "\n";
- }
- strBoard += (6 - i) + " ";
- for (int j = 0; j < board[i].length; j++) {
- char c = board[i][j];
- strBoard += c == 0 ? ". " : c + " ";
- }
- }
- return strBoard;
- }
- }
- public class Main {
- public static void main (String args[]) {
- List<String> moves = new ArrayList<>();
- Scanner scanner = new Scanner(System.in);
- while(scanner.hasNext()) {
- String move = scanner.nextLine().replaceAll("\\s", "");
- if(!Character.isUpperCase(move.charAt(0)) || !Character.isLowerCase(move.charAt(1))) {
- System.out.print("Try again: ");
- continue;
- }
- try {
- moves.add(move);
- new ConnectFour(moves);
- } catch (ArrayIndexOutOfBoundsException aioobe) {
- moves.remove(moves.size() - 1);
- System.out.print("Try again: ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement