Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Program The Game This is a simple game played on a linear board with squares numbered from 0 to 100. The player starts at

JAVA Program

The Game

This is a simple game played on a linear board with squares numbered from 0 to 100. The player starts at position 0, and the object of the game is to land on position 100 exactly.

Objectives

By the end of this program, the student will have demonstrated the ability to

  • Write static methods
  • Call a static method in another class
  • Pass parameters to a method
  • Return values from a method
  • Write loops
  • Write if statements
  • Generate random numbers
  • Apply special rules to game play
  • Identify when the object of a game has been satisfied, or when the user wishes to terminate the game
  • Write a user interface enabling a user to execute various commands

Gameplay

The player starts on square 0.

The player makes a series of moves. The player may choose to move 1 square, or the player may choose to roll two dice, and move the number of squares represented by the sum of the dice. The players move puts him at an initial position.

The players initial position may be changed to a final position by the following rules. The rules are checked in the following order, and only the first applicable rule should be applied:

  1. If the initial position is past square 100, the final position is calculated by subtracting 100 from the initial position. For example, if the player is on square 94 and rolls 10, thus giving an initial position of 104, the final position will be position 4 (104 100).
  2. If the initial position ends in 5 (5, 15, 25, etc.), the player is moved an extra 5 places for the final position. For example, if the player is on position 33 and rolls 2, placing him on position 35, he is moved 5 extra spaces, putting him at position 40. If a player is on position 44 and chooses to move 1 square to position 45, he is moved 5 squares for a final position of 50.
  3. If the initial position is evenly divisible by 13 (13, 26, 39, etc.), the final position is calculated by subtracting twice the players movement from the initial position. For example, if a player is on position 29 and rolls 10, the initial position is 39, which is evenly divisible by 1 The final position is calculated by subtracting twice the movement (2 * 10) from the initial position of 39, yielding a final position of 19. If a player at position 25 chooses to move 1, the initial position is 26. The final position is calculated by subtracting twice the movement (2 * 1) from the initial position of 26, giving a final position of 24.
  4. If none of the above conditions hold, the initial position becomes the final position.

When the player lands on position 100, the game terminates.

Program Structure

Utils

You are to write a class Utils. This class has the following static methods:

  • rollthis method takes no parameters and returns an int value representing one roll of one die. It should return a value from 1 through 6 (inclusive).
  • movethis method takes two int parameters. The first is the players current position on the game board. The second represents a movement amount, which can be from 1 to 12 (inclusive). This method returns an int value representing the players final position on the board. This method should implement the gameplay rules listed above to determine the new position.

Game

You are to write a class Game that has only a main method. The main method keeps track of the players current position on the board, queries him for his move, uses the roll and move methods to execute the players choice, and terminates when either the player specifies to quit the game, or when the player lands on position 100.

Sample Execution

The following are sample games. Probably youll just want to scan the notes:

Game 1

You are now at position 0

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

We start at position 0 and roll two dice. They totaled 8. We dont know if we rolled two 4s or a 5 and a 3 or a 6 and a 2. The total was 8, so we move to position 8

You rolled 8 and moved 8 spaces

You are now at position 8

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 10 and moved 10 spaces

You are now at position 18

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 7 and moved 12 spaces

Were at position 18 and rolled a 7. That puts us on position 25, which will move us up to position 30 automatically.

You are now at position 30

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 8 and moved 8 spaces

You are now at position 38

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

1

You are now at position 37

We make a dumb move. We were on position 38, and chose to move 1. That should put us on position 39. But 39 is evenly divisible by 13. So we subtracted twice the move from the initial position, and we wind up at position 37.

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 11 and moved 11 spaces

You are now at position 48

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 2 and moved 2 spaces

You are now at position 50

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 7 and moved 7 spaces

You are now at position 57

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 8 and moved 13 spaces

You are now at position 70

Heres another land on a 5 situation. We are on position 57. We roll 8, which moves us to position 65. Landing on 65 automatically moves us to position 70.

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 6 and moved 6 spaces

You are now at position 76

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 4 and moved 4 spaces

You are now at position 80

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 5 and moved 10 spaces

You are now at position 90

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 6 and moved 6 spaces

You are now at position 96

Were on position 96, and feel lucky. Instead of walking by 1s to 100, we roll. It turns out well roll a 10. This gives us 106. Since were over 100, we calculate our new position as 106 100, or 6.

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 10 and moved -90 spaces

You are now at position 6

Just exit this game.

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

0

Bye

Game 2

You are now at position 0

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 4 and moved 4 spaces

You are now at position 4

I move 1, which puts me on 5, which automatically moves me up to 10.

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

1

You are now at position 10

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 2 and moved 2 spaces

You are now at position 12

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 8 and moved 8 spaces

You are now at position 20

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 8 and moved 8 spaces

You are now at position 28

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 8 and moved 8 spaces

You are now at position 36

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 12 and moved 12 spaces

You are now at position 48

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 9 and moved 9 spaces

You are now at position 57

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 7 and moved 7 spaces

You are now at position 64

Below is another at 64, move 1 to 65, automatically move up to 70.

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

1

You are now at position 70

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 3 and moved 3 spaces

You are now at position 73

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 8 and moved 8 spaces

You are now at position 81

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 7 and moved 7 spaces

You are now at position 88

Im at position 88 and about to roll a 7. That takes me to 95, and being a 5 automatically moves me up an extra 5 to 100. I win.

What do you want to do?

0 to exit

1 to move 1 space,

2 to roll

2

You rolled 7 and moved 12 spaces

You won

Bye

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

1st Edition

1597496251, 978-1597496254

More Books

Students also viewed these Databases questions

Question

What steps does a proper analysis include?

Answered: 1 week ago

Question

Explain the forces that influence how people handle conflict

Answered: 1 week ago