🚊 How to make a terminal-based metro game
Tags: showdev, python, tutorial, programming
Published: 2023-11-24 03:40:54

I got this idea from an [Instagram reel](https://www.instagram.com/p/CuzIt2yAKW-/) that I've seen recently where these people play a game to see which metro stop it'll take them to! I thought it'd be good practice to digitize this and make it into a terminal game. If you're not familiar with the Taipei metro system, don't worry. You can easily replicate the steps here to suit your city's metro system and make a unique one for the metro system of your choice!


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8cfobjaxmnsb6ll349a5.gif)

## 🚊 Let's get into the code ##

First, you want to create dictionaries for each of the stations in selected metro lines. My city's MRT stations come with an associated number, but if your metro doesn't, then just assign a number to each from one direction to another. Create a dictionary for each metro line.  

```python
brown = {1: 'taipei zoo', 2: 'muhza', 3: 'wanfang community', 4: 'wanfang hospital', 5: 'xinhai', 6: 'linguang', 7: 'liuzangli', 
         8: 'technology building', 9: 'daan', 10: 'zhongxiao fuxing', 11: 'nanjing fuxing', 12: 'zhongshan junior high', 
         13: 'songshan airport', 14: 'dahzi', 15: 'jiannan road', 16: 'xihu', 17: 'gangqian', 18: 'wende', 19: 'neihu', 20: 'dahu park', 
         21: 'huzhou', 22: 'donghu', 23: 'nangang software park', 24: 'nangang exhibtion center'}

```
Next, it's time for getting the user's inputs. You want to get which station they're starting from and what line that station is on. 

```python
starting_station = input("Which station are you starting from? ")
color_line = input("Which color is your station on? ")
```
You want to convert the `color_line` input to a dictionary to easier access later. So...

```python 
color_dict = eval(color_line)
```

Then, we want to emulate a coin toss to see if it's heads or tails. If it's heads, you will go north/east from where you are. If it's tails, you will go south/west. Don't forget to `import random` at the beginning of the file.

```python
coin_flip = ['heads','tails'] 
coin_flip_result = random.choice(coin_flip)
```
You want to do the same thing for rolling a dice too. In which you can go like this.

```python
dice = [1, 2, 3, 4, 5, 6]
randomize_number = random.choice(dice)
```

Then comes the fun part, implementing the logic! I used a nested if/else conditional to make it work. 

```python

for station in color_dict: 
    if starting_station == color_dict.get(station):
        # station is the key aka number
        if coin_flip_result == 'heads':
        # increase station number 
            destination = station + randomize_number
            print(f"🪙 Your coin flip turned out to be {coin_flip_result}")
            print(f"🎲 You are going {randomize_number} stops in {heads} direction on the {color_line} line")
        elif coin_flip_result == 'tails':
        # decrease station number 
            destination = station - randomize_number
            print(f"🪙 Your coin flip turned out to be {coin_flip_result}")
            print(f"🎲 You are going {randomize_number} stops in {tails} direction on the {color_line} line")
```
I also used a try/exception to catch an index out of bounds error just in case you're starting at a station that's towards the end of the lines and you number from the dice exceeds what is possible. 

```python 
try: 
    print(f"🥁 Your destination is {color_dict[destination]}!")

except:
    print("☹️ Try again, index out of bound")
```

## 💭 Conclusion ##

And that's it! Make sure to run `python3 filename` to run it while you're working on it! Here's the [Github repo](https://github.com/ivavay/mrt-game) if you need a fuller look at it.