21 game: Difference between revisions

2,413 bytes added ,  3 years ago
Added Wren
(Added Wren)
Line 5,603:
</Window>
</lang>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
{{libheader|Wren-ioutil}}
<lang ecmascript>import "/fmt" for Conv
import "/ioutil" for Input
import "random" for Random
 
var total = 0
var quit = false
 
var getChoice = Fn.new {
while (true) {
var input = Input.integer("Your choice 1 to 3: ", 0, 3)
if (input == 0) {
quit = true
return
}
var newTotal = total + input
if (newTotal > 21) {
System.print("Too big, try again")
} else {
total = newTotal
System.print("Running total is now %(total)")
return
}
}
}
 
var rand = Random.new()
var computer = Conv.itob(rand.int(2))
System.print("Enter 0 to quit at any time\n")
if (computer) {
System.print("The computer will choose first")
} else {
System.print("You will choose first")
}
System.print("\nRunning total is now 0\n")
var round = 1
while (true) {
System.print("ROUND %(round):\n")
for (i in 0..1) {
if (computer) {
var choice = (total < 18) ? 1 + rand.int(3) : 21 - total
total = total + choice
System.print("The computer chooses %(choice)")
System.print("Running total is now %(total)")
if (total == 21) {
System.print("\nSo, commiserations, the computer has won!")
return
}
} else {
getChoice.call()
if (quit) {
System.print("OK, quitting the game")
return
}
if (total == 21) {
System.print("\nSo, congratulations, you've won!")
return
}
}
System.print()
computer = !computer
}
round = round + 1
}</lang>
 
{{out}}
Sample game:
<pre>
Enter 0 to quit at any time
 
You will choose first
 
Running total is now 0
 
ROUND 1:
 
Your choice 1 to 3: 3
Running total is now 3
 
The computer chooses 2
Running total is now 5
 
ROUND 2:
 
Your choice 1 to 3: 3
Running total is now 8
 
The computer chooses 1
Running total is now 9
 
ROUND 3:
 
Your choice 1 to 3: 3
Running total is now 12
 
The computer chooses 3
Running total is now 15
 
ROUND 4:
 
Your choice 1 to 3: 2
Running total is now 17
 
The computer chooses 3
Running total is now 20
 
ROUND 5:
 
Your choice 1 to 3: 1
Running total is now 21
 
So, congratulations, you've won!
</pre>
9,479

edits