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);
}
}