Advertisement
shooqie

Connect Four

Aug 5th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.71 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. class ConnectFour {
  6.  
  7.     char winner;
  8.     int move;
  9.  
  10.     // four adjacent points
  11.     String one;
  12.     String two;
  13.     String three;
  14.     String four;
  15.  
  16.     public ConnectFour(List<String> moves) {
  17.         char[][] board = new char[6][7];
  18.         int move = 0;
  19.         for (String m : moves) {
  20.             move++;
  21.             board = set(board, m.charAt(0));
  22.             if (isWinner(board)) {
  23.                 this.winner = 'X';
  24.                 break;
  25.             }
  26.             board = set(board, m.charAt(1));
  27.             if (isWinner(board)) {
  28.                 this.winner = 'O';
  29.                 break; // double check to account for a situation where both players would win in the same turn
  30.             }
  31.         }
  32.  
  33.         this.move = move;
  34.  
  35.         if(winner == 0) {
  36.             System.out.println(printboard(board) + "\n\nNo Winner\n");
  37.             return;
  38.         }
  39.  
  40.         System.out.println(printboard(board) + "\n");
  41.         System.out.print(String.format("%s won at move %d (with %s %s %s %s)",
  42.                 this.winner, this.move, this.one, this.two, this.three, this.four));
  43.     }
  44.  
  45.  
  46.     private static char[][] set(char[][] board, char c) {
  47.         char player = Character.isUpperCase(c) ? 'X' : 'O';
  48.         int col = 5;
  49.         int row = (int) Character.toUpperCase(c) - 65;
  50.         while (board[col][row] != 0) {
  51.             col--;
  52.         }
  53.         board[col][row] = player;
  54.         return board;
  55.     }
  56.  
  57.     private static boolean bingo(char a, char b, char c, char d) {
  58.         return a != 0 && b != 0 && c != 0 && d != 0
  59.                 && a == b && b == c && c == d;
  60.     }
  61.  
  62.     private boolean isWinner(char[][] board) {
  63.         // horizontally
  64.         for (int i = 0; i < 6; i++) {
  65.             for (int j = 0; j < 4; j++) {
  66.                 char one = board[i][j];
  67.                 char two = board[i][j + 1];
  68.                 char three = board[i][j + 2];
  69.                 char four = board[i][j + 3];
  70.                 if (bingo(one, two, three, four)) {
  71.                     this.one = coordToPoint(i, j);
  72.                     this.two = coordToPoint(i, j + 1);
  73.                     this.three = coordToPoint(i, j + 2);
  74.                     this.four = coordToPoint(i, j + 3);
  75.                     return true;
  76.                 }
  77.             }
  78.         }
  79.  
  80.         // vertically
  81.         for (int i = 5; i > 2; i--) {
  82.             for (int j = 0; j < 7; j++) {
  83.                 char one = board[i][j];
  84.                 char two = board[i - 1][j];
  85.                 char three = board[i - 2][j];
  86.                 char four = board[i - 3][j];
  87.                 if (bingo(one, two, three, four)) {
  88.                     this.one = coordToPoint(i, j);
  89.                     this.two = coordToPoint(i - 1, j);
  90.                     this.three = coordToPoint(i - 2, j);
  91.                     this.four = coordToPoint(i - 3, j);
  92.                     return true;
  93.                 }
  94.             }
  95.         }
  96.  
  97.         // diagonally top left -> bottom right
  98.         for (int i = 0; i < 3; i++) {
  99.             for (int j = 0; j < 4; j++) {
  100.                 char one = board[i][j];
  101.                 char two = board[i + 1][j + 1];
  102.                 char three = board[i + 2][j + 2];
  103.                 char four = board[i + 3][j + 3];
  104.                 if (bingo(one, two, three, four)) {
  105.                     this.one = coordToPoint(i, j);
  106.                     this.two = coordToPoint(i + 1, j + 1);
  107.                     this.three = coordToPoint(i + 2, j + 2);
  108.                     this.four = coordToPoint(i + 3, j + 3);
  109.                     return true;
  110.                 }
  111.             }
  112.         }
  113.  
  114.         // diagonally top right -> bottom left
  115.         for (int i = 5; i > 2; i--) {
  116.             for (int j = 0; j < 4; j++) {
  117.                 char one = board[i][j];
  118.                 char two = board[i - 1][j + 1];
  119.                 char three = board[i - 2][j + 2];
  120.                 char four = board[i - 3][j + 3];
  121.                 if (bingo(one, two, three, four)) {
  122.                     this.one = coordToPoint(i, j);
  123.                     this.two = coordToPoint(i - 1, j + 1);
  124.                     this.three = coordToPoint(i - 2, j + 2);
  125.                     this.four = coordToPoint(i - 3, j + 3);
  126.                     return true;
  127.                 }
  128.             }
  129.         }
  130.  
  131.         return false;
  132.     }
  133.  
  134.     private static String coordToPoint(int i, int j) {
  135.         return (char) (j + 65) + String.valueOf((6 - i));
  136.     }
  137.  
  138.     private static String printboard(char[][] board) {
  139.         String strBoard = "\n  A B C D E F G\n";
  140.         for (int i = 0; i < board.length; i++) {
  141.             if (i != 0) {
  142.                 strBoard += "\n";
  143.             }
  144.             strBoard += (6 - i) + " ";
  145.             for (int j = 0; j < board[i].length; j++) {
  146.                 char c = board[i][j];
  147.                 strBoard += c == 0 ? ". " : c + " ";
  148.             }
  149.         }
  150.         return strBoard;
  151.     }
  152. }
  153.  
  154. public class Main {
  155.     public static void main (String args[]) {
  156.         List<String> moves = new ArrayList<>();
  157.         Scanner scanner = new Scanner(System.in);
  158.         while(scanner.hasNext()) {
  159.             String move = scanner.nextLine().replaceAll("\\s", "");
  160.             if(!Character.isUpperCase(move.charAt(0)) || !Character.isLowerCase(move.charAt(1))) {
  161.                 System.out.print("Try again: ");
  162.                 continue;
  163.             }
  164.             try {
  165.                 moves.add(move);
  166.                 new ConnectFour(moves);
  167.             } catch (ArrayIndexOutOfBoundsException aioobe) {
  168.                 moves.remove(moves.size() - 1);
  169.                 System.out.print("Try again: ");
  170.             }
  171.         }
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement