June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,21 +1,19 @@
text
computer_play(record plays, record beats)
{
integer a, total;
integer a, c, total;
text s;
total = r_q_integer(plays, "rock") + r_q_integer(plays, "paper")
+ r_q_integer(plays, "scissors");
total = plays["rock"] + plays["paper"] + plays["scissors"];
a = drand(total - 1);
r_first(plays, s);
do {
if (a < r_q_integer(plays, s)) {
for (s, c in plays) {
if (a < c) {
break;
}
a -= plays[s];
} while (rsk_greater(plays, s, s));
a -= c;
}
return r_q_text(beats, s);
beats[s];
}
integer
@ -26,26 +24,25 @@ main(void)
file f;
text s;
computer = 0;
human = 0;
computer = human = 0;
f_affix(f, "/dev/stdin");
f.stdin;
r_put(beats, "rock", "paper");
r_put(beats, "paper", "scissors");
r_put(beats, "scissors", "rock");
beats["rock"] = "paper";
beats["paper"] = "scissors";
beats["scissors"] = "rock";
r_put(plays, "rock", 1);
r_put(plays, "paper", 1);
r_put(plays, "scissors", 1);
plays["rock"] = 1;
plays["paper"] = 1;
plays["scissors"] = 1;
while (1) {
o_text("Your choice [rock/paper/scissors]:\n");
if (f_line(f, s) == -1) {
if (f.line(s) == -1) {
break;
}
if (!r_key(plays, s)) {
if (!plays.key(s)) {
o_text("Invalid choice, try again\n");
} else {
text c;
@ -54,9 +51,9 @@ main(void)
o_form("Human: ~, Computer: ~\n", s, c);
if (!compare(s, c)) {
if (s == c) {
o_text("Draw\n");
} elif (!compare(c, beats[s])) {
} elif (c == beats[s]) {
computer += 1;
o_text("Computer wins\n");
} else {
@ -64,7 +61,7 @@ main(void)
o_text("Human wins\n");
}
r_up(plays, s);
plays.up(s);
o_form("Score: Human: ~, Computer: ~\n", human, computer);
}