Technical Article 1: Command Line Game
Published: 2022-04-18 20:20:29

This document explains the design of the command line game (CLI game) project; Dungeons and Dragons. A CLI game is where a user interacts directly with the terminal and the program asks for their input. It is designed for beginner programmers who are learning Python and assumes you have a basic knowledge of loops, conditional statements, boolean logic, F-strings, string formatting and operators. After reading this document, you will know how to structure a basic CLI game.

The design of the game is a single `while` loop with multiple `if` and nested `if` statements using Boolean values to determine when the loop terminates. 

Python 3.10.2 is used:

```
name = input("What is your name? ")
print(f"Hello {name}! Welcome to the game of Dungeons and Dragons!")
playing = True # while loop will evaluate to True until exit condition is met
second_choice = "1"
decision = "N"
has_sword = False # Will only change to True if player takes sword in room 1 

while playing: # While True
    choice = input(f"Which door will you choose? 1 or 2? ")
    if choice == "1":
        print("You have entered an empty room")
        second_choice = input(f"Would you like to 1. Return to the previous room or 2. Interact further? "
                        "Note: Type '1' or '2' ")
        if second_choice == "2":
            print("You found a sword!")
            decision = input("Will you take the sword? Y or N? (Note: capital sensitive choice) ")
            if decision == "Y": # Second nested 'if' statement required as a second decision 
                has_sword = True # Boolean value updated 
            print("Returning to previous room")
            continue # While loop restarts with new Boolean value
        
    elif choice == "2":
        print("YOU HAVE ENCOUNTERED A DRAGON!")
        dragon_choice = input(f"Would you like to 1. Return to the previous room or 2. Interact further? "
                        "Note: Type '1' or '2' ")
        if dragon_choice == "1":
            continue
        elif dragon_choice == "2" and has_sword == False: 
            print("You have been eaten, game over!")
            playing = False # While loop terminates
        elif dragon_choice == "2" and has_sword:
            print("You have defeated the dragon with your sword! Congratulations, you win the game!")
            playing = False # While loop terminates

```
The game is classic Dungeons and Dragons. The player must chose a door and decide whether to interact with the room or not. In order to win the game, they must find the sword in the empty room first and then interact with the dragon. If they interact with the dragon without interacting with the empty room and choosing to take the sword, they will lose the game.

A `while` loop is used so that the loop will run continuously until an exit condition is met. I.e. The player wins or loses the game.

F-strings have been used to make the code more legible and simplify string formatting for the user name.

`print(f"Hello {name}! Welcome to the game of Dungeons and Dragons!")` 

The variable `playing` has been set to `True` so that the `while` loop will run continuously until the exit condition is met. 

```playing = True```

In order to win the game, the player must chose to interact with the empty room **and** take the sword. The variable `has_sword` has been set to `False` so that the player must first find the sword and take it. The Boolean is then changed to `True` and the loop restarts as per the `continue` statement.

```
while playing: # While True
    choice = input(f"Which door will you choose? 1 or 2? ")
    if choice == "1":
        print("You have entered an empty room")
        second_choice = input(f"Would you like to 1. Return to the previous room or 2. Interact further? "
                        "Note: Type '1' or '2' ")
        if second_choice == "2":
            print("You found a sword!")
            decision = input("Will you take the sword? Y or N? (Note: capital sensitive choice) ")
            if decision == "Y": # Second nested 'if' statement required as a second decision 
                has_sword = True # Boolean value updated 
            print("Returning to previous room")
            continue # While loop restarts with new Boolean value
```
The player must then choose the door containing the dragon and interact. If they return to the previous room, the first `if` statement will execute and the `continue` statement will restart the `while` loop. 
```
elif choice == "2":
        print("YOU HAVE ENCOUNTERED A DRAGON!")
        dragon_choice = input(f"Would you like to 1. Return to the previous room or 2. Interact further? "
                        "Note: Type '1' or '2' ")
        if dragon_choice == "1":
            continue
```
The subsequent `elif` clauses dictate if the player wins or loses the game depending on whether they have the sword or not. The variable `playing` is set to `False` and the loop terminates. 
```
elif dragon_choice == "2" and has_sword == False: 
            print("You have been eaten, game over!")
            playing = False # While loop terminates
        elif dragon_choice == "2" and has_sword:
            print("You have defeated the dragon with your sword! Congratulations, you win the game!")
            playing = False # While loop terminates
```