-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode
More file actions
114 lines (89 loc) · 2.66 KB
/
Code
File metadata and controls
114 lines (89 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// This code was wriiten in Ready to Program using Java:
import java.awt.*;
import hsa.Console;
public class LoopingAssignment
{
static Console c; // The output console
public static void main (String[] args)
{
c = new Console ();
String name;
int c1, c2, p1, p2, ctotal, ptotal, card1, card2;
char choice;
c1 = (int) (Math.random () * 10 + 1); // Compuers card 1
c2 = (int) (Math.random () * 10 + 1); // Compuers card 2
p1 = (int) (Math.random () * 10 + 1); // Persons card 1
p2 = (int) (Math.random () * 10 + 1); // Persons card 2
ctotal = c1 + c2; // Total of computers card
ptotal = p1 + p2; // Total of persons card
c.println ("Welcome to Blackjack!!!");
c.println ();
//Input data
c.println ("What is your name?");
name = c.readString ();
c.println ();
c.println ("Welcome to BlackJack " + name + ", are you destined to win?");
c.println ();
c.println ("Find out by playing Black Jack");
c.println ();
//Computers Crad Display:
c.println ("Computer X " + c2);
//Person Card Display:
c.println (name + " " + p1 + " " + p2);
c.println ();
// Input:
c.print ("Hit/Stick (h/s) ? ");
// h = person will darw a new card
// s = computer will darw a new card
choice = c.getChar ();
c.println (choice);
//While Loopig:
while (choice == 'h' && ptotal <= 21) // Will excute this code if the users wants to draw a card and their total is not already over a 21;
{
card1 = (int) (Math.random () * 10 + 1); // new card user draws
ptotal = ptotal + card1;
c.println ("You drew a " + card1); // Display
if (ptotal <= 21)
{
c.print ("Hit/Stick (h/s) ? ");
choice = c.getChar ();
c.println (choice);
}
}
if (ptotal > 21)
{
c.println ();
c.println ("You busted, You lose"); // Output
}
else // You didn't lose
{
while (ctotal <= 17) // compter will keep on drawing cards until its total is remain less thsn 17
{
card2 = (int) (Math.random () * 10 + 1); // computers new card
ctotal = ctotal + card2;
c.println ("Computer drew a " + card2 + " "); // Diaplay
}
if (ctotal > 21)
{
c.println ("Computer busted, you win! "); // Output
}
}
c.println ();
if (ctotal > 17 && ctotal <21 && ptotal<21) // Computer will excute this if the users and computer total are less than 21
{
if (ctotal < ptotal) // Computer won
{
c.println ("You win " + ptotal + " to " + ctotal);
}
if
(ptotal < ctotal) // Person won
{
c.println ("Computer win " + ctotal + " to " + ptotal);
}
if (ctotal == ptotal) // Tie Game
{
c.println ("It's a tie game ");
}
}
} // main method
} // LoopingAssignment class