How to resolve the algorithm Pig the dice game step by step in the Common Lisp programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Pig the dice game step by step in the Common Lisp programming language
Table of Contents
Problem Statement
The game of Pig is a multiplayer game played with a single six-sided die. The object of the game is to reach 100 points or more. Play is taken in turns. On each person's turn that person has the option of either:
Create a program to score for, and simulate dice throws for, a two-person game.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pig the dice game step by step in the Common Lisp programming language
Source code in the common programming language
(defconstant +max-score+ 100)
(defconstant +n-of-players+ 2)
(let ((scores (make-list +n-of-players+ :initial-element 0))
(current-player 0)
(round-score 0))
(loop
(format t "Player ~d: (~d, ~d). Rolling? (Y)"
current-player
(nth current-player scores)
round-score)
(if (member (read-line) '("y" "yes" "") :test #'string=)
(let ((roll (1+ (random 6))))
(format t "~tRolled ~d~%" roll)
(if (= roll 1)
(progn
(format t
"~tBust! you lose ~d but still keep your previous ~d~%"
round-score (nth current-player scores))
(setf round-score 0)
(setf current-player
(mod (1+ current-player) +n-of-players+)))
(incf round-score roll)))
(progn
(incf (nth current-player scores) round-score)
(setf round-score 0)
(when (>= (apply #'max scores) 100)
(return))
(format t "~tSticking with ~d~%" (nth current-player scores))
(setf current-player (mod (1+ current-player) +n-of-players+)))))
(format t "~%Player ~d wins with a score of ~d~%" current-player
(nth current-player scores)))
You may also check:How to resolve the algorithm Pangram checker step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Factor programming language
You may also check:How to resolve the algorithm Hostname step by step in the PL/SQL programming language
You may also check:How to resolve the algorithm Vector products step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the VBScript programming language