Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 underscores on the second line showing how many spots. For example, if the car has moved 3 spots total, it would look like:

__/1\__

_____-O---O-

The track is 20 spots long.

The track is drawn as 27 ~s. 20 spots that the cars have to move plus 7 move because that is how wide the car is when it is drawn

The track looks like:

~~~~~~~~~~~~~~~~~~~~~~~~~~~

When either car reaches the 20th spot, the game is over

How to play the game:

Ask the user if he/she wants a regular roll or to go for double or nothing.

For a regular roll, move the car that many spaces forward

For a double or nothing roll: if you roll an odd number, move twice that many spaces; if you roll an even number, then move no spaces

Then roll the die for the computer. Move the computers car that many spaces

After both cars move, draw both cars on their track

Print the track

When one of the cars has moved at least 20 spaces, the game is over. Print a message to say who has won

Your program should have 2 class/files:

RaceGame

Contains main()

RaceCarNeeds 2 instance variables

Cars number

Cars position

Need get and set methods for cars position

Need a way to set the cars number. You can do this with a set method or a constructor with a parameter

May want a method to print the car

May want another method to move the car forward a number of spaces (updates cars position variable)

Requirements:

Put System.out.println() after each call to nextInt(). This will help you match the Test Program and make it easier to use it.

Before the game starts, prompt the user for a set roll or a random roll. If the user enters -1, then let each roll of the die be a random number (see the next note). If the user enters anything else, then let every roll of the die be that number. Obviously, each roll of a die will not always be the same number, but it is a great feature that allows us to test the game in a variety of ways where we always know what should happen next.

Use a random number generator to roll the die when the user chooses -1 at the first prompt for random. (Well cover this in more detail in Chapter 6)

Import Random

import java.util.Random;

Declare a Random number generator variable

Random rand = new Random();

Call the Randoms nextInt() method every time you want another random number. Let move be an int variable. This will return a random number between 1 and 6

move = rand.nextInt(6) + 1;

Use a random number generator to roll the die when the user chooses -1 at the first prompt for random. (Well cover this in more detail in Chapter 6)

Sample Run #1: (the highlighted text is what the user types)

Dice roll or -1 for random? 5

__/1\__

-O---O-

__/2\__

-O---O-

~~~~~~~~~~~~~~~~~~~~~~~~~~~

1=regular move, 2=double or nothing? 1

You move 5 spaces

Computer moves 5 spaces

__/1\__

_____-O---O-

__/2\__

_____-O---O-

~~~~~~~~~~~~~~~~~~~~~~~~~~~

1=regular move, 2=double or nothing? 1

You move 5 spaces

Computer moves 5 spaces

__/1\__

__________-O---O-

__/2\__

__________-O---O-

~~~~~~~~~~~~~~~~~~~~~~~~~~~

1=regular move, 2=double or nothing? 1

You move 5 spaces

Computer moves 5 spaces

__/1\__

_______________-O---O-

__/2\__

_______________-O---O-

~~~~~~~~~~~~~~~~~~~~~~~~~~~

1=regular move, 2=double or nothing? 1

You move 5 spaces

Computer moves 5 spaces

__/1\__

____________________-O---O-

__/2\__

____________________-O---O-

~~~~~~~~~~~~~~~~~~~~~~~~~~~

It's a tie

Sample Run #2: (the highlighted text is what the user types)

Dice roll or -1 for random? 6

__/1\__

-O---O-

__/2\__

-O---O-

~~~~~~~~~~~~~~~~~~~~~~~~~~~

1=regular move, 2=double or nothing? 2

Sorry, you don't move

Computer moves 6 spaces

__/1\__

-O---O-

__/2\__

______-O---O-

~~~~~~~~~~~~~~~~~~~~~~~~~~~

1=regular move, 2=double or nothing? 1

You move 6 spaces

Computer moves 6 spaces

__/1\__

______-O---O-

__/2\__

____________-O---O-

~~~~~~~~~~~~~~~~~~~~~~~~~~~

1=regular move, 2=double or nothing? 1

You move 6 spaces

Computer moves 6 spaces

__/1\__

____________-O---O-

__/2\__

__________________-O---O-

~~~~~~~~~~~~~~~~~~~~~~~~~~~

1=regular move, 2=double or nothing? 1

You move 6 spaces

Computer moves 6 spaces

__/1\__

__________________-O---O-

__/2\__

________________________-O---O-

~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sorry, you lose

Sample Run #3: (the highlighted text is what the user types)

Dice roll or -1 for random? 5

__/1\__

-O---O-

__/2\__

-O---O-

~~~~~~~~~~~~~~~~~~~~~~~~~~~

1=regular move, 2=double or nothing? 2

Yay - you doubled your move, 10 spaces

Computer moves 5 spaces

__/1\__

__________-O---O-

__/2\__

_____-O---O-

~~~~~~~~~~~~~~~~~~~~~~~~~~~

1=regular move, 2=double or nothing? 2

Yay - you doubled your move, 10 spaces

Computer moves 5 spaces

__/1\__

____________________-O---O-

__/2\__

__________-O---O-

~~~~~~~~~~~~~~~~~~~~~~~~~~~

Congratulations, you win!

Expected Progress: (Be prepared to show progress each week during lab time)

Week 1

Create RaceGame.java, add prompts, roll die once for first turn

Week 2

Create RaceCar.java, add methods. For now, let drawing the car just print the cars position

Have main() create 2 RaceCar objects, set their position based on the first roll, and print the cars new position

Week 3

Add loop to main() that will continue to roll the die and stop when a car wins

Add code to choose between a set roll and a random roll

Print the winner

Week 4

Add code to draw the cars and the track

Add code for double or nothing choice

Final testing

Extra Notes:

Did you correctly name the package/folder?

Did you correctly name the class/file?

Did you include comments?

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

More Books

Students also viewed these Databases questions