Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Ryan Allarey
- */
- import java.util.Scanner;
- // user input
- import java.util.concurrent.ThreadLocalRandom;
- // random number generator for AI
- public class ConnectFourSolo {
- // variables
- static int height = 6;
- static int width = 7;
- static int win = 0;
- static boolean game = true;
- static int turn = 0;
- // board
- static char [][] grid = new char [width][height];
- public static void main(String [] args) {
- // tells user how to play
- System.out.println("Use 0-6 to choose what column you want");
- // creates board
- CreateBoard();
- // prints board
- PrintBoard();
- // checks through all win conditions
- CheckWin();
- // checks for four in a row for 'X' pieces
- CheckRowX();
- // checks for four in a row for 'O' pieces
- CheckRowO();
- // checks for four in column for 'X' pieces
- CheckColumnX();
- // checks for four in column for 'O' pieces
- CheckColumnO();
- // checks for four in a diagonal from upper left to bottom right for 'x' pieces
- CheckDiagonal1X();
- // checks for four in a diagonal from upper left to bottom right for 'O' pieces
- CheckDiagonal1O();
- //// checks for four in a diagonal from upper right to bottom left for 'X' pieces
- CheckDiagonal2X();
- // checks for four in a diagonal from upper right to bottom left for 'O' pieces
- CheckDiagonal2O();
- //main game loop
- while(game){
- while (turn <= 41 && turn > -1) {
- /**
- * player 1's turn, and goes through every win condition after every turn
- * determines if player won
- */
- if (win == 0) {
- Player1();
- turn ++;
- PrintBoard();
- CheckWin();
- }
- // player 2's turn, goes through every win condition to determine if won.
- if (win == 0){
- Player2();
- turn ++;
- PrintBoard();
- CheckWin();
- }
- // after 42 turns, the game is declared a draw.
- if (turn >= 41) {
- System.out.println("It's a draw!");
- game = false;
- }
- }
- }
- }
- public static void CreateBoard() {
- //fills board with '.' for the width and height
- for (int row = 0; height > row; row ++) {
- for (int column = 0; width > column; column ++) {
- grid[column][row] = '.';
- }
- }
- }
- public static void PrintBoard() {
- //prints the board
- System.out.println("0123456");
- for (int row = 0; height > row; row ++) {
- for (int column = 0; width > column; column ++) {
- System.out.print(grid[column][row]);
- }
- System.out.println();
- }
- System.out.println();
- }
- /**
- * Main method that allows player one to choose a column for their piece.
- * Tells user which column to choose. If the column they choose is greater than
- * the available columns, it will state that that is not an option.
- * If the column is full, the method calls the method again and the player can choose
- * another column.
- */
- public static void Player1() {
- // indicates turn
- System.out.println("Player 1's turn. Please choose a column.");
- // sets scanner
- Scanner scan = new Scanner(System.in);
- // gets input
- int column = scan.nextInt();
- if (column > 6){
- System.out.println("That is not an option");
- }
- for (int bottomrow = 5; bottomrow >= 0; bottomrow --){
- if (grid[column][bottomrow] == '.') {
- grid[column][bottomrow] = 'X';
- break;
- }
- if (grid[column][0] == 'X' || grid[column][0] == 'O') {
- System.out.println("That column is full!");
- Player1();
- break;
- }
- }
- }
- /**
- * Main method for AI's turn. Calls upon a random generator to declare the
- * AI's turn. If the column is full, a new number will be called.
- */
- public static void Player2() {
- // indicates turn, player 2 is AI.
- System.out.println("Player 2's turn. Please choose a column.");
- // creates random generator for the AI's turn.
- int column = ThreadLocalRandom.current().nextInt(0, 6);
- if (column > 6){
- System.out.println("That is not an option");
- }
- for (int bottomrow = 5; bottomrow >= 0; bottomrow --){
- if (grid[column][bottomrow] == '.') {
- grid[column][bottomrow] = 'O';
- break;
- }
- if (grid[column][0] == 'X' || grid[column][0] == 'O') {
- System.out.println("That column is full!");
- Player2();
- break;
- }
- }
- }
- /**
- * Method goes through all win conditions to determine if any player has won the game.
- * This will check vertical, horizontal, and all diagonal directions for four in a row.
- */
- public static void CheckWin() {
- CheckRowX();
- CheckRowO();
- CheckColumnX();
- CheckColumnO();
- CheckDiagonal1X();
- CheckDiagonal1O();
- CheckDiagonal2X();
- CheckDiagonal2O();
- }
- /**
- * This will go through all the rows to check if there are four in a row horizontally.
- * This method is specifically for "X" pieces. If found, will set main loop to false ending
- * the game.
- */
- public static void CheckRowX() {
- for (int row = 0; row <= 5; row++) {
- for(int col = 0; col <= 3; col++) {
- if (grid[col][row] == 'X' && grid[col + 1][row] == 'X' && grid[col + 2][row] == 'X' && grid[col + 3][row] == 'X') {
- win = 1;
- System.out.println("Player 1 wins!");
- game = false;
- turn = -1;
- break;
- }
- }
- }
- }
- /**
- * This will go through all the rows to check if there are four in a row horizontally.
- * This method is specifically for "O" pieces. If found, will set main loop to false ending
- * the game.
- */
- public static void CheckRowO() {
- for (int row = 0; row <= 5; row++) {
- for(int col = 0; col <= 3; col++) {
- if (grid[col][row] == 'O' && grid[col + 1][row] == 'O' && grid[col + 2][row] == 'O' && grid[col + 3][row] == 'O') {
- win = 1;
- System.out.println("Player 2 wins!");
- game = false;
- turn = -1;
- break;
- }
- }
- }
- }
- /**
- * This will go through all the columns to check if there are four in a row vertically.
- * This method is specifically for "X" pieces. If found, will set main loop to false ending
- * the game.
- */
- public static void CheckColumnX() {
- for (int col = 0; col <= 6; col++) {
- for(int row = 0; row <= 2; row++) {
- if (grid[col][row] == 'X' && grid[col][row + 1] == 'X' && grid[col][row + 2] == 'X' && grid[col][row + 3] == 'X') {
- win = 1;
- System.out.println("Player 1 wins!");
- game = false;
- turn = -1;
- break;
- }
- }
- }
- }
- /**
- * This will go through all the columns to check if there are four in a row vertically.
- * This method is specifically for "O" pieces. If found, will set main loop to false ending
- * the game.
- */
- public static void CheckColumnO() {
- for (int col = 0; col <= 6; col++) {
- for(int row = 0; row <= 2; row++) {
- if (grid[col][row] == 'O' && grid[col][row + 1] == 'O' && grid[col][row + 2] == 'O' && grid[col][row + 3] == 'O') {
- win = 1;
- System.out.println("Player 2 wins!");
- game = false;
- turn = -1;
- break;
- }
- }
- }
- }
- /**
- * This will check for four pieces diagonally starting from top left to bottom right.
- * This win check is specific to "X" pieces. If found, will set main loop to false ending
- * the game.
- */
- public static void CheckDiagonal1X() {
- for (int row = 0; row < 3; row++) {
- for (int col = 0; col < 4; col++) {
- if (grid[col][row] == 'X' && grid[col + 1][row + 1] == 'X' && grid[col + 2][row + 2] == 'X' && grid[col + 3][row + 3] == 'X') {
- win = 1;
- System.out.println("Player 1 wins!");
- game = false;
- turn = -1;
- break;
- }
- }
- }
- }
- /**
- * This will check for four pieces diagonally starting from top left to bottom right.
- * This win check is specific to "O" pieces. If found, will set main loop to false ending
- * the game.
- */
- public static void CheckDiagonal1O() {
- for (int row = 0; row < 3; row++) {
- for (int col = 0; col < 4; col++) {
- if (grid[col][row] == 'O' && grid[col + 1][row + 1] == 'O' && grid[col + 2][row + 2] == 'O' && grid[col + 3][row + 3] == 'O') {
- win = 1;
- System.out.println("Player 2 wins!");
- game = false;
- turn = -1;
- break;
- }
- }
- }
- }
- /**
- * This will check for four pieces diagonally starting from top right to bottom left.
- * This win check is specific to "X" pieces. If found, will set main loop to false ending
- * the game.
- */
- public static void CheckDiagonal2X() {
- for (int col = 6; col >= 3; col--) {
- for (int row = 0; row <= 2; row++) {
- if (grid[col][row] == 'X' && grid[col - 1][row + 1] == 'X' && grid[col - 2][row + 2] == 'X' && grid[col - 3][row + 3] == 'X') {
- win = 1;
- System.out.println("Player 1 wins!");
- game = false;
- turn = -1;
- break;
- }
- }
- }
- }
- /**
- * This will check for four pieces diagonally starting from top left to bottom right.
- * This win check is specific to "O" pieces. If found, will set main loop to false ending
- * the game.
- */
- public static void CheckDiagonal2O() {
- for (int row = 0; row <= 2; row++) {
- for (int col = 6; col >= 3; col--) {
- if (grid[col][row] == 'O' && grid[col - 1][row + 1] == 'O' && grid[col - 2][row + 2] == 'O' && grid[col - 3][row + 3] == 'O') {
- win = 1;
- System.out.println("Player 2 wins!");
- game = false;
- turn = -1;
- break;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment