September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -12,8 +12,8 @@ computer_play(record plays, record beats)
if (a < r_q_integer(plays, s)) {
break;
}
a -= r_q_integer(plays, s);
} while (r_greater(plays, s, s));
a -= plays[s];
} while (rsk_greater(plays, s, s));
return r_q_text(beats, s);
}
@ -56,7 +56,7 @@ main(void)
if (!compare(s, c)) {
o_text("Draw\n");
} elif (!compare(c, r_q_text(beats, s))) {
} elif (!compare(c, beats[s])) {
computer += 1;
o_text("Computer wins\n");
} else {
@ -64,7 +64,7 @@ main(void)
o_text("Human wins\n");
}
r_r_integer(plays, s, r_q_integer(plays, s) + 1);
r_up(plays, s);
o_form("Score: Human: ~, Computer: ~\n", human, computer);
}

View file

@ -1,62 +0,0 @@
DIM pPLchoice(1 TO 3) AS INTEGER, pCMchoice(1 TO 3) AS INTEGER
DIM choices(1 TO 3) AS STRING
DIM playerwins(1 TO 3) AS INTEGER
DIM playerchoice AS INTEGER, compchoice AS INTEGER
DIM playerwon AS INTEGER, compwon AS INTEGER, tie AS INTEGER
DIM tmp AS INTEGER
' Do it this way for QBasic; FreeBASIC supports direct array assignment.
DATA "rock", "paper", "scissors"
FOR tmp = 1 to 3
READ choices(tmp)
NEXT
DATA 3, 1, 2
FOR tmp = 1 to 3
READ playerwins(tmp)
NEXT
RANDOMIZE TIMER
DO
' Computer chooses first to ensure there's no "cheating".
compchoice = INT(RND * (pPLchoice(1) + pPLchoice(2) + pPLchoice(3) + 3))
SELECT CASE compchoice
CASE 0 to (pPLchoice(1))
' Player past choice: rock; choose paper.
compchoice = 2
CASE (pPLchoice(1) + 1) TO (pPLchoice(1) + pPLchoice(2) + 1)
' Player past choice: paper; choose scissors.
compchoice = 3
CASE (pPLchoice(1) + pPLchoice(2) + 2) TO (pPLchoice(1) + pPLchoice(2) + pPLchoice(3) + 2)
' Player past choice: scissors; choose rock.
compchoice = 1
END SELECT
PRINT "Rock, paper, or scissors ";
DO
PRINT "[1 = rock, 2 = paper, 3 = scissors, 0 to quit]";
INPUT playerchoice
LOOP WHILE (playerchoice < 0) OR (playerchoice > 3)
IF 0 = playerchoice THEN EXIT DO
pCMchoice(compchoice) = pCMchoice(compchoice) + 1
pPLchoice(playerchoice) = pPLchoice(playerchoice) + 1
PRINT "You chose "; choices(playerchoice); " and I chose "; choices(compchoice); ". ";
IF (playerchoice) = compchoice THEN
PRINT "Tie!"
tie = tie + 1
ELSEIF (compchoice) = playerwins(playerchoice) THEN
PRINT "You won!"
playerwon = playerwon + 1
ELSE
PRINT "I won!"
compwon = compwon + 1
END IF
LOOP
PRINT "Some useless statistics:"
PRINT "You won "; STR$(playerwon); " times, and I won "; STR$(compwon); " times; "; STR$(tie); " ties."
PRINT , choices(1), choices(2), choices(3)
PRINT "You chose:", pPLchoice(1), pPLchoice(2), pPLchoice(3)
PRINT " I chose:", pCMchoice(1), pCMchoice(2), pCMchoice(3)

View file

@ -1,16 +0,0 @@
20 LET P=0: LET Q=0: LET Z=0
30 INPUT "Rock, paper, or scissors (1 = rock, 2 = paper, 3 = scissors)? ", A
40 IF A>3 THEN GOTO 400
50 IF A=3 THEN LET A=4
60 IF A<1 THEN GOTO 400
70 CHOOSE C=3 : LET D=4: FOR B=1 TO C+1 : LET D = D+D : NEXT : GOTO (A+D)*10
90 Z=Z+1 : PRINT "We both chose 'rock'. It's a draw." : GOTO 30
100 P=P+1 : PRINT "You chose 'paper', I chose 'rock'. You win.." : GOTO 30
120 Q=Q+1 : PRINT "You chose 'scissors', I chose 'rock'. I win!" : GOTO 30
170 Q=Q+1 : PRINT "You chose 'rock', I chose 'paper'. I win!" : GOTO 30
180 Z=Z+1 : PRINT "We both chose 'paper'. It's a draw." : GOTO 30
200 P=P+1 : PRINT "You chose 'scissors', I chose 'paper'. You win.." : GOTO 30
330 P=P+1 : PRINT "You chose 'rock', I chose 'scissors'. You win.." : GOTO 30
340 Q=Q+1 : PRINT "You chose 'paper', I chose 'scissors'. I win!" : GOTO 30
360 Z=Z+1 : PRINT "We both chose 'scissors'. It's a draw." : GOTO 30
400 PRINT "There were ";Z;" draws. I lost ";P;" times, you lost ";Q;" times." : END

View file

@ -0,0 +1,71 @@
@echo off
setlocal enabledelayedexpansion
set choice1=rock
set choice2=paper
set choice3=scissors
set freq1=0
set freq2=0
set freq3=0
set games=0
set won=0
set lost=0
set tie=0
:start
cls
echo Games - %games% : Won - %won% : Lost - %lost% : Ties - %tie%
choice /c RPS /n /m "[R]ock, [P]aper or [S]cissors? "
set /a choicequant+=1
set choice=%errorlevel%
set /a rocklimit=100 / %choicequant% * %freq3%
set /a scissorslimit=100 - (100 / %choicequant% * %freq2%)
set /a randchoice=%random% %% 100 + 1
set compchoice=2
if %randchoice% geq %scissorslimit% set compchoice=3
if %randchoice% leq %rocklimit% set compchoice=1
cls
echo Player: !choice%choice%! ^|vs^| Computer: !choice%compchoice%!
goto %compchoice%
:1
if %choice%==1 goto tie
if %choice%==2 goto win
if %choice%==3 goto loss
:2
if %choice%==1 goto loss
if %choice%==2 goto tie
if %choice%==3 goto win
:3
if %choice%==1 goto win
if %choice%==2 goto loss
if %choice%==3 goto tie
:win
set /a won+=1
echo Player wins^!
goto end
:loss
set /a lost+=1
echo Computer Wins^!
goto end
:tie
set /a tie+=1
echo Tie^!
goto end
:end
set /a games+=1
set /a freq%choice%+=1
pause
goto start

View file

@ -1,60 +0,0 @@
import std.stdio, std.random, std.string, std.array,
std.typecons, std.traits, std.conv, std.algorithm;
enum Choice { rock, paper, scissors }
immutable order = [EnumMembers!Choice];
uint[order.length] choiceFrequency;
Choice whatBeats(in Choice ch) pure nothrow {
return order[(order.countUntil(ch) + 1) % $];
}
Nullable!Choice checkWinner(in Choice a, in Choice b)
pure /*nothrow*/ {
alias TResult = typeof(return);
if (b == whatBeats(a))
return TResult(b);
else if (a == whatBeats(b))
return TResult(a);
return TResult();
}
Choice getRandomChoice() /*nothrow*/ {
if (choiceFrequency[].sum == 0)
return uniform!Choice;
return order[choiceFrequency.dice].whatBeats;
}
void main() {
writeln("Rock-Paper-Scissors Game");
while (true) {
write("Your choice (empty to end game): ");
immutable humanChoiceStr = readln.strip.toLower;
if (humanChoiceStr.empty)
break;
Choice humanChoice;
try {
humanChoice = humanChoiceStr.to!Choice;
} catch (ConvException e) {
writeln("Wrong input: ", humanChoiceStr);
continue;
}
immutable compChoice = getRandomChoice;
write("Computer picked ", compChoice, ", ");
// Don't register the player choice until after
// the computer has made its choice.
choiceFrequency[humanChoice]++;
immutable winner = checkWinner(humanChoice, compChoice);
if (winner.isNull)
writeln("Nobody wins!");
else
writeln(winner.get, " wins!");
}
}

View file

@ -1,55 +0,0 @@
import std.stdio, std.random, std.string, std.conv, std.array,
std.typecons;
enum Choice { rock, paper, scissors }
bool beats(in Choice c1, in Choice c2) pure nothrow {
return (c1 == Choice.paper && c2 == Choice.rock) ||
(c1 == Choice.scissors && c2 == Choice.paper) ||
(c1 == Choice.rock && c2 == Choice.scissors);
}
Choice genMove(in int r, in int p, in int s) {
immutable x = uniform!"[]"(1, r + p + s);
if (x < s) return Choice.rock;
if (x <= s + r) return Choice.paper;
else return Choice.scissors;
}
Nullable!To maybeTo(To, From)(From x) {
try {
return typeof(return)(x.to!To);
} catch (ConvException e) {
return typeof(return)();
}
}
void main() {
int r = 1, p = 1, s = 1;
while (true) {
write("rock, paper or scissors? ");
immutable hs = readln.strip.toLower;
if (hs.empty)
break;
immutable h = hs.maybeTo!Choice;
if (h.isNull) {
writeln("Wrong input: ", hs);
continue;
}
immutable c = genMove(r, p, s);
writeln("Player: ", h, " Computer: ", c);
if (beats(h, c)) writeln("Player wins\n");
else if (beats(c, h)) writeln("Player loses\n");
else writeln("Draw\n");
final switch (h.get) {
case Choice.rock: r++; break;
case Choice.paper: p++; break;
case Choice.scissors: s++; break;
}
}
}

View file

@ -1,36 +1,59 @@
import System.Random
data Choice = Rock | Paper | Scissors
deriving (Show, Eq)
import System.Random (randomRIO)
data Choice
= Rock
| Paper
| Scissors
deriving (Show, Eq)
beats :: Choice -> Choice -> Bool
beats Paper Rock = True
beats Scissors Paper = True
beats Rock Scissors = True
beats _ _ = False
genrps (r,p,s) = fmap rps rand
where rps x | x <= s = Rock
| x <= s+r = Paper
| otherwise = Scissors
rand = randomRIO (1,r+p+s) :: IO Int
genrps :: (Int, Int, Int) -> IO Choice
genrps (r, p, s) = rps <$> rand
where
rps x
| x <= s = Rock
| x <= s + r = Paper
| otherwise = Scissors
rand = randomRIO (1, r + p + s) :: IO Int
getrps = fmap rps getLine
where rps "scissors" = Scissors
rps "rock" = Rock
rps "paper" = Paper
rps _ = error "invalid input"
getrps :: IO Choice
getrps = rps <$> getLine
where
rps "scissors" = Scissors
rps "rock" = Rock
rps "paper" = Paper
rps _ = error "invalid input"
game (r,p,s) = do putStrLn "rock, paper or scissors?"
h <- getrps
c <- genrps (r,p,s)
putStrLn ("Player: " ++ show h ++ " Computer: " ++ show c)
putStrLn (if beats h c then "player wins\n"
else if beats c h then "player loses\n"
else "draw\n")
let rr = if h == Rock then r+1 else r
pp = if h == Paper then p+1 else p
ss = if h == Scissors then s+1 else s
game (rr,pp,ss)
game :: (Int, Int, Int) -> IO a
game (r, p, s) = do
putStrLn "rock, paper or scissors?"
h <- getrps
c <- genrps (r, p, s)
putStrLn ("Player: " ++ show h ++ " Computer: " ++ show c)
putStrLn
(if beats h c
then "player wins\n"
else if beats c h
then "player loses\n"
else "draw\n")
let rr =
if h == Rock
then r + 1
else r
pp =
if h == Paper
then p + 1
else p
ss =
if h == Scissors
then s + 1
else s
game (rr, pp, ss)
main = game (1,1,1)
main :: IO a
main = game (1, 1, 1)

View file

@ -6,7 +6,7 @@ const msg = [
"Scissors cut paper",
]
say <<"EOT";
say <<"EOT"
\n>> Rock Paper Scissors <<\n
** Enter 'r', 'p', or 's' as your play.
** Enter 'q' to exit the game.
@ -17,7 +17,7 @@ var plays = 0
var aScore = 0
var pScore = 0
var pcf = [0,0,0] # pcf = player choice frequency
var aChoice = 3.irand # ai choice for first play is completely random
var aChoice = pick(0..2) # ai choice for first play is completely random
loop {
var pi = Sys.scanln("Play: ")
@ -34,19 +34,19 @@ loop {
++plays
# show result of play
">> My play: %-8s".printf(rps[aChoice])
">> My play: %-8s".printf(rps[aChoice])
given ((aChoice - pChoice + 3) % 3) {
when (0) { say "Tie." }
when (1) { "%-*s %s".printlnf(30, msg[aChoice], 'My point'); aScore++ }
when (2) { "%-*s %s".printlnf(30, msg[pChoice], 'Your point'); pScore++ }
when (1) { "%-*s %s".printlnf(30, msg[aChoice], 'My point'); aScore++ }
when (2) { "%-*s %s".printlnf(30, msg[pChoice], 'Your point'); pScore++ }
}
# show score
"%-6s".printf("%d:%d" % (pScore, aScore))
# compute ai choice for next play
given (plays.irand) { |rn|
given (plays.rand.int) { |rn|
case (rn < pcf[0]) { aChoice = 1 }
case (pcf[0]+pcf[1] > rn) { aChoice = 2 }
default { aChoice = 0 }