Task

Implement the classic children's game Rock-paper-scissors, as well as a simple predictive   AI   (artificial intelligence)   player.

Rock Paper Scissors is a two player game.

Each player chooses one of rock, paper or scissors, without knowing the other player's choice.

The winner is decided by a set of rules:

   Rock beats scissors
   Scissors beat paper
   Paper beats rock


If both players choose the same thing, there is no winner for that round.

For this task, the computer will be one of the players.

The operator will select Rock, Paper or Scissors and the computer will keep a record of the choice frequency, and use that information to make a weighted random choice in an attempt to defeat its opponent.



Extra credit

Support additional choices   additional weapons.




DllCall("AllocConsole")
Write("Welcome to Rock-Paper-Scissors`nMake a choice: ")
 
cR := cP := cS := 0 ; user choice count
userChoice := Read()
Write("My choice: " . cpuChoice := MakeChoice(1, 1, 1))
 
Loop
{
	Write(DecideWinner(userChoice, cpuChoice) . "`nMake A Choice: ")
	cR += SubStr(userChoice, 1, 1) = "r", cP += InStr(userChoice, "P"), cS += InStr(userChoice, "S")
	userChoice := Read()
	Write("My Choice: " . cpuChoice := MakeChoice(cR, cP, cS))
}
 
MakeChoice(cR, cP, cS){
	; parameters are number of times user has chosen each item
	total := cR + cP + cS
 
	Random, rand, 0.0, 1.0
	if (rand >= 0 and rand <= cR / total)
		return "Paper"
	else if (rand > cR / total and rand <= (cR + cP) / total)
		return "Scissors"
	else
		return "Rock"
}
 
DecideWinner(user, cpu){
	user := SubStr(user, 1, 1), cpu := SubStr(cpu, 1, 1)
	if (user = cpu)
		return "`nTie!"
	else if (user = "r" and cpu = "s") or (user = "p" and cpu = "r") or (user = "s" and cpu = "p")
		return "`nYou Win!"
	else
		return "`nI Win!"
}
 
Read(){
	FileReadLine, a, CONIN$, 1
	return a
}
Write(txt){
	FileAppend, % txt, CONOUT$
}