Question

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

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.


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,
image of 5-level spinner


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:

**o*******

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:

import java.util.*;

public class OpolyDriver{
  
  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();
  }
}


Here is a sample run:

Enter an int - the size of the board:

> 17

Board Size: 17
o**************** 100
*o*************** 100
*****o*********** 100
**********o****** 100
************o**** 100
o**************** 400  // 100 added to reward, then score is doubled at location 0
****o************ 400
********o******** 400
**********o****** 400
*************o*** 400
o**************** 900  // 10th turn - so reward reduced by 50; then 100 added; then score doubled
***o************* 900
*******o********* 1800 // at location 7, so score doubled
game over
rounds of play: 12
final reward: 1800



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:

Random rand;
rand = new Random();

//OR
rand = new Random(123456);


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.

import java.util.*;

public class OpolyDriver{
  
  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);
     int seed = 123456;
    Opoly g = new Opoly(boardSize, seed);
    g.playGame();
  }
}



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:

  display the initial board
  while the game is not over
     spin and move
     display the board
  display the winner report
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

}

}

Add a comment
Know the answer?
Add Answer to:
For this assignment, your job is to create a simple game called Opoly. The objectives of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this assignment, your job is to create a version of a carnival game in which...

    For this assignment, your job is to create a version of a carnival game in which mechanical dogs race along a track. The racing game is called DogTrack. DogTrack is also the name of the java source file you must submit to complete the assignment. The DogTrack class should be written so that the driver class below, DogDriver, works as intended. A dog track has N discrete positions, 0 through N-1. The value of N is determined interactively in the...

  • Purpose of it is to coordinate the interaction between the user and the lower-level functionality of...

    Purpose of it is to coordinate the interaction between the user and the lower-level functionality of the game. There are two overloaded constructors, one for constructing a brand new game and one for constructing a game from a file. w - Move Up    s - Move Down    a - Move Left    d - Move Right    q - Quit and Save Board If the user inputs any one of these characters, execute the corresponding move. This should result in a refreshed...

  • You will write a two-class Java program that implements the Game of 21. This is a...

    You will write a two-class Java program that implements the Game of 21. This is a fairly simple game where a player plays against a “dealer”. The player will receive two and optionally three numbers. Each number is randomly generated in the range 1 to 11 inclusive (in notation: [1,11]). The player’s score is the sum of these numbers. The dealer will receive two random numbers, also in [1,11]. The player wins if its score is greater than the dealer’s...

  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe {     private final int BOARDSIZE = 3; // size of the board     private enum Status { WIN, DRAW, CONTINUE }; // game states     private char[][] board; // board representation     private boolean firstPlayer; // whether it's player 1's move     private boolean gameOver; // whether...

  • Game Development: Uno For this assignment you will be creating the game of Uno (See the...

    Game Development: Uno For this assignment you will be creating the game of Uno (See the accompanying pdf for the instructions of the game). Your version will adhere to all the rules except that only the next player can issue a challenge against the previous player in regards to penalties, and your games must have at least three (3) players and at most nine (9) players. To begin, you must create the class Card which contains: Private string field named...

  • Code in JAVA You are asked to implement “Connect 4” which is a two player game....

    Code in JAVA You are asked to implement “Connect 4” which is a two player game. The game will be demonstrated in class. The game is played on a 6x7 grid, and is similar to tic-tac-toe, except that instead of getting three in a row, you must get four in a row. To take your turn, you choose a column, and slide a checker of your color into the column that you choose. The checker drops into the slot, falling...

  • 1. create a class called MemoryBoard. a. MemoryBoard will represent a grid of memory squares. it...

    1. create a class called MemoryBoard. a. MemoryBoard will represent a grid of memory squares. it needs to track all of the squares on the board, the cars set it's using, the width and the height of the board, the number of turns that have been taken, and the current player's id. 2. create a arraylist to store the squares on the board. it should store Square objects. the class should also store the width(an int), the number of turns...

  • In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and...

    In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and crosses on the 3 x 3 game board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row in the game board wins the game. Super Tic Tac Toe game also has a 3 x 3 game board with super grid. Each super grid itself is a traditional Tic Tac Toe board. Winning a game of Tic...

  • 1 Overview For this assignment you are required to write a Java program that plays (n,...

    1 Overview For this assignment you are required to write a Java program that plays (n, k)-tic-tac-toe; (n, k)-tic- tac-toe is played on a board of size n x n and to win the game a player needs to put k symbols on adjacent positions of the same row, column, or diagonal. The program will play against a human opponent. You will be given code for displaying the gameboard on the screen. 2 The Algorithm for Playing (n, k)-Tic-Tac-Toe The...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT