Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create HighLowCardGame.java #18

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions HighLowCardGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import java.util.*;

public class HighLowCardGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();

int score = 0;
boolean playAgain = true;

while (playAgain) {
List<Integer> deck = initializeDeck();
Collections.shuffle(deck, random);
int currentCard = deck.get(0);

System.out.println("Welcome to the High-Low Card Game!");
System.out.println("Current Card: " + currentCard);

System.out.print("Will the next card be (H)igh or (L)ow? Enter 'H' or 'L': ");
String userGuess = scanner.next().toUpperCase();

int nextCard = deck.get(1);
System.out.println("Next Card: " + nextCard);

if ((userGuess.equals("H") && nextCard > currentCard) ||
(userGuess.equals("L") && nextCard < currentCard)) {
System.out.println("Correct! You guessed right.");
score++;
} else {
System.out.println("Incorrect. Game Over. Your score: " + score);
score = 0;
}

System.out.print("Do you want to play again? (Y/N): ");
String playAgainInput = scanner.next().toUpperCase();
playAgain = playAgainInput.equals("Y");
}

System.out.println("Thank you for playing!");
scanner.close();
}

public static List<Integer> initializeDeck() {
List<Integer> deck = new ArrayList<>();
for (int i = 2; i <= 14; i++) { // 2 to Ace (14)
deck.add(i);
}
return deck;
}
}