// The "BlackJack" class.
import java.awt.*;
import hsa.Console;
public class BlackJack
{
static Console c; // The output console
public static int GetSuite ()
{
int y;
y = (int) ((Math.random () * 4) + 1) * 15;
return y;
}
public static int GetNumber ()
{
int x;
x = (int) ((Math.random () * 13) + 1);
return x;
}
public static void DrawClubs (int x, int y)
{
int trianglex[] = {x + 2, x + 6, x + 10};
int triangley[] = {y + 16, y + 10, y + 16};
c.setColor (Color.black);
c.fillOval (x + 0, y + 5, 8, 8);
c.fillOval (x + 5, y + 5, 8, 8);
c.fillOval (x + 2, y, 8, 8);
c.fillPolygon (trianglex, triangley, 3);
}
public static void DrawSpades (int x, int y)
{
int trianglex[] = {x + 2, x + 6, x + 10};
int triangley[] = {y + 16, y + 10, y + 16};
int diamondx[] = {x, x + 6, x + 12};
int diamondy[] = {y + 6, y, y + 6};
c.setColor (Color.black);
c.fillOval (x + 0, y + 5, 8, 8);
c.fillOval (x + 5, y + 5, 8, 8);
c.fillPolygon (trianglex, triangley, 3);
c.fillPolygon (diamondx, diamondy, 3);
}
public static void DrawHearts (int x, int y)
{
int diamondx[] = {x + 7, x + 2, x + 7, x + 12};
int diamondy[] = {y + 4, y + 9, y + 14, y + 9};
c.setColor (Color.red);
c.fillOval (x + 0, y + 0, 10, 10);
c.fillOval (x + 5, y + 0, 10, 10);
c.fillPolygon (diamondx, diamondy, 4);
}
public static void DrawDiamonds (int x, int y)
{
int diamondx[] = {x + 7, x, x + 7, x + 15};
int diamondy[] = {y, y + 7, y + 15, y + 7};
c.setColor (Color.red);
c.fillPolygon (diamondx, diamondy, 4);
}
public static void DrawCard (int NumOfCards, int Suite, int Num)
{
int x, y = 60;
x = (NumOfCards * 155);
c.setColor (new Color (130, 90, 30));
c.drawRoundRect (x, 25, 140, 190, 10, 10);
if (Suite == 15)
DrawSpades (x + 70, y);
else if (Suite == 30)
DrawHearts (x + 70, y);
else if (Suite == 45)
DrawClubs (x + 70, y);
else if (Suite == 60)
DrawDiamonds (x + 70, y);
c.setFont (new Font ("VERDANA", Font.BOLD, 100));
if (Num == 10)
c.drawString (" " + Num, (x - 35), 200);
else if (Num == 1)
c.drawString ("A", (x + 35), 200);
else if (Num == 13)
c.drawString ("K", (x + 35), 200);
else if (Num == 12)
c.drawString ("Q", (x + 30), 180);
else if (Num == 11)
c.drawString ("J", (x + 45), 200);
else
c.drawString (" " + Num, (x + 7), 200);
}
public static void DrawCard_Ash (int NumOfCards_Ash, int Suite, int Num)
{
int x, y = 285;
x = (NumOfCards_Ash * 155);
c.setColor (new Color (130, 90, 30));
c.drawRoundRect (x, 265, 140, 190, 10, 10);
if (Suite == 15)
DrawSpades (x + 70, y);
else if (Suite == 30)
DrawHearts (x + 70, y);
else if (Suite == 45)
DrawClubs (x + 70, y);
else if (Suite == 60)
DrawDiamonds (x + 70, y);
c.setFont (new Font ("VERDANA", Font.BOLD, 100));
if (Num == 10)
c.drawString (" " + Num, (x - 35), 440);
else if (Num == 1)
c.drawString ("A", (x + 35), 440);
else if (Num == 13)
c.drawString ("K", (x + 35), 440);
else if (Num == 12)
c.drawString ("Q", (x + 30), 420);
else if (Num == 11)
c.drawString ("J", (x + 45), 440);
else
c.drawString (" " + Num, (x + 7), 440);
}
public static int GetCard (int NumOfCards)
{
int[] PreviousCard = new int [100];
int Num, Suite, NewCard, Card = 0, i = 0;
do
{
Num = GetNumber ();
Suite = GetSuite ();
NewCard = Num + Suite;
}
while (NewCard == PreviousCard [i]);
PreviousCard [i] = NewCard;
i++;
DrawCard (NumOfCards, Suite, Num);
if (Num > 10)
Card = 11;
else
Card = Num;
return Card;
}
public static int GetAsh_Card (int NumOfCards_Ash)
{
int[] PreviousCard = new int [100];
int Num, Suite, NewCard, Ash_Card = 0, i = 0;
do
{
Num = GetNumber ();
Suite = GetSuite ();
NewCard = Num + Suite;
}
while (NewCard == PreviousCard [i]);
DrawCard_Ash (NumOfCards_Ash, Suite, Num);
PreviousCard [i] = NewCard;
i++;
if (Num > 10)
Ash_Card = 11;
else
Ash_Card = Num;
return Ash_Card;
}
public static void cleartop ()
{
c.setColor (Color.black);
c.fillRect (0, 480, 1000, 60);
c.setFont (new Font ("VERDANA", Font.PLAIN, 30));
c.setColor (Color.yellow);
}
public static void clearbottom ()
{
c.setColor (Color.black);
c.fillRect (0, 540, 1000, 60);
c.setFont (new Font ("VERDANA", Font.PLAIN, 30));
c.setColor (Color.yellow);
}
public static void drawbar (int money, int CardValue, int amount)
{
c.setColor (Color.blue);
c.fillRect (0, 600, 1000, 100);
c.setFont (new Font ("TIMES NEW ROMAN", Font.BOLD, 30));
c.setColor (Color.white);
c.drawString ("Your Money: $" + money, 5, 635);
c.drawString ("Bet Amount: $" + amount, 300, 635);
c.drawString ("Player's value of cards:" + CardValue, 600, 635);
}
public static int Loss (int money, int amount)
{
money = money - amount;
return money;
}
public static int Win (int money, int amount)
{
money = money + amount;
return money;
}
public static int Bet (int money)
{
int amount = 0;
c.setFont (new Font ("VERDANA", Font.PLAIN, 30));
c.setColor (Color.yellow);
c.drawString ("\nHow much would you like to bet?(0 to not bet)", 5, 510);
c.setCursor (26, 92);
amount = c.readInt ();
cleartop ();
while (amount > money || amount < 0)
{
if (amount > money)
{
cleartop ();
c.setColor (Color.yellow);
c.drawString ("\nYou gotta earn it before you can bet it!", 5, 510);
}
if (amount < 0)
{
cleartop ();
c.setColor (Color.yellow);
c.drawString ("\nThere is no such thing as negative money bro!", 5, 510);
}
c.setColor (Color.yellow);
c.drawString ("\nHow much would you like to bet?(0 to not bet)", 5, 570);
c.setCursor (29, 92);
amount = c.readInt ();
clearbottom ();
}
cleartop ();
clearbottom ();
return amount;
}
public static void main (String[] args)
{
c = new Console (34, 120);
String name;
char NewGame;
do
{
int money = 1000;
char NewHand;
c.clear ();
c.setColor (Color.black);
c.fillRect (0, 0, 1000, 800);
c.setFont (new Font ("VERDANA", Font.BOLD, 50));
c.setColor (Color.green);
c.drawString ("Welcome to BlackJack!", 150, 200);
c.drawString ("Please enter your name: ", 150, 350);
c.setCursor (22, 42);
name = c.readString ();
do
{
c.clear ();
int Card = 0, Ash_Card = 0, CardValue = 0, Ash_CardValue = 0, amount = 0, NumOfCards, NumOfCards_Ash;
char Hit = 'N', Bet;
boolean win = false, loss = false;
c.setFont (new Font ("TIMES NEW ROMAN", Font.BOLD, 30));
c.setColor (Color.blue);
c.drawString (" " + name, 400, 20);
c.drawString ("ASH", 390, 245);
c.setColor (Color.black);
c.fillRect (0, 480, 1000, 120);
drawbar (money, CardValue, amount);
cleartop ();
c.drawString ("\nWould you like to place a bet? (y/n)", 5, 510);
Bet = c.getChar ();
while (Bet != 'y' && Bet != 'n' && Bet != 'Y' && Bet != 'N')
{
cleartop ();
c.drawString ("\nInvalid entry, please enter y or n", 5, 510);
clearbottom ();
c.drawString ("\nWould you like to place a bet? (y/n)", 5, 570);
Bet = c.getChar ();
clearbottom ();
}
drawbar (money, CardValue, amount);
cleartop ();
if (Bet == 'Y' || Bet == 'y')
amount = Bet (money);
for (NumOfCards = 0 ; NumOfCards < 2 ; NumOfCards++)
{
Card = GetCard (NumOfCards);
CardValue = CardValue + Card;
}
c.setColor (new Color (130, 90, 30));
c.fillRoundRect (5, 265, 140, 190, 10, 10);
c.fillRoundRect (155, 265, 140, 190, 10, 10);
c.setColor (Color.black);
c.fillRoundRect (165, 275, 120, 170, 10, 10);
c.fillRoundRect (15, 275, 120, 170, 10, 10);
c.setFont (new Font ("ALGERIAN", Font.PLAIN, 15));
c.setColor (Color.blue);
c.drawString ("BLACKJACK", 30, 360);
c.drawString ("BLACKJACK", 180, 360);
do
{
drawbar (money, CardValue, amount);
if (CardValue == 21)
{
cleartop ();
c.drawString ("\nBlackJack, You win!", 5, 510);
money = Win (money, amount);
win = true;
}
else if (CardValue >= 22)
{
cleartop ();
c.drawString ("\nOoops, you busted. Ash Wins!", 5, 510);
money = Loss (money, amount);
loss = true;
}
else
{
cleartop ();
c.drawString ("\nWould you like to take a hit? (y/n)", 5, 510);
Hit = c.getChar ();
if (Hit == 'y' || Hit == 'Y')
{
Card = GetCard (NumOfCards);
NumOfCards++;
CardValue = CardValue + Card;
}
}
}
while ((Hit == 'y' || Hit == 'Y') && (!win && !loss));
c.setColor (Color.white);
c.fillRect (0, 265, 295, 220);
cleartop ();
for (NumOfCards_Ash = 0 ; NumOfCards_Ash < 2 ; NumOfCards_Ash++)
{
Ash_Card = GetAsh_Card (NumOfCards_Ash);
Ash_CardValue = Ash_CardValue + Ash_Card;
}
if ((Ash_CardValue == 21) && (!win && !loss))
{
cleartop ();
c.drawString ("\nBlackJack for Ash, he wins", 5, 510);
money = Loss (money, amount);
loss = true;
}
else if ((Ash_CardValue > 21) && (!win && !loss))
{
cleartop ();
c.drawString ("\nBummer, Ash busted. You win!", 5, 510);
money = Win (money, amount);
win = true;
}
else if (Ash_CardValue <= CardValue && (!loss && !win))
{
while (Ash_CardValue <= 16 && (!loss && !win))
{
Ash_Card = GetAsh_Card (NumOfCards_Ash);
NumOfCards_Ash++;
Ash_CardValue = Ash_CardValue + Ash_Card;
if (Ash_CardValue > 21)
{
cleartop ();
c.drawString ("\nBummer, Ash busted. You win!", 5, 510);
money = Win (money, amount);
win = true;
}
}
}
if (!loss && !win)
{
if (Ash_CardValue > CardValue)
{
cleartop ();
c.drawString ("\nAsh has higher numbers than you, Ash wins!", 5, 510);
money = Loss (money, amount);
loss = true;
}
else if (Ash_CardValue == CardValue)
{
if (NumOfCards_Ash > NumOfCards)
{
cleartop ();
c.setFont (new Font ("VERDANA", Font.BOLD, 20));
c.drawString ("\nYou guys have the same numbers but Ash has more cards, so Ash wins!", 5, 510);
money = Loss (money, amount);
loss = true;
}
else
{
cleartop ();
c.drawString ("\nYou guys have the same numbers but " + name + " has more cards, so " + name + " wins!", 5, 510);
money = Win (money, amount);
win = true;
}
}
else
{
cleartop ();
c.drawString ("\nYou have higher numbers than Ash, You win!", 5, 510);
money = Win (money, amount);
win = true;
}
}
drawbar (money, CardValue, amount);
do
{
clearbottom ();
c.drawString ("\nWould you like to play a new hand?(y/n)", 5, 570);
NewHand = c.getChar ();
if (NewHand != 'y' && NewHand != 'n' && NewHand != 'Y' && NewHand != 'N')
{
cleartop ();
c.drawString ("\nInvalid entry, please enter y or n", 5, 510);
}
}
while (NewHand != 'y' && NewHand != 'n' && NewHand != 'Y' && NewHand != 'N');
}
while (money > 0 && (NewHand == 'y' || NewHand == 'Y'));
c.clear ();
c.setFont (new Font ("TIMES NEW ROMAN", Font.BOLD, 45));
c.setColor (Color.black);
c.fillRect (0, 0, 1000, 800);
c.setColor (Color.green);
if (NewHand == 'y' || NewHand == 'Y')
c.drawString ("You are Bankcrupt!", 20, 100);
c.drawString ("\nYour total money: $" + money, 20, 200);
c.drawString ("\nYour net Profit: $" + (money - 1000), 20, 300);
do
{
c.drawString ("\nWould you like to restart the whole game?(y/n)", 20, 500);
NewGame = c.getChar ();
if (NewGame != 'y' && NewGame != 'n' && NewGame != 'Y' && NewGame != 'N')
c.drawString ("\nInvalid input, please enter y or n", 20, 600);
}
while (NewGame != 'y' && NewGame != 'n' && NewGame != 'Y' && NewGame != 'N');
}
while (NewGame == 'y' || NewGame == 'Y')
;
c.clear ();
c.setFont (new Font ("TIMES NEW ROMAN", Font.BOLD, 50));
c.setColor (Color.white);
c.fillRect (0, 0, 1000, 800);
c.setColor (Color.blue);
c.drawString ("\n\nThank you for playing BlackJack!", 100, 100);
c.drawString ("Developed by Ashraf Abedin.", 120, 200);
c.drawString ("Hope you had fun!", 220, 300);
} // main method
} // BlackJack class