Java coding:othello

profileCharliesang

This is an individual project, to be completed on your own. It is considered dishonest either to read someone else's solution or to provide a classmate with a copy of your work. Do not make the mistake of thinking that superficial changes in a program (such as altering comments, changing variable names, or interchanging statements) can be used to avoid detection. If you cannot do the work yourself, it is extremely unlikely that you can succeed in disguising someone else's work. We are adamant that cheating in any form is not tolerated. Even the most trivial assignment is better not done than if you cheat to complete it.

Prerequisites

  1. Primitive types
  2. Conditionals
  3. Loops
  4. Arrays

Learning Objectives

  1. Problem Solving
  2. Traversing and manipulating 2-dimensional arrays
  3. Using nested loops

Introduction

In this project, you will implement the logic of the game Othello, also known as Reversi.

Game Rules

Othello is a strategy board game for two players on a squared checkered board. It is played with identical disks that are white on one side and black on the other. Players take turns placing disks on the board with their assigned color facing up.

The game starts with four disks placed in a square at the center of the board in a standard diagonal pattern: two facing white side up, two facing black side up and same-colored disk on a diagonal with each other. These four initial disks are not placed by the player.

The black player moves first. He must place a disk with the black side up on the board, in such a position that there exists at least one straight occupied line between the new disk and another dark disk, with one or more contiguous white disks bounded between them. During a move, all bounded disks of the opponent's color are turned over to the current's player color. A straight line involves: horizontal, vertical and diagonal lines in every possible direction. It is possible to bound disks in more than one direction during a single move.

Players take turns to play. When a player doesn't have any valid moves, his turned is skipped. The game must keep going while there are valid moves by either player. The goal of the game is to have the majority of the disks displaying your color when there are no more possible moves.

We suggest that you play an online version of the game to grasp the idea.

  

Your Task

In this project, you will implement the rules and logic for Othello. You are provided with the skeleton code containing the basic methods that you need to complete to launch this game on the java console. You have the freedom to use additional methods and variables to structure your solution, as long as the basic methods behave as expected.

Implementation Details

This project consists of just one class:

  1. Othello: it implements the logic of the game. You will implement the missing methods.

You should begin coding after you have read the entire handout, understood the problem and made a plan for solving it.

Variables

The Othello class will contain the following class variables:

private static final int NONE = 0;
private static final int BLACK = 1;
private static final int WHITE = 2;

and the following instance variables:

public int[][] board; 

The variables spelled in all capitals represent the possible values for positions in the board. They are constant, which is why they are declared final. Final is a Java modifier to indicate that the value of these fields cannot change. Constants defined this way cannot be reassigned, and trying to do so will produce a compile-time error. By convention, the names of constant values are spelled in uppercase letters. This convention is used in many programming languages. We recommend that you follow this convention when using constant values.

The board variable is a 2-d squared array that represents the actual board where the game is played. Each position is specified by its row and column. Each position will contain NONE when there are no disks in it, BLACK when there is a black disk facing up and WHITE when there is a white disk facing up.

Methods

You need to complete the following basic methods to implement the game's logic. A skeleton code is provided below.

  • public Othello(int length) : The constructor initializes the instance variables and creates the starting board configuration of the Othello game. The input length represents the size of the squared board (same length of rows and columns). Refer to the rules above to see what is the initial state of the game. You can assume that the length will always be an even number.
  • public boolean isValid(int row, int col, int player) : returns true if the specified position is a valid move for the specified player. For a position to be valid, it should be in bounds, empty and playable by the specified player. Refer to rules above to see what is a playable position.
  • public boolean hasValidMoves(int player) : scans the complete board to look for positions that are valid moves for the specified player. If there are no valid moves for the input player, it returns false. If there is at least one valid move, it returns true.
  • public void makeMove(int row, int col, int player) : The specified player makes the move on position (row, col). Refer to the rules above.
  • public int playGame() : this method implements the logic of the game by making use of the previous methods. Players take turns to play and the position has to be obtained by prompting the users. You have to make sure that players enter valid positions. In case they choose an invalid position, keep asking until they choose a valid one. However, if a player doesn't have any possible valid moves, there is no need to prompt him and his turned is skipped. Print the board at the beginning of the game and after each turn. The game must keep going until none of the players have valid moves. When the game is over, remember to congratulate the winner :)

To print the board, use the method provided to you in the skeleton: public void printBoard(int turn).

Remember that you have the freedom to add the auxiliary methods or variables that you deem helpful, as well as to reuse methods that you have coded, as long as specified methods behave as expected. An example execution is illustrated below.

Test Cases

These test cases are to help you during your development. There are no test cases for the method playGame and you will have to test it manually.Project02StudentTest

Example

This is an example with a board of size 4×4. We recommend that you begin to test your code with small boards.

    0   1   2   3  
  ----------------
0 |   | * |   |   |
  ----------------
1 | * | O | X |   |
  ----------------
2 |   | X | O | * |
  ----------------
3 |   |   | * |   |
  ----------------
X 2 - O: 2

Current player: X
Enter a valid position
Row: 
0
Column: 
1

    0   1   2   3  
  ----------------
0 | * | X | * |   |
  ----------------
1 |   | X | X |   |
  ----------------
2 | * | X | O |   |
  ----------------
3 |   |   |   |   |
  ----------------
X 4 - O: 1

Current player: O
Enter a valid position
Row: 
0
Column: 
0

    0   1   2   3  
  ----------------
0 | O | X |   |   |
  ----------------
1 | * | O | X |   |
  ----------------
2 |   | X | O | * |
  ----------------
3 |   |   | * |   |
  ----------------
X 3 - O: 3

Current player: X
Enter a valid position
Row: 
3
Column: 
2

    0   1   2   3  
  ----------------
0 | O | X | * |   |
  ----------------
1 |   | O | X | * |
  ----------------
2 |   | X | X |   |
  ----------------
3 |   | * | X | * |
  ----------------
X 5 - O: 2

Current player: O
Enter a valid position
Row: 
1
Column: 
3

    0   1   2   3  
  ----------------
0 | O | X | * | * |
  ----------------
1 |   | O | O | O |
  ----------------
2 |   | X | X | * |
  ----------------
3 |   |   | X |   |
  ----------------
X 4 - O: 4

Current player: X
Enter a valid position
Row: 
0
Column: 
3

    0   1   2   3  
  ----------------
0 | O | X | * | X |
  ----------------
1 |   | O | X | O |
  ----------------
2 |   | X | X |   |
  ----------------
3 |   | * | X | * |
  ----------------
X 6 - O: 3

Current player: O
Enter a valid position
Row: 
3
Column: 
1

    0   1   2   3  
  ----------------
0 | O | X |   | X |
  ----------------
1 | * | O | X | O |
  ----------------
2 |   | O | O | * |
  ----------------
3 | * | O | X |   |
  ----------------
X 4 - O: 6

Current player: X
Enter a valid position
Row: 
3
Column: 
0

    0   1   2   3  
  ----------------
0 | O | X | * | X |
  ----------------
1 |   | O | X | O |
  ----------------
2 | * | X | O |   |
  ----------------
3 | X | X | X |   |
  ----------------
X 7 - O: 4

Current player: O
Enter a valid position
Row: 
0
Column: 
2

    0   1   2   3  
  ----------------
0 | O | O | O | X |
  ----------------
1 |   | O | O | O |
  ----------------
2 |   | X | O | * |
  ----------------
3 | X | X | X |   |
  ----------------
X 5 - O: 7

Current player: X
Enter a valid position
Row: 
2
Column: 
3

    0   1   2   3  
  ----------------
0 | O | O | O | X |
  ----------------
1 |   | O | O | X |
  ----------------
2 |   | X | X | X |
  ----------------
3 | X | X | X | * |
  ----------------
X 8 - O: 5

Current player: O
Enter a valid position
Row: 
3
Column: 
3

    0   1   2   3  
  ----------------
0 | O | O | O | X |
  ----------------
1 | * | O | O | X |
  ----------------
2 |   | X | O | X |
  ----------------
3 | X | X | X | O |
  ----------------
X 7 - O: 7

Current player: X
Enter a valid position
Row: 
2
Column: 
0
Enter a valid position
Row: 
1
Column: 
0

    0   1   2   3  
  ----------------
0 | O | O | O | X |
  ----------------
1 | X | X | X | X |
  ----------------
2 | * | X | O | X |
  ----------------
3 | X | X | X | O |
  ----------------
X 10 - O: 5

Current player: O
Enter a valid position
Row: 
2
0Column: 


    0   1   2   3  
  ----------------
0 | O | O | O | X |
  ----------------
1 | O | O | X | X |
  ----------------
2 | O | O | O | X |
  ----------------
3 | X | X | X | O |
  ----------------
X 7 - O: 9

Color O WINS!

Process finished with exit code 0

Code

Othello.java
public class Othello {
 
    public static final int NONE = 0;
    public static final int BLACK = 1;
    public static final int WHITE = 2;
 
    public int[][] board;
 
    public Othello(int length) {
       //TO-DO: create the initial state of the game
    }
 
    public boolean isValid(int row, int col, int player) {
        //TO-DO
        return false;
    }
 
    public boolean hasValidMoves(int player) {
        //TO-DO
        return false;
    }
 
    public void makeMove(int row, int col, int player) {
        //TO-DO
    }
 
    public void playGame() {
       //TO-DO
    }
 
   /**
     * This method prints the board to the console
     * @param turn current turn
     */
   public void printBoard(int turn) {
        int numBlacks = 0;
        int numWhites = 0;
        System.out.println();
        System.out.printf("   ");
        for (int i = 0; i < this.board.length; i++) {
            System.out.printf(" " + i + "  ");
        }
        System.out.printf("\n  ");
        for (int i = 0; i < this.board.length; i++) {
            System.out.printf("----");
        }
        System.out.println();
        for (int i = 0; i < this.board.length; i++) {
            System.out.printf(i + " |");
            for (int j = 0; j < this.board.length; j++) {
                if (this.board[i][j] == WHITE) {
                    System.out.printf(" O |");
                    numWhites++;
                } else if (this.board[i][j] == BLACK) {
                    System.out.printf(" X |");
                    numBlacks++;
                } else if (isValid(i, j, turn)) {
                    System.out.printf(" * |");
                } else {
                    System.out.printf("   |");
                }
            }
            System.out.println();
            System.out.printf("  ");
            for (int j = 0; j < this.board.length; j++) {
                System.out.printf("----");
            }
            System.out.println();
 
        }
        System.out.println("Black: " + numBlacks + " - " + "White: " + numWhites);
        System.out.println();
 
    }
 
    /**
     * Main method to run a round of othello
     * @param args command line parameters
     */
    public static void main(String[] args) {
        // you may change the input to experiment with other boards
        Othello game = new Othello(4);
        game.playGame();
    }
 
 
}

Grading Rubric

  • 05% - Constructor: Othello()
  • 30% - isValid
  • 10% - hasValidMoves
  • 30% makeMove
  • 20% - playGame (Manual grading: it will be graded after the deadline)
    • 5% - taking turns
    • 5% - interaction with the user
    • 5% - continuing / finishing the game
    • 5% - reporting the winner
  • 05% - Coding Style

Extra Credit: Optional

We have implemented a game where both players need to input their position to play. In most games, one of the players is played by the computer. Creating a good Othello automated player is an Artificial Intelligence problem that is outside of the scope of our class.

However, with CS180 experience, you could create a couple of naive yet functional computer “players” to provide the experience of automated gaming. Think of a very simple strategy that you could use and, in a different module, implement an automated player and include it in your Othello program. You have the freedom to modify the methods, add extra methods, print statements, etc, anything that you deem helpful.

    • 8 years ago
    • 60
    Answer(1)

    Purchase the answer to view it

    blurred-text
    NOT RATED
    • attachment
      othello.txt
    Bids(1)