RyanAllarey

ConnectFour

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