-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRockPaperScissors.java
65 lines (51 loc) · 1.89 KB
/
RockPaperScissors.java
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
import comp102x.IO;
import comp102x.Canvas;
/**
* This is the main game class for the game Rock-Paper-Scissors
*/
public class RockPaperScissors
{
/**
* This is the entry point of the game - run it to play the game
*/
public static void main(String args[])
{
//create the canvas which will have player's choice and computer's choice drawn on it
Canvas canvas = new Canvas(480, 960);
//create the Player object
Player player = new Player();
//create the Computer object
Computer computer = new Computer();
do {
// clear the canvas
canvas.removeAll();
//make the choices
player.makeChoice();
computer.makeChoice();
//show the choices
player.showChoice(canvas);
computer.showChoice(canvas);
//get the game result by comparing the player's choice with the computer's choice
int gameResult = player.getChoice().compareWith(computer.getChoice());
//show the appropriate gameover message based on the game result
switch(gameResult)
{
case 1: //1 indicates a win
IO.outputln("You won!");
break;
case -1: //-1 indicates a lose
IO.outputln("You lost!");
break;
case 0: //0 indicates a draw
IO.outputln("Draw!");
break;
}
} while (player.playAgain());
}
public int compareWith(Choice anotherCh){
Choice choice1 = new Choice(0); // Rock
Choice choice2 = new Choice(1); // Paper
int result = choice1.compareWith(choice2); // -1
return result;
}
}