import java.util.*; public class CardGame { static Scanner scanner = new Scanner(System.in); static Random random = new Random(); public static void main(String[] args) { System.out.println("Welcome to the card game! Here are the instructions:"); System.out.println("- You will randomly draw a card from a deck of 52 cards."); System.out.println("- The computer will also draw a card from the same deck."); System.out.println("- If your card has a higher face value, you win. Otherwise, you lose."); System.out.println("- If your card has the same face value as the computer's card, the suit determines the order."); int gamesPlayed = 0; int userWins = 0; int computerWins = 0; boolean[] cards = new boolean[52]; // false means the card is available, true means it's been drawn while (true) { System.out.println("\nNew game!"); int[] userCard = drawCard(cards); int[] computerCard = drawCard(cards); System.out.println("Your card is " + cardToString(userCard)); System.out.println("Computer's card is " + cardToString(computerCard)); int userValue = userCard[1]; int computerValue = computerCard[1]; if (userValue > computerValue) { System.out.println("You win!"); userWins++; } else if (userValue < computerValue) { System.out.println("You lose!"); computerWins++; } else { // same face value, compare suits int userSuit = userCard[0]; int computerSuit = computerCard[0]; if (userSuit > computerSuit) { System.out.println("You win!"); userWins++; } else { System.out.println("You lose!"); computerWins++; } } gamesPlayed++; if (gamesPlayed == 26) { System.out.println("\nYou have played 26 games, which is half of the deck."); System.out.println("Do you want to shuffle the deck and start a new game? (y/n)"); String answer = scanner.next(); if (answer.equals("y")) { Arrays.fill(cards, false); gamesPlayed = 0; userWins = 0; computerWins = 0; } else { break; } } else { System.out.println("\nDo you want to play another game? (y/n)"); String answer = scanner.next(); if (!answer.equals("y")) { break; } } } System.out.println("\nThanks for playing!"); System.out.println("You played " + gamesPlayed + " games."); System.out.println("You won " + userWins + " times."); System.out.println("The computer won " + computerWins + " times."); } public static int[] drawCard(boolean[] cards) { while (true) { int suit = random.nextInt(4) + 1; int value = random.nextInt(13) + 2; int index = (suit - 1) * 13 + (value - 2); if (!cards[index]) { cards[index] = true; return new int[] { suit, value }; } } } public static String cardToString(int[] card) { String suitStr = ""; switch (card[0]) { case 1: suitStr = "club"; break; case 2: suitStr = "diamond"; break; case 3: suitStr = "heart"; break; case 4: suitStr = "spade"; break; } String valueStr; switch (card[1]) { case 11: valueStr = "Jack"; break; case 12: valueStr = "Queen"; break; case 13: valueStr = "King"; break; case 14: valueStr = "Ace"; break; default: valueStr = Integer.toString(card[1]); break; } return valueStr + " of " + suitStr + "s"; } }
import java.util.*; public class CardGame { static Scanner scanner = new Scanner(System.in); static Random random = new Random(); public static void main(String[] args) { System.out.println("Welcome to the card game! Here are the instructions:"); System.out.println("- You will randomly draw a card from a deck of 52 cards."); System.out.println("- The computer will also draw a card from the same deck."); System.out.println("- If your card has a higher face value, you win. Otherwise, you lose."); System.out.println("- If your card has the same face value as the computer's card, the suit determines the order."); int gamesPlayed = 0; int userWins = 0; int computerWins = 0; boolean[] cards = new boolean[52]; // false means the card is available, true means it's been drawn while (true) { System.out.println("\nNew game!"); int[] userCard = drawCard(cards); int[] computerCard = drawCard(cards); System.out.println("Your card is " + cardToString(userCard)); System.out.println("Computer's card is " + cardToString(computerCard)); int userValue = userCard[1]; int computerValue = computerCard[1]; if (userValue > computerValue) { System.out.println("You win!"); userWins++; } else if (userValue < computerValue) { System.out.println("You lose!"); computerWins++; } else { // same face value, compare suits int userSuit = userCard[0]; int computerSuit = computerCard[0]; if (userSuit > computerSuit) { System.out.println("You win!"); userWins++; } else { System.out.println("You lose!"); computerWins++; } } gamesPlayed++; if (gamesPlayed == 26) { System.out.println("\nYou have played 26 games, which is half of the deck."); System.out.println("Do you want to shuffle the deck and start a new game? (y/n)"); String answer = scanner.next(); if (answer.equals("y")) { Arrays.fill(cards, false); gamesPlayed = 0; userWins = 0; computerWins = 0; } else { break; } } else { System.out.println("\nDo you want to play another game? (y/n)"); String answer = scanner.next(); if (!answer.equals("y")) { break; } } } System.out.println("\nThanks for playing!"); System.out.println("You played " + gamesPlayed + " games."); System.out.println("You won " + userWins + " times."); System.out.println("The computer won " + computerWins + " times."); } public static int[] drawCard(boolean[] cards) { while (true) { int suit = random.nextInt(4) + 1; int value = random.nextInt(13) + 2; int index = (suit - 1) * 13 + (value - 2); if (!cards[index]) { cards[index] = true; return new int[] { suit, value }; } } } public static String cardToString(int[] card) { String suitStr = ""; switch (card[0]) { case 1: suitStr = "club"; break; case 2: suitStr = "diamond"; break; case 3: suitStr = "heart"; break; case 4: suitStr = "spade"; break; } String valueStr; switch (card[1]) { case 11: valueStr = "Jack"; break; case 12: valueStr = "Queen"; break; case 13: valueStr = "King"; break; case 14: valueStr = "Ace"; break; default: valueStr = Integer.toString(card[1]); break; } return valueStr + " of " + suitStr + "s"; } }
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
import java.util.*;
public class CardGame {
static Scanner scanner = new Scanner(System.in);
static Random random = new Random();
public static void main(String[] args) {
System.out.println("Welcome to the card game! Here are the instructions:");
System.out.println("- You will randomly draw a card from a deck of 52 cards.");
System.out.println("- The computer will also draw a card from the same deck.");
System.out.println("- If your card has a higher face value, you win. Otherwise, you lose.");
System.out.println("- If your card has the same face value as the computer's card, the suit determines the order.");
int gamesPlayed = 0;
int userWins = 0;
int computerWins = 0;
boolean[] cards = new boolean[52]; // false means the card is available, true means it's been drawn
while (true) {
System.out.println("\nNew game!");
int[] userCard = drawCard(cards);
int[] computerCard = drawCard(cards);
System.out.println("Your card is " + cardToString(userCard));
System.out.println("Computer's card is " + cardToString(computerCard));
int userValue = userCard[1];
int computerValue = computerCard[1];
if (userValue > computerValue) {
System.out.println("You win!");
userWins++;
} else if (userValue < computerValue) {
System.out.println("You lose!");
computerWins++;
} else {
// same face value, compare suits
int userSuit = userCard[0];
int computerSuit = computerCard[0];
if (userSuit > computerSuit) {
System.out.println("You win!");
userWins++;
} else {
System.out.println("You lose!");
computerWins++;
}
}
gamesPlayed++;
if (gamesPlayed == 26) {
System.out.println("\nYou have played 26 games, which is half of the deck.");
System.out.println("Do you want to shuffle the deck and start a new game? (y/n)");
String answer = scanner.next();
if (answer.equals("y")) {
Arrays.fill(cards, false);
gamesPlayed = 0;
userWins = 0;
computerWins = 0;
} else {
break;
}
} else {
System.out.println("\nDo you want to play another game? (y/n)");
String answer = scanner.next();
if (!answer.equals("y")) {
break;
}
}
}
System.out.println("\nThanks for playing!");
System.out.println("You played " + gamesPlayed + " games.");
System.out.println("You won " + userWins + " times.");
System.out.println("The computer won " + computerWins + " times.");
}
public static int[] drawCard(boolean[] cards) {
while (true) {
int suit = random.nextInt(4) + 1;
int value = random.nextInt(13) + 2;
int index = (suit - 1) * 13 + (value - 2);
if (!cards[index]) {
cards[index] = true;
return new int[] { suit, value };
}
}
}
public static String cardToString(int[] card) {
String suitStr = "";
switch (card[0]) {
case 1:
suitStr = "club";
break;
case 2:
suitStr = "diamond";
break;
case 3:
suitStr = "heart";
break;
case 4:
suitStr = "spade";
break;
}
String valueStr;
switch (card[1]) {
case 11:
valueStr = "Jack";
break;
case 12:
valueStr = "Queen";
break;
case 13:
valueStr = "King";
break;
case 14:
valueStr = "Ace";
break;
default:
valueStr = Integer.toString(card[1]);
break;
}
return valueStr + " of " + suitStr + "s";
}
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 5 steps with 6 images
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY