-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
172 lines (142 loc) · 4.47 KB
/
Copy pathGame.java
File metadata and controls
172 lines (142 loc) · 4.47 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
import java.util.Scanner;
import java.util.Random;
public class Game
{
public static void main(String[] args)
{
//State the instructions
printInstructions();
//create input for user
//user plays game
gameCode(askUser());
}
public static void printInstructions()
{
System.out.println("Pershult beats Klyket, Skadis");
System.out.println("Klyket beats Tjusig, Hovolm");
System.out.println("Tjusig beats Pershult, Skadis");
System.out.println("Skadis Beats Hovolm, Klyket");
System.out.println("Hovolm beats Pershult, Tjusig");
}
public static String askUser()
{
Scanner scan = new Scanner(System.in);
//prompt user about playing
System.out.print("Would you like to play? ");
String user = scan.nextLine();
while(user.equals("y") != true && user.equals("n") != true)
{
System.out.print("Error: invalid entry. Please enter y or n.\n");
System.out.print("Would you like to play? ");
user = scan.nextLine();
}
return user;
}
public static void gameCode(String userAnswer)
{
int gameCount = 0;
int userWinCount = 0;
int compWinCount = 0;
int roundsPlayed = 0;
String compTurn;
String userTurn;
Scanner scan = new Scanner(System.in);
Random rand = new Random();
while(userAnswer.equals("y"))
{
System.out.print("Enter a move: ");
userTurn = scan.nextLine();
while(checkUserTurn(userTurn) != true)
{
System.out.print("Error: invaild entry. Please enter one of the five moves.\n");
System.out.print("Enter a move: ");
userTurn = scan.nextLine();
}
System.out.println("Your move is " + userTurn);
int compNum = rand.nextInt(5) + 1;
compTurn = computerTurn(compNum);
System.out.println("The computer picked " + compTurn);
if(whoWins(userTurn, compTurn))
userWinCount++;
else
compWinCount++;
roundsPlayed++;
System.out.print("Would you like to play again? ");
userAnswer = "";
userAnswer = scan.nextLine();
}
results(roundsPlayed, userWinCount, compWinCount);
}
public static boolean checkUserTurn(String turn)
{
String check = turn.toLowerCase();
if(check.equals("Skadis".toLowerCase()) == false && check.equals("Tjusig".toLowerCase()) == false &&
check.equals("Klyket".toLowerCase()) == false && check.equals("Hovolm".toLowerCase()) == false &&
check.equals("Pershult".toLowerCase()) == false)
return false;
return true;
}
public static String computerTurn(int num)
{
String compWord = "";
if(num == 1)
compWord = "Skadis".toLowerCase();
else if(num == 2)
compWord = "Tjusig".toLowerCase();
else if(num == 3)
compWord = "Klyket".toLowerCase();
else if(num == 4)
compWord = "Hovolm".toLowerCase();
else if(num == 5)
compWord = "Pershult".toLowerCase();
return compWord;
}
public static boolean whoWins(String userTurn, String compTurn)
{
if(userTurn.toLowerCase().equals("Pershult".toLowerCase()) && compTurn.equals("Klyket".toLowerCase()) || compTurn.equals("Skadis".toLowerCase()))
{
userWon();
return true;
}
else if(userTurn.toLowerCase().equals("Klyket".toLowerCase()) && compTurn.equals("Tjusig".toLowerCase()) || compTurn.equals("Hovolm".toLowerCase()))
{
userWon();
return true;
}
else if(userTurn.toLowerCase().equals("Tjusig".toLowerCase()) && compTurn.equals("Pershult".toLowerCase()) || compTurn.equals("Skadis".toLowerCase()))
{
userWon();
return true;
}
else if(userTurn.toLowerCase().equals("Skadis".toLowerCase()) && compTurn.equals("Hovolm".toLowerCase()) || compTurn.equals("Klyket".toLowerCase()))
{
userWon();
return true;
}
else if(userTurn.toLowerCase().equals("Hovolm".toLowerCase()) && compTurn.equals("Pershult".toLowerCase()) || compTurn.equals("Tjusig".toLowerCase()))
{
userWon();
return true;
}
else if(userTurn.toLowerCase().equals(compTurn.toLowerCase()))
{
System.out.println("Better luck next time");
return false;
}
else
{
System.out.println("Better luck next time");
return false;
}
}
public static void userWon()
{
System.out.println("Congradulations! You won!");
}
public static void results(int rounds, int userWin, int compWin)
{
System.out.println("You played " + rounds + " games");
System.out.println("You won " + userWin +" times");
System.out.println("You lost " + compWin + " times");
}
}