June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,153 +1,89 @@
|
|||
main: (
|
||||
|
||||
# rock/paper/scissors game #
|
||||
|
||||
# counts of the number of times the player has chosen each move #
|
||||
# we initialise each to 1 so that the total isn't zero when we are #
|
||||
# choosing the computer's first move (as in the Ada version) #
|
||||
INT r count := 1;
|
||||
INT p count := 1;
|
||||
INT s count := 1;
|
||||
|
||||
# counts of how many games the player and computer have won #
|
||||
INT player count := 0;
|
||||
INT computer count := 0;
|
||||
|
||||
print( ( "rock/paper/scissors", newline, newline ) );
|
||||
|
||||
WHILE
|
||||
|
||||
CHAR player move;
|
||||
|
||||
# get the players move - r => rock #
|
||||
# p => paper #
|
||||
# s => scissors #
|
||||
# q => quit #
|
||||
|
||||
BEGIN
|
||||
# rock/paper/scissors game #
|
||||
# counts of the number of times the player has chosen each move #
|
||||
# we initialise each to 1 so that the total isn't zero when we are #
|
||||
# choosing the computer's first move (as in the Ada version) #
|
||||
INT r count := 1;
|
||||
INT p count := 1;
|
||||
INT s count := 1;
|
||||
# counts of how many games the player and computer have won #
|
||||
INT player count := 0;
|
||||
INT computer count := 0;
|
||||
print( ( "rock/paper/scissors", newline, newline ) );
|
||||
WHILE
|
||||
print( ( "Please enter your move (r/p/s) or q to quit: " ) );
|
||||
readf( ( $ a $, player move ) );
|
||||
read( ( newline ) );
|
||||
|
||||
( player move /= "r"
|
||||
AND player move /= "p"
|
||||
AND player move /= "s"
|
||||
AND player move /= "q"
|
||||
)
|
||||
CHAR player move;
|
||||
# get the players move - r => rock, p => paper, s => scissors #
|
||||
# q => quit #
|
||||
WHILE
|
||||
print( ( "Please enter your move (r/p/s) or q to quit: " ) );
|
||||
read( ( player move, newline ) );
|
||||
( player move /= "r"
|
||||
AND player move /= "p"
|
||||
AND player move /= "s"
|
||||
AND player move /= "q"
|
||||
)
|
||||
DO
|
||||
print( ( "Unrecognised move", newline ) )
|
||||
OD;
|
||||
# continue playing until the player chooses quit #
|
||||
player move /= "q"
|
||||
DO
|
||||
print( ( "Unrecognised move", newline ) )
|
||||
OD;
|
||||
|
||||
# continue playing until the player chooses quit #
|
||||
player move /= "q"
|
||||
|
||||
DO
|
||||
|
||||
# decide the computer's move based on the player's history #
|
||||
|
||||
CHAR computer move;
|
||||
|
||||
INT move count = r count + p count + s count;
|
||||
|
||||
# predict player will play rock if the random number #
|
||||
# is in the range 0 .. rock / total #
|
||||
# predict player will play paper if the random number #
|
||||
# is in the range rock / total .. ( rock + paper ) / total #
|
||||
# predict player will play scissors otherwise #
|
||||
|
||||
REAL r limit = r count / move count;
|
||||
REAL p limit = r limit + ( p count / move count );
|
||||
|
||||
REAL random move = next random;
|
||||
|
||||
IF random move < r limit
|
||||
THEN
|
||||
# we predict the player will choose rock - we choose paper #
|
||||
computer move := "p"
|
||||
|
||||
ELIF random move < p limit
|
||||
THEN
|
||||
# we predict the player will choose paper - we choose scissors #
|
||||
computer move := "s"
|
||||
|
||||
ELSE
|
||||
# we predict the player will choose scissors - we choose rock #
|
||||
computer move := "r"
|
||||
|
||||
FI;
|
||||
|
||||
print( ( "You chose: " + player move, newline ) );
|
||||
print( ( "I chose: " + computer move, newline ) );
|
||||
|
||||
IF player move = computer move
|
||||
THEN
|
||||
# both players chose the same - draw #
|
||||
print( ( "We draw", newline ) )
|
||||
|
||||
ELSE
|
||||
# players chose different moves - there is a winner #
|
||||
|
||||
BOOL player wins;
|
||||
|
||||
IF player move = "r"
|
||||
THEN
|
||||
# player chose rock - rock blunts scissors #
|
||||
# but is wrapped by paper #
|
||||
player wins := ( computer move = "s" )
|
||||
|
||||
ELIF player move = "p"
|
||||
THEN
|
||||
# player chose paper - paper wraps rock #
|
||||
# but is cut by scissors #
|
||||
player wins := ( computer move = "r" )
|
||||
|
||||
# decide the computer's move based on the player's history #
|
||||
CHAR computer move;
|
||||
INT move count = r count + p count + s count;
|
||||
# predict player will play rock if the random number #
|
||||
# is in the range 0 .. rock / total #
|
||||
# predict player will play paper if the random number #
|
||||
# is in the range rock / total .. ( rock + paper ) / total #
|
||||
# predict player will play scissors otherwise #
|
||||
REAL r limit = r count / move count;
|
||||
REAL p limit = r limit + ( p count / move count );
|
||||
REAL random move = next random;
|
||||
IF random move < r limit THEN
|
||||
# we predict the player will choose rock - we choose paper #
|
||||
computer move := "p"
|
||||
ELIF random move < p limit THEN
|
||||
# we predict the player will choose paper - we choose scissors #
|
||||
computer move := "s"
|
||||
ELSE
|
||||
# player chose scissors - scissors cut paper #
|
||||
# but are blunted by rock #
|
||||
player wins := ( computer move = "p" )
|
||||
|
||||
# we predict the player will choose scissors - we choose rock #
|
||||
computer move := "r"
|
||||
FI;
|
||||
|
||||
IF player wins
|
||||
THEN
|
||||
player count +:= 1;
|
||||
print( ( "You win", newline ) )
|
||||
|
||||
print( ( "You chose: " + player move, newline ) );
|
||||
print( ( "I chose: " + computer move, newline ) );
|
||||
IF player move = computer move THEN
|
||||
# both players chose the same - draw #
|
||||
print( ( "We draw", newline ) )
|
||||
ELSE
|
||||
computer count +:= 1;
|
||||
print( ( "I win", newline ) )
|
||||
FI;
|
||||
|
||||
print( ( ( "You won: "
|
||||
+ whole( player count , 0 )
|
||||
+ ", I won: "
|
||||
+ whole( computer count, 0 )
|
||||
# players chose different moves - there is a winner #
|
||||
IF ( player move = "r" AND computer move = "s" )
|
||||
OR ( player move = "p" AND computer move = "r" )
|
||||
OR ( player move = "s" AND computer move = "p" )
|
||||
THEN
|
||||
player count +:= 1;
|
||||
print( ( "You win", newline ) )
|
||||
ELSE
|
||||
computer count +:= 1;
|
||||
print( ( "I win", newline ) )
|
||||
FI;
|
||||
print( ( "You won: "
|
||||
, whole( player count , 0 )
|
||||
, ", I won: "
|
||||
, whole( computer count, 0 )
|
||||
, newline
|
||||
)
|
||||
)
|
||||
, newline
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
FI;
|
||||
|
||||
IF player move = "r"
|
||||
THEN
|
||||
# player chose rock #
|
||||
r count +:= 1
|
||||
|
||||
ELIF player move = "p"
|
||||
THEN
|
||||
# player chose paper #
|
||||
p count +:= 1
|
||||
|
||||
ELSE
|
||||
# player chose scissors #
|
||||
s count +:= 1
|
||||
|
||||
FI
|
||||
|
||||
OD;
|
||||
|
||||
print( ( "Thanks for a most enjoyable game", newline ) )
|
||||
|
||||
)
|
||||
FI;
|
||||
IF player move = "r" THEN
|
||||
# player chose rock #
|
||||
r count +:= 1
|
||||
ELIF player move = "p" THEN
|
||||
# player chose paper #
|
||||
p count +:= 1
|
||||
ELSE
|
||||
# player chose scissors #
|
||||
s count +:= 1
|
||||
FI
|
||||
OD;
|
||||
print( ( "Thanks for a most enjoyable game", newline ) )
|
||||
END
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,22 +1,15 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define LEN 3
|
||||
|
||||
int rand_i(int n)
|
||||
/* pick a random index from 0 to n-1, according to probablities listed
|
||||
in p[] which is assumed to have a sum of 1. The values in the probablity
|
||||
list matters up to the point where the sum goes over 1 */
|
||||
int rand_idx(double *p, int n)
|
||||
{
|
||||
int rand_max = RAND_MAX - (RAND_MAX % n);
|
||||
int ret;
|
||||
while ((ret = rand()) >= rand_max);
|
||||
return ret/(rand_max / n);
|
||||
}
|
||||
|
||||
int weighed_rand(int *tbl, int len)
|
||||
{
|
||||
int i, sum, r;
|
||||
for (i = 0, sum = 0; i < len; sum += tbl[i++]);
|
||||
if (!sum) return rand_i(len);
|
||||
|
||||
r = rand_i(sum) + 1;
|
||||
for (i = 0; i < len && (r -= tbl[i]) > 0; i++);
|
||||
double s = rand() / (RAND_MAX + 1.0);
|
||||
int i;
|
||||
for (i = 0; i < n - 1 && (s -= p[i]) >= 0; i++);
|
||||
return i;
|
||||
}
|
||||
|
||||
|
|
@ -27,9 +20,10 @@ int main()
|
|||
const char *names[] = { "Rock", "Paper", "Scissors" };
|
||||
char str[2];
|
||||
const char *winner[] = { "We tied.", "Meself winned.", "You win." };
|
||||
double p[LEN] = { 1./3, 1./3, 1./3 };
|
||||
|
||||
while (1) {
|
||||
my_action = (weighed_rand(user_rec, 3) + 1) % 3;
|
||||
my_action = rand_idx(p,LEN);
|
||||
|
||||
printf("\nYour choice [1-3]:\n"
|
||||
" 1. Rock\n 2. Paper\n 3. Scissors\n> ");
|
||||
|
|
@ -37,7 +31,10 @@ int main()
|
|||
/* scanf is a terrible way to do input. should use stty and keystrokes */
|
||||
if (!scanf("%d", &user_action)) {
|
||||
scanf("%1s", str);
|
||||
if (*str == 'q') return 0;
|
||||
if (*str == 'q') {
|
||||
printf("Your choices [rock : %d , paper : %d , scissors %d] ",user_rec[0],user_rec[1], user_rec[2]);
|
||||
return 0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
user_action --;
|
||||
|
|
|
|||
79
Task/Rock-paper-scissors/Kotlin/rock-paper-scissors.kotlin
Normal file
79
Task/Rock-paper-scissors/Kotlin/rock-paper-scissors.kotlin
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
// version 1.2.10
|
||||
|
||||
import java.util.Random
|
||||
|
||||
const val choices = "rpsq"
|
||||
val rand = Random()
|
||||
|
||||
var pWins = 0 // player wins
|
||||
var cWins = 0 // computer wins
|
||||
var draws = 0 // neither wins
|
||||
var games = 0 // games played
|
||||
val pFreqs = arrayOf(0, 0, 0) // player frequencies for each choice (rps)
|
||||
|
||||
fun printScore() = println("Wins: You $pWins, Computer $cWins, Neither $draws\n")
|
||||
|
||||
fun getComputerChoice(): Char {
|
||||
// make a completely random choice until 3 games have been played
|
||||
if (games < 3) return choices[rand.nextInt(3)]
|
||||
val num = rand.nextInt(games)
|
||||
return when {
|
||||
num < pFreqs[0] -> 'p'
|
||||
num < pFreqs[0] + pFreqs[1] -> 's'
|
||||
else -> 'r'
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
println("Enter: (r)ock, (p)aper, (s)cissors or (q)uit\n")
|
||||
while (true) {
|
||||
printScore()
|
||||
var pChoice: Char
|
||||
while (true) {
|
||||
print("Your choice r/p/s/q : ")
|
||||
val input = readLine()!!.toLowerCase()
|
||||
if (input.length == 1) {
|
||||
pChoice = input[0]
|
||||
if (pChoice in choices) break
|
||||
}
|
||||
println("Invalid choice, try again")
|
||||
}
|
||||
if (pChoice == 'q') {
|
||||
println("OK, quitting")
|
||||
return
|
||||
}
|
||||
val cChoice = getComputerChoice()
|
||||
println("Computer's choice : $cChoice")
|
||||
if (pChoice == 'r' && cChoice == 's') {
|
||||
println("Rock breaks scissors - You win!")
|
||||
pWins++
|
||||
}
|
||||
else if (pChoice == 'p' && cChoice == 'r') {
|
||||
println("Paper covers rock - You win!")
|
||||
pWins++
|
||||
}
|
||||
else if (pChoice == 's' && cChoice == 'p') {
|
||||
println("Scissors cut paper - You win!")
|
||||
pWins++
|
||||
}
|
||||
else if (pChoice == 's' && cChoice == 'r') {
|
||||
println("Rock breaks scissors - Computer wins!")
|
||||
cWins++
|
||||
}
|
||||
else if (pChoice == 'r' && cChoice == 'p') {
|
||||
println("Paper covers rock - Computer wins!")
|
||||
cWins++
|
||||
}
|
||||
else if (pChoice == 'p' && cChoice == 's') {
|
||||
println("Scissors cut paper - Computer wins!")
|
||||
cWins++
|
||||
}
|
||||
else {
|
||||
println("It's a draw!")
|
||||
draws++
|
||||
}
|
||||
pFreqs[choices.indexOf(pChoice)]++
|
||||
games++
|
||||
println()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,66 +1,60 @@
|
|||
math.randomseed(os.time()) -- Randomness needed for cpuMove function
|
||||
math.random()
|
||||
|
||||
function cpuMove()
|
||||
local totalChance = playerRecord.R + playerRecord.P + playerRecord.S
|
||||
if totalChance == 0 then -- First game, unweighted random
|
||||
local choice = math.random(1, 3)
|
||||
if choice == 1 then return "R" end
|
||||
if choice == 2 then return "P" end
|
||||
if choice == 3 then return "S" end
|
||||
end
|
||||
local choice = math.random(1, totalChance) -- Weighted random bit
|
||||
if choice <= playerRecord.R then return "P" end
|
||||
if choice <= playerRecord.R + playerRecord.P then return "S" end
|
||||
return "R"
|
||||
local totalChance = record.R + record.P + record.S
|
||||
if totalChance == 0 then -- First game, unweighted random
|
||||
local choice = math.random(1, 3)
|
||||
if choice == 1 then return "R" end
|
||||
if choice == 2 then return "P" end
|
||||
if choice == 3 then return "S" end
|
||||
end
|
||||
local choice = math.random(1, totalChance) -- Weighted random bit
|
||||
if choice <= record.R then return "P" end
|
||||
if choice <= record.R + record.P then return "S" end
|
||||
return "R"
|
||||
end
|
||||
|
||||
function playerMove() -- Get user input for choice of 'weapon'
|
||||
local choice
|
||||
repeat
|
||||
os.execute("cls") -- Windows specific command, change per OS
|
||||
print("\nRock, Paper, Scissors")
|
||||
print("=====================\n")
|
||||
print("Scores -\tPlayer:", score.player)
|
||||
print("\t\tCPU:", score.cpu .. "\n\t\tDraws:", score.draws)
|
||||
io.write("\nChoose [R]ock [P]aper or [S]cissors: ")
|
||||
choice = string.upper(string.sub(io.read(), 1, 1))
|
||||
until choice == "R" or choice == "P" or choice == "S"
|
||||
return choice
|
||||
local choice
|
||||
repeat
|
||||
os.execute("cls") -- Windows specific command, change per OS
|
||||
print("\nRock, Paper, Scissors")
|
||||
print("=====================\n")
|
||||
print("Scores -\tPlayer:", score.player)
|
||||
print("\t\tCPU:", score.cpu .. "\n\t\tDraws:", score.draws)
|
||||
io.write("\nChoose [R]ock [P]aper or [S]cissors: ")
|
||||
choice = io.read():upper():sub(1, 1)
|
||||
until choice == "R" or choice == "P" or choice == "S"
|
||||
return choice
|
||||
end
|
||||
|
||||
function checkWinner (c, p) -- Decide who won, increment scores
|
||||
io.write("I chose ")
|
||||
if c == "R" then print("rock...") end
|
||||
if c == "P" then print("paper...") end
|
||||
if c == "S" then print("scissors...") end
|
||||
if c == p then
|
||||
print("\nDraw!")
|
||||
score.draws = score.draws + 1
|
||||
elseif (c == "R" and p == "P") or
|
||||
(c == "P" and p == "S") or
|
||||
(c == "S" and p == "R") then
|
||||
print("\nYou win!")
|
||||
score.player = score.player + 1
|
||||
else
|
||||
print("\nYou lose!")
|
||||
score.cpu = score.cpu + 1
|
||||
end
|
||||
-- Decide who won, increment scores
|
||||
function checkWinner (c, p)
|
||||
io.write("I chose ")
|
||||
if c == "R" then print("rock...") end
|
||||
if c == "P" then print("paper...") end
|
||||
if c == "S" then print("scissors...") end
|
||||
if c == p then
|
||||
print("\nDraw!")
|
||||
score.draws = score.draws + 1
|
||||
elseif (c == "R" and p == "P") or
|
||||
(c == "P" and p == "S") or
|
||||
(c == "S" and p == "R") then
|
||||
print("\nYou win!")
|
||||
score.player = score.player + 1
|
||||
else
|
||||
print("\nYou lose!")
|
||||
score.cpu = score.cpu + 1
|
||||
end
|
||||
end
|
||||
|
||||
function updateRecord (move) -- Keep track of player move history
|
||||
if move == "R" then playerRecord.R = playerRecord.R + 1 end
|
||||
if move == "P" then playerRecord.P = playerRecord.P + 1 end
|
||||
if move == "S" then playerRecord.S = playerRecord.S + 1 end
|
||||
end
|
||||
|
||||
score = {player = 0, cpu = 0, draws = 0} -- Start of main procedure
|
||||
playerRecord = {R = 0, P = 0, S = 0}
|
||||
-- Main procedure
|
||||
math.randomseed(os.time())
|
||||
score = {player = 0, cpu = 0, draws = 0}
|
||||
record = {R = 0, P = 0, S = 0}
|
||||
local playerChoice, cpuChoice
|
||||
repeat
|
||||
cpuChoice = cpuMove()
|
||||
playerChoice = playerMove()
|
||||
updateRecord(playerChoice)
|
||||
checkWinner(cpuChoice, playerChoice)
|
||||
io.write("\nPress ENTER to continue or enter 'Q' to quit . . . ")
|
||||
until string.upper(string.sub(io.read(), 1, 1)) == "Q"
|
||||
cpuChoice = cpuMove()
|
||||
playerChoice = playerMove()
|
||||
record[playerChoice] = record[playerChoice] + 1
|
||||
checkWinner(cpuChoice, playerChoice)
|
||||
io.write("\nPress ENTER to continue or enter 'Q' to quit . . . ")
|
||||
until io.read():upper():sub(1, 1) == "Q"
|
||||
|
|
|
|||
28
Task/Rock-paper-scissors/PHP/rock-paper-scissors.php
Normal file
28
Task/Rock-paper-scissors/PHP/rock-paper-scissors.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
echo "<h1>" . "Choose: ROCK - PAPER - SCISSORS" . "</h1>";
|
||||
echo "<h2>";
|
||||
echo "";
|
||||
|
||||
$player = strtoupper( $_GET["moves"] );
|
||||
$wins = [
|
||||
'ROCK' => 'SCISSORS',
|
||||
'PAPER' => 'ROCK',
|
||||
'SCISSORS' => 'PAPER'
|
||||
];
|
||||
$a_i = array_rand($wins);
|
||||
echo "<br>";
|
||||
echo "Player chooses " . "<i style=\"color:blue\">" . $player . "</i>";
|
||||
echo "<br>";
|
||||
echo "<br>" . "A.I chooses " . "<i style=\"color:red\">" . $a_i . "</i>";
|
||||
|
||||
$results = "";
|
||||
if ($player == $a_i){
|
||||
$results = "Draw";
|
||||
} else if($wins[$a_i] == $player ){
|
||||
$results = "A.I wins";
|
||||
} else {
|
||||
$results = "Player wins";
|
||||
}
|
||||
|
||||
echo "<br>" . $results;
|
||||
?>
|
||||
|
|
@ -19,7 +19,7 @@ my %vs = (
|
|||
|
||||
my %choices = %vs<options>.map({; $_.substr(0,2).lc => $_ });
|
||||
my $keys = %choices.keys.join('|');
|
||||
my $prompt = %vs<options>.map({$_.subst(/(\w\w)/,->$/{"[$0]"})}).join(' ')~"? ";
|
||||
my $prompt = %vs<options>.map({$_.subst(/(\w\w)/, -> $/ {"[$0]"})}).join(' ')~"? ";
|
||||
my %weight = %choices.keys »=>» 1;
|
||||
|
||||
my @stats = 0,0,0;
|
||||
|
|
|
|||
|
|
@ -1,19 +1,19 @@
|
|||
/*REXX program plays rock─paper─scissors with a human; tracks what human tends to use. */
|
||||
!= '────────'; err=! '***error***'; @.=0 /*some constants for this program. */
|
||||
prompt=! 'Please enter one of: Rock Paper Scissors (or Quit)'
|
||||
!= '────────'; err=! '***error***'; @.=0 /*some constants for this program. */
|
||||
prompt= ! 'Please enter one of: Rock Paper Scissors (or Quit)'
|
||||
$.p='paper' ; $.s='scissors'; $.r='rock' /*list of the choices in this program. */
|
||||
t.p=$.r ; t.s=$.p ; t.r=$.s /*thingys that beats stuff. */
|
||||
w.p=$.s ; w.s=$.r ; w.r=$.p /*stuff " " thingys. */
|
||||
b.p='covers'; b.s='cuts' ; b.r='breaks' /*verbs: how the choice wins. */
|
||||
|
||||
do forever; say; say prompt; say /*prompt the CBLF; then get a response.*/
|
||||
c=word($.p $.s $.r, random(1, 3)) /*choose the computer's first pick. */
|
||||
do forever; say; say prompt; say /*prompt the CBLF; then get a response.*/
|
||||
c=word($.p $.s $.r, random(1, 3) ) /*choose the computer's first pick. */
|
||||
m=max(@.r, @.p, @.s); c=w.r /*prepare to examine the choice history*/
|
||||
if @.p==m then c=w.p /*emulate JC's: The Amazing Karnac. */
|
||||
if @.s==m then c=w.s /* " " " " " */
|
||||
c1=left(c,1) /*C1 is used for faster comparing. */
|
||||
parse pull u; a=strip(u) /*get the CBLF's choice/pick (answer). */
|
||||
upper a c1 ; a1=left(a,1) /*uppercase choices, get 1st character.*/
|
||||
c1=left(c, 1) /*C1 is used for faster comparing. */
|
||||
parse pull u; a=strip(u) /*get the CBLF's choice/pick (answer). */
|
||||
upper a c1 ; a1=left(a, 1) /*uppercase choices, get 1st character.*/
|
||||
ok=0 /*indicate answer isn't OK (so far). */
|
||||
select /*process/verify the CBLF's choice. */
|
||||
when words(u)==0 then say err 'nothing entered'
|
||||
|
|
@ -27,9 +27,8 @@ b.p='covers'; b.s='cuts' ; b.r='breaks' /*verbs: how the choice wins.
|
|||
|
||||
if \ok then iterate /*answer ¬OK? Then get another choice.*/
|
||||
@.a1=@.a1+1 /*keep a history of the CBLF's choices.*/
|
||||
say ! 'computer chose: ' c
|
||||
if a1==c1 then do; say ! 'draw.'; iterate; end
|
||||
if $.a1==t.c1 then say ! 'the computer wins. ' ! $.c1 b.c1 $.a1
|
||||
else say ! 'you win! ' ! $.a1 b.a1 $.c1
|
||||
end /*forever*/
|
||||
/*stick a fork in it, we're all done. */
|
||||
say ! 'computer chose: ' c
|
||||
if a1== c1 then do; say ! 'draw.'; iterate; end
|
||||
if $.a1==t.c1 then say ! 'the computer wins. ' ! $.c1 b.c1 $.a1
|
||||
else say ! 'you win! ' ! $.a1 b.a1 $.c1
|
||||
end /*forever*/ /*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
|
|
@ -7,17 +7,17 @@ w.p= $.L $.s ; w.s= $.v $.r ; w.r= $.v $.p ; w.L= $.r $
|
|||
b.p='covers disproves'; b.s="cuts decapitates"; b.r='breaks crushes'; b.L="eats poisons"; b.v='vaporizes smashes' /*how the choice wins.*/
|
||||
whom.1=! 'the computer wins. ' !; whom.2=! "you win! " !; win=words(t.p)
|
||||
|
||||
do forever; say; say prompt; say /*prompt CBLF; then get a response. */
|
||||
c=word($.p $.s $.r $.L $.v,random(1,5)) /*the computer's first choice/pick. */
|
||||
do forever; say; say prompt; say /*prompt CBLF; then get a response. */
|
||||
c=word($.p $.s $.r $.L $.v, random(1, 5) ) /*the computer's first choice/pick. */
|
||||
m=max(@.r,@.p,@.s,@.L,@.v) /*used in examining CBLF's history. */
|
||||
if @.p==m then c=word(w.p,random(1,2)) /*emulate JC's The Amazing Karnac. */
|
||||
if @.s==m then c=word(w.s,random(1,2)) /* " " " " " */
|
||||
if @.r==m then c=word(w.r,random(1,2)) /* " " " " " */
|
||||
if @.L==m then c=word(w.L,random(1,2)) /* " " " " " */
|
||||
if @.v==m then c=word(w.v,random(1,2)) /* " " " " " */
|
||||
c1=left(c,1) /*C1 is used for faster comparing. */
|
||||
parse pull u; a=strip(u) /*obtain the CBLF's choice/pick. */
|
||||
upper a c1 ; a1=left(a,1) /*uppercase the choices, get 1st char. */
|
||||
if @.p==m then c=word(w.p, random(1, 2) ) /*emulate JC's The Amazing Karnac. */
|
||||
if @.s==m then c=word(w.s, random(1, 2) ) /* " " " " " */
|
||||
if @.r==m then c=word(w.r, random(1, 2) ) /* " " " " " */
|
||||
if @.L==m then c=word(w.L, random(1, 2) ) /* " " " " " */
|
||||
if @.v==m then c=word(w.v, random(1, 2) ) /* " " " " " */
|
||||
c1=left(c, 1) /*C1 is used for faster comparing. */
|
||||
parse pull u; a=strip(u) /*obtain the CBLF's choice/pick. */
|
||||
upper a c1 ; a1=left(a, 1) /*uppercase the choices, get 1st char. */
|
||||
ok=0 /*indicate answer isn't OK (so far). */
|
||||
select /* [↓] process the CBLF's choice/pick.*/
|
||||
when words(u)==0 then say err 'nothing entered.'
|
||||
|
|
@ -26,23 +26,22 @@ whom.1=! 'the computer wins. ' !; whom.2=! "you win! " !; win=words(t.p)
|
|||
when abbrev('LIZARD', a) |,
|
||||
abbrev('ROCK', a) |,
|
||||
abbrev('PAPER', a) |,
|
||||
abbrev('Vulcan', a) |,
|
||||
abbrev('SPOCK', a,2) | ,
|
||||
abbrev('VULCAN', a) |,
|
||||
abbrev('SPOCK', a,2) |,
|
||||
abbrev('SCISSORS',a,2) then ok=1 /*it's a valid choice for the human. */
|
||||
otherwise say err 'you entered a bad choice: ' u
|
||||
end /*select*/
|
||||
|
||||
if \ok then iterate /*answer ¬OK? Then get another choice.*/
|
||||
@.a1=@.a1+1 /*keep a history of the CBLF's choices.*/
|
||||
@.a1= @.a1 + 1 /*keep a history of the CBLF's choices.*/
|
||||
say ! 'computer chose: ' c
|
||||
if a1==c1 then say ! 'draw.' /*Oh rats! The contest ended up a draw*/
|
||||
else do who=1 for 2 /*either the computer or the CBLF won. */
|
||||
if who==2 then parse value a1 c1 with c1 a1
|
||||
do j=1 for win /*see who won. */
|
||||
if $.a1\==word(t.c1,j) then iterate /*not this 'un. */
|
||||
say whom.who $.c1 word(b.c1,j) $.a1 /*notify winner.*/
|
||||
if $.a1 \== word(t.c1, j) then iterate /*not this 'un. */
|
||||
say whom.who $.c1 word(b.c1, j) $.a1 /*notify winner.*/
|
||||
leave /*leave J loop.*/
|
||||
end /*j*/
|
||||
end /*who*/
|
||||
end /*forever*/
|
||||
/*stick a fork in it, we're all done. */
|
||||
end /*forever*/ /*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
40
Task/Rock-paper-scissors/Ring/rock-paper-scissors.ring
Normal file
40
Task/Rock-paper-scissors/Ring/rock-paper-scissors.ring
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
# Project : Rock-paper-scissors
|
||||
# Date : 2018/03/25
|
||||
# Author : Gal Zsolt [~ CalmoSoft ~]
|
||||
# Email : <calmosoft@gmail.com>
|
||||
|
||||
see "
|
||||
welcome to the game of rock-paper-scissors.
|
||||
each player guesses one of these three, and reveals it at the same time.
|
||||
rock blunts scissors, which cut paper, which wraps stone.
|
||||
if both players choose the same, it is a draw!
|
||||
when you've had enough, choose q.
|
||||
"
|
||||
g = ["rock","paper","scissors"]
|
||||
total=0 draw=0
|
||||
pwin=0 cwin=0
|
||||
see "what is your move (press r, p, or s)?"
|
||||
while true
|
||||
c=random(2)+1
|
||||
give q
|
||||
gs = floor((substr("rpsq",lower(q))))
|
||||
if gs>3 or gs<1
|
||||
summarise()
|
||||
exit
|
||||
ok
|
||||
total = total + 1
|
||||
see"you chose " + g[gs] + " and i chose " + g[c] + nl
|
||||
temp = gs-c
|
||||
if temp = 0
|
||||
see ". it's a draw"
|
||||
draw = draw + 1
|
||||
ok
|
||||
if temp = 1 or temp = -2
|
||||
see ". you win!"
|
||||
pwin = pwin + 1
|
||||
ok
|
||||
if temp = (-1) or temp = 2
|
||||
see ". i win!"
|
||||
cwin = cwin + 1
|
||||
ok
|
||||
end
|
||||
83
Task/Rock-paper-scissors/Rust/rock-paper-scissors.rust
Normal file
83
Task/Rock-paper-scissors/Rust/rock-paper-scissors.rust
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
extern crate rand;
|
||||
#[macro_use]
|
||||
extern crate rand_derive;
|
||||
|
||||
use std::io;
|
||||
use rand::Rng;
|
||||
use Choice::*;
|
||||
|
||||
#[derive(PartialEq, Clone, Copy, Rand, Debug)]
|
||||
enum Choice {
|
||||
Rock,
|
||||
Paper,
|
||||
Scissors,
|
||||
}
|
||||
|
||||
fn beats(c1: Choice, c2: Choice) -> bool {
|
||||
(c1 == Rock && c2 == Scissors) || (c1 == Scissors && c2 == Paper) || (c1 == Paper && c2 == Rock)
|
||||
}
|
||||
|
||||
fn ai_move<R: Rng>(rng: &mut R, v: [usize; 3]) -> Choice {
|
||||
// weighted random choice, a dynamic version of `rand::distributions::WeightedChoice`
|
||||
let rand = rng.gen_range(0, v[0] + v[1] + v[2]);
|
||||
if rand < v[0] {
|
||||
Paper
|
||||
} else if rand < v[0] + v[1] {
|
||||
Scissors
|
||||
} else {
|
||||
Rock
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut rng = rand::thread_rng();
|
||||
|
||||
println!("Rock, paper, scissors!");
|
||||
let mut ai_choice: Choice = rng.gen();
|
||||
let mut ucf = [0, 0, 0]; // user choice frequency
|
||||
let mut score = [0, 0];
|
||||
|
||||
loop {
|
||||
println!("Please input your move: 'r', 'p' or 's'. Type 'q' to quit");
|
||||
|
||||
let mut input = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut input)
|
||||
.expect("failed to read line");
|
||||
let u_choice = match input.to_lowercase().trim() {
|
||||
s if s.starts_with('r') => {
|
||||
ucf[0] += 1;
|
||||
Rock
|
||||
}
|
||||
s if s.starts_with('p') => {
|
||||
ucf[1] += 1;
|
||||
Paper
|
||||
}
|
||||
s if s.starts_with('s') => {
|
||||
ucf[2] += 1;
|
||||
Scissors
|
||||
}
|
||||
s if s.starts_with('q') => break,
|
||||
_ => {
|
||||
println!("Please enter a correct choice!");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
println!("You chose {:?}, I chose {:?}.", u_choice, ai_choice);
|
||||
if beats(u_choice, ai_choice) {
|
||||
score[0] += 1;
|
||||
println!("You win!");
|
||||
} else if u_choice == ai_choice {
|
||||
println!("It's a tie!");
|
||||
} else {
|
||||
score[1] += 1;
|
||||
println!("I win!");
|
||||
}
|
||||
println!("-Score: You {}, Me {}", score[0], score[1]);
|
||||
|
||||
// only after the 1st iteration the AI knows the stats and can make
|
||||
// its weighted random move
|
||||
ai_choice = ai_move(&mut rng, ucf);
|
||||
}
|
||||
println!("Thank you for the game!");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue