In the card game in-between two cards are dealt face up on the table and players
ID: 1273011 • Letter: I
Question
In the card game in-between two cards are dealt face up on the table and players bet whether the value of the next card will be between them or not. You will write a program for a simplified version of in-between.
Functional requirements:
1. The program will prompt the user for a name, then for the number of cards in the deck. In this version of in-between the deck consists of N cards, each labeled with a distinct integer from 1 to N. For smooth solo game play, the size of the deck must be a multiple of 3. Reprompt if the user enters a value that is NOT a positive integer divisible by 3. (Use the % operator for this.)
2. Create a deck of N cards, implemented an array of integers.
3. Shuffle the deck. This entails randomly selecting 2 positions in the deck, swapping the cards (ints) located at those positions, and repeating this process N times.
4. Deal 2 cards from the deck, displaying their values to the player and updating the effective size of the deck.
5. Display the player's account balance and prompt the player to bet. The player begins the game with 10 points. Each round he must bet exactly 1 point, either on between (b), or outside (o). If the player enters a value other than b or o, the program reprompts.
6. Deal a card from the deck. If the player was correct his score goes up 1, if not it goes down 1.
7. If the player has 0 points, the game is over. Otherwise, prompt the player to see if he wants to continue. If no, the game is over. If yes, continue.
8. If the deck is empty, back to step 2. Otherwise, go back to step 4.
WHEN GAME IS OVER
1. open the high scores file for reading, save each line into a String array; assume no more than 50 entries
2. if there is no line beginning with the player's name, or if the player's current score is bigger than his/her high score, then add a line or update the high score for the user.
3. write the String array to the high scores file
What is done so far.
import java.util.Random;
import java.util.Scanner;
public class Finalproject {
public static void main(String[] args) {
int balance = 10;
Scanner in = new Scanner(System.in);
boolean cont = true;
while(cont) {
int deck[];
System.out.print("Enter the number of cards in the deck: ");
int length = in.nextInt();
while(length % 3 != 0) {
System.out.println("Number of cards is not a multiple of 3");
System.out.print("Enter the number of cards in the deck: ");
length = in.nextInt();
}
deck = new int[length];
initDeck(deck);
shuffle(deck);
while(balance > 0 && length > 0) {
int card1 = deck[length-1];
int card2 = deck[length-2];
length -= 2;
System.out.println("First card: " + card1);
System.out.println("Second card: " + card2);
System.out.println("Account balance: " + balance + " points");
System.out.print("Bet on either between(b) or outside(o): ");
char bet = in.next().toLowerCase().charAt(0);
while(bet != 'b' && bet != 'o') {
System.out.println("Not a valid bet");
System.out.print("Bet on either between(b) or outside(o): ");
bet = in.next().toLowerCase().charAt(0);
}
int card3 = deck[length-1];
length--;
System.out.println("Third card: " + card3);
char result = 'o';
if((card3 > card1 && card3 < card2) || (card3 > card2 && card3 < card1)) {
result = 'b';
}
if(bet == result) {
System.out.println("Correct! You gain 1 point.");
balance++;
}
else {
System.out.println("Incorrect, you lose 1 point");
balance--;
}
if(balance > 0) {
System.out.print("Would you like to continue(yes/no)? ");
String answer = in.next().toLowerCase();
if(answer.equals("no")) {
cont = false;
length = 0;
}
}
else {
System.out.println("You are out of points");
cont = false;
}
}
if(length == 0) {
System.out.println("The deck is empty, make a new deck!");
}
}
System.out.println("The game is over!");
System.out.println("Your final total is " + balance + " points.");
}
public static void initDeck(int[] deck) {
for(int i = 0; i < deck.length; ++i) {
deck[i] = i+1;
}
}
public static void swap(int[] deck, int p1, int p2) {
int temp = deck[p1];
deck[p1] = deck[p2];
deck[p2] = temp;
}
public static void shuffle(int[] deck) {
Random rand = new Random();
for(int i = 0; i < deck.length; ++i) {
swap(deck,Math.abs(rand.nextInt() % deck.length),Math.abs(rand.nextInt() % deck.length));
}
}
}
Explanation / Answer
import java.util.Random;
public class Acey
{
public static void main(String[] args)
{
int cardLeft, cardMiddle, cardRight;
int vLeft, vMiddle, vRight;
int cardIndex;
int cards[] = new int[52];
int score, t1, t2, bet;
boolean anotherTurn;
System.out.println("Acey-Deucey ");
System.out.println("Two cards are dealt face up.");
System.out.println("You bet whether the next card dealt has a value between the displayed cards. ");
// Start new game loop
do
{
System.out.println("New card deck shuffled");
cards = NIntegers(52);
score = 100;
cardIndex = 0;
anotherTurn = true;
// Start player turn loop
do
{
cardLeft = cards[cardIndex];
vLeft = cardLeft % 13 + 1;
cardRight = cards[cardIndex + 1];
vRight = cardRight % 13 + 1;
cardMiddle = cards[cardIndex + 2];
vMiddle = cardMiddle % 13 + 1;
// swap left and right if left is larger
if (vLeft > vRight)
{
t1 = cardLeft;
t2 = vLeft;
cardLeft = cardRight;
vLeft = vRight;
cardRight = t1;
vRight = t2;
}
System.out.println(" You have " + score + " points.");
DisplayCard(cardLeft, "First");
DisplayCard(cardRight, "Second");
cardIndex += 3;
do
{
bet = Keyin.inInt("There are " + (52 - cardIndex) + " cards remaining. What is your bet?");
if (bet > score)
{
System.out.println("You can only bet " + score);
}
}
while (bet > score);
if (bet == 0)
{
System.out.println("Not too sure of yourself, huh?");
}
DisplayCard(cardMiddle, "Next");
if (vMiddle >= vLeft && vMiddle <= vRight)
{
score += bet;
System.out.println("You win!!!");
if (bet == 0)
{
System.out.println("You should have bet something.");
}
}
else
{
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.