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 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 computer’s 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
Car’s number
Car’s position
Need get and set methods for car’s position
Need a way to set the car’s 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 car’s 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. (We’ll 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 Random’s 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. (We’ll 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 car’s position Have main() create 2 RaceCar objects, set their position based on the first roll, and print the car’s 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?
RaceCar.java
public class RaceCar
{
int number;
int position;
int no_of_moves;
// parametarized constructor
RaceCar(int number, int position)
{
this.number = number;
this.position = position;
this.no_of_moves = 0;
}
public void setPosition(int position)
{
// set the position
this.position = position;
}
public int getPosition(int position)
{
return this.position;
}
public void moveForward(int spaces)
{
// update the position
setPosition(this.position + spaces);
this.no_of_moves += 1;
}
// check if won
public boolean ifWon()
{
if(position >= 20)
return true;
return false;
}
public void printCar()
{
int i;
// print the upper car
for(i = 1; i <= 2 * no_of_moves; i++)
System.out.print(" ");
System.out.println("__/" + this.number + "\\__");
// print lower car
for(i = 1; i <= position; i++)
System.out.print("_");
System.out.println("-O---O-");
}
}
RaceGame.java
import java.util.*;
public class RaceGame
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
// create object of type random
Random rand = new Random();
// create object of type RaceCar
RaceCar user = new RaceCar(1,0);
// create object of type RaceCar
RaceCar computer = new RaceCar(2,0);
System.out.print("Dice roll or -1 for random? ");
int move = sc.nextInt();
System.out.println();
if(move == -1)
move = rand.nextInt(6) + 1;
user.printCar();
computer.printCar();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~");
while(true)
{
System.out.print("1=regular move, 2=double or nothing? ");
int n = sc.nextInt();
System.out.println("\n");
if(n == 1)
{
System.out.println("You move "+ move + " spaces");
// move user forward
user.moveForward(move);
}
else
{
if(move % 2 == 0)
{
System.out.println("Sorry, you don't move");
// move user forward
user.moveForward(0);
}
else
{
System.out.println("Yay - you doubled your move, " + (2 * move) +"spaces");
// move user forward
user.moveForward(2 * move);
}
}
System.out.println("Computer move "+ move + " spaces");
// move computer forward
computer.moveForward(move);
// print cars
user.printCar();
computer.printCar();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~");
if(user.ifWon() && computer.ifWon())
{
System.out.println("It's a tie");
break;
}
else if(user.ifWon())
{
System.out.println("Congratulations, you win!");
break;
}
else if(computer.ifWon())
{
System.out.println("Sorry, you lose");
break;
}
}
}
}
Sample Output:


Java How to Program, Early Objects ISBN-13: 9780133813432 Project Name: C2800_Proj1_RaceGame Source File Name: (Submit zip...
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...
Can anyone help me with this java program? We are still very early with lessons so no advanced code please. And if you comment throughout the code that would be incredibly helpful. Thank you Create 2 Java files for this assignment: Die.java and TestDie.java Part 1 - The Die Class The program Design a Die class that contains the following attributes: sides: represents how many sides a dice has. A dice usually has 6 sides but sometimes could have...
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...
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...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...
Please, can you help me solve these questions? Thanks! 1- Roll a dice (get a random number from 1 to 6) 2-Print value of the dice 3- Keep track of running total points(display of points) 4-Ask the user to continue (y/n) 5- If the user gets 21 points, print " bingo" 6- If the user more than 21 points, print "sorry you lose" 7- If the user decides to stop the game, print his final points.
Write a script that prints a randomly generated integer between 1 and 6. When I run your script, it should print out only the number that would appear on the die like this: 5 and nothing else. Write a script that takes input for the number of sides on the die. An n sided die should return a random integer between 1 and n with the same stipulations as in Exercise 1. You should take n from the user as...
Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will pick a move (Rock Paper Scissors) from 3 radio buttons and click the play button to play the game. The application will use random number generator to pick a move for the computer. Then, the application display the computer's move and also display whether you won or lost the game, or the game is tie if both moves are the same. The application must...
Hello, Could you please help me code this program in Java? It is important that all rules are carefully followed. Using the PairOfDice class from PP 4.9, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a...