Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a GUI program for playing ConnectFour Rules of Connect Four: - Acceptable

ID: 3678970 • 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: - 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

import java.util.Scanner;

class createBoard{

    byte[][] createboard = new byte[6][7];

   

    public createBoard(){

        createboard = 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,},   

        };

    }

//to check if it is a legal move

   

    public boolean isitaLegalMove(int column){

        return createboard[0][column]==0;

    }

   

    //This is a function for placing a Move on the board

    public boolean placeyourMove(int column, int player){

        if(!isitaLegalMove(column)) {System.out.println("This is an Illegal move!"); return false;}

        for(int i=5;i>=0;--i){

            if(createboard[i][column] == 0) {

                createboard[i][column] = (byte)player;

                return true;

            }

        }

        return false;

    }

   

    public void undoMove(int column){

        for(int i=0;i<=5;++i){

            if(createboard[i][column] != 0) {

                createboard[i][column] = 0;

                break;

            }

        }       

    }

    //This function is used to Print the board

    public void displayBoard(){

        System.out.println();

        for(int i=0;i<=5;++i){

            for(int j=0;j<=6;++j){

                System.out.print(createboard[i][j]+" ");

            }

            System.out.println();

        }

        System.out.println();

    }

}

public class create2ndplayer {

    private Board b;

    private Scanner scan;

    private int nextMoveLocation=-1;

    private int maxDepth = 8;

   

    public create2ndplayer(Board b){

        this.b = b;

        scan = new Scanner(System.in);

    }

   

   

   

    //Opponent's turn

   

   

    public void letOpponentMove(){

        System.out.println("choose your move (1-7): ");

        int move = scan.nextInt();

        while(move<1 || move > 7 || !b.isitaLegalMove(move-1)){

            System.out.println("Invalid move. Your move (1-7): ");

            move = scan.nextInt();

        }

       

        //Here we will Assume 2 is the opponent

        b.placeyourMove(move-1, (byte)2);

    }

   

   

   

    //This is the final game Result

    public int gameResult(Board b){

        int aiScore = 0, humanScore = 0;

        for(int i=5;i>=0;--i){

            for(int j=0;j<=6;++j){

                if(b.createboard[i][j]==0) continue;

               

                //Checking cells to the right

                if(j<=3){

                    for(int k=0;k<4;++k){

                            if(b.createboard[i][j+k]==1) aiScore++;

                            else if(b.createboard[i][j+k]==2) humanScore++;

                            else break;

                    }

                    if(aiScore==4)return 1; else if (humanScore==4)return 2;

                    aiScore = 0; humanScore = 0;

                }

               

                //we will be checking cells up

                if(i>=3){

                    for(int k=0;k<4;++k){

                            if(b.reateboard[i-k][j]==1) aiScore++;

                            else if(b.createboard[i-k][j]==2) humanScore++;

                            else break;

                    }

                    if(aiScore==4)return 1; else if (humanScore==4)return 2;

                    aiScore = 0; humanScore = 0;

                }

                

                // now we will be checking diagonal up-right

                if(j<=3 && i>= 3){

                    for(int k=0;k<4;++k){

                        if(b.createboard[i-k][j+k]==1) aiScore++;

                        else if(b.createboard[i-k][j+k]==2) humanScore++;

                        else break;

                    }

                    if(aiScore==4)return 1; else if (humanScore==4)return 2;

                    aiScore = 0; humanScore = 0;

                }

               

                // now we will be checking diagonal up-left

                if(j>=3 && i>=3){

                    for(int k=0;k<4;++k){

                        if(b.createboard[i-k][j-k]==1) aiScore++;

                        else if(b.createboard[i-k][j-k]==2) humanScore++;

                        else break;

                  }

                    if(aiScore==4)return 1; else if (humanScore==4)return 2;

                    aiScore = 0; humanScore = 0;

                }

            }

        }

       

        for(int j=0;j<7;++j){

            //Game has not ended yet

            if(b.createboard[0][j]==0)return -1;

        }

        //The game is drawn!

        return 0;

    }

   

    int calculateScore(int aiScore, int moreMoves){  

        int moveScore = 4 - moreMoves;

        if(aiScore==0)return 0;

        else if(aiScore==1)return 1*moveScore;

        else if(aiScore==2)return 10*moveScore;

        else if(aiScore==3)return 100*moveScore;

        else return 1000;

    }

   

    //now we will evaluate board favorableness for AI(2nd player)

    public int evaluateBoard(Board b){

     

        int aiScore=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.createboard[i][j]==0 || b.createboard[i][j]==2) continue;

                

                if(j<=3){

                    for(k=1;k<4;++k){

                        if(b.createboard[i][j+k]==1)aiScore++;

                        else if(b.createboard[i][j+k]==2){aiScore=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.createboard[m][column]==0)moreMoves++;

                                else break;

                            }

                        }

                   

                    if(moreMoves!=0) score += calculateScore(aiScore, moreMoves);

                    aiScore=1;  

                    blanks = 0;

                }

               

                if(i>=3){

                    for(k=1;k<4;++k){

                        if(b.createboard[i-k][j]==1)aiScore++;

                        else if(b.reateboard[i-k][j]==2){aiScore=0;break;}

                    }

                    moreMoves = 0;

                   

                    if(aiScore>0){

                        int column = j;

                        for(int m=i-k+1; m<=i-1;m++){

                         if(b.createboard[m][column]==0)moreMoves++;

                            else break;

                        }

                    }

                    if(moreMoves!=0) score += calculateScore(aiScore, moreMoves);

                    aiScore=1;

                    blanks = 0;

                }

                

                if(j>=3){

                    for(k=1;k<4;++k){

                        if(b.createboard[i][j-k]==1)aiScore++;

                        else if(b.createboard[i][j-k]==2){aiScore=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.createboard[m][column]==0)moreMoves++;

                                else break;

                            }

                        }

                   

                    if(moreMoves!=0) score += calculateScore(aiScore, moreMoves);

                    aiScore=1;

                    blanks = 0;

                }

                

                if(j<=3 && i>=3){

                    for(k=1;k<4;++k){

                        if(b.createboard[i-k][j+k]==1)aiScore++;

                        else if(b.createboard[i-k][j+k]==2){aiScore=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.createboard[m][column]==0)moreMoves++;

                                else if(b.createboard[m][column]==1);

                                else break;

                            }

                        }

                        if(moreMoves!=0) score += calculateScore(aiScore, moreMoves);

                        aiScore=1;

                        blanks = 0;

                    }

                }

                

                if(i>=3 && j>=3){

                    for(k=1;k<4;++k){

                        if(b.createboard[i-k][j-k]==1)aiScore++;

                        else if(b.createboard[i-k][j-k]==2){aiScore=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.createboard[m][column]==0)moreMoves++;

                                else if(b.createboard[m][column]==1);

                                else break;

                            }

                        }

                        if(moreMoves!=0) score += calculateScore(aiScore, moreMoves);

                        aiScore=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 evaluateBoard(b);

       

        int maxScore=Integer.MIN_VALUE, minScore = Integer.MAX_VALUE;

        for(int j=0;j<=6;++j){

            if(!b.isitLegalMove(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("This is the given score for location "+j+" = "+currentScore);

                        if(maxScore==currentScore) nextMoveLocation = j;

                    }

            }else if(turn==2){

                    b.placeyourMove(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 playAgainstAIConsole(){

        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")) letOpponentMove();

        b.displayBoard();

        b.placeyourMove(3, 1);

        b.displayBoard();

       

        while(true){

            letOpponentMove();

            b.displayBoard();

           

            int gameResult = gameResult(b);

            if(gameResult==1){System.out.println("player 2 Wins!");break;}

            else if(gameResult==2){System.out.println("You Win!");break;}

            else if(gameResult==0){System.out.println("Draw!");break;}

           

            b.placeyorMove(getAIMove(), 1);

            b.displayBoard();

            gameResult = gameResult(b);

            if(gameResult==1){System.out.println("player2 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) {

        createBoard z = new createBoard();

        create2ndplayer cz = new create2ndplayer(z);

        cz.playAgainstAIConsole();

    }

}