You will be creating two separate programs – one written in C++ and one written
ID: 3796266 • Letter: Y
Question
You will be creating two separate programs – one written in C++ and one written in Java.
You will be simulating a game of horse in basketball. The rules we will use for our simulation are as follows:
• The game must be played with two players.
• Each player will simulate shooting the basketball.
• If one player hits their shot while the other misses their shot, the player that missed the shot collects a letter – beginning with the letter ‘H’ of the word “HORSE” and iterating through each letter for each subsequent miss.
o If both players hit their shot, or both miss their shot, then we continue to the next round.
• The game continues until one player has collected all of the letters necessary to spell the word “HORSE” – at which point that player is declared the loser of the game.
http://www.wikihow.com/Play-Horse-(the-Basketball-Game)
Your goal is to create a program to simulate this game. This game will be turn-based, meaning that one player will go – followed by the next, and so on. The game will continue until one player wins. At which point you should prompt the user if they would like to play again (Y|N).
Development Process:
You are required to implement the following items as part of this assignment:
• Your C++ program must contain three files (2 .cpp and 1 .h file). These files should be named as such: Game.cpp, Player.cpp, Player.h.
• Your Java program must contain the following two files: Game.java and Player.java.
• Your Game class must store two Players on the Heap.
• Your Player class must use a character array to store the word.
• In the C++ version we will need to manage our memory accordingly – no memory leaks!
• You can use a random number generator to generate whether or not the Player hits the shot – let’s assume each Player has a 50% shooting percentage as a default value.
• When a game finishes we should prompt the user if they would like to play again. The options available to them should be ‘Y’ or ‘N’.
o If they select ‘Y,’ then the program should simulate a new game – with all new Players.
o If they select ‘N,’ the program should terminate.
Below is example output of what your program should display when executed:
Welcome to the Game of Horse!
Player #1: Hit Shot!
Player #2: Hit Shot!
Player #1: Missed Shot!
Player #2: Hit Shot!
Player #1: Added an 'H'
Player #1: Hit Shot!
Player #2: Missed Shot!
Player #2: Added an 'H'
Player #1: Missed Shot!
Player #2: Hit Shot!
Player #1: Added an 'O'
Player #1: Hit Shot!
Player #2: Missed Shot!
Player #2: Added an 'O'
Player #1: Hit Shot!
Player #2: Missed Shot!
Player #2: Added an 'R'
Player #1: Missed Shot!
Player #2: Hit Shot!
Player #1: Added an 'R'
Player #1: Missed Shot!
Player #2: Missed Shot!
Player #1: Hit Shot!
Player #2: Missed Shot!
Player #2: Added an 'S'
Player #1: Missed Shot!
Player #2: Missed Shot!
Player #1: Missed Shot!
Player #2: Hit Shot!
Player #1: Added an 'S'
Player #1: Missed Shot!
Player #2: Missed Shot!
Player #1: Hit Shot!
Player #2: Missed Shot!
Player #2: Added an 'E'
Player 1 Wins :: Player 2 = HORSE
Would you like to play again (Y|N)? :
Explanation / Answer
package Stack;
import java.util.Random;
import java.util.Scanner;
public class Game {
Player player1 = new Player();
Player player2 = new Player();
public void play(){
while(true){
boolean player1Hit = player1.makeHit();
boolean player2Hit = player2.makeHit();
System.out.println();
if(player1Hit == player2Hit){
if(player1Hit){
System.out.println("Player #1 :Hit Shot!" );
System.out.println("Player #2 :Hit Shot!" );
}
else{
System.out.println("Player #1 :Missed Shot!" );
System.out.println("Player #2 :Missed Shot!" );
}
continue;
}
else if(player1Hit && !player2Hit){
player2.setNextChar();
System.out.println("Player #1 :Hit Shot!" );
System.out.println("Player #2 :Added an '"+player2.getCurChar()+"'" );
}
else if(!player1Hit && player2Hit){
player1.setNextChar();
System.out.println("Player #1 :Added an '"+player1.getCurChar()+"'" );
System.out.println("Player #2 :Hit Shot!" );
}
if(player1.isLoser()){
System.out.println("Player 2 Wins! :: Player 1 HORSE ");
}
if(player2.isLoser()){
System.out.println("Player 1 Wins! :: Player 2 HORSE ");
}
if(player2.isLoser() || player1.isLoser()){
System.out.println("Would you like to play again (Y)? :");
Scanner sc = new Scanner(System.in);
String ans = sc.next();
if(!ans.equalsIgnoreCase("y"))
break;
player1.reset();
player2.reset();
}
}
}
public static void main(String[] args) {
System.out.println("Welcome to the game of HORSE");
Game game = new Game();
game.play();
}
}
class Player{
char[] horse;
int winCount;
public Player(){
horse = new char[5];
horse[0] = 'H';
horse[1] = 'O';
horse[2] = 'R';
horse[3] = 'S';
horse[4] = 'E';
winCount = -1;
}
void setNextChar(){
if(winCount < horse.length)
winCount++;
}
public char getCurChar(){
if(winCount>=0)
return horse[winCount];
return '*';
}
public boolean makeHit(){
Random r = new Random();
return r.nextBoolean();
}
public boolean isLoser(){
if(winCount==horse.length-1)
return true;
else
return false;
}
public void reset(){
winCount = -1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.