Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'd like to add these options to my blackjack game in Java. It's part of my final project, I'd really appreciate the help. player chips

I'd like to add these options to my blackjack game in Java. It's part of my final project, I'd really appreciate the help.

player chips -- player starts out with a given number of chips and places bets with them, winning or losing chips based on the outcome of each hand.

repeat -- game continues across multiple hands until all but one player runs out of chips

This is my BlackJackGame class.

Check other post for Card class.

import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /** * Write a description of class ScrabbleGame here. * * @author (your name) * @version (a version number or a date) */ public class BlackJackGame { public static void main(String [] args) throws FileNotFoundException { Scanner keyboard = new Scanner(System.in); System.out.println("\f"); String answer; Card [] deck = new Card[52];

//a) finish the makeDeck method below. This method fills the deck array // with info for 52 cards read from cards.txt makeDeck(deck);

System.out.println("The first card in deck is " + deck[0]); System.out.println("The last card in deck is " + deck[51]);

Card [] playerHand = new Card[10]; // player can have up to 10 cards Card [] computerHand = new Card[10];

int playerCount = 0, computerCount = 0; int playerScore=0, computerScore=0;

System.out.print("Player hand = "); //b) add the printCards method below main. It only prints playerCount cards in the array printCards(playerHand, playerCount);

//c) add the blackJackValue method below main, then test with the following // two lines of code. The blackJack value is the rank for ranks 1-10, // and 10 for all others // Card test = new Card('S',12); // System.out.println("The blackjack value of test card = " + blackJackValue(test));

//d) finish the score method below main. It adds up and returns // the blackJackValue of the first count cards in the array passed to it // initially the score will be zero. System.out.println("Player score = " + score(playerHand, playerCount));

System.out.print("Player, hit or stay? "); answer = keyboard.next();

while (answer.equals("hit")) { System.out.println("Dealing ... "); //e) finish the deal method below main. It picks a random index from 0 to 51 // and returns the card in the deck array at that index playerHand[playerCount] = deal(deck); playerCount++;

System.out.print("Player hand = "); printCards(playerHand, playerCount);

playerScore = score(playerHand, playerCount); System.out.println("Player score = " + playerScore);

System.out.print("Player, hit or stay? "); answer = keyboard.next(); }

System.out.print("Computer: ");

//f) finish the while loop for the computer's turn

while (computerScore < 16) { // the computer automatically stays at 16 System.out.println("Dealing ... "); // deal a card to the computerHand array computerHand[computerCount] = deal(deck); computerCount++; System.out.print("Computer hand = "); printCards(computerHand, computerCount); computerScore = score(computerHand, computerCount); System.out.println("Computer score = " + computerScore); // increase computerCount // print the computer hand

// compute the computerScore

// print the computerScore }

// g) determine and print the outcome of the game if(computerScore> 21 && playerScore> 21){ System.out.println("Tie!"); } else if (playerScore >21){ System.out.println("Player busts, Computer wins!"); } else if (computerScore >21){ System.out.println("Computer busts, Player wins!"); } else if (computerScore == playerScore){ System.out.println("Tie!"); } else { System.out.println("Player wins!"); } } // END OF MAIN METHOD public static void makeDeck(Card [] deck) throws FileNotFoundException { // make a Scanner inputFile and connect to file cards.txt Scanner inputFile = new Scanner(new File("cards.txt")); int k = 0; // while loop, as long as k<52 and there is more info in file while(k<52 && inputfile.hasnext()){ char suit = inputFile.next().charAt(0); int rank = inputFile.nextInt(); deck[k] = new card(suit, rank); k++; }>

// read an int from the file, store in rank

// make a Card temp a new Card with the suit and rank you read

// assign temp to the next location in the deck array

// add 1 to k for next time around

}

public static Card deal(Card [] deck) { // use the randomInt method to generate // a random integer between 0 (inclusive) to 52 (exclusive) Card random = deck [randomInt(0,52)]; return random; // return the card in deck at your randomIndex

// delete this after writing the above }

public static int randomInt(int low, int high) { // returns a random int from low (inclusive) to high (exclusive) // Determine the length of the range. int range = high - low;

// Give a random number from the range. return (int)(range*Math.random()) + low;

}

public static int score(Card [] hand, int count) { int score = 0; for (int i = 0; i < count; i++){ score += blackJackValue(hand[i]); } return score; // fix }

public static int blackJackValue(Card c) { // if the rank of Card c is less than or equal to 10 // just return the rank if(c.getRank()<11){ return c.getRank(); } return 10;

// otherwise, return 10

// delete this after writing the above } public static void printCards(Card[] cards, int count){ for(int i=0; i< count; i++){ System.out.print(cards[i] + ", "); } System.out.println(); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Contemporary Issues In Database Design And Information Systems Development

Authors: Keng Siau

1st Edition

1599042894, 978-1599042893

More Books

Students also viewed these Databases questions

Question

What is the raison-dtre behind pricing of a depositary receipt?

Answered: 1 week ago

Question

1. What are the pros and cons of diversity for an organisation?

Answered: 1 week ago

Question

1. Explain the concept of diversity and equality in the workplace.

Answered: 1 week ago