71 lines
1.3 KiB
Text
71 lines
1.3 KiB
Text
text
|
|
computer_play(record plays, record beats)
|
|
{
|
|
integer a, c, total;
|
|
text s;
|
|
|
|
total = plays["rock"] + plays["paper"] + plays["scissors"];
|
|
a = drand(total - 1);
|
|
for (s, c in plays) {
|
|
if (a < c) {
|
|
break;
|
|
}
|
|
a -= c;
|
|
}
|
|
|
|
beats[s];
|
|
}
|
|
|
|
integer
|
|
main(void)
|
|
{
|
|
integer computer, human;
|
|
record beats, plays;
|
|
file f;
|
|
text s;
|
|
|
|
computer = human = 0;
|
|
|
|
f.stdin;
|
|
|
|
beats["rock"] = "paper";
|
|
beats["paper"] = "scissors";
|
|
beats["scissors"] = "rock";
|
|
|
|
plays["rock"] = 1;
|
|
plays["paper"] = 1;
|
|
plays["scissors"] = 1;
|
|
|
|
while (1) {
|
|
o_text("Your choice [rock/paper/scissors]:\n");
|
|
if (f.line(s) == -1) {
|
|
break;
|
|
}
|
|
|
|
if (!plays.key(s)) {
|
|
o_text("Invalid choice, try again\n");
|
|
} else {
|
|
text c;
|
|
|
|
c = computer_play(plays, beats);
|
|
|
|
o_form("Human: ~, Computer: ~\n", s, c);
|
|
|
|
if (s == c) {
|
|
o_text("Draw\n");
|
|
} elif (c == beats[s]) {
|
|
computer += 1;
|
|
o_text("Computer wins\n");
|
|
} else {
|
|
human += 1;
|
|
o_text("Human wins\n");
|
|
}
|
|
|
|
plays.up(s);
|
|
|
|
o_form("Score: Human: ~, Computer: ~\n", human, computer);
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|