Created
July 22, 2012 22:33
-
-
Save Longlius/3161248 to your computer and use it in GitHub Desktop.
Connect Four written in babby Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Name: Matthew Longley | |
* Lecture Instructor: Kriangsiri Malasri | |
* Lecture Section: COMP 1900-002 | |
* Assignment: Programming Assignment 4, Connect Four | |
* Date: 26 April 2010 | |
*/ | |
import java.util.*; | |
public class ConnectFour | |
{ | |
public static void main(String[] args) | |
{ | |
Scanner s = new Scanner(System.in); | |
int gameReplayChoice; | |
//Collects number of columns and rows from user and error checks these inputs | |
System.out.println("Welcome to EXTREME Connect Four!"); | |
do{ | |
int numColumns; | |
int numRows; | |
do{ | |
System.out.println("How many rows are on this board?"); | |
numRows = s.nextInt(); | |
System.out.println("How many columns are on this board?"); | |
numColumns = s.nextInt(); | |
}while((numRows < 4) || (numColumns < 4)); | |
//declares, instantiates, and displays the gameBoard array | |
int[][] gameBoard = new int[numRows][numColumns]; | |
displayBoard(gameBoard); | |
int column; | |
int dropStatus; | |
//Do-while handles the flow of the game. Each player takes a move, the legitimacy of that move is evaluated, the array is updated, the game status is checked, and the board is displayed. This continues until | |
//checkGameStatus returns something other than a 0 | |
do{ | |
do{ | |
System.out.println("Which column will player 1 drop a piece in?"); | |
column = s.nextInt(); | |
dropStatus = dropPiece(gameBoard, column, 1); | |
if(dropStatus == 0){ | |
break; | |
}else if(dropStatus == 1){ | |
System.out.println("***Error! Column Selection is out of Bounds, please try again! ***"); | |
displayBoard(gameBoard); | |
}else if(dropStatus == 2){ | |
System.out.println("Column " + column + " is full. Please choose another."); | |
displayBoard(gameBoard); | |
} | |
}while(dropStatus != 0); | |
displayBoard(gameBoard); | |
if(checkGameStatus(gameBoard) != 0){ | |
break; | |
} | |
do{ | |
System.out.println("Which column will player 2 drop a piece in?"); | |
column = s.nextInt(); | |
dropStatus = dropPiece(gameBoard, column, 2); | |
if(dropStatus == 0){ | |
break; | |
}else if(dropStatus == 1){ | |
System.out.println("***Error! Column Selection is out of Bounds, please try again! ***"); | |
displayBoard(gameBoard); | |
}else if(dropStatus == 2){ | |
System.out.println("Column " + column + " is full. Please choose another."); | |
displayBoard(gameBoard); | |
} | |
}while(dropStatus != 0); | |
displayBoard(gameBoard); | |
if(checkGameStatus(gameBoard) != 0){ | |
break; | |
} | |
}while(checkGameStatus(gameBoard) == 0); | |
//Goes through all the possibilities of checkGameStatus, and determines if player 1 or player 2 has won. It then checks to see if a tie was declared. | |
if(checkGameStatus(gameBoard) == 1){ | |
System.out.println("Player 1 wins!"); | |
}else if(checkGameStatus(gameBoard) == 2){ | |
System.out.println("Player 2 wins!"); | |
}else if(checkGameStatus(gameBoard) == 3){ | |
System.out.println("No one wins. The game is a tie!"); | |
} | |
//Asks the player if they want to play again and modifies gameReplayChoice accordingly. If gameReplayChoice is 1 at the end, then the game restarts. | |
System.out.println("Would you like to play again? (1 for Yes. Anything else for no.)"); | |
gameReplayChoice = s.nextInt(); | |
}while(gameReplayChoice == 1); | |
} | |
public static void displayBoard(int[][] gameBoard) | |
{ | |
System.out.println(); | |
//For-loop displays column numbers on top | |
for(int i = 0; i < gameBoard[0].length; i++){ | |
System.out.print(" " + i + " "); | |
} | |
System.out.println(); | |
//Nested for-loop goes through each point in the array and displays either an empty box, a 1 counter, or a 2 counter. | |
for(int i = 0; i < gameBoard.length; i++){ | |
for(int j = 0; j < gameBoard[i].length; j++){ | |
if(gameBoard[i][j] == 0){ | |
System.out.print("| |"); | |
}else if(gameBoard[i][j] == 1){ | |
System.out.print("|(1)|"); | |
}else if(gameBoard[i][j] == 2){ | |
System.out.print("|(2)|"); | |
} | |
} | |
System.out.println(); | |
} | |
} | |
public static int dropPiece(int[][] gameBoard, int col, int player) | |
{ | |
//Error-checks the column choice by the player. | |
if(((col > (gameBoard[0].length - 1)) || (col < 0))){ | |
return 1; | |
} | |
//Goes from the bottom of the board to the top. It then places the player's piece in the first empty space it finds. | |
for(int i = (gameBoard.length - 1); i >= 0; i--){ | |
if(gameBoard[i][col] == 0){ | |
gameBoard[i][col] = player; | |
return 0; | |
} | |
} | |
//If no empty spaces are found, a 2 is returned signifying a full column. | |
return 2; | |
} | |
public static int checkGameStatus(int[][] board) | |
{ | |
//Sets a tie counter to check for a tie later on. | |
int tieCounter = 0; | |
//Nested for-loops check every single space on the board for three adjacent pieces in every direction. If conditionals are in place so the arrays aren't pulled out-of-bounds. This works because even if one piece is skipped | |
//over due to the conditional, at least one piece in that line will be picked up later in the loop. In this way, the algorithm used to find a win still works. | |
for(int i = 0; i < board.length; i++){ | |
for(int j = 0; j < board[0].length; j++){ | |
if((i+3) <= board.length - 1){ | |
if((board[i][j] == board[i+1][j]) && (board[i+1][j] == board[i+2][j]) && (board[i+2][j] == board[i+3][j]) && (board[i+3][j] == 1)){ | |
return 1; | |
} | |
} | |
if((i-3) >= 0){ | |
if((board[i][j] == board[i-1][j]) && (board[i-1][j] == board[i-2][j]) && (board[i-2][j] == board[i-3][j]) && (board[i-3][j] == 1)){ | |
return 1; | |
} | |
} | |
if((j+3) <= board[0].length - 1){ | |
if((board[i][j] == board[i][j+1]) && (board[i][j+1] == board[i][j+2]) && (board[i][j+2] == board[i][j+3]) && (board[i][j+3] == 1)){ | |
return 1; | |
} | |
} | |
if((j-3) >= 0){ | |
if((board[i][j] == board[i][j-1]) && (board[i][j-1] == board[i][j-2]) && (board[i][j-2] == board[i][j-3]) && (board[i][j-3] == 1)){ | |
return 1; | |
} | |
} | |
if(((i+3) <= board.length - 1) && ((j+3) <= board[0].length - 1)){ | |
if((board[i][j] == board[i+1][j+1]) && (board[i+1][j+1] == board[i+2][j+2]) && (board[i+2][j+2] == board[i+3][j+3]) && (board[i+3][j+3] == 1)){ | |
return 1; | |
} | |
} | |
if(((i-3) >= 0) && ((j-3) >= 0)){ | |
if((board[i][j] == board[i-1][j-1]) && (board[i-1][j-1] == board[i-2][j-2]) && (board[i-2][j-2] == board[i-3][j-3]) && (board[i-3][j-3] == 1)){ | |
return 1; | |
} | |
} | |
if(((i+3) <= board.length - 1) && ((j-3) >= 0)){ | |
if((board[i][j] == board[i+1][j-1]) && (board[i+1][j-1] == board[i+2][j-2]) && (board[i+2][j-2] == board[i+3][j-3]) && (board[i+3][j-3] == 1)){ | |
return 1; | |
} | |
} | |
if(((i-3) >= 0) && ((j+3) <= board[0].length - 1)){ | |
if((board[i][j] == board[i-1][j+1]) && (board[i-1][j+1] == board[i-2][j+2]) && (board[i-2][j+2] == board[i-3][j+3]) && (board[i-3][j+3] == 1)){ | |
return 1; | |
} | |
} | |
/// | |
if((i+3) <= board.length - 1){ | |
if((board[i][j] == board[i+1][j]) && (board[i+1][j] == board[i+2][j]) && (board[i+2][j] == board[i+3][j]) && (board[i+3][j] == 2)){ | |
return 2; | |
} | |
} | |
if((i-3) >= 0){ | |
if((board[i][j] == board[i-1][j]) && (board[i-1][j] == board[i-2][j]) && (board[i-2][j] == board[i-3][j]) && (board[i-3][j] == 2)){ | |
return 2; | |
} | |
} | |
if((j+3) <= board[0].length - 1){ | |
if((board[i][j] == board[i][j+1]) && (board[i][j+1] == board[i][j+2]) && (board[i][j+2] == board[i][j+3]) && (board[i][j+3] == 2)){ | |
return 2; | |
} | |
} | |
if((j-3) >= 0){ | |
if((board[i][j] == board[i][j-1]) && (board[i][j-1] == board[i][j-2]) && (board[i][j-2] == board[i][j-3]) && (board[i][j-3] == 2)){ | |
return 2; | |
} | |
} | |
if(((i+3) <= board.length - 1) && ((j+3) <= board[0].length - 1)){ | |
if((board[i][j] == board[i+1][j+1]) && (board[i+1][j+1] == board[i+2][j+2]) && (board[i+2][j+2] == board[i+3][j+3]) && (board[i+3][j+3] == 2)){ | |
return 2; | |
} | |
} | |
if(((i-3) >= 0) && ((j-3) >= 0)){ | |
if((board[i][j] == board[i-1][j-1]) && (board[i-1][j-1] == board[i-2][j-2]) && (board[i-2][j-2] == board[i-3][j-3]) && (board[i-3][j-3] == 2)){ | |
return 2; | |
} | |
} | |
if(((i+3) <= board.length - 1) && ((j-3) >= 0)){ | |
if((board[i][j] == board[i+1][j-1]) && (board[i+1][j-1] == board[i+2][j-2]) && (board[i+2][j-2] == board[i+3][j-3]) && (board[i+3][j-3] == 2)){ | |
return 2; | |
} | |
} | |
if(((i-3) >= 0) && ((j+3) <= board[0].length - 1)){ | |
if((board[i][j] == board[i-1][j+1]) && (board[i-1][j+1] == board[i-2][j+2]) && (board[i-2][j+2] == board[i-3][j+3]) && (board[i-3][j+3] == 2)){ | |
return 2; | |
} | |
} | |
//This last conditional keeps track of how many pieces are filling the board. | |
if(board[i][j] != 0){ | |
tieCounter++; | |
} | |
} | |
} | |
//This conditional basically evaluates whether a tie has occured. It compares tieCounter to the total square area of the board and decides whether or not it is filled. Due to the way checkGameStatus is coded, this is always | |
//checked last, so if a win has not been returned at this point in the code, and all the spaces are filled up, then the game is a tie. | |
if(tieCounter == (board[0].length * board.length)){ | |
return 3; | |
}else{ | |
//If nothing else is returned, then the game is unfinished and a 0 is returned. | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment