Opoly works this way: The board is a circular track of variable length (the user
ID: 3675786 • Letter: O
Question
Opoly works this way: The board is a circular track of variable length (the user determines the length when the game app runs). There is only one player, who begins the game at position 0.
Thus, if the board length is 20, then the board locations start at position 0 and end at position 19. The player starts with a reward of 100, and the goal of the game is to reach or exceed reward value 1000. When this reward value is reached or exceeded, the game is over. When the game ends, your program should report the number of turns the player has taken, and the final reward amount attained.
In Opoly the game piece advances via a spinner - a device that takes on one of the values 1, 2, 3, 4, 5 at random, with each of the five spin values equally likely. The spin method you will write simulates a spinner. More about spinners here: http://www.mathsisfun.com/data/spinner.php,
The circular nature of the board means that if the player advances to a position beyond the board size, the position will "wrap" around to the beginning. For example, if the board size was 20, the first position would be 0, and the last position would be 19. If a player was on position 18 and the spin result was 4, the new position would be 2. Although there are several ways to calculate this, a convenient way uses modular arithmetic: (position + spinResult) mod boardSize.
Although the board is circular, you should draw the state of the board as a single "line", using an 'o' to represent the current player position, and * represent all other positions.
Thus if the board size is 10, then this board drawing:
means that the player is at location 2 on the board.
Here are the other Opoly game rules:
If your board piece lands on a board cell that is evenly divisible by 7, your reward doubles.
If you land on the final board cell, you must go back 3 spaces. Thus if the board size is 20, the last position is position 19, and if you land there, you should go back to position 16. (If the position of the last cell is evenly divisible by 7, no extra points are added, but if the new piece location, 3 places back, IS evenly divisible by 7, then extra points ARE added).
If you make it all the way around the board, you get 100 points. Note that if you land exactly on location 0, you first receive 100 extra points (for making it all the around), and then your score is doubled, since 0 is evenly divisible by 7,
Every tenth move (that is, every tenth spin of the spinner, move numbers 10,20,30,... etc.), reduces the reward by 50 points. This penalty is applied up front, as soon as the 10th or 20th or 30th move is made, even if other actions at that instant also apply. Notice that with this rule it's possible for the reward amount to become negative.
Here is the driver class for the game:
Here is a sample run:
REQUIRED CODE STRUCTURE:
Your Opoly class must include the following methods (in addition to the Opoly constructor) and must implement the method calls as specified:
playGame - The top-level method that controls the game. No return value, no parameters. Must call drawBoard, displayReport, spinAndMove, isGameOver.
spinAndMove - spins the spinner and then advances the piece according to the rules of the game. No return value, no parameters. Must call spin and move.
spin - generates an integer value from 1 to 5 at random- all equally likely. Returns an integer, no parameters.
move - advances the piece according to the rules of the game. No return value, takes an integer parameter that is a value from 1 to 5.
isGameOver - checks if game termination condition has been met. Returns true if game is over, false otherwise. No parameters.
drawBoard - draws the board using *'s and an o to mark the current board position. Following each board display you should also report the current reward. No return value, no parameters.
displayReport - reports the end of the game, and gives the number of rounds of play, and the final reward. No return value, no parameters.
ALSO REQUIRED: Comment each method with a brief description of what it does.
Development tips:
Define the attributes for the Opoly class. At any instant, what is the "state" of the board? What data do you need to keep track of to give the final report? The answers to these questions will help you define the attributes of the Opoly class.
Write the Opoly constructor. Infer this definition from the call in the OpolyDriver code.
Write "stub code" for all of the methods listed above. Stub code is the method header with no body. You can simply return some value if required, such as return true; for a method that returns a boolean. Of course, methods that return voiddon't need a return statement. Then, write the playGame method calling the methods outlined above. You can then start to implement the methods so that you can run the code, increasing the functionality a little at a time. (This is called incremental development). Another simplification to start with: just implement the rule that increases the reward by 100 every time you circle the board. You can add the other rules later.
Think of what the playGame method does in pseudocode:
Enter your Opoly class code in the box below:
NOTE: you may NOT use any import statements in your class. Use the Math class random method to generate your random numbers. Do NOT import the java.util library. Note: you do not need an array to solve this problem.
Explanation / Answer
OpolyDriver.java
import java.util.*;
public class OpolyDriver {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter an int - the size of the board");
Scanner s = new Scanner(System.in);
int boardSize = s.nextInt();
s.close();
System.out.println("Board Size: " + boardSize);
Opoly g = new Opoly(boardSize);
g.playGame();
}
}
Opoly.java
public class Opoly {
//Initialize these at the start so any method can use them
private int board;
private int reward=12;
private int position=0;
private int roll;
private int rounds=0;
public Opoly(int length){
//pull the board length from the driver
board=length;
}
public void playGame(){
System.out.println("Game has begun.");
//Runs the game, doesn't run if position=board since position the report gets run every roll
while(position<board){
spinAndMove();
drawBoard();
displayReport();
}
}
public int spin(){
//Had to do this since random() returns double
int dice;
//changes double to integer
dice=(int) (1+(Math.random() * 4));
roll=dice;
//Debug text for myself
//System.out.println("Roll: " + roll);
//Returns roll for use in other methods
return roll;
}
public int move(){
//new position calculated, check in if statements
position=roll+position;
//If goes beyond end, goes back to original position
if(position>board){
position=position-roll;
}
//If divisible by 5 and not next to last square, double
if((position%5)==0 && position!=(board-1)){
reward=reward*2;
}
//If next to last square, divide by 5, move back to 0
if(position==(board-1)){
reward=reward/5;
position=0;
}
//Returns position for use in other methods
return position;
}
public void spinAndMove(){
//Just runs spin() and move() as name suggest
spin();
move();
}
public boolean isGameOver(){
//Checks if the game is over for the report, gives true/false
if(position>=board)
return true;
else
return false;
}
public void drawBoard(){
//If you use 0 extra * written
int j=1;
//Draws * up to position
while(j<position){
System.out.print("*");
j++;
}
//Draws O at position
while(j==position){
System.out.print("O");
j++;
}
//Draws remaining * up (and including) last square
while(j>position && j<=board){
System.out.print("*");
j++;
}
//Prints the reward, then moves to next line so as not to clutter space
System.out.println(" " + reward);
}
public void displayReport(){
//If the game is over, prints report
if(isGameOver()==true){
System.out.println("The game is over.");
System.out.println("Number of rounds: " + (rounds+1));
System.out.println("Final reward: " + reward);
}
//If it isn't, adds a tally to the number of rounds
else
rounds=rounds+1;
}
}
sample output
Enter an int - the size of the board
17
Board Size: 17
Game has begun.
*O*************** 100
***O************* 100
****O************ 200
******O********** 200
**********O****** 200
*************O*** 200
*************O*** 200
****************O 200
The game is over.
Number of rounds: 8
Final reward: 200
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.