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
|
package Q2;
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissorsLizardSpock {
private int consecutiveWins = 0;
private int lastWinner;
public static final int LIZARD = 4;
public static final int PAPER = 2;
public static final int PLAYER1 = 1;
public static final int PLAYER2 = 2;
public static final int ROCK = 1;
public static final int SCISSORS = 3;
public static final int SPOCK = 5;
public int getConsecutiveWins() {
return this.consecutiveWins;
}
public int getLastWinner() {
return this.lastWinner;
}
public int random() {
return new Random().nextInt(4) + 1;
}
public void play(int player1, int player2) {
int player1Roll = random();
int player2Roll = random();
int consecutiveWins = 0;
int round = 0;
this.puts("Staring a new game of Rock, paper, scissors, lizard, spock...");
while (true) {
round++;
this.newline();
this.puts("Round: %d", round);
this.puts("Player 1: %d", player1Roll);
this.puts("Player 2: %d", player2Roll);
int winner = this.determineWinner(player1Roll, player2Roll);
this.puts("The winner of this round is player %d!", winner);
if (this.lastWinner == winner) {
this.consecutiveWins++;
} else {
this.lastWinner = winner;
this.consecutiveWins = 1;
}
this.puts("Player %d has %d consecutive wins.", this.lastWinner, this.consecutiveWins);
if (this.consecutiveWins == 4) {
this.newline();
this.puts("***********************************");
this.puts("The winner of the game is player %d!", this.lastWinner);
this.puts("***********************************");
this.newline();
return;
}
}
}
public static String convert(int i) {
return String.format("Player %d", i);
}
private int determineWinner(int player1Roll, int player2Roll) {
if (player1Roll > player2Roll) {
return PLAYER1;
}
return PLAYER2;
}
private void puts(String message, Object... args) {
System.out.println(String.format(message, args));
}
private void newline() {
System.out.println();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
RockPaperScissorsLizardSpock game = new RockPaperScissorsLizardSpock();
game.play(RockPaperScissorsLizardSpock.PLAYER1, RockPaperScissorsLizardSpock.PLAYER2);
}
}
|