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

I am trying to make a chessboard, and apply the \"connect four\" game. Now, i ha

ID: 640140 • Letter: I

Question

I am trying to make a chessboard, and apply the "connect four" game.

Now, i having trouble doing several methods. Need Some help

here is the chessboard i made :

private int[][] chessBoard = {
{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}
};

1.// randomly starts,( either start with the blue disk or red)
public void randomStart() {

2. // start a new game
public void restart() {

3.// check if it can add more discs or not
public boolean isFull(int x, int y) {

Explanation / Answer

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.GridLayout;

import java.util.Scanner;

import javax.swing.JFrame;

import javax.swing.JPanel;

/**

* ConnectFour: Two-player console, graphics.

*/

@SuppressWarnings("serial")

public class ConnectPanel extends JFrame{

    // Name-constants to represent the seeds and cell contents

    public static final int EMPTY = 0;

    public static final int PLAYER1 = 1;

    public static final int PLAYER2 = 2;

    // Name-constants to represent the various states of the game

    public static final int PLAYING = 0;

    // The game board and the game status   

    public static int currentState = 1; // the current state of the game, set initially equal to 1 (to start a new game)

    public static int currentPlayer; // the current player (PLAYER1 or PLAYER2)

    public static int currentRow, currentCol; // current seed's row and column

    public static int Rows = 7, Columns = 8, AmountToWin = 4;

    public static int theSeed;

    public static int[][] board;

    public static Scanner input = new Scanner(System.in); // the input Scanner

    public static void main(String[] args) {

        // Initialize the game-board and current status

        while ( currentState != PLAYING){ // after finishing a game, this makes it possible starting a new one

            board = new int[Rows][Columns]; // game board in 2D array

            ConnectPanel frame = new ConnectPanel(theSeed, currentRow, currentCol);

            frame.setSize(100*Columns, 100*Rows);

            frame.setLocationRelativeTo(null);

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            frame.setVisible(true);

            initGame(Rows, Columns, board);    

            // Play the game once

            do {

                playerMove(currentPlayer, Rows, Columns, board, AmountToWin); // update currentRow and currentCol

                currentPlayer = (currentPlayer == PLAYER1) ? PLAYER2 : PLAYER1; // Switch player

            } while (currentState == PLAYING); // repeat if not game-over

        }

    }

    public static void initGame(int Rows, int Columns, int[][] board) {    

        for (int row = 0; row < Rows; ++row) {

            for (int col = 0; col < Columns; ++col) {

                board[row][col] = 0; // all cells empty

            }

        }

        currentState = PLAYING; // ready to play

        currentPlayer = 1; // player 1 plays first

    }

    public static void playerMove(int theSeed, int Rows, int Columns, int[][] board, int AmountToWin) {     

            if (theSeed == 1) {

                System.out.println("Player 1, enter a column to place your chip (1-8): ");

            } else {

                System.out.println("Player 2, enter a column to place your chip (1-8): ");

            }

            int col = input.nextInt() - 1;

            int row = availableRow(col, Rows, board); //finds the first empty row by corresponding column

            currentRow = row;

            currentCol = col;

            board[row][currentCol] = theSeed; // update game-board content

            ConnectPanel frame = new ConnectPanel(theSeed, currentRow, currentCol);

            frame.setSize(100*Columns, 100*Rows);

            frame.setLocationRelativeTo(null);

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            frame.setVisible(true);

    }   

    /** Print the game board */

    public ConnectPanel(int theSeed, int currentCol, int currentRow){

        JPanel p1 = new JPanel();

        p1.setLayout(new GridLayout(Rows, Columns));

        for (int i=0; i<Columns*Rows; i++) {

            p1.add(new ArcsPanel());

        }

        add(p1, BorderLayout.CENTER);

    }

    class ArcsPanel extends JPanel {

        public void paintComponent(Graphics g) {

            super.paintComponent(g);

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

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

                    int x1 = 100*(Columns - 1);

                    int y1 = 100*(Rows - 1);

                    g.fillOval(x1, y1, 100, 100);

                    int xCenter = getWidth() / 2;

                    int yCenter = getHeight() / 2;

                    int radius = (int)(Math.min(getWidth(), getHeight())*0.4);

                    int x2 = xCenter - radius;

                    int y2 = yCenter - radius;

                    g.fillArc(x2, y2, 2*radius, 2*radius, 0, 360);

                    setBackground(Color.BLACK);

                    if (theSeed == 1) {

                        int x3 = 100*(currentCol - 1);

                        int y3 = 100*(currentRow - 1);

                        g.fillOval(x3, y3, 100, 100);

                        g.setColor(Color.RED);

                    }

                    if (theSeed == 2) {

                        int x3 = 100*(currentCol - 1);

                        int y3 = 100*(currentRow - 1);

                        g.fillOval(x3, y3, 100, 100);

                        g.setColor(Color.BLUE);

                    }

                    else {

                        int x3 = 100*(currentCol - 1);

                        int y3 = 100*(currentRow - 1);

                        g.fillOval(x3, y3, 100, 100);

                        g.setColor(Color.WHITE);

                    }

                }

            }

        }

    }

    /**finds the first empty space in a column starting at the bottom.*/

    public static int availableRow( int col, int Rows, int[][] board){

        for( int row = Rows -1; row >= 0; row--){

            if(board[row][col] == EMPTY){

                return row;           

            }

        } return -1;

    }        

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote