In this project you will implement the rules and logic for


Java coding: Othello

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.

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.

Solution Preview :

Prepared by a verified Expert
JAVA Programming: In this project you will implement the rules and logic for
Reference No:- TGS01508019

Now Priced at $80 (50% Discount)

Recommended (97%)

Rated (4.9/5)