Program 1: Blackjack Part of the card game Blackjack involves dealing two cards
ID: 674500 • Letter: P
Question
Program 1: Blackjack
Part of the card game Blackjack involves dealing two cards from a shuffled deck of cards; to each of the players. (There’s more to it than that, but we’ll start with this).
Create a function that returns a string representing a randomly chosen card, for example, “6 of Clubs” or “King of Hearts”.
Your program should ask how many players there are. Then, it should use the function to “deal” two random cards to each player. Print the cards each player gets on the screen.
Extra question there’s more work to do before this program will play a game with you. Think about what you would need to do to turn this into a program that can play Blackjack.
Program 2:
Rock, Paper, Scissor Game
Write a program that lets the user play the game of Rock,Paper, Scissors against the computer.
The program should work as follows.
1. When the program begins, a random integer number in therange of 1 through 3 is generated. If
the number is 1, then the computer has chosen rock. If thenumber is 2, then the computer has
chosen paper. If the number is 3, then the computer has chosenscissors. (Don’t display the
computer’s choice yet)
2. The user enters his or her choice of “rock”,“paper”, or “scissors” at the keyboard.(You can use a
menu if you prefer)
3. The computer’s choice is displayed.
4. A winner is selected according to the followingrules:
If one player chooses rock and the other player choosesscissors, then rock wins.
If one player chooses scissors and the other player choosespaper, then scissors wins.
If one player chooses paper and the other player chooses rock,then paper wins.
If both players make the same choice, the game must be playedagain to determine the winner
Explanation / Answer
**********program 2********************
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissorsGame
{
public static void main(String[] args)
{
String computer;
String user;
Scanner keyboard = new Scanner(System.in);
do
{
computer = computerChoice();
user = userChoice(keyboard);
String output = determineWinner(computer, user);
System.out.println(output);
}
while (user.equalsIgnoreCase(computer));
keyboard.close();
}
public static String computerChoice()
{
Random rand = new Random();
int num = rand.nextInt(3) + 1;
return getChoice (num) ;
}
public static String getChoice (int number)
{
String choice;
switch (number)
{
case 1:
choice = "rock";
break;
case 2:
choice = "paper";
break;
case 3:
choice = "scissors";
break;
default:
choice = null;
}
return choice;
}
public static String userChoice(Scanner keyboard)
{
System.out.print("Enter 1 - rock, 2 - paper, or 3 - scissors: ");
int userChoice = keyboard.nextInt();
String play = getChoice (userChoice);
// Validate the choicewhile (play == null) {
System.out.print("Enter 1 - rock, 2 - paper, or 3 - scissors: ");
userChoice = keyboard.nextInt();
play = getChoice (userChoice);
}
// Return the user's choice.
return play;
}
public static String determineWinner (String computerChoice, String userChoice)
{
checkNotNull(computerChoice, "computer must have a choice");
checkNotNull(userChoice, "user must have a choice");
String output;
output = "The computer's choice was " + computerChoice + ". ";
output += "The user's choice was " + userChoice + ". ";
if (userChoice.equalsIgnoreCase("rock"))
{
if (computerChoice.equalsIgnoreCase("scissors"))
output += "Rock beats scissors. The user wins!";
else if (computerChoice.equalsIgnoreCase("paper"))
output += "Paper beats rock. The computer wins!";
else
output += "The game is tied! Play again...";
}
else if (userChoice.equalsIgnoreCase("paper"))
{
if (computerChoice.equalsIgnoreCase("scissors"))
output += "Scissors beats paper. The computer wins!";
else if (computerChoice.equalsIgnoreCase("rock"))
output += "Paper beats rock. The user wins!";
else
output += "The game is tied! Play again...";
}
else if (userChoice.equalsIgnoreCase("scissors"))
{
if (computerChoice.equalsIgnoreCase("rock"))
output += "Rock beats scissors. The computer wins!";
else if (computerChoice.equalsIgnoreCase("paper"))
output += "Scissors beats paper. The user wins!";
else
output += "The game is tied! Play again...";
}
return output;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.