wisemonkeys logo
FeedNotificationProfileManage Forms
FeedNotificationSearchSign in
wisemonkeys logo

Blogs

Rock, Paper, Scissors Game in Common Lisp

profile
Om Rawal
Jul 16, 2025
0 Likes
0 Discussions
1 Reads


1. Code Analysis

Let’s walk through how the code works, step by step:

a. Setup

(defun rock-paper-scissors ()
(let ((choices '("rock" "paper" "scissors"))
(random-state (make-random-state t)))
  1. I defined a function called rock-paper-scissors.
  2. Inside, I created a list of choices: "rock", "paper", and "scissors".
  3. I also made a random state to generate random numbers.

b. Welcome Message

(format t "~%--- Welcome to the Rock, Paper, Scissors Game! ---~%")
(format t "Instructions: Type 'rock', 'paper', or 'scissors' to play.~%")
(format t "You can also type 'quit' anytime to exit the game.~%~%")
  1. Prints a welcome message and tells the player how to play.

c. The Game Loop

(loop
(format t "Your move: ")
(let ((player-move (string-downcase (read-line))))
...
  1. Starts an infinite loop for continuous gameplay.
  2. Asks the player to type their move.
  3. Converts the input to lowercase so it works even if the player types “Rock” or “ROCK”.

d. Handling “quit”

(if (string= player-move "quit")
(progn
(format t "Thanks for playing! Goodbye.~%")
(return))
  1. If the user types quit, the program says goodbye and exits the loop.

e. Validating Input

(if (not (member player-move choices :test #'string=))
(format t "Oops! Please enter only 'rock', 'paper', or 'scissors'.~%")
  1. Checks if the user typed a valid move.
  2. If not, it prints a warning and loops back to ask again.

f. Computer’s Move

(let* ((computer-move (nth (random 3 random-state) choices)))
(format t "Computer chose: ~A~%" computer-move)
  1. Picks a random move from the list of choices.
  2. Prints out what the computer chose.

g. Determining the Winner

(cond
((string= player-move computer-move)
(format t "It's a tie! We both chose ~A.~%" player-move))

((or (and (string= player-move "rock") (string= computer-move "scissors"))
(and (string= player-move "scissors") (string= computer-move "paper"))
(and (string= player-move "paper") (string= computer-move "rock")))
(format t "You win! ~A beats ~A.~%" player-move computer-move))

(t
(format t "You lose! ~A beats ~A.~%" computer-move player-move))))
  1. Checks for a tie first.
  2. Then checks if the player wins.
  3. Otherwise, prints that the player lost.

2. Output

Here’s what a sample run looks like:



Comments ()


Sign in

Read Next

Understanding Toddler Tantrums: What They Really Mean

Blog banner

Blog name

Blog banner

Memory management

Blog banner

A little bit of salt is all the hash needs!

Blog banner

ITIL Version 3 and 4 differenciation?

Blog banner

Why do you need a personalised bag tag for your kids' school bags?

Blog banner

EVOLUTION OF MICROPROCESSOR

Blog banner

STUDY OF SYSTEM HACKING TOOLS

Blog banner

Data carving - using hex editor

Blog banner

The Sunny Side of Instagram

Blog banner

AI and cyber Security

Blog banner

Explain Kernel in OS

Blog banner

Sleep Disorder: The Silent Link Between Sleep and Mental Health

Blog banner

Concurrency:Deadlock and Starvation

Blog banner

Deadlock and Starvation

Blog banner

Importance of modern technology era

Blog banner

Predicting Student Performance with Data Science

Blog banner

World’s rarest passport owned by 500 people.

Blog banner

Danger assessment in GIS

Blog banner

Time Series Analysis

Blog banner

Dancing Classes In Mumbai

Blog banner

Disk Management

Blog banner

Critical success factors

Blog banner

Evolution of Operating System

Blog banner

Capacity management in ITSM

Blog banner

Is It Too Late to Straighten My Teeth as an Adult?

Blog banner

Continual service improvement vs maintenance phase in IT

Blog banner

Secure Hypertext transfer protocol

Blog banner

Fashion marketing in india

Blog banner

The Essential Guide to Dynamic Arrays vs. Linked Lists: Which to Use and When ?

Blog banner

Outlook.com

Blog banner

Compromising Mobile Platforms

Blog banner

Network Security Risks

Blog banner

Title: Modern Operating Systems: Powering the Digital Era

Blog banner

Types of email

Blog banner

Data Analytics in Data Science

Blog banner

How Does Preschool Help Children Become Better Decision Makers?

Blog banner

Service Design Model

Blog banner

Ethical Hacking

Blog banner

Travel Geek ‘The last $50k in Switzerland’

Blog banner

Firewall

Blog banner

Koinex is shutting down and here is how you can withdraw...

Blog banner