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

Need to make a Reversi/Othello Board game in JAVA The board size can be N x N (s

ID: 3778319 • Letter: N

Question

Need to make a Reversi/Othello Board game in JAVA

The board size can be N x N (so 4x4 is default, and it could be larger like 5x5, 6x6 etc. NEEDS TO BE ABLE TO WORK WITH ODD SIZE)

Player 1 is going to be you and your going to be the black tiles, and the AI is going to be the white tiles. Black tiles will start the game, and then white will go after them, then back to black, repeat. The players must put a disc of their color on an empty square, adjacent to their opponent's disc. Any of the oppponents tiles in between the tile put down, and another of your tiles will cause them to flip to your color. The winner is decided when the board is completely filled or no viable moves are left, and whoever has the most pieces.

This program is meant to be simple design (no GUI or big interface designs), broken into multiple classes (can be extension's off the class or abstract interface off each other), and the user will be entering the coordinates of where to put the pieces (so just using scanner method)?. The start of the game must always have 4 tiles put down in the center (so the center can change depending on the size of the board), and the gameplay would look something like this

Again, coding done in JAVA is what I'm looking for.

Scenario in which the game ends early.

JAVA code

BW Success: Black move at C1, 0) BW Score: Black: 4, White: 1 Success: White move at C2, 0D

Explanation / Answer

package Reversi_Othello_GameReversiOthello;

import java.awt.*;
import java.applet.*;

public class Reversi_Othello_GameReversiOthello extends Applet implements Runnable {

    Thread runner;                // declare a thread for this GameReversiOthello

    boolean black_shown = false;    // flag to signal pause
    boolean show_em = false;

    final int BLACK = 1;          // declare state of each square
    final int WHITE = 2;
    final int EMPTY = 0;
    final int OFFBOARD = -1;

    final static int GameReversiOthello[][] = new int[10][10];    //10x10 matrix of squares

    protected int Count_Black = 0;
    protected int Count_White = 0;

    Event evt;
    int x, y;

    public void start() // create a thread and start it
    {
        if (runner == null) {
            black_shown = false;
            runner = new Thread(this);
            runner.start();
        }
    }

    public void stop() // stop the thread
    {
        if (runner != null) {
            runner.stop();
            runner = null;
            black_shown = false;
        }
    }

    public synchronized void run() // initialize screen
    {
        setBackground(Color.green);

        for (int i = 0; i < 10; i++) // initialize off-board squares
        {
            GameReversiOthello[i][0] = OFFBOARD;
            GameReversiOthello[i][9] = OFFBOARD;
            GameReversiOthello[0][i] = OFFBOARD;
            GameReversiOthello[9][i] = OFFBOARD;
        }

        for (int i = 1; i < 9; i++) // initialize GameReversiOthello board to be empty
        {
            for (int j = 1; j < 9; j++) {
                GameReversiOthello[i][j] = EMPTY;
            }
        }

        GameReversiOthello[4][4] = WHITE;          // except for initial set up
        GameReversiOthello[5][4] = BLACK;
        GameReversiOthello[4][5] = BLACK;
        GameReversiOthello[5][5] = WHITE;

        while (runner != null) // signal thread to wait after painting
        {                         // black and before white responds
            while (!black_shown) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
                black_shown = false;
                showStatus("You Move is Awesome!! Keep Going");
                pause(1000);
                whiteResponds();
            }
        }
    }

    // BLACK clicked on a square - update screen
    public synchronized boolean mouseUp(Event evt, int x, int y) {
        Dimension d = size();             // find out which square was clicked
        int c = (x * 8) / d.width + 1;        // column
        int r = (y * 8) / d.height + 1;       // row
        boolean black_done;               // true if black cannot move anywhere

        if (MoveLegal(r, c, BLACK, WHITE, true)) {
            GameReversiOthello[r][c] = BLACK;             // set that square to black
            repaint();
            black_shown = true;
            notify();
        } else {
            showStatus("Not a legal move");
        }
        black_done = true;                 // check if black can move anywhere
        for (int i = 1; i < 9; i++) {
            for (int j = 1; j < 9; j++) {
                if (MoveLegal(i, j, BLACK, WHITE, false)) {
                    black_done = false;
                }
            }
        }

        if (black_done) // black cannot move - white finishes up
        {
            for (int i = 1; i < 65; i++) {
                whiteResponds();
            }
        }

        return true;
    }

    // computer responds with a move
    public void whiteResponds() {
        boolean found;                     // true if a legal square is found
        int i, j;    // indices for loops

        found = false;
        if (MoveLegal(1, 1, WHITE, BLACK, true)) // first check corners
        {
            GameReversiOthello[1][1] = WHITE;
            found = true;
        }
        if ((!found) && (MoveLegal(8, 8, WHITE, BLACK, true))) {
            GameReversiOthello[8][8] = WHITE;
            found = true;
        }
        if ((!found) && (MoveLegal(1, 8, WHITE, BLACK, true))) {
            GameReversiOthello[1][8] = WHITE;
            found = true;
        }
        if ((!found) && (MoveLegal(8, 1, WHITE, BLACK, true))) {
            GameReversiOthello[8][1] = WHITE;
            found = true;
        }

        i = 3;    // check center squares
        while ((!found) && (i < 7)) {
            j = 3;
            while ((!found) && (j < 7)) {
                if (MoveLegal(i, j, WHITE, BLACK, true)) {
                    GameReversiOthello[i][j] = WHITE;
                    found = true;
                }
                j++;
            }
            i++;
        }

        i = 3;
        while ((!found) && (i < 7)) // then check edges except for those
        {                           // surrounding a corner
            if (MoveLegal(1, i, WHITE, BLACK, true)) {
                GameReversiOthello[1][i] = WHITE;
                found = true;
            }
            if ((!found) && (MoveLegal(8, i, WHITE, BLACK, true))) {
                GameReversiOthello[8][i] = WHITE;
                found = true;
            }
            if ((!found) && (MoveLegal(i, 1, WHITE, BLACK, true))) {
                GameReversiOthello[i][1] = WHITE;
                found = true;
            }
            if ((!found) && (MoveLegal(i, 8, WHITE, BLACK, true))) {
                GameReversiOthello[i][8] = WHITE;
                found = true;
            }
            i++;
        }

        i = 3;
        while ((!found) && (i < 7)) // next check inner edges
        {
            if (MoveLegal(2, i, WHITE, BLACK, true)) {
                GameReversiOthello[2][i] = WHITE;
                found = true;
            }
            if ((!found) && (MoveLegal(7, i, WHITE, BLACK, true))) {
                GameReversiOthello[7][i] = WHITE;
                found = true;
            }
            if ((!found) && (MoveLegal(i, 2, WHITE, BLACK, true))) {
                GameReversiOthello[i][2] = WHITE;
                found = true;
            }
            if ((!found) && (MoveLegal(i, 7, WHITE, BLACK, true))) {
                GameReversiOthello[i][7] = WHITE;
                found = true;
            }
            i++;
        }

        i = 1;  // finally squares surrounding a corner
        while ((!found) && (i < 9)) {
            j = 1;
            while ((!found) && (j < 9)) {
                if (MoveLegal(i, j, WHITE, BLACK, true)) {
                    found = true;
                    GameReversiOthello[i][j] = WHITE;
                }
                j++;
            }
            i++;
        }

        repaint();
    }

    // decide if the move is legal
    public boolean MoveLegal(int r, int c, int color, int othercolor,
            boolean flip) {
        int i, j;                                  // position on board
        boolean legal;                            // true if legal move
        int stepcount;                            // counts the stepping
        // across the board

        legal = false;

        if (GameReversiOthello[r][c] == EMPTY) // square clicked must be empty
        {
            for (int xdir = -1; xdir < 2; xdir++) {
                for (int ydir = -1; ydir < 2; ydir++) {
                    stepcount = 0;
                    do {
                        stepcount++;
                        i = r + stepcount * xdir; // so many steps along x-axis
                        j = c + stepcount * ydir; // so many steps along y-axis
                    } while ((i > 0) && (i < 9) && (j > 0) && (j < 9)
                            && (GameReversiOthello[i][j] == othercolor));
                    if ((i > 0) && (i < 9) && (j > 0) && (j < 9)
                            && (stepcount > 1)
                            && // You must move more than one step for legal move
                            (GameReversiOthello[i][j] == color)) {
                        legal = true;
                        if (flip) {
                            for (int k = 1; k < stepcount; k++) {
                                GameReversiOthello[r + xdir * k][c + ydir * k] = color;
                            }
                        }
                    }
                }
            }
        }
        if (legal == true) {
            return true;
        } else {
            return false;
        }
    }

    void pause(int time) // time delay
    {
        try {
            runner.sleep(time);
        } catch (InterruptedException e) {
        }
    }

    // paint the screen with the right configuartion
    public void paint(Graphics g) {
        Dimension d = size();
        g.setColor(Color.black);
        int xoff = d.width / 8;
        int yoff = d.height / 8;

        Count_Black = 0;                        // initialize counts to 0
        Count_White = 0;
        boolean done;                         // true if GameReversiOthello is over

        for (int i = 1; i <= 8; i++) // draw the lines
        {
            g.drawLine(i * xoff, 0, i * xoff, d.height);
            g.drawLine(0, i * yoff, d.width, i * yoff);
        }

        for (int i = 1; i < 9; i++) // scan board for black discs
        {
            for (int j = 1; j < 9; j++) {
                if (GameReversiOthello[i][j] == BLACK) // draw BLACK discs
                {
                    g.fillOval((j * yoff + 3) - yoff, (i * xoff + 3) - xoff, 33, 33);
                    Count_Black++;
                }
            }
        }

        g.setColor(Color.white);
        for (int i = 1; i < 9; i++) // scan board for white discs
        {
            for (int j = 1; j < 9; j++) {
                if (GameReversiOthello[i][j] == WHITE) // draw WHITE discs
                {
                    g.fillOval((j * yoff + 3) - yoff, (i * xoff + 3) - xoff, 33, 33);
                    Count_White++;
                }
            }
        }

        g.setColor(Color.red);

        done = true;

        for (int i = 1; i < 9; i++) {
            for (int j = 1; j < 9; j++) {
                if ((MoveLegal(i, j, BLACK, WHITE, false))
                        || (MoveLegal(i, j, WHITE, BLACK, false))) {
                    done = false;
                }
            }
        }

        if (done == true) {
            if (Count_White > Count_Black) {
                g.drawString("Game won By White with " + Count_White + " discs.", 10, 20);
            } else if (Count_Black > Count_White) {
                g.drawString("Game won By Black " + Count_Black + " discs.", 10, 20);
            } else {
                g.drawString("Tied GameReversiOthello", 10, 20);
            }
        } else {
            if (Count_White > Count_Black) {
                g.drawString("White is winning with " + Count_White + " discs", 10, 20);
            } else if (Count_Black > Count_White) {
                g.drawString("Black is winning with " + Count_Black + " discs", 10, 20);
            } else {
                g.drawString("Currently the game is Tie", 10, 20);
            }
        }

        if (show_em == true) {
            for (int i = 1; i < 9; i++) {
                for (int j = 1; j < 9; j++) {
                    if (MoveLegal(i, j, BLACK, WHITE, false)) {
                        g.fillOval((j * yoff + 15) - yoff, (i * xoff + 15) - xoff, 5, 5);
                    }
                }
            }
        }
    }

    public void Action() {
        if (show_em == false) {
            show_em = true;
            repaint();
        } else {
            show_em = false;
            repaint();
        }
    }
}

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