Question

JAVA Program The Game This is a simple game played on a linear board with squares...

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 player’s move puts him at an “initial position.”

The player’s 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 player’s 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:

  • roll—this 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).
  • move—this method takes two int parameters. The first is the player’s 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 player’s 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 player’s current position on the board, queries him for his move, uses the roll and move methods to execute the player’s 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 you’ll 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 don’t 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

We’re 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

Here’s 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

We’re on position 96, and feel lucky. Instead of walking by 1s to 100, we roll. It turns out we’ll roll a 10. This gives us 106. Since we’re 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

I’m 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is your answer...Code in Java


import java.util.Scanner;
import java.util.Random;

class Util
{
//this 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).
public static int roll()
{
Random r=new Random();
int diceValue= r.nextInt(6)+1;
return diceValue;
}
//this method takes two int parameters. The first is the player’s 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 player’s final position on the board
public static int move(int playerPos,int moveAmount)
{
int initialPostion=playerPos+moveAmount;
int finalPosition;
//If the initial position is past square 100, the final
//position is calculated by subtracting 100 from the initial position
if(initialPostion>100)
{
finalPosition=initialPostion-100;
}
//If the initial position ends in 5 (5, 15, 25, etc.),
//the player is moved an extra 5 places for the final position.
else if(initialPostion%10==5)
{
finalPosition=initialPostion+5;
}
//If the initial position is evenly divisible by 13 (13, 26, 39, etc.),
//the final position is calculated by subtracting twice the player’s movement from the initial position
else if(initialPostion%13==0)
{
finalPosition=initialPostion-2*moveAmount;
}
//If none of the above conditions hold, the initial position becomes the final position.
else
{
finalPosition=initialPostion;
}
int totalMove=finalPosition-playerPos;
//print the message
if(moveAmount!=1)
System.out.println("You rolled "+ moveAmount+" and moved "+totalMove+" spaces");
return finalPosition;
}
}
public class Game
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int choice;
int initialPos=0;//store current position
do
{
System.out.println("You are at position "+initialPos);
System.out.println("What do you want to do?");
System.out.println(" 0 to exit \n 1 to move 1 space, \n 2 to roll");
choice=sc.nextInt();
//user select 1
if(choice==1)
{
initialPos=Util.move(initialPos,1);
}
//user select 2
else if(choice==2)
{
int dice1=Util.roll();
int dice2=Util.roll();
int total=dice1+dice2;
initialPos=Util.move(initialPos,total);
}
else
{
break;
}
if(initialPos==100)
{
System.out.println("You Won ");
break;
}
}while(choice>0);
System.out.println("Bye ");
}
  
}

//output

Add a comment
Know the answer?
Add Answer to:
JAVA Program The Game This is a simple game played on a linear board with squares...
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
  • To decide your fate, you will play a board game on a simple board: a linear...

    To decide your fate, you will play a board game on a simple board: a linear track with 16 sequential spaces numbered from 1 to 15. The “zero” space is marked “Start,” and your token is placed on it. The jailer has one silver coin and places it on a random numbered space. The jailer then gives you a gold coin, and you are allowed to place it on any numbered space that you want except that the coins must...

  • INTRODUCTION: Play sorry with two die, and one peg. Move your piece down the board first....

    INTRODUCTION: Play sorry with two die, and one peg. Move your piece down the board first. But rolling certain numbers will change the pace of the game. INCLUDE IN YOUR ASSIGNMENT: Annotation is a major part of any program. At the top of each of your C++ programs, you should have at least four lines of documentation: // Program name: tictactoe.cpp // Author: Twilight Sparkle // Date last updated: 5/26/2016 // Purpose: Play the game of Tic-Tac-Toe ASSIGNMENT: Sorry Game...

  • "Chutes and Ladders" is a popular board game for children. The game consists of a board...

    "Chutes and Ladders" is a popular board game for children. The game consists of a board that has squares which are numbered from 1 to 100, and players have counters which start on the theoretical square 0. On each player’s turn, the player generates a random integer number from 1 to 6 (e.g. by rolling a die or spinning a wheel) and move their marker through the board that many spaces. If you land at the bottom of a ladder...

  • The Rules of Pig Pig is a folk jeopardy dice game with simple rules: Two players...

    The Rules of Pig Pig is a folk jeopardy dice game with simple rules: Two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 (”pig”) is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player’s turn, the player is faced with two decisions: roll If the player rolls a 1: the player scores nothing and it becomes the...

  • Java How to Program, Early Objects ISBN-13: 9780133813432 Project Name: C2800_Proj1_RaceGame Source File Name: (Submit zip...

    Java How to Program, Early Objects ISBN-13: 9780133813432 Project Name: C2800_Proj1_RaceGame Source File Name: (Submit zip of these) RaceGame.java (contains main) RaceCar.java                                                                                                                                                              RaceGame is a text based car racing game. The user is car #1 and the computer is car #2. A car is drawn using 2 lines. The number on the first line is 1 for the user and 2 for the computer. __/1\__ -O---O- When the car has moved down the track, print underscore’s on the second line...

  • Two player Dice game In C++ The game of ancient game of horse, not the basketball...

    Two player Dice game In C++ The game of ancient game of horse, not the basketball version, is a two player game in which the first player to reach a score of 100 wins. Players take alternating turns. On each player’s turn he/she rolls a six-sided dice. After each roll: a. If the player rolls a 3-6 then he/she can either Roll again or Hold. If the player holds then the player gets all of the points summed up during...

  • I'm writing a program in game for a dice game. During the game you decide if...

    I'm writing a program in game for a dice game. During the game you decide if you want to roll or pass to the next player, mine so far will just keep rolling until you roll the same number again (besides six) and then move to the next player. How do I get it to manually change players by the player response "y" or "n"? This is what I have for this part of the game simulator and it doesn't...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • The game of Pig is a simple two-player dice game in which the first player to...

    The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn, a player rolls a six-sided die: If the player rolls a 1, then the player gets no new points and it becomes the other player’s turn. If the player rolls 2 through 6, then he or she can either ROLL AGAIN or HOLD:      At this point, the sum of all rolls...

  • For your final project you will incorporate all your knowledge of Java that you learned in this class by programming the game of PIG. The game of Pig is a very simple jeopardy dice game in which two p...

    For your final project you will incorporate all your knowledge of Java that you learned in this class by programming the game of PIG. The game of Pig is a very simple jeopardy dice game in which two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player's turn,...

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