Repository navigation
Expand file tree
/
Copy pathWar.java
More file actions
152 lines (143 loc) · 6.38 KB
/
Copy pathWar.java
File metadata and controls
152 lines (143 loc) · 6.38 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.lang.reflect.Array;
import java.util.ArrayList;
/**
* War game class
*
* @author Mr. Jaffe
* @version 2022-10-19
*/
public class War
{
/**
* Constructor for the game
* Include your initialization here -- card decks, shuffling, etc
* Run the event loop after you've done the initializations
*/
private Deck player1;
private Deck player2;
//ArrayList dealtList1 = new ArrayList();
//ArrayList dealtList2 = new ArrayList();
public War()
{
// Initializations here...
Deck deck = new Deck();
deck.initializeNewDeck();
deck.shuffle();
Deck[] halves = deck.dealDeck();
player1 = halves[0];
player2 = halves[1];
// ...then run the event loop
this.runEventLoop();
}
/**
* This is the game's event loop. The code in here should come
* from the War flowchart you created for this game
*/
public void runEventLoop() {
try{
while (player1.getDeckSize() > 0 && player2.getDeckSize() > 0) {
Card card1 = player1.dealCardFromDeck();
Card card2 = player2.dealCardFromDeck();
if (card1.getRank() > card2.getRank()) {
player1.addCardToDeck(card1);
player1.addCardToDeck(card2);
player1.shuffle();
player2.shuffle();
System.out.println("P1 won round");
System.out.println("P1: " + player1.getDeckSize());
System.out.println("P2: " + player2.getDeckSize());
} else if (card1.getRank() < card2.getRank()) {
player2.addCardToDeck(card1);
player2.addCardToDeck(card2);
player1.shuffle();
player2.shuffle();
System.out.println("P2 won round");
System.out.println("P1: " + player1.getDeckSize());
System.out.println("P2: " + player2.getDeckSize());
} else {
// ArrayList<Card> war1 = new ArrayList<Card>();
// ArrayList<Card> war2 = new ArrayList<Card>();
// war1.add(card1);
// war2.add(card2);
if(card1.getRank() == card2.getRank()) {
Card first1 = player1.dealCardFromDeck();
Card first2 = player1.dealCardFromDeck();
Card second1 = player1.dealCardFromDeck();
Card second2 = player1.dealCardFromDeck();
System.out.println("WAR");
Card third1 = player1.dealCardFromDeck();
Card third2 = player2.dealCardFromDeck();
if(third1.getRank() > third2.getRank()) {
player1.addCardToDeck(card1);
player1.addCardToDeck(card2);
player1.addCardToDeck(first1);
player1.addCardToDeck(first2);
player1.addCardToDeck(second1);
player1.addCardToDeck(second2);
player1.addCardToDeck(third1);
player1.addCardToDeck(third2);
player1.shuffle();
player2.shuffle();
System.out.println("This is p1");
System.out.println("pog");
System.out.println("P1: " + player1.getDeckSize());
System.out.println("P2: " + player2.getDeckSize());
System.out.println("P1 has won the war");
} else if(third1.getRank() < third2.getRank()) {
player2.addCardToDeck(card1);
player2.addCardToDeck(card2);
player2.addCardToDeck(first1);
player2.addCardToDeck(first2);
player2.addCardToDeck(second1);
player2.addCardToDeck(second2);
player2.addCardToDeck(third1);
player2.addCardToDeck(third2);
player1.shuffle();
player2.shuffle();
System.out.println("This is p2");
System.out.println("pog");
System.out.println("P1: " + player1.getDeckSize());
System.out.println("P2: " + player2.getDeckSize());
System.out.println("P2 has won the war");
} else {
player1.addCardToDeck(card1);
player2.addCardToDeck(card2);
player1.addCardToDeck(first1);
player2.addCardToDeck(first2);
player1.addCardToDeck(second1);
player2.addCardToDeck(second2);
player1.addCardToDeck(third1);
player2.addCardToDeck(third2);
System.out.println("TIE");
continue;
}
}
if (player1.getDeckSize() <= 0) {
System.out.println("P1 Loser");
}
if (player2.getDeckSize() <= 0) {
System.out.println("P2 Loser");
}
}
}
} catch(Exception IndexOutOfBoundsException){
if(player1.getDeckSize() == 0){
System.out.println("P2 has won the war");
System.out.println("P1: "+player1.getDeckSize());
System.out.println("P2: "+player2.getDeckSize());
System.out.println("P1 Loser");
} else if(player2.getDeckSize() == 0){
System.out.println("P1 has own the war");
System.out.println("P1: "+player1.getDeckSize());
System.out.println("P2: "+player2.getDeckSize());
System.out.println("P2 Loser");
}
}
}
/**
* The main method is called when Java starts your program
*/
public static void main(String[] args) {
War war = new War();
}
}