I was given this assignment:
For this assignment, your job is to create a kind of lobotomized version of a board game (such as Monopoly) called Opoly.
Opoly works this way: The board is a straight linear track of variable length (you determine the length when you start up the game). There is only one playing piece, which begins the game just off the board, at position 0. Thus, if the board length is 20, then the board positions start at position 1 and end at position 20. To finish the game, the piece must land exactly on the last cell of the board (e.g., cell 20 in the example above).
The object of the game is to acquire reward. The reward amount is initialized to 12. If your board piece lands on a board cell that is divisible evenly by 5, your reward doubles. However, if your piece lands one cell shy of the final board cell, your reward is reduced to 1/5 of its current value (via integer division), and your piece must go back to the start position - position 0. Note, though, that if the position of the last cell is divisible evenly by 5, then reward is doubled just before the game ends.
In Opoly the game piece advances via a spinner - a device that takes on the values 1-2-3-4-5 at random, with each advance value equally likely.
Two additional rules:
1) if a spin would move the piece beyond the end of the board, the piece should not advance at all (thus, if the piece is at location 18 of a 20 cell board, and if the spinner spins a 5, this is counted as a move but the piece remains at location 18.) If a piece doesn't move at all, its current reward amount should remain unchanged, even if the piece sits at a location that is divisible evenly by 5.
2) if the next to last board location is divisible by 5, and if the piece lands on this location, the reward is reduced to 1/5 of its current value only - the reward is NOT also doubled. Example: the board size is 26, and the piece is at location 23, with reward 30. Suppose the spinner spins a 2. This puts the piece on the next to the last location on the board (25). This resets the reward to 6 = 30 * 1/5, and places the piece at location 0. Even though location 25 is divisible by 5, no doubling happens.
and given this driver:
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(); } }
This is the code that I wrote to go with the driver:
import java.util.*; public class Opoly{ private int width; private int spinner; private int o; int[] opoly; private int currentPos; private int moves; private int reward; private int round; public Opoly(int o){ this.o = o; opoly = new int[o]; currentPos = 0; reward = 12; } public void spin(){ spinner = (int)(Math.random()*6);//generates a random integer between 1 and 5 and stores it in spinner } public void move(){ currentPos = currentPos+spinner; } public int spinAndMove(){ round=0; while(currentPos < opoly.length-1){ spin(); move(); round++; if(currentPos % 5==0){ reward=reward*2; } if(currentPos==opoly.length-1){ currentPos=currentPos; break; } if(currentPos > opoly.length-1){ currentPos=currentPos; round++; } if(currentPos==opoly.length-2){ currentPos=0; reward=reward/5; } if(currentPos % 5==0) reward=reward*2; } return round; } public void isGameOver(){ if(moves==opoly.length-1) System.out.println("game over"); } public void drawBoard(){ while(opoly.length<currentPos){ System.out.print('*'); if(opoly.length>currentPos){ System.out.print('*'+reward); } else System.out.print('O'); } } public void displayReport(){ isGameOver(); System.out.println("Total rounds: "+round); System.out.println("Total reward: "+reward); } public void playGame(){ while(currentPos !=opoly.length-1){ spinAndMove(); drawBoard(); if(currentPos==opoly.length-1){ drawBoard(); displayReport(); break; }} }
This only prints out the end result of the game, but doesn't print out the drawBoard method. Any help is appreciated!