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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 21 game step by step in the Arturo 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 Arturo programming language

Source code in the arturo programming language

print "-----------------------------"
print " Welcome to 21 Game"
print "-----------------------------"

players: ["A" "B"]
currentPlayer: sample players
nextPlayer: first players -- currentPlayer
runningTotal: new 0
num: 0

getNum: function [][
    result: strip input "Enter a number (1,2,3) / x to exit: "
    if result="x" -> exit
    return result
]

loop.forever @[currentPlayer nextPlayer] 'plays [
    print ["Running total:" runningTotal]
    print ["Player" plays "turn"]

    num: getNum

    while [or? [not? numeric? num][not? contains? 1..3 to :integer num]][
        num: getNum
    ]

    runningTotal: runningTotal + to :integer num

    print ""

    if runningTotal=21 [
        print ["Running total is 21. Player" plays "won!"]
        exit
    ]
]


  

You may also check:How to resolve the algorithm Roots of a function step by step in the Octave programming language
You may also check:How to resolve the algorithm Repunit primes step by step in the Perl programming language
You may also check:How to resolve the algorithm Queue/Usage step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Vigenère cipher/Cryptanalysis step by step in the Vedit macro language programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Haxe programming language