-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWar.java
More file actions
102 lines (90 loc) · 2.4 KB
/
Copy pathWar.java
File metadata and controls
102 lines (90 loc) · 2.4 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
package Java;
import java.util.Scanner;
@SuppressWarnings("unused")
public class War implements Play {
private static Scanner scanner = new Scanner(System.in);
private static boolean done,tie;
private static int playerTotal,dealerTotal,playerPoints,dealerPoints,points;
private static Deck deck;
private static String input;
@Override
public void explainRules() {
// TODO Auto-generated method stub
System.out.println("Draw card");
}
@Override
public void setUp() {
// TODO Auto-generated method stub
deck = new Deck();
deck.shuffle();
done = tie = false;
playerPoints = dealerPoints = playerTotal = dealerTotal = 0; points = 1;
}
@Override
public void playGame() {
// TODO Auto-generated method stub
explainRules();
setUp();
while(!done){
while(true){
System.out.println("\nPlayer points:"+playerPoints);
System.out.println("Dealer points:"+dealerPoints);
System.out.print("\nDraw:");
input = scanner.next();
if(input.toLowerCase().equals("y")){
break;
}
else if(input.toLowerCase().equals("n")){
done=true;
break;
}
System.out.println("Not a valid input.");
}if(done){System.out.print("\n");break;}
try{
System.out.print("Player: ");
playerTotal = deck.dealRaw();
System.out.print("Dealer: ");
dealerTotal = deck.dealRaw();
}
catch(IndexOutOfBoundsException e){
System.out.println("\n\nOut of cards!\n");
done=true;
break;
}
if(playerTotal>dealerTotal){
playerPoints+=points;
points = 1;
System.out.println("You won!");
}
else if(playerTotal<dealerTotal){
dealerPoints+=points;
points = 1;
System.out.println("You lost!");
}
else{
points+=3;
tie=true;
System.out.println("Tie!");
}
}
if(playerPoints>dealerPoints){
System.out.println("Player points:"+playerPoints);
System.out.println("Dealer points:"+dealerPoints+"\n");
System.out.println("You beat the dealer!");
}
else if(playerPoints<dealerPoints){
System.out.println("Player points:"+playerPoints);
System.out.println("Dealer points:"+dealerPoints+"\n");
System.out.println("Dealer beat you!");
}
else{
System.out.println("Player points:"+playerPoints);
System.out.println("Dealer points:"+dealerPoints+"\n");
System.out.println("Player and dealer tied!");
}
}
@Override
public void gameOver(String winner) {
// TODO Auto-generated method stub
}
}