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

**********IN JAVA**************** Creating the game Reversi/Othello in Java lang

ID: 3605617 • Letter: #

Question

**********IN JAVA****************

Creating the game Reversi/Othello in Java language

******NEEDS TO BE AN OOP PROGRAM********

This is an OOP project. Follow low coupling, high cohesion in your class design. Having only one class with only one main method is not acceptable.

MULTIPLE CLASSES REQUIRED!!

Don't worry about the UML diagram as I can create that after going through and understanding the code.

LEAVING COMMENTS IS VERY HELPFUL IN HELPING ME UNDERSTAND THE PROGRAM!!

You are implementing a strategy board game played by two players, Black and White. It is played on an N by N board. The winner is the player who has more discs of his color than his opponent at the end of the game. This will happen when neither of the two players has a legal move or there are no spaces left on the board Starting Position At the beginning of the game, two black discs and two white discs are placed in the middle of the board Black always begins, and the two players subsequently take turns moving Making a Move At his turn, a player must place a disc of his color on one of the empty squares of the board, adjacent to an opponent's disc. No two matching colors are connected vertically or horizontally so a miniature-chequered pattern is made The Game Both players take it in turns to make their move which consists of placing one piece down in a legally acceptable position and then turning any of the opposing player's pieces where applicable A legal move is one that consists of, for example, a black piece being placed on the board that creates a straight line (vertical, horizontal or diagonal which diagonal is optional for this project) made up of a black piece at either end and only white pieces in between. When a player achieves this, they must complete the move by turning any white pieces in between the two black so that they line becomes entirely black. This turning action must be completed for every legal turning line that is created with the placing of the new piece Players will then continue to move alternately until they get to the end of the game and a winner is decided. This decision is reached by identifying which of the two opponents has the most pieces on the board Different version of the Game 1. Player versus the computer (Mandatory for this project) 2. Computer versus computer (Optional for this project 3. Player versus player Optional for this ect A) Draw a UML class diagram. ldentify classes and the relation between them (for example inheritance or aggregation) Use a standard tool to draw UML diagram such as starUML which you can download it from http://staruml.io/download. Manual UML class . diagrams are not accepted B) Convert the class diagram to java classes Note NUM OF PLAYERS= 2 · NUM OF ROW= N NUMBER OF COLUMN = N Color is either black "B" or "W"

Explanation / Answer

Source Code:

package reversi;

import java.util.HashSet;

import java.util.Scanner;

import java.util.Set;

class Reversi_Board

{

    public class Cell

    {

        int x, y;

        Cell(int x, int y)

        {

            this.x = x;

            this.y = y;

        }

        @Override

        public String toString()

        {

            return "["+x+", "+y+"]";

        }

        @Override

        public boolean equals(Object obj)

        {

            return obj.hashCode()==this.hashCode();

        }

        @Override

        public int hashCode()

        {

            return Integer.parseInt(x+""+y);

        }

    }

    private final char[][] R_board;

    int White_Score, Black_Score, balance;

    private final char Board_Xaxis[] = new char[]{'0','1','2','3','4','5','6','7'};

    public Reversi_Board()

    {

        R_board = new char[][]{

            {'_','_','_','_','_','_','_','_',},{'_','_','_','_','_','_','_','_',},

            {'_','_','_','_','_','_','_','_',},{'_','_','_','W','B','_','_','_',},

            {'_','_','_','B','W','_','_','_',},{'_','_','_','_','_','_','_','_',},

            {'_','_','_','_','_','_','_','_',},{'_','_','_','_','_','_','_','_',},

        };

    }

    private void Find_Locations(char _Player1, char _Player2, HashSet<Cell> Pos)

    {

        for(int i=0;i<8;++i)

        {

            for(int j=0;j<8;++j)

            {

                if(R_board[i][j] == _Player2)

                {

                    int p = i, q = j;

                    if(i-1>=0 && j-1>=0 && R_board[i-1][j-1] == '_')

                    {

                        i = i+1; j = j+1;

                        while(i<7 && j<7 && R_board[i][j] == _Player2)

                        {

                            i++;j++;

                        }

                        if(i<=7 && j<=7 && R_board[i][j] == _Player1) Pos.add(new Cell(p-1, q-1));

                    }

                    i=p;j=q;

                    if(i-1>=0 && R_board[i-1][j] == '_')

                    {

                        i = i+1;

                        while(i<7 && R_board[i][j] == _Player2)

                        i++;

                        if(i<=7 && R_board[i][j] == _Player1) Pos.add(new Cell(p-1, q));

                    }

                    i=p;

                    if(i-1>=0 && j+1<=7 && R_board[i-1][j+1] == '_')

                    {

                        i = i+1; j = j-1;

                        while(i<7 && j>0 && R_board[i][j] == _Player2)

                        {

                            i++;j--;

                         

                        }

                        if(i<=7 && j>=0 && R_board[i][j] == _Player1) Pos.add(new Cell(p-1, q+1));

                    }

                    i=p;j=q;

                    if(j-1>=0 && R_board[i][j-1] == '_')

                    {

                        j = j+1;

                        while(j<7 && R_board[i][j] == _Player2)

                        j++;

                        if(j<=7 && R_board[i][j] == _Player1) Pos.add(new Cell(p, q-1));

                    }

                    j=q;

                    if(j+1<=7 && R_board[i][j+1] == '_')

                    {

                        j=j-1;

                        while(j>0 && R_board[i][j] == _Player2)

                        j--;

                        if(j>=0 && R_board[i][j] == _Player1) Pos.add(new Cell(p, q+1));

                    }

                    j=q;

                    if(i+1<=7 && j-1>=0 && R_board[i+1][j-1] == '_')

                    {

                        i=i-1;j=j+1;

                        while(i>0 && j<7 && R_board[i][j] == _Player2)

                        {

                            i--;j++;

                         

                        }

                        if(i>=0 && j<=7 && R_board[i][j] == _Player1) Pos.add(new Cell(p+1, q-1));

                    }

                    i=p;j=q;

                    if(i+1 <= 7 && R_board[i+1][j] == '_')

                    {

                        i=i-1;

                        while(i>0 && R_board[i][j] == _Player2)

                        i--;

                        if(i>=0 && R_board[i][j] == _Player1) Pos.add(new Cell(p+1, q));

                    }

                    i=p;

                    if(i+1 <= 7 && j+1 <=7 && R_board[i+1][j+1] == '_')

                    {

                        i=i-1;j=j-1;

                        while(i>0 && j>0 && R_board[i][j] == _Player2)

                        {

                            i--;j--;

                         

                        }

                        if(i>=0 && j>=0 && R_board[i][j] == _Player1)Pos.add(new Cell(p+1, q+1));

                    }

                    i=p;j=q;

                    }

                }

        }

    }

    public void display_RBoard(Reversi_Board b)

    {

        System.out.print(" ");

        for(int i=0;i<8;++i)System.out.print(Board_Xaxis[i]+" ");

        System.out.println();

        for(int i=0;i<8;++i)

        {

            System.out.print((i+1)+" ");

            for(int j=0;j<8;++j)

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

            System.out.println();

        }

        System.out.println();

    }

    public int Final_Result(Set<Cell> White_Locations, Set<Cell> Black_Locations)

    {

        Score_Updates();

        if(balance == 0)

        {

            if(White_Score > Black_Score)

                return 1;

            else if(Black_Score > White_Score)

                return -1;

            else return 0;

        }

        if(White_Score==0 || Black_Score == 0)

        {

            if(White_Score > 0)

            return 1;

            else if(Black_Score > 0)

            return -1;

        }

        if(White_Locations.isEmpty() && Black_Locations.isEmpty())

        {

            if(White_Score > Black_Score)

            return 1;

            else if(Black_Score > White_Score)

            return -1;

            else

            return 0;

      }

        return -2;

    }

    public HashSet<Cell> get_Locations(char _Player1, char _Player2)

    {

        HashSet<Cell> Pos = new HashSet<>();

        Find_Locations(_Player1, _Player2, Pos);

        return Pos;

    }

  

    public void show_Locations(HashSet<Cell> loc, char _Player1, char _Player2)

    {

        for(Cell p:loc)

            R_board[p.x][p.y]='*';

        display_RBoard(this);

        for(Cell p:loc)

            R_board[p.x][p.y]='_';

    }

    public void place_Move(Cell pt, char _Player1, char _Player2)

    {

        int i = pt.x, j = pt.y;

        R_board[i][j] = _Player1;

        int p = i, q = j;

     

        if(i-1>=0 && j-1>=0 && R_board[i-1][j-1] == _Player2)

        {

            i = i-1; j = j-1;

            while(i>0 && j>0 && R_board[i][j] == _Player2)

            {

                i--;j--;

            }

            if(i>=0 && j>=0 && R_board[i][j] == _Player1)

            {

                while(i!=p-1 && j!=q-1)

                R_board[++i][++j]=_Player1;

             

            }

        }

        i=p;j=q;

        if(i-1>=0 && R_board[i-1][j] == _Player2)

        {

            i = i-1;

            while(i>0 && R_board[i][j] == _Player2)

            i--;

            if(i>=0 && R_board[i][j] == _Player1)

            {

                while(i!=p-1)R_board[++i][j]=_Player1;

             

            }

        }

        i=p;

        if(i-1>=0 && j+1<=7 && R_board[i-1][j+1] == _Player2)

        {

            i = i-1; j = j+1;

            while(i>0 && j<7 && R_board[i][j] == _Player2)

            {

                i--;j++;

             

            }

            if(i>=0 && j<=7 && R_board[i][j] == _Player1)

            {

                while(i!=p-1 && j!=q+1)

                R_board[++i][--j] = _Player1;

             

            }

        }

        i=p;j=q;

        if(j-1>=0 && R_board[i][j-1] == _Player2)

        {

            j = j-1;

            while(j>0 && R_board[i][j] == _Player2)

            j--;

            if(j>=0 && R_board[i][j] == _Player1)

            {

                while(j!=q-1)

                R_board[i][++j] = _Player1;

             

            }

        }

        j=q;

        if(j+1<=7 && R_board[i][j+1] == _Player2)

        {

            j=j+1;

            while(j<7 && R_board[i][j] == _Player2)

            j++;

            if(j<=7 && R_board[i][j] == _Player1)

            {

                while(j!=q+1)

                R_board[i][--j] = _Player1;

             

          }

        }

        j=q;

        if(i+1<=7 && j-1>=0 && R_board[i+1][j-1] == _Player2)

        {

            i=i+1;j=j-1;

            while(i<7 && j>0 && R_board[i][j] == _Player2)

            {

                i++;

                j--;

            }

            if(i<=7 && j>=0 && R_board[i][j] == _Player1)

            {

                while(i!=p+1 && j!=q-1)

                R_board[--i][++j] = _Player1;

             

            }

        }

        i=p;j=q;

        if(i+1 <= 7 && R_board[i+1][j] == _Player2)

        {

            i=i+1;

            while(i<7 && R_board[i][j] == _Player2)

            i++;

            if(i<=7 && R_board[i][j] == _Player1)

            {

                while(i!=p+1)

                R_board[--i][j] = _Player1;

             

            }

        }

        i=p;

        if(i+1 <= 7 && j+1 <=7 && R_board[i+1][j+1] == _Player2)

        {

            i=i+1;j=j+1;

            while(i<7 && j<7 && R_board[i][j] == _Player2)

            {

                i++;

                j++;

             

            }

            if(i<=7 && j<=7 && R_board[i][j] == _Player1)

            while(i!=p+1 && j!=q+1)

            R_board[--i][--j] = _Player1;

        }

    }

    public void Score_Updates()

    {

        White_Score = 0; Black_Score = 0; balance = 0;

        for(int i=0;i<8;++i)

        {

            for(int j=0;j<8;++j)

            {

                if(R_board[i][j]=='W')

                White_Score++;

                else if(R_board[i][j]=='B')

                Black_Score++;

                else

                balance++;

            }

        }

    }

    public int X_coordinate(char x)

    {

        for(int i=0;i<8;++i)

        if(Board_Xaxis[i]==Character.toLowerCase(x)||Board_Xaxis[i]==Character.toUpperCase(x))

        return i;

        return -1;

    }

}

public class Reversi

{

    public static void Play_Game(Reversi_Board b)

    {

        Scanner sc = new Scanner(System.in);

        Reversi_Board.Cell move_C = b.new Cell(-1, -1);

        System.out.println("Black Moves first");

     

        int res;

        Boolean skip_C;

        String input_Cell;

     

        while(true)

        {

            skip_C = false;

         

            HashSet<Reversi_Board.Cell> Black_Locations = b.get_Locations('B', 'W');

            HashSet<Reversi_Board.Cell> White_Locations = b.get_Locations('W', 'B');

            b.show_Locations(Black_Locations, 'B', 'W');

            res = b.Final_Result(White_Locations, Black_Locations);

         

            if(res == 0)

            {

                System.out.println("Draw");

                break;

            }

            else if(res==1)

            {

                System.out.println("White wins: "+b.White_Score+":"+b.Black_Score);

                break;

            }

            else if(res==-1)

            {

                System.out.println("Black wins: "+b.Black_Score+":"+b.White_Score);

                break;

            }

            if(Black_Locations.isEmpty())

            {

                    System.out.println("Black needs to skip_C... Passing to white");

                    skip_C = true;

            }

            if(!skip_C)

            {

                System.out.println("Black move at: ");

                input_Cell = sc.next();

                move_C.y = b.X_coordinate(input_Cell.charAt(1));

                move_C.x = (Integer.parseInt(input_Cell.charAt(3)+"")-1);

             

                while(!Black_Locations.contains(move_C))

                {

                    System.out.println("Invalid move! Black move at: ");

                    input_Cell = sc.next();

                    move_C.y = b.X_coordinate(input_Cell.charAt(1));

                    move_C.x = Integer.parseInt((input_Cell.charAt(3)+""))-1;

                }

                b.place_Move(move_C, 'B', 'W');

                b.Score_Updates();

                System.out.println(" Score: Black: "+b.Black_Score+" White: "+b.White_Score);

            }

            skip_C = false;

            White_Locations = b.get_Locations('W', 'B');

            Black_Locations = b.get_Locations('B', 'W');

            b.show_Locations(White_Locations, 'W', 'B');

            res = b.Final_Result(White_Locations, Black_Locations);

            if(res==0){System.out.println("pt is a draw.");

            break;

             

            }

            else if(res==1){System.out.println("White wins: "+b.White_Score+":"+b.Black_Score);

            break;

             

            }

            else if(res==-1)

            {

                System.out.println("Black wins: "+b.Black_Score+":"+b.White_Score);

                break;

             

            }

            if(White_Locations.isEmpty())

            {

                    System.out.println("White needs to skip... Passing to Black");

                    skip_C = true;

            }

            if(!skip_C)

            {

            System.out.println("White move at: ");

            input_Cell = sc.next();

            move_C.y = b.X_coordinate(input_Cell.charAt(1));

            move_C.x = (Integer.parseInt(input_Cell.charAt(3)+"")-1);

            while(!White_Locations.contains(move_C))

            {

                System.out.println("Invalid move! White move at: ");

                input_Cell = sc.next();

                move_C.y = b.X_coordinate(input_Cell.charAt(1));

                move_C.x = (Integer.parseInt(input_Cell.charAt(3)+"")-1);

            }

            b.place_Move(move_C, 'W', 'B');

            b.Score_Updates();

            System.out.println(" White: "+b.White_Score+" Black: "+b.Black_Score);

            }

        }

    }

     

    public static void main(String[] args)

    {

        Reversi_Board b = new Reversi_Board();

        Play_Game(b);

    }

}