play_GOT: A CLI Game Based on the Game of Thrones | Ruby

Sophie G.
5 min readJun 11, 2019

This is my first Ruby CLI project — and it coincides with the fact that eight seasons of Game of Thrones ended in the most miserable way, more so than anyone could have imagined. So I decided to reject that reality and substitute my own.

And here we go: what if a game that allows anyone to write their own legend of the Game of Thrones? You can select your own House, find allies, conquer enemies, and claim secret weapons that give you mysterious powers. Rally your men. You can be the next guardian of Westeros!

Excited by this idea, I quickly jot it down on a piece of paper before the floating thoughts are gone, and it became like this –

To win the Game of Thrones, one must:

1 - Survive the Winter that sweeps through Westeros randomly.2 - Conquer all your enemies or turn them into your allies.

Some basic stats will influence one’s chances of winning, including:

stamina - % probability of surviving the Winter (a random event)tactic - % probability of fleeing from an enemyloyalty - % probability of getting an ally 

Turning everyone into allies will be the quickest way to win. Each additional ally increases your stamina by 5 percent. However, making allies also has risks. A failed negotiation might trigger a fight. And a lost fight will decrease stamina by 5 percent.

Of course, you are given a chance to escape before a fight begins. Your tactic is your chance of successfully fleeing from an enemy.

Fight is a simple game of Rock, Paper, Scissors. Best 3 out of 5. The winner takes over the loser’s Secret Weapon and gets one step closer to the throne.

Winter is a random event. Surviving it, you will continue the game. Otherwise, the game is over.

Secret Weapons are wild cards, each serving a unique, unspoken purpose.

Once the skeleton was drawn, I began my coding.

I named it: play_GOT

Using Bundler, I created a Ruby gem:

$ bundle gem play_GOT

This command creates a scaffold directory for the new gem and initializes a Git repository in this directory so that I can start committing right away.

The directory is pre-populated with the following files:

Gemfile: Used to manage gem dependencies in developing environments.Rakefile: Requires Bundler and adds the build, install and release Rake tasks by way of calling Bundler::GemHelper.install_tasks. CODE_OF_CONDUCT.md: Provides a code of conduct that all contributors are to follow.LICENSE.txt: Includes the MIT license. .gitignore: Defines paths to ignore in Git.play_GOT.gemspec: Provides provide information for the gem such as the name, description and homepage. Specifies the dependencies that gem needs to run.README:md: Provides an overview of the gem including instructions for the users to install.

And the following folders:

/bin: Where the application is run. It should set up the console, define the environment given which paths/files are loaded, and start the application./lib: Where all my code resides.

Step 1: Setup

There is still some housekeeping to be done before the coding happens. For example, creating a Git repository:

git@github.com:sophieqgu/play_GOT.git

Adding a remote:

$ git remote add origin git@github.com:sophieqgu/play_GOT.git

Configuring the environment by creating a new /config folder and a new environment.rb file. Requiring any necessary tools that will come handy in the development:

  • rest-client is a simple HTTP and REST client for Ruby. It works with the JSON library to get and parse the response from a JSON API. The API I will be using is called An API of Ice And Fire. It provides comprehensive facts and details about the characters in Game of Thrones in an easily readable data format that I will be building into my game.
  • pry is a runtime developer console and IRB alternative that allows one to type Ruby code and inspect the results instantly, in the middle of a program. It is a tremendously useful tool for debugging.
  • colorize is a Ruby string class extension. It adds color and text effects to my command lines so that some of the words or sentences stand out from others. I use it to style my code and highlight important information for players.

Creating a run file that starts the program. Requiring that environment in the run file:

That leads me to step 2.

Step 2: Parsing an API, Instantiating a Class

The requirement of this project is to demonstrate a way of:

  • Pulling information from an external source;
  • Making use of that information.

Following the Single Responsibility Principle (SRP), I created an PlayGOT::API class specifically for handling API data. Every time this class is instantiated, it calls rest-client to make an HTTP GET request to the URL of An API of Ice And Fire, parses the JSON response and saves the results to an instance variable called ‘response’.

It then iterates through ‘response’, finds the specific attributes that I will be needing for the House objects in my game (i.e. :name, :region, :words, :titles, and :ancestral_weapons), and creates the House instances.

The PlayGOT::House class takes info from the source it is given and assigns them to its instances when initialized. It also has a set of class methods to:

  • #self.all: Show all the instances that have been created.
  • #self.list_all: List all the instances with indexes.
  • #self.find(i): Find a specific instance given an index.

Step 3: Player Moves

The PlayGOT::Player class specifies the decisions or actions a player can take in the game, including:

  • Give itself a name when the first time a Player is initiated;
  • Choose a House where the Player belongs;
  • Show the Player’s current status, such as allegiance, stats, weapons, allies, and enemies;
  • Select an enemy to negotiate or fight;
  • Make an ally, and if successful, modify the status according to the rules; or failed, fight or flee;
  • Roll the dice to flee;
  • Play Rock, Paper, Scissors to fight;
  • Take one’s chances during Winter.

Rock, Paper, Scissors is the simplest game that I thought simulates advance and retreat in a battle. Its entire logic is encapsulated in its own class to be tweaked as needed without interference with other Player functions.

Step 4: The Game CLI

All components are done. The last but not least step is tying all processes together. This is done in the PlayGOT::CLI class, which defines the CLI interface and the logical flow of the game.

According to my design, it will:

  • Start by initiating a Player and asking for a name;
  • Display all Houses in the game;
  • Let Player choose a House;
  • Display menu as follows;
1 - Read the rules of the game.
2 - View your current status.
3 - Go find an ally.
4 - Go attack an enemy.
5 - Read about secret weapons.
6 - Exit the game.
  • Remember turns. Allow Winter to strike every five turns that menu is called.
  • Define win scenario. Congratulate Player when all enemies are destroyed.
  • Define exit scenario. Exit when confirmed.

Play it! play_GOT

That summarizes my project. At time of writing, the secret weapons modules are still being developed! I am having a lot of fun brainstorming different uses and functions and ways to get them. So definitely stay tuned for my next update!

This gem is now published at RubyGems.org: https://rubygems.org/gems/play_GOT

View the Github Repository here: https://github.com/sophieqgu/play_GOT

You can try it out by entering the following command in your terminal:

$ gem install play_GOT

That’s it! Let me know what you think.

--

--