How to resolve the algorithm 21 game step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 21 game step by step in the Wren programming language

Table of Contents

Problem Statement

21 is a two player game, the game is played by choosing a number (1, 2, or 3) to be added to the running total. The game is won by the player whose chosen number causes the running total to reach exactly 21. The running total starts at zero. One player will be the computer. Players alternate supplying a number to be added to the running total.

Write a computer program that will:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm 21 game step by step in the Wren programming language

Source code in the wren programming language

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
}


  

You may also check:How to resolve the algorithm XML/Output step by step in the zkl programming language
You may also check:How to resolve the algorithm Truncate a file step by step in the XPL0 programming language
You may also check:How to resolve the algorithm A+B step by step in the Emojicode programming language
You may also check:How to resolve the algorithm Percolation/Bond percolation step by step in the Raku programming language
You may also check:How to resolve the algorithm Hunt the Wumpus step by step in the Rust programming language