How can I write a card class with at least two constructors, one default constru
ID: 3662075 • Letter: H
Question
How can I write a card class with at least two constructors, one default constructor with no parameters, and get methods that can be used with the following code for Blackjack? I also tried to incorporate a betting system in the code but I'm not sure how to make it work properly. Can anyone help me figure out how to fix it?
import java.util.Random;
import java.util.Scanner;
public class Blackjack
{
String type;
int currentcard;
int total = 0; //total in hand
static int dealerTotal = 0; //dealer's total
static int playerTotal = 0; //player's total, static variables allow totals to be compared at the end
int money = 100; //Amount of money user has
int bet; //Amount user bets on a game
boolean userWins; //Did user win the game?
Random ran = new Random();
Scanner sc = new Scanner(System.in); //to input whether player wants to hit or stay
public Blackjack(String player)
{
type = player;
System.out.println("Welcome to Blackjack! The game has now started.");
Deal();
game();
}
{while(true) //betting system
{
System.out.println("You have " + money + " dollars.");
do
{
System.out.println("How many dollars do you want to bet?");
bet = sc.nextInt();
if (bet < 0 || bet > money)
{
System.out.println("Your bet must be between 0 and " + money + ".");
}
} while (bet < 0 || bet > money);
if (bet == 0)
{
break;
}
if (userWins)
{
money = money + bet;
} else
{
money = money - bet;
}
System.out.println();
if (money == 0)
{
System.out.println("Looks like you've are out of money!");
break;
}
}
System.out.println();
System.out.println("You leave with $" + money + '.');
}
void displayCard()
{
currentcard = ran.nextInt(14);
while(currentcard == 0)
{
currentcard = ran.nextInt(14);
}
switch(currentcard)
{ //cases 11, 12, 13 = user drew a J, Q, or K
case 11:
System.out.println(type + " Drew a Jack");
total = total + 10;
System.out.println(type + " Current total: " + total);
break;
case 12:
System.out.println(type + " Drew a Queen");
total = total + 10;
System.out.println(type + " Current Total: " + total);
break;
case 13:
System.out.println(type + " Drew a King");
total = total + 10;
System.out.println(type + " Current Total: " + total);
case 14:
System.out.println(type + " Drew an Ace");
total = total + 11;
System.out.println(type + " Current Total: " + total);
break;
default:
System.out.println(type + " Drew a " + currentcard);
total = total + currentcard;
System.out.println(type + " Current Total: " + total);
}
}
void Deal()
{
System.out.println("First card is ");
displayCard();
System.out.println("Second card is ");
displayCard();
}
void game()
{
if(total <= 21)
{
if(!(type.equals("Dealer")))
{
System.out.println("Would you like to hit? Y for yes, S for stand.");
if (sc.nextLine().equals("Y"))
{
displayCard();
game();
}
else
{
System.out.println("Your Total: " + total);
playerTotal = total;
}
}
}
else
{
System.out.println(type + " Busted: " + total);
// System.exit(0);
}
if(type.equals("Dealer"))
if((total < 17 && total <=21) || total < playerTotal)
{
displayCard();
game();
}
else //dealer does not save, total is busted
{
if(total <21)
dealerTotal = total;
}
}
void WhoWon()
{
if(dealerTotal == 0)
{
System.out.println("Dealer Busted");
}
if(playerTotal == 0)
{
System.out.println("Player Busted");
}
if(dealerTotal > playerTotal)
System.out.println("Dealer Won");
}
{
if (dealerTotal< playerTotal)
{
System.out.println("Player Won");
}
{
if (dealerTotal == playerTotal)
{
System.out.println("Push (Draw)");
}
}
}
public static void main(String[] args)
{
Blackjack player = new Blackjack("Player");
Blackjack dealer = new Blackjack("Dealer");
player.WhoWon();
dealer.WhoWon();
}
}
Explanation / Answer
New Code
import java.util.Scanner;
public class myBlackJackGame
{
private final myPlayer[] myplayers = new myPlayer[2];
public static void main(String[] args)
{
myBlackJackGame mygame = new myBlackJackGame();
Scanner newSC = new Scanner(System.in);
System.out.println("BlackJack Game!");
System.out.println("your name:");
String nameOfUser = newSC.nextLine();
System.out.println("Hai " + nameOfUser + "! Start play! ");
myPlayer myuser = new myUser(nameOfUser);
mygame.myplayers[0] = myuser;
myPlayer mydealer = new myDealer();
mygame.myplayers[1] = mydealer;
myuser.gameStart();
boolean userLostByDefault = mygame.myplayerTurn(myuser, mydealer);
if(userLostByDefault)
{
mygame.lostByDefault(newSC);
}
mydealer.gameStart();
boolean dealerLostByDefault = mygame.myplayerTurn(mydealer, myuser);
if(dealerLostByDefault)
{
mygame.lostByDefault(newSC);
}
myPlayer gamewinner = mygame.whoWon(mygame.myplayers, mydealer);
System.out.println(myuser + "'s total: " + myuser.getmyTotal() + ". " +
mydealer + "'s total: " + mydealer.getmyTotal() + ". ");
System.out.printf("%s WoN!", gamewinner);
if (mygame.againGo(newSC) == true)
{
main(null);
}
}
private boolean myplayerTurn(myPlayer player1, myPlayer player2)
{
boolean turnEnd = false;
boolean lostByDefault = false;
while(turnEnd == false)
{
turnEnd = player1.want2Stay();
if(turnEnd == false)
{
player1.drawmyCard();
System.out.println(player1 + "'s total: " + player1.getmyTotal() + " ");
}
}
if(player1.getmyTotal() > 21)
{
System.out.println(player1 + "'s total > 21, " + player1 + " lostByDefault " +
player2 + " WoN ");
lostByDefault = true;
}
return lostByDefault;
}
private myPlayer whoWon(myPlayer[] myplayers, myPlayer mydealer)
{
myPlayer gamewinner = myplayers[0];
for(int i = 1; i < myplayers.length; i++)
{
myPlayer elements = myplayers[i];
if(elements.getmyTotal() > gamewinner.getmyTotal())
{
gamewinner = elements;
}
else if(elements.getmyTotal() == gamewinner.getmyTotal())
{
gamewinner = mydealer;
}
}
return gamewinner;
}
private void lostByDefault(Scanner newSC)
{
if(againGo(newSC) == true)
{
main(null);
}
System.exit(0);
}
private boolean againGo(Scanner newSC)
{
Boolean againGo = null;
while(againGo == null)
{
System.out.println("Like 2 play another-round?");
String myIn = newSC.nextLine();
String myAns = myIn.toLowerCase();
switch(myAns)
{
case "y":
case "yes":
againGo = true;
break;
case "n":
case "no":
againGo = false;
break;
default:
System.out.println("Invalid answer");
break;
}
System.out.println();
}
return againGo;
}
}
/* myCard.java */
import java.util.Random;
public class myCard
{
private final int myValue;
myCard()
{
myValue = myrandomCard();
}
public int getmyValue()
{
return myValue;
}
private int myrandomCard()
{
int mini = 2;
int maxi = 11;
int myrange = (maxi - mini);
int myrandom = new Random().nextInt(myrange + 1) + mini;
return myrandom;
}
}
/* myDealer.java */
import java.util.Arrays;
public class myDealer implements myPlayer
{
private final String mydealer = "myDealer";
myCard[] mydealerHand = new myCard[0];
myDealer()
{
mydealerHand = Arrays.copyOf(mydealerHand, mydealerHand.length + 2);
mydealerHand[0] = new myCard();
mydealerHand[1] = new myCard();
System.out.println("The mydealer has " + mydealerHand[0].getmyValue() + " showing, and a hidden card.");
System.out.println("Total is hidden ");
}
@Override
public void gameStart()
{
System.out.println("Now, myDealer's turn");
int mytotal = getmyTotal();
System.out.println("Hidden card: " + mydealerHand[1].getmyValue() + " " +
"Total: " + mytotal + ". ");
}
@Override
public void drawmyCard()
{
mydealerHand = Arrays.copyOf(mydealerHand, mydealerHand.length + 1);
mydealerHand[mydealerHand.length - 1] = new myCard();
int lastCard = mydealerHand.length - 1;
System.out.println("myDealer drew " + mydealerHand[lastCard].getmyValue());
}
@Override
public boolean want2Stay()
{
boolean mystay = false;
int mytotal = getmyTotal();
if(mytotal > 16)
{
mystay = true;
System.out.println("myDealer stays.");
}
else
{
System.out.println("myDealer hits.");
}
return mystay;
}
@Override
public int getmyTotal()
{
int mytotalValue = 0;
for (myCard c : mydealerHand)
{
int mycardValue = c.getmyValue();
mytotalValue = mytotalValue + mycardValue;
}
return mytotalValue;
}
@Override
public String toString()
{
return mydealer;
}
}
/* myPlayer.java */
public interface myPlayer
{
void gameStart();
void drawmyCard();
boolean want2Stay();
int getmyTotal();
}
/* myUser.java */
import java.util.Arrays;
import java.util.Scanner;
public class myUser implements myPlayer
{
private final String myuser;
private myCard[] myuserHand = new myCard[0];
myUser(String name)
{
myuser = name;
myuserHand = Arrays.copyOf(myuserHand, myuserHand.length + 2);
myuserHand[0] = new myCard();
myuserHand[1] = new myCard();
System.out.println("U get a " + myuserHand[0].getmyValue() + " & a " + myuserHand[1].getmyValue() + ".");
int mytotal = getmyTotal();
System.out.println("Ur total: " + mytotal + " ");
}
@Override
public void gameStart()
{
System.out.println("Ur turn!");
}
@Override
public int getmyTotal()
{
int mytotalValue = 0;
for (myCard c : myuserHand)
{
int mycardValue = c.getmyValue();
mytotalValue = mytotalValue + mycardValue;
}
return mytotalValue;
}
@Override
public void drawmyCard()
{
myuserHand = Arrays.copyOf(myuserHand, myuserHand.length + 1);
myuserHand[myuserHand.length - 1] = new myCard();
int lastCard = myuserHand.length - 1;
System.out.println("U drew a " + myuserHand[lastCard].getmyValue());
}
@Override
public boolean want2Stay()
{
Boolean mystay = null;
while(mystay == null)
{
Scanner newSC = new Scanner(System.in);
System.out.print("U like to hit or stay?");
String myIn = newSC.nextLine();
String stayOrHit = myIn.toLowerCase();
switch(stayOrHit)
{
case "hit":
mystay = false;
break;
case "stay":
mystay = true;
break;
default:
System.out.println("Not valid");
break;
}
}
return mystay;
}
@Override
public String toString()
{
return myuser;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.