Rock-paper-scissors: Difference between revisions
Content added Content deleted
Line 185: | Line 185: | ||
computer_play(record plays, record beats) |
computer_play(record plays, record beats) |
||
{ |
{ |
||
integer a, total; |
integer a, c, total; |
||
text s; |
text s; |
||
total = |
total = plays["rock"] + plays["paper"] + plays["scissors"]; |
||
+ r_q_integer(plays, "scissors"); |
|||
a = drand(total - 1); |
a = drand(total - 1); |
||
for (s, c in plays) { |
|||
if (a < c) { |
|||
if (a < r_q_integer(plays, s)) { |
|||
break; |
break; |
||
} |
} |
||
a -= |
a -= c; |
||
} |
|||
} while (rsk_greater(plays, s, s)); |
|||
beats[s]; |
|||
} |
} |
||
Line 210: | Line 208: | ||
text s; |
text s; |
||
computer = 0; |
computer = human = 0; |
||
human = 0; |
|||
f.stdin; |
|||
beats["rock"] = "paper"; |
|||
beats["paper"] = "scissors"; |
|||
beats["scissors"] = "rock"; |
|||
plays["rock"] = 1; |
|||
plays["paper"] = 1; |
|||
plays["scissors"] = 1; |
|||
while (1) { |
while (1) { |
||
o_text("Your choice [rock/paper/scissors]:\n"); |
o_text("Your choice [rock/paper/scissors]:\n"); |
||
if |
if (f.line(s) == -1) { |
||
break; |
break; |
||
} |
} |
||
if (! |
if (!plays.key(s)) { |
||
o_text("Invalid choice, try again\n"); |
o_text("Invalid choice, try again\n"); |
||
} else { |
} else { |
||
Line 238: | Line 235: | ||
o_form("Human: ~, Computer: ~\n", s, c); |
o_form("Human: ~, Computer: ~\n", s, c); |
||
if |
if (s == c) { |
||
o_text("Draw\n"); |
o_text("Draw\n"); |
||
} elif |
} elif (c == beats[s]) { |
||
computer += 1; |
computer += 1; |
||
o_text("Computer wins\n"); |
o_text("Computer wins\n"); |
||
Line 248: | Line 245: | ||
} |
} |
||
plays.up(s); |
|||
o_form("Score: Human: ~, Computer: ~\n", human, computer); |
o_form("Score: Human: ~, Computer: ~\n", human, computer); |