# Opoly game

• October 15th, 2012, 11:19 AM
ksahakian21
Opoly game
Opoly Game Rules:
Reward starts at 20
1. Board cells divisible evenly by 5 double the reward
2. If the piece lands on the second to last cell of the board, reward is reduced to 1/4 of its current value.
3. If a spin would move the piece beyond the end of the board, the piece should not advance at all and its current reward remains unchanged unless rule 6 is met.
4. If the piece lands on the second to last cell of the board, the reward is only reduced to 1/4 and no other points are given, even if rule 1 is met.
5. If the piece ever lands on board position boardSize/2, 25 points are added to the reward.
6. Every tenth move, the reward is reduced by 10.

The Opoly class must make use of the following methods:
spin
move
spinAndMove
isGameOver
drawBoard
displayReport

OpolyDriver class:

Code :

```import java.util.*;   public class OpolyDriver{   public static void main(String[] args){ System.out.println("Enter an int - the size of the board"); Scanner s = new Scanner(System.in); int boardSize = s.nextInt(); System.out.println("Board Size: " + boardSize); Opoly g = new Opoly(boardSize); g.playGame(); } }```

My Code: Opoly class

Code :

```public class Opoly{   int reward = 20; int boardSize; int pos = 0; int round = 0;   int position; boolean moveCheck; int a = 0;   // Creates an opoly method to take in boardSize public Opoly(int boardSize){ this.boardSize = boardSize; }   // Generates a random number between 1 and 5 public void spin(){ position = pos + (int)(1+ Math.random()*5); }   // Based on the value of "Position" in the spin() method, move() determines if and where // that value is on the board, then gives pos a value based on the rules of the game. public void move(){ if(position <= boardSize){ pos = position; moveCheck = true; //moveCheck keeps track of if pos has moved and able to get points } if(position == (boardSize - 1)){ pos = 0; System.out.println("hit second to last position"); moveCheck = true; } else{moveCheck = false;}   //Gives "a" a value depending on whether moveCheck is true or false. if(moveCheck == true){a = a+2;} else{a = a+1;} }   // Calls on the spin() and move() methods to play a round public void spinAndMove(){ spin(); move(); round = round + 1; }   // Based on where pos is on the board, this method calculates the reward score public void rewardCalc(){ if((pos%5 == 0) && (pos != 0) && (a%2 == 0) && (pos != boardSize - 1) && (pos != (boardSize/2))){ reward = (reward * 2); System.out.println("DOUBLE!"); } if(pos == boardSize-1){ reward = (reward/4); } if(pos == (boardSize/2)){ reward = (reward + 25); } if(round%10 == 0){ reward = reward - 10; } }   // Draws each line of the board as the game is played public void drawBoard(){ for(int j = 0; j<= boardSize; j++){ if(j == pos){System.out.print("o");} else{System.out.print("*");} } System.out.println(" " + reward); }   // Determines if the game is over public boolean isGameOver(){ if(pos == boardSize){return true;} else{return false;} }   // Main method which calls on the other methods to play a whole game of Opoly public void playGame(){ do { drawBoard(); spinAndMove(); rewardCalc(); } while((pos != boardSize) && (isGameOver() == false));   if(isGameOver() == true){ for(int j = 0; j<1; j++){ drawBoard(); displayReport(); } } }   // Prints the final reward and rounds of play to the console public void displayReport(){ System.out.println(""); System.out.println("Rounds of play: " + round); System.out.println("Final Reward: " + reward); } }```

I'm having trouble having the board displayed correctly for both the first and last round. As the drawBoard() method is right now, it is adding a cell to each line of the board. the o should count as a cell and take the place of the * at whatever position it is at that round.
I tried changing
Code :

`for(int j = 0; j<= boardSize; j++)`
to
Code :

`for(int j = 0; j<= boardSize-1; j++)`
But then it isn't showing the last line of the game, and the value of the second to last cell is messed up.

In addition to that problem, if anyone has any advice of cleaning the code up a little bit.
• October 15th, 2012, 01:50 PM
jps
Re: Opoly game
Quote:

the o should count as a cell and take the place of the * at whatever position
Which lines of code contain the logic for this? Locate them and test them to verify they perform as expected.

Quote:

for(int j = 0; j<= boardSize; j++)
for(int j = 0; j<= boardSize-1; j++)
Do you understand what you are changing? Do you understand what boardSize is as in it's actual value? Print it out if you are curious, and just for giggles print out j right beside it. {System.out.println("j=" + j + " / " + boardSize);} or something to the like.