HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Marbles game with classes

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withgamemarblesclasses

Problem

Project contains 3 classes:

/*************************************************************************************
The purpose of this project is to create 3 classes. Pile contains the number of marbles
in a randomly generated pile, and also a remove method to determine how many marbles are
to be removed. The player class determines the behavior of each type of player. There are
three types of players. Simple computer, Smart computer, and Human. These are all of enum
type. The program will use a randomly generated number to determine who goes first and
which level of skill the computer player is set at. Each player will draw a number of marbles
from the pile until the pile reaches 1. Whoever pulls the last marble will lose.
A player must choose between 1 and half the remaining pile.
***************************************************************************************/


Game

```
import java.util.*;

public class Game {

private static void printHeading(int projectNum, String projectName){ //creates an identifier method.

System.out.println("Chris Olson");
System.out.println("CMSC 255-002, Spring 2013");
System.out.println("Project " + projectNum);
System.out.println(projectName);
System.out.println();
}

public static void main(String[] args) {

printHeading(8,"Nim");
Scanner in = new Scanner(System.in);

Pile marbles = new Pile(); //creates initial Pile object

//Sets variables and objects to enum type
int marblesToRemove = 0;
Player humanPlayer = new Player(Player.Type.HUMAN);
Player compPlayer = new Player(Player.Type.SMART_COMPUTER);
Player secondPlayer = new Player(Player.Type.HUMAN);
Player firstPlayer = new Player(Player.Type.HUMAN);
Random randomNum = new Random(); //creates new random object to generate random numbers
int selection = 0;

//Interface
System.out.println("****WELCOME TO

Solution

There is a lot of code in main(). It would be better to break this up into smaller pieces.

There is a lot of extra white space. There generally is no need for multiple consecutive empty lines.

Within playTurn(), you separate a sequence of if-else if blocks with an empty line. A better solution would be to use curly brackets. This will clearly mark the sections to both humans and computers. Without the curly brackets, adding a second indented statement after the first will always execute (or be a syntax error if it is followed by an else.

Enums are a bad way to implement different strategies. It requires a lot of boilerplate code to be written in many different places. In addition, it makes it very easy to miss a case when adding a new strategy. A better solution would be to define an interface that has a method for each decision or action point. Then, the Player would accept some implementation of this interfaces. This makes adding a new strategy simple and requires no changes to Player.

If you do need to make a decision based on the value of a enum, a switch statement is better than a number of if else statements. It is easier for the compiler to optimize the makes it clearer to someone readying the code that each enum value has a distinct action.

Context

StackExchange Code Review Q#37521, answer score: 6

Revisions (0)

No revisions yet.