For this assignment your job is to create a simple game call

For this assignment, your job is to create a simple game called Opoly.
The objectives of this assignment are to:

Break down a problem into smaller, easier problems.

Write Java methods that call upon other methods to accomplish tasks.

Use a seed value to generate random a sequence of random numbers.

Learn the coding practice of writing methods that perform a specific task.

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.


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:
NOTE: Use the position index for rule calculations. The index starts at 0 and ends at boardLength-1.

1. If your board piece lands on a board cell that is evenly divisible by 7, your reward doubles.

2. 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).

3. 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,

4. 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:



The above example demonstrates a completely random run of Opoly. You must also allow for a seed value to be used to control the sequence of random numbers. This is helpful in debugging a game and in studying the game\'s properties.
The java.util.Random class has two constructors:


The first initializes the Random variable to an object that produces (pseudo) random numbers; The second uses a seed value, and will produce the same sequence of random numbers each time the code is run. This allows you to observe the behavior of your game and to debug it if necessary.

Therefore, declare a variable of the Random class in your Opoly class. Add another constructor (besides the single parameter constructor) that has another parameter for a seed value. That constructor will use the seed to initialize the instance of Random to a new Random class object using the seed to the Random constructor.



REQUIRED CODE STRUCTURE:
Your Opoly class must include the following methods and must implement the method calls as specified:

1. You must write two constructors: one that takes a single int parameter- the board size, and another constructor that takes two int parameters: the board size, and the seed for the random number generator. The one parameter constructor initializes an instance of the Random class using the Random no parameter constructor, and the second constructor initializes the Random instance using the seed value passed in.

2. playGame - The top-level method that controls the game. No return value, no parameters. Must call drawBoard, spinAndMove, isGameOver.

3. 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.

4. spin - generates an integer value from 1 to 5 at random- all equally likely. Returns an integer, no parameters. Use the java.util.Random class to generate your random numbers. You may NOT use Math.random() for this project (80% off).

5. 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.

6. isGameOver - checks if game termination condition has been met. Returns true if game is over, false otherwise. No parameters.

7. 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.

8. displayReport - reports the end of the game, and gives the number of rounds of play, and the final reward. No return value, no parameters.

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 constructors. Use the seed value during development.

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 void don\'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:

Solution

Answer:

import java.util.*;

//DRIVER

public class OpolyDriver

{

//MAIN METHOD

public static void main(String[] args){

    System.out.println(\"Enter an int > 3 - 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();

}

}

//Opoly.java

class Opoly

{

//BOARD SIZE

private static int polyBoardSize;

//SPIN VALUE

private static int spinVal;

//PLAYER REWARDS

private static int game_Reward;

//NO OF TURNS GAMES PLAYED

private static int polyGamesPlayed;

//1D ARRAY FOR GAME BOARD

private static char[] poly_Board;

//TEMP VARAIBLE

private static boolean firstTimeVal;

//constructor

public Opoly(int bSize)

{

    polyBoardSize = bSize;

    game_Reward = 100;

    polyGamesPlayed = 0;

    poly_Board = new char[polyBoardSize];

    firstTimeVal = true;

}

//PLAY THE GAME

public void playGame(){

//DISPLAY THE BOARD

    drawBoard();

     //PLAY THE GAME UNTIL USER SCORES MORE THAN 1000

    while (!isGameOver()){

      spinAndMove();

      drawBoard();

    }

     //DISPLAY THE REPORT AFTER GAME FINISHES

    displayReport();

}

public static void spin()

{

//RETURN AN INT VALUE 1-5

    spinVal = (1 + (int)(Math.random() * 5));

}

//MOVE PLAYER\'S POSITION IN THE BOARD

public static void move()

{

//CHECK IF 10 TURNS PLAYED

    if (polyGamesPlayed % 10 == 0)

      game_Reward = game_Reward - 50;

    for (int aa = 0; aa < polyBoardSize; aa++)

     {

      if (poly_Board[aa] == \'o\')

     {

        poly_Board[aa] = \'*\';

        if (aa == (polyBoardSize - 1))

          {

          poly_Board[aa] = \'*\';

          poly_Board[aa - 3] = \'o\';

          if (((aa - 3) % 7 == 0) && (aa - 3 != 0))

            game_Reward = game_Reward * 2;

          if (((aa- 3) + spinVal) >= polyBoardSize)

          {

            poly_Board[aa - 3] = \'*\';

           game_Reward = game_Reward + 100;

            poly_Board[((aa - 3) + spinVal) - polyBoardSize] = \'o\';

          }

          else if (((aa - 3) + spinVal) <= polyBoardSize)

          {

            poly_Board[aa - 3] = \'*\';

            poly_Board[(aa - 3) + spinVal] = \'o\';

          }

        }

        else if ((aa + spinVal) >= polyBoardSize)

          {

          game_Reward = game_Reward + 100;

          poly_Board[(aa + spinVal) - polyBoardSize] = \'o\';

        }

        else if ((aa + spinVal) <= polyBoardSize)

          poly_Board[aa + spinVal] = \'o\';

        aa = polyBoardSize;

      }

    }

}

//GET SPIN VALUE AND MOVE

public static void spinAndMove()

{

    polyGamesPlayed++;

     spin();

    move();

    for (int a1 = 0; a1< polyBoardSize-1; a1++)

     {

      if (poly_Board[a1] == \'o\')

     {

        if (a1 == 0)

          game_Reward = game_Reward * 2;

        else if ((a1 % 7 == 0) && (a1!= (polyBoardSize - 1)))

          game_Reward = game_Reward * 2;

      }

    }

}

public static boolean isGameOver()

{

//return TRUE IF PLAYER GETS REWARDS MORE THAN 1000

    boolean is_game_over = false;

    if (game_Reward >= 1000)

      is_game_over = true;

    return is_game_over;

}

public static void drawBoard()

{

//CHECK IF BOARD IS DISPLAYED FOR THE FIRST TIME

    if (firstTimeVal)

     {

      poly_Board[0] = \'o\';

      for(int b = 1; b < polyBoardSize; b++)

        poly_Board[b] = \'*\';

    }

    for(int b = 0; b < polyBoardSize; b++)

      System.out.print(poly_Board[b]);

    System.out.println(\" \" + game_Reward);

    firstTimeVal = false;

}

//DISPLAY THE REPORT

public static void displayReport()

{

    System.out.println(\"GAME OVER\");

    System.out.println(\"ROUNDS OF PLAY: \" + polyGamesPlayed);

    System.out.println(\"FINAL REWARD: \" + game_Reward);

}

}

For this assignment, your job is to create a simple game called Opoly. The objectives of this assignment are to: Break down a problem into smaller, easier probl
For this assignment, your job is to create a simple game called Opoly. The objectives of this assignment are to: Break down a problem into smaller, easier probl
For this assignment, your job is to create a simple game called Opoly. The objectives of this assignment are to: Break down a problem into smaller, easier probl
For this assignment, your job is to create a simple game called Opoly. The objectives of this assignment are to: Break down a problem into smaller, easier probl
For this assignment, your job is to create a simple game called Opoly. The objectives of this assignment are to: Break down a problem into smaller, easier probl
For this assignment, your job is to create a simple game called Opoly. The objectives of this assignment are to: Break down a problem into smaller, easier probl
For this assignment, your job is to create a simple game called Opoly. The objectives of this assignment are to: Break down a problem into smaller, easier probl

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site