RyanAllarey

ConnectFourAI

Nov 1st, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.40 KB | None | 0 0
  1. /**
  2.  * @author Ryan Allarey
  3.  */
  4. import java.util.Scanner;
  5. // user input
  6. import java.util.concurrent.ThreadLocalRandom;
  7. // random number generator for AI
  8. public class ConnectFourSolo {
  9.  
  10.     // variables
  11.     static int height = 6;
  12.     static int width = 7;
  13.     static int win = 0;
  14.     static boolean game = true;
  15.     static int turn = 0;
  16.  
  17.     // board
  18.     static char [][] grid = new char [width][height];
  19.     public static void main(String [] args) {
  20.         // tells user how to play
  21.         System.out.println("Use 0-6 to choose what column you want");
  22.  
  23.         // creates board
  24.         CreateBoard();
  25.  
  26.         // prints board
  27.         PrintBoard();
  28.  
  29.         // checks through all win conditions
  30.         CheckWin();
  31.  
  32.         // checks for four in a row for 'X' pieces
  33.         CheckRowX();
  34.  
  35.         // checks for four in a row for 'O' pieces
  36.         CheckRowO();
  37.  
  38.         // checks for four in column for 'X' pieces
  39.         CheckColumnX();
  40.  
  41.         // checks for four in column for 'O' pieces
  42.         CheckColumnO();
  43.  
  44.         // checks for four in a diagonal from upper left to bottom right for 'x' pieces
  45.         CheckDiagonal1X();
  46.  
  47.         // checks for four in a diagonal from upper left to bottom right for 'O' pieces
  48.         CheckDiagonal1O();
  49.  
  50.         //// checks for four in a diagonal from upper right to bottom left for 'X' pieces
  51.         CheckDiagonal2X();
  52.  
  53.         // checks for four in a diagonal from upper right to bottom left for 'O' pieces
  54.         CheckDiagonal2O();
  55.  
  56.         //main game loop
  57.         while(game){
  58.             while (turn <= 41 && turn > -1) {
  59.                 /**
  60.                  * player 1's turn, and goes through every win condition after every turn
  61.                  * determines if player won
  62.                  */
  63.  
  64.                 if (win == 0) {
  65.                     Player1();
  66.                     turn ++;
  67.                     PrintBoard();
  68.                     CheckWin();
  69.                 }
  70.                 // player 2's turn, goes through every win condition to determine if won.
  71.                 if (win == 0){
  72.                     Player2();
  73.                     turn ++;
  74.                     PrintBoard();
  75.                     CheckWin();
  76.                 }
  77.                 // after 42 turns, the game is declared a draw.
  78.                 if (turn >= 41) {
  79.                     System.out.println("It's a draw!");
  80.                     game = false;
  81.                 }
  82.             }
  83.         }
  84.     }
  85.  
  86.     public static void CreateBoard() {
  87.         //fills board with '.' for the width and height
  88.         for (int row = 0; height > row; row ++) {
  89.             for (int column = 0; width > column; column ++) {
  90.                 grid[column][row] = '.';
  91.             }
  92.         }
  93.     }
  94.  
  95.     public static void PrintBoard() {
  96.         //prints the board
  97.         System.out.println("0123456");
  98.         for (int row = 0; height > row; row ++) {
  99.             for (int column = 0; width > column; column ++) {
  100.                 System.out.print(grid[column][row]);
  101.             }
  102.             System.out.println();
  103.         }
  104.         System.out.println();
  105.     }
  106.  
  107.     /**
  108.      * Main method that allows player one to choose a column for their piece.
  109.      * Tells user which column to choose. If the column they choose is greater than
  110.      * the available columns, it will state that that is not an option.
  111.      * If the column is full, the method calls the method again and the player can choose
  112.      * another column.
  113.      */
  114.     public static void Player1() {
  115.         // indicates turn
  116.         System.out.println("Player 1's turn. Please choose a column.");
  117.  
  118.         // sets scanner
  119.         Scanner scan = new Scanner(System.in);
  120.  
  121.         // gets input
  122.         int column = scan.nextInt();
  123.         if (column > 6){
  124.             System.out.println("That is not an option");
  125.         }
  126.         for (int bottomrow = 5; bottomrow >= 0; bottomrow --){
  127.             if (grid[column][bottomrow] == '.') {
  128.                 grid[column][bottomrow] = 'X';
  129.                 break;
  130.             }
  131.             if (grid[column][0] == 'X' || grid[column][0] == 'O') {
  132.                 System.out.println("That column is full!");
  133.                 Player1();
  134.                 break;
  135.             }
  136.         }
  137.  
  138.     }
  139.  
  140.     /**
  141.      * Main method for AI's turn. Calls upon a random generator to declare the
  142.      * AI's turn. If the column is full, a new number will be called.
  143.      */
  144.     public static void Player2() {
  145.         // indicates turn, player 2 is AI.
  146.         System.out.println("Player 2's turn. Please choose a column.");
  147.        
  148.         // creates random generator for the AI's turn.
  149.         int column = ThreadLocalRandom.current().nextInt(0, 6);
  150.         if (column > 6){
  151.             System.out.println("That is not an option");
  152.         }
  153.         for (int bottomrow = 5; bottomrow >= 0; bottomrow --){
  154.             if (grid[column][bottomrow] == '.') {
  155.                 grid[column][bottomrow] = 'O';
  156.                 break;
  157.             }
  158.             if (grid[column][0] == 'X' || grid[column][0] == 'O') {
  159.                 System.out.println("That column is full!");
  160.                 Player2();
  161.                 break;
  162.             }
  163.         }
  164.     }
  165.  
  166.     /**
  167.      * Method goes through all win conditions to determine if any player has won the game.
  168.      * This will check vertical, horizontal, and all diagonal directions for four in a row.
  169.      */
  170.     public static void CheckWin() {
  171.         CheckRowX();
  172.         CheckRowO();
  173.         CheckColumnX();
  174.         CheckColumnO();
  175.         CheckDiagonal1X();
  176.         CheckDiagonal1O();
  177.         CheckDiagonal2X();
  178.         CheckDiagonal2O();
  179.     }
  180.  
  181.     /**
  182.      * This will go through all the rows to check if there are four in a row horizontally.
  183.      * This method is specifically for "X" pieces. If found, will set main loop to false ending
  184.      * the game.
  185.      */
  186.     public static void CheckRowX() {
  187.         for (int row = 0; row <= 5; row++) {
  188.             for(int col = 0; col <= 3; col++) {
  189.                 if (grid[col][row] == 'X' && grid[col + 1][row] == 'X' && grid[col + 2][row] == 'X' && grid[col + 3][row] == 'X') {
  190.                     win = 1;
  191.                     System.out.println("Player 1 wins!");
  192.                     game = false;
  193.                     turn = -1;
  194.                     break;
  195.                 }
  196.             }
  197.         }
  198.     }
  199.  
  200.     /**
  201.      * This will go through all the rows to check if there are four in a row horizontally.
  202.      * This method is specifically for "O" pieces. If found, will set main loop to false ending
  203.      * the game.
  204.      */
  205.     public static void CheckRowO() {
  206.         for (int row = 0; row <= 5; row++) {
  207.             for(int col = 0; col <= 3; col++) {
  208.                 if (grid[col][row] == 'O' && grid[col + 1][row] == 'O' && grid[col + 2][row] == 'O' && grid[col + 3][row] == 'O') {
  209.                     win = 1;
  210.                     System.out.println("Player 2 wins!");
  211.                     game = false;
  212.                     turn = -1;
  213.                     break;
  214.                 }
  215.             }
  216.         }
  217.     }
  218.  
  219.     /**
  220.      * This will go through all the columns to check if there are four in a row vertically.
  221.      * This method is specifically for "X" pieces. If found, will set main loop to false ending
  222.      * the game.
  223.      */
  224.     public static void CheckColumnX() {
  225.         for (int col = 0; col <= 6; col++) {
  226.             for(int row = 0; row <= 2; row++) {
  227.                 if (grid[col][row] == 'X' && grid[col][row + 1] == 'X' && grid[col][row + 2] == 'X' && grid[col][row + 3] == 'X') {
  228.                     win = 1;
  229.                     System.out.println("Player 1 wins!");
  230.                     game = false;
  231.                     turn = -1;
  232.                     break;
  233.                 }
  234.             }
  235.         }
  236.     }
  237.  
  238.     /**
  239.      * This will go through all the columns to check if there are four in a row vertically.
  240.      * This method is specifically for "O" pieces. If found, will set main loop to false ending
  241.      * the game.
  242.      */
  243.     public static void CheckColumnO() {
  244.         for (int col = 0; col <= 6; col++) {
  245.             for(int row = 0; row <= 2; row++) {
  246.                 if (grid[col][row] == 'O' && grid[col][row + 1] == 'O' && grid[col][row + 2] == 'O' && grid[col][row + 3] == 'O') {
  247.                     win = 1;
  248.                     System.out.println("Player 2 wins!");
  249.                     game = false;
  250.                     turn = -1;
  251.                     break;
  252.                 }
  253.             }
  254.         }
  255.     }
  256.  
  257.     /**
  258.      * This will check for four pieces diagonally starting from top left to bottom right.
  259.      * This win check is specific to "X" pieces. If found, will set main loop to false ending
  260.      * the game.
  261.      */
  262.     public static void CheckDiagonal1X() {
  263.         for (int row = 0; row < 3; row++) {
  264.             for (int col = 0; col < 4; col++) {
  265.                 if (grid[col][row] == 'X' && grid[col + 1][row + 1] == 'X' && grid[col + 2][row + 2] == 'X' && grid[col + 3][row + 3] == 'X') {
  266.                     win = 1;
  267.                     System.out.println("Player 1 wins!");
  268.                     game = false;
  269.                     turn = -1;
  270.                     break;
  271.                 }
  272.             }
  273.         }
  274.     }
  275.  
  276.     /**
  277.      * This will check for four pieces diagonally starting from top left to bottom right.
  278.      * This win check is specific to "O" pieces. If found, will set main loop to false ending
  279.      * the game.
  280.      */
  281.     public static void CheckDiagonal1O() {
  282.         for (int row = 0; row < 3; row++) {
  283.             for (int col = 0; col < 4; col++) {
  284.                 if (grid[col][row] == 'O' && grid[col + 1][row + 1] == 'O' && grid[col + 2][row + 2] == 'O' && grid[col + 3][row + 3] == 'O') {
  285.                     win = 1;
  286.                     System.out.println("Player 2 wins!");
  287.                     game = false;
  288.                     turn = -1;
  289.                     break;
  290.                 }
  291.             }
  292.         }
  293.     }
  294.  
  295.     /**
  296.      * This will check for four pieces diagonally starting from top right to bottom left.
  297.      * This win check is specific to "X" pieces. If found, will set main loop to false ending
  298.      * the game.
  299.      */
  300.     public static void CheckDiagonal2X() {
  301.         for (int col = 6; col >= 3; col--) {
  302.             for (int row = 0; row <= 2; row++) {
  303.                 if (grid[col][row] == 'X' && grid[col - 1][row + 1] == 'X' && grid[col - 2][row + 2] == 'X' && grid[col - 3][row + 3] == 'X') {
  304.                     win = 1;
  305.                     System.out.println("Player 1 wins!");
  306.                     game = false;
  307.                     turn = -1;
  308.                     break;
  309.                 }
  310.             }
  311.         }
  312.     }
  313.  
  314.     /**
  315.      * This will check for four pieces diagonally starting from top left to bottom right.
  316.      * This win check is specific to "O" pieces. If found, will set main loop to false ending
  317.      * the game.
  318.      */
  319.     public static void CheckDiagonal2O() {
  320.         for (int row = 0; row <= 2; row++) {
  321.             for (int col = 6; col >= 3; col--) {
  322.                 if (grid[col][row] == 'O' && grid[col - 1][row + 1] == 'O' && grid[col - 2][row + 2] == 'O' && grid[col - 3][row + 3] == 'O') {
  323.                     win = 1;
  324.                     System.out.println("Player 2 wins!");
  325.                     game = false;
  326.                     turn = -1;
  327.                     break;
  328.                 }
  329.             }
  330.         }
  331.     }
  332. }
Add Comment
Please, Sign In to add comment