Create a GUI program for playing ConnectFour Rules of Connect Four: - Acceptable
ID: 3681437 • Letter: C
Question
Create a GUI program for playing ConnectFour Rules of Connect Four: - Acceptable "connection" directions: top-to-bottom, left-to-right, diagonally up-and-to-the-right, diagonally up-and-to-the-left. - "Connection" length: Configurable - Board size: Configurable (see below) - Player count: 2 (Man vs Man for now). Feel free to extend to Man vs Computer if you feel brave enough. - Acceptable spaces: any available space that is not claimed already that is directly above a claimed space or is the bottom space of a column. Evaluation: -
CREATE GUI..
The code will be evaluated based on design and organization. Appropriate use of patterns is highly encouraged. - Write your code as if you are writing production quality code. Consider how maintainable, extensible, elegant, and readable your code is. - The code will also be evaluated on the UI and ease of use. - The code will NOT be evaluated at all on performance, so please don't waste any time optimizing for performance.
Explanation / Answer
I have written a java code for the same:
working code:
import java.util.Scanner;
class GameBoard{
byte[][] GameBoard = new byte[6][7];
public GameBoard(){
GameBoard = new byte[][]{
{0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,},
};
}
public boolean checkMove(int column){
return GameBoard[0][column]==0;
}
public boolean placeMove(int column, int player){
if(!checkMove(column)) {System.out.println("Illegal move!"); return false;}
for(int i=5;i>=0;--i){
if(GameBoard[i][column] == 0) {
GameBoard[i][column] = (byte)player;
return true;
}
}
return false;
}
public void undoMove(int column){
for(int i=0;i<=5;++i){
if(GameBoard[i][column] != 0) {
GameBoard[i][column] = 0;
break;
}
}
}
public void displayGameBoard(){
System.out.println();
for(int i=0;i<=5;++i){
for(int j=0;j<=6;++j){
System.out.print(GameBoard[i][j]+" ");
}
System.out.println();
}
System.out.println();
}
}
public class ConnectFour {
private GameBoard b;
private Scanner scan;
private int nextMoveLocation=-1;
private int maxDepth = 8;
public ConnectFour(GameBoard b){
this.b = b;
scan = new Scanner(System.in);
}
public void Computer_Move(){
System.out.println("Your move (1-7): ");
int move = scan.nextInt();
while(move<1 || move > 7 || !b.checkMove(move-1)){
System.out.println("Invalid move. Your move (1-7): ");
move = scan.nextInt();
}
b.placeMove(move-1, (byte)2);
}
public int gameResult(GameBoard b){
int computerScore = 0, humanScore = 0;
for(int i=5;i>=0;--i){
for(int j=0;j<=6;++j){
if(b.GameBoard[i][j]==0) continue;
if(j<=3){
for(int k=0;k<4;++k){
if(b.GameBoard[i][j+k]==1) computerScore++;
else if(b.GameBoard[i][j+k]==2) humanScore++;
else break;
}
if(computerScore==4)return 1; else if (humanScore==4)return 2;
computerScore = 0; humanScore = 0;
}
if(i>=3){
for(int k=0;k<4;++k){
if(b.GameBoard[i-k][j]==1) computerScore++;
else if(b.GameBoard[i-k][j]==2) humanScore++;
else break;
}
if(computerScore==4)return 1; else if (humanScore==4)return 2;
computerScore = 0; humanScore = 0;
}
if(j<=3 && i>= 3){
for(int k=0;k<4;++k){
if(b.GameBoard[i-k][j+k]==1) computerScore++;
else if(b.GameBoard[i-k][j+k]==2) humanScore++;
else break;
}
if(computerScore==4)return 1; else if (humanScore==4)return 2;
computerScore = 0; humanScore = 0;
}
if(j>=3 && i>=3){
for(int k=0;k<4;++k){
if(b.GameBoard[i-k][j-k]==1) computerScore++;
else if(b.GameBoard[i-k][j-k]==2) humanScore++;
else break;
}
if(computerScore==4)return 1; else if (humanScore==4)return 2;
computerScore = 0; humanScore = 0;
}
}
}
for(int j=0;j<7;++j){
if(b.GameBoard[0][j]==0)return -1;
}
return 0;
}
int calculateScore(int computerScore, int moreMoves){
int moveScore = 4 - moreMoves;
if(computerScore==0)return 0;
else if(computerScore==1)return 1*moveScore;
else if(computerScore==2)return 10*moveScore;
else if(computerScore==3)return 100*moveScore;
else return 1000;
}
public int eval_Board(GameBoard b){
int computerScore=1;
int score=0;
int blanks = 0;
int k=0, moreMoves=0;
for(int i=5;i>=0;--i){
for(int j=0;j<=6;++j){
if(b.GameBoard[i][j]==0 || b.GameBoard[i][j]==2) continue;
if(j<=3){
for(k=1;k<4;++k){
if(b.GameBoard[i][j+k]==1)computerScore++;
else if(b.GameBoard[i][j+k]==2){computerScore=0;blanks = 0;break;}
else blanks++;
}
moreMoves = 0;
if(blanks>0)
for(int c=1;c<4;++c){
int column = j+c;
for(int m=i; m<= 5;m++){
if(b.GameBoard[m][column]==0)moreMoves++;
else break;
}
}
if(moreMoves!=0) score += calculateScore(computerScore, moreMoves);
computerScore=1;
blanks = 0;
}
if(i>=3){
for(k=1;k<4;++k){
if(b.GameBoard[i-k][j]==1)computerScore++;
else if(b.GameBoard[i-k][j]==2){computerScore=0;break;}
}
moreMoves = 0;
if(computerScore>0){
int column = j;
for(int m=i-k+1; m<=i-1;m++){
if(b.GameBoard[m][column]==0)moreMoves++;
else break;
}
}
if(moreMoves!=0) score += calculateScore(computerScore, moreMoves);
computerScore=1;
blanks = 0;
}
if(j>=3){
for(k=1;k<4;++k){
if(b.GameBoard[i][j-k]==1)computerScore++;
else if(b.GameBoard[i][j-k]==2){computerScore=0; blanks=0;break;}
else blanks++;
}
moreMoves=0;
if(blanks>0)
for(int c=1;c<4;++c){
int column = j- c;
for(int m=i; m<= 5;m++){
if(b.GameBoard[m][column]==0)moreMoves++;
else break;
}
}
if(moreMoves!=0) score += calculateScore(computerScore, moreMoves);
computerScore=1;
blanks = 0;
}
if(j<=3 && i>=3){
for(k=1;k<4;++k){
if(b.GameBoard[i-k][j+k]==1)computerScore++;
else if(b.GameBoard[i-k][j+k]==2){computerScore=0;blanks=0;break;}
else blanks++;
}
moreMoves=0;
if(blanks>0){
for(int c=1;c<4;++c){
int column = j+c, row = i-c;
for(int m=row;m<=5;++m){
if(b.GameBoard[m][column]==0)moreMoves++;
else if(b.GameBoard[m][column]==1);
else break;
}
}
if(moreMoves!=0) score += calculateScore(computerScore, moreMoves);
computerScore=1;
blanks = 0;
}
}
if(i>=3 && j>=3){
for(k=1;k<4;++k){
if(b.GameBoard[i-k][j-k]==1)computerScore++;
else if(b.GameBoard[i-k][j-k]==2){computerScore=0;blanks=0;break;}
else blanks++;
}
moreMoves=0;
if(blanks>0){
for(int c=1;c<4;++c){
int column = j-c, row = i-c;
for(int m=row;m<=5;++m){
if(b.GameBoard[m][column]==0)moreMoves++;
else if(b.GameBoard[m][column]==1);
else break;
}
}
if(moreMoves!=0) score += calculateScore(computerScore, moreMoves);
computerScore=1;
blanks = 0;
}
}
}
}
return score;
}
public int minimax(int depth, int turn){
int gameResult = gameResult(b);
if(gameResult==1)return Integer.MAX_VALUE;
else if(gameResult==2)return Integer.MIN_VALUE;
else if(gameResult==0)return 0;
if(depth==maxDepth)return eval_Board(b);
int maxScore=Integer.MIN_VALUE, minScore = Integer.MAX_VALUE;
for(int j=0;j<=6;++j){
if(!b.checkMove(j)) continue;
if(turn==1){
b.placeMove(j, 1);
int currentScore = minimax(depth+1, 2);
maxScore = Math.max(currentScore, maxScore);
if(depth==0){
System.out.println("Score for location "+j+" = "+currentScore);
if(maxScore==currentScore) nextMoveLocation = j;
}
}else if(turn==2){
b.placeMove(j, 2);
int currentScore = minimax(depth+1, 1);
minScore = Math.min(currentScore, minScore);
}
b.undoMove(j);
}
return turn==1?maxScore:minScore;
}
public int getAIMove(){
nextMoveLocation = -1;
minimax(0, 1);
return nextMoveLocation;
}
public void computer_Move(){
int humanMove=-1;
Scanner scan = new Scanner(System.in);
System.out.println("Would you like to play first? (yes/no) ");
String answer = scan.next().trim();
if(answer.equalsIgnoreCase("yes")) Computer_Move();
b.displayBoard();
b.placeMove(3, 1);
b.displayBoard();
while(true){
Computer_Move();
b.displayBoard();
int gameResult = gameResult(b);
if(gameResult==1){System.out.println("computer Wins!");break;}
else if(gameResult==2){System.out.println("You Win!");break;}
else if(gameResult==0){System.out.println("Draw!");break;}
b.placeMove(getAIMove(), 1);
b.displayBoard();
gameResult = gameResult(b);
if(gameResult==1){System.out.println("computer Wins!");break;}
else if(gameResult==2){System.out.println("You Win!");break;}
else if(gameResult==0){System.out.println("Draw!");break;}
}
}
public static void main(String[] args) {
GameBoard b = new GameBoard();
ConnectFour computer = new ConnectFour(b);
computer.computer_Move();
}
}