Redo project 6 using ArrayList. import java.util.Scanner; 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!"); Card userCard = drawCard(cards); Card computerCard = drawCard(cards); System.out.println("Your card is " + userCard); System.out.println("Computer's card is " + computerCard); int userValue = userCard.getValue(); int computerValue = computerCard.getValue(); 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.getSuit(); int computerSuit = computerCard.getSuit(); 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 Card 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 Card(suit, value); } } } public static class Card { private int suit; private int value; public Card(int suit, int value) { this.suit = suit; this.value = value; } public int getSuit() { return suit; } public int getValue() { return value; } public String toString() { String suitStr = ""; switch (suit) { case 1: suitStr = "club"; break; case 2: suitStr = "diamond"; break; case 3: suitStr = "heart"; break; case 4: suitStr = "spade"; break; } String valueStr; switch (value) { case 11: valueStr = "Jack"; break; case 12: valueStr = "Queen"; break; case 13: valueStr = "King"; break; case 14: valueStr = "Ace"; break; default: valueStr = Integer.toString(value); break; } return valueStr + " of " + suitStr + "s"; } } }
Redo project 6 using ArrayList. import java.util.Scanner; 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!"); Card userCard = drawCard(cards); Card computerCard = drawCard(cards); System.out.println("Your card is " + userCard); System.out.println("Computer's card is " + computerCard); int userValue = userCard.getValue(); int computerValue = computerCard.getValue(); 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.getSuit(); int computerSuit = computerCard.getSuit(); 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 Card 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 Card(suit, value); } } } public static class Card { private int suit; private int value; public Card(int suit, int value) { this.suit = suit; this.value = value; } public int getSuit() { return suit; } public int getValue() { return value; } public String toString() { String suitStr = ""; switch (suit) { case 1: suitStr = "club"; break; case 2: suitStr = "diamond"; break; case 3: suitStr = "heart"; break; case 4: suitStr = "spade"; break; } String valueStr; switch (value) { case 11: valueStr = "Jack"; break; case 12: valueStr = "Queen"; break; case 13: valueStr = "King"; break; case 14: valueStr = "Ace"; break; default: valueStr = Integer.toString(value); break; } return valueStr + " of " + suitStr + "s"; } } }
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
Redo project 6 using ArrayList.
import java.util.Scanner;
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!");
Card userCard = drawCard(cards);
Card computerCard = drawCard(cards);
System.out.println("Your card is " + userCard);
System.out.println("Computer's card is " + computerCard);
int userValue = userCard.getValue();
int computerValue = computerCard.getValue();
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.getSuit();
int computerSuit = computerCard.getSuit();
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 Card 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 Card(suit, value);
}
}
}
public static class Card {
private int suit;
private int value;
public Card(int suit, int value) {
this.suit = suit;
this.value = value;
}
public int getSuit() {
return suit;
}
public int getValue() {
return value;
}
public String toString() {
String suitStr = "";
switch (suit) {
case 1: suitStr = "club"; break;
case 2: suitStr = "diamond"; break;
case 3: suitStr = "heart"; break;
case 4: suitStr = "spade"; break;
}
String valueStr;
switch (value) {
case 11: valueStr = "Jack"; break;
case 12: valueStr = "Queen"; break;
case 13: valueStr = "King"; break;
case 14: valueStr = "Ace"; break;
default: valueStr = Integer.toString(value); 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 5 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education