Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am having trouble touble turning this into a GUI program using javafx. Could you do this and use textboxes and buttons to make it

I am having trouble touble turning this into a GUI program using javafx. Could you do this and use textboxes and buttons to make it run. Thanks.

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

import java.util.Scanner;

public class blackjack3 {

static Random random = new Random();

public static String getCard(int value){

if(value <= 10){

return String.valueOf(value);

}

else if(value == 11){

return "J";

}

else if(value == 12){

return "Q";

}

else if(value == 13){

return "K";

}

else if(value == 14){

return "A";

}

return "";

}

public static int dealCard(){

return 2 + random.nextInt(13);

}

public static void printCards(List cards, String msg, boolean showOnlyFirst){

System.out.print(msg + ": ");

for(int i = 0; i < cards.size(); ++i){

if(!showOnlyFirst || i == 0)

System.out.print(getCard(cards.get(i)) + " ");

else

System.out.println(" ");

}

if(!showOnlyFirst){

System.out.print("(" + getTotal(cards) + ")");

}

System.out.println();

}

public static void printCards(List cards, String msg) {

printCards(cards, msg, false);

}

public static int getTotal(List cards){

int total = 0;

for(int i = 0; i < cards.size(); ++i){

total += cards.get(i);

}

return total;

}

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.print("Enter initial amount: ");

double balance = in.nextDouble(), bet;

String choice;

while(true){

List playerCards = new ArrayList<>();

List dealerCards = new ArrayList<>();

boolean gameOver = false;

// take bet amount

while(true) {

System.out.print("Enter bet amount(0-" + balance + "): ");

bet = in.nextDouble();

if(bet >= 0 && bet <= balance){

break;

}

else{

System.out.println("Error. Invalid bet amount. Try again!!");

}

}

// deal cards

playerCards.add(dealCard());

playerCards.add(dealCard());

if(getTotal(playerCards) == 21){

printCards(playerCards, "Player: ");

System.out.println("Player has Blackjack, player won");

gameOver = true;

balance += bet;

}

else if(getTotal(playerCards) > 21){

printCards(playerCards, "Player: ");

System.out.println("Player is busted!! Dealer won");

gameOver = true;

balance -= bet;

}

if(!gameOver) {

dealerCards.add(dealCard());

dealerCards.add(dealCard());

printCards(playerCards, "Player cards: ");

printCards(dealerCards, "Dealer cards: ", true);

// keep dealing cards to player

while (true) {

System.out.print("Hit(H) or Stay(S): ");

choice = in.next();

if (choice.equalsIgnoreCase("H")) {

playerCards.add(dealCard());

printCards(playerCards, "Player cards: ");

if (getTotal(playerCards) > 21) {

System.out.println("Player is busted!! Dealer won");

gameOver = true;

balance -= bet;

break;

}

} else {

printCards(playerCards, "Player cards: ");

break;

}

}

if(!gameOver) {

// keep dealing cards to dealer

while (true) {

if (getTotal(dealerCards) >= getTotal(playerCards)) {

printCards(dealerCards, "Dealer cards: ");

System.out.println("Dealer won.");

balance -= bet;

gameOver = true;

break;

} else {

int total = getTotal(dealerCards);

if (total >= 17) {

printCards(dealerCards, "Dealer cards: ");

break;

} else if (total < getTotal(playerCards)) {

dealerCards.add(dealCard());

printCards(dealerCards, "Dealer cards: ");

if (getTotal(dealerCards) > 21) {

System.out.println("Dealer is busted!! player won");

balance += bet;

gameOver = true;

break;

}

} else {

printCards(dealerCards, "Dealer cards: ");

break;

}

}

}

}

if(!gameOver){

if(getTotal(playerCards) > getTotal(dealerCards)){

System.out.println("Player won.");

balance += bet;

}

else{

System.out.println("Dealer won.");

balance -= bet;

}

}

}

System.out.println("Balance amount is: $" + balance);

// check if user wants to play again

if(balance < 0){

break;

}

else{

System.out.print("Do you want to play again(y or n): ");

choice = in.next();

if(choice.equalsIgnoreCase("n"))

break;

}

}

}

}

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

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

More Books

Students also viewed these Databases questions

Question

3. Would you say that effective teamwork saved their lives?

Answered: 1 week ago