-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathGame.java
More file actions
175 lines (155 loc) · 5.87 KB
/
Copy pathGame.java
File metadata and controls
175 lines (155 loc) · 5.87 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.polymars.game;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class Game {
public static final int WIN = 0;
public static final int LOSE = 1;
public static final int TIE = 2;
static final int MAX_HAND = 5;
static final ArrayList<String> elements = new ArrayList<>(Arrays.asList("fire", "water", "snow"));
static boolean newGame = true;
static boolean startMatch = false;
static Deck deck;
static Hand playerHand;
static Bank playerBank;
static Hand opponentHand;
static Bank opponentBank;
static Card currentPlayerCard = null;
static Card currentOpponentCard = null;
static Random rand = new Random();
public static String game(String input)
{
if (newGame)
{
newGame = false;
deck = new Deck();
deck.createCards();
playerHand = new Hand();
createHand(playerHand);
playerBank = new Bank();
opponentHand = new Hand();
createHand(opponentHand);
opponentBank = new Bank();
return "Welcome to Card Jitsu Voice. Would you like to start a match against an AI opponent?";
}
if (!startMatch)
{
if (input.equals("yes"))
{
startMatch = true;
return "Choose a card from your hand to play first. Your hand is made up of " + playerHand.toString() + ".";
}
else if (input.equals("no"))
{
newGame = true;
return "Alright. Feel free to start a match at any time!";
}
else {
return "I didn't get that. Answer yes or no.";
}
}
if (input.equals("stop"))
{
newGame = true;
startMatch = false;
return "Thanks for playing!";
}
if (input.equals("playerbank"))
{
if (playerBank.toString().equals(""))
{
return "Your bank currently is empty.";
}
return "Your bank currently has " + playerBank.toString() + ".";
}
if (input.equals("opponentbank"))
{
if (opponentBank.toString().equals(""))
{
return "Your opponent's bank currently is empty.";
}
return "Your opponent's bank currently has " + opponentBank.toString() + ".";
}
if (input.equals("hand"))
{
return "You currently have " + playerHand.toString() + " in your hand.";
}
String[] args = input.split("\\s+");
if (args.length == 2 && elements.contains(args[0].toLowerCase()) && !Float.isNaN(Integer.parseInt(args[1])) && Integer.parseInt(args[1]) > 1 && Integer.parseInt(args[1]) < 13)
{
currentPlayerCard = playerHand.useCard(elements.indexOf(args[0].toLowerCase()), Integer.parseInt(args[1]));
if (currentPlayerCard == null)
{
return "You don't have that card! Try picking a new one. You currently have " + playerHand.toString() + ".";
}
playerHand.addCard(deck.deal());
currentOpponentCard = opponentHand.useCard(rand.nextInt(MAX_HAND));
opponentHand.addCard(deck.deal());
String response = "";
switch (wonRound(currentPlayerCard, currentOpponentCard))
{
case WIN:
playerBank.addCard(currentPlayerCard);
response = ("Your " + currentPlayerCard + " beat your opponent's " + currentOpponentCard + ".");
break;
case LOSE:
opponentBank.addCard(currentOpponentCard);
response = ("Your " + currentPlayerCard + " lost against your opponent's " + currentOpponentCard + ".");
break;
case TIE:
response = ("Your " + currentPlayerCard + " tied with your opponent's " + currentOpponentCard + ".");
break;
}
if (playerBank.hasWon())
{
newGame = true;
startMatch = false;
return response + " You won, with " + playerBank.getWinningComboAsString() + "! Feel free to play again at any time.";
}
if (opponentBank.hasWon())
{
newGame = true;
startMatch = false;
return response + " You lost. Your opponent had " + opponentBank.getWinningComboAsString() + ". Feel free to play again at any time.";
}
return response + " Now pick a new card from your hand. You currently have " + playerHand.toString() + ".";
}
else
{
return "Sorry, I didn't get that. Try picking a card again. You currently have " + playerHand.toString() + ".";
}
}
static void createHand(Hand hand)
{
for (int i = 0; i < MAX_HAND; i++)
{
hand.addCard(deck.deal());
}
}
static int wonRound(Card playerCard, Card opponentCard)
{
if (isTypeAdvantage(playerCard.getElement(), opponentCard.getElement()))
{
return WIN;
}
if (isTypeAdvantage(opponentCard.getElement(), playerCard.getElement()))
{
return LOSE;
}
//elements must be the same (i hope)
if (playerCard.getValue() == opponentCard.getValue())
{
return TIE;
}
if (playerCard.getValue() > opponentCard.getValue())
{
return WIN;
}
return LOSE;
}
static boolean isTypeAdvantage(int elementX, int elementY)
{
return (elementX == Cards.FIRE && elementY == Cards.SNOW) || (elementX == Cards.WATER && elementY == Cards.FIRE) || (elementX == Cards.SNOW && elementY == Cards.WATER);
}
}