Hangman CLI Ruby Game
Tags: ruby
Published: 2020-04-01 23:51:29

### Indroduction

For my first project at Flatiron School SF I did a **Hangman** CLI Ruby Game. Which means that our Game ran in the Command Line. To run the game the player typed **bundle exec ruby app/hangmangame.rb**  into the command line after navigating to the current directory. This game was written in Ruby and utilizes YAML for saving / loading games.

For our Game Interface when you start playing Hangman you will be given a visual on correct letters and wrong letters as well as a hangman graphic itself to let you know how close you are. When a new game is started, you will have 10 total chances to guess the secret 5 letter word before the man is hung!

**Here is the link my team and I's code: https://github.com/brookebachman00/module_1_hangman_game**

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/qk8mn3xminzb4u1klatq.png)

*I also made sure that the Player could only type one letter at a time and it has to be a letter!*

### Classes, Tables and Relationships


I had 3 Tables. A Player table, Word table and a PlayerWord table. The PlayerWord class is our join table. The relationships are:

- A Player has many player_words and has many words through player_words.

- A Word has many player_words and has many players through player_words.

- A PlayerWord belongs to a player and a word


In our app and app/models I had 5 different classes: 

- A Player class

- Word class

- PlayerWord class (Join Class)

- String class which is where all our colour classes are defined to make it more visually appealing to the player.

- Hangman class which is the hangman graphics

- Hangmangame class which holds all of our methods and rules for the game.


### Database and Classes Relationships


Firstly, I used **ActiveRecord** which is an **ORM**(Object Relational Mapping). Using Actie Record, just like using an ORM, can make fetching and representing data easier. ActiveRecord allows you to write SQL queries but also more complicated ones, using the object-oriented paradigm of a programming language(Ruby in this project).

To manage our database schema I used a domain-specific language called **Migrations**. Migrations are stored in files which are executed against any database that Active Record supports using rake. Our migrations where for our three tables: Player table, Word table and a PlayerWord table.

Our Secret words where pulled from database in our **seeds.rb** file.

When a new player comes to play the game and enter a username they are immediately added to the database. When an old player comes to play again and types in their username it collects all their previous game data and points that they had. Then when that player plays Hangman and gets more points their score is updated. At the end of each game it will tell the player their updated score. I did 10 points every time you got the word correct and no points when you lose. 

I also made sure that a user never has the same word twice. 

Lastly, I created a top player method. Which tells the top player that they have the highest score out of ‘x’ amount of people which I got from the database.

*All these methods are defined in my Hangmangame class and connected to my database.*

#### Extra
I used **binding.pry** to essentially 'pry' into the current binding or context of the code, from outside your file and to test it or check for errors.


Feel free to leave a comment with your thoughts or questions below!

From Bronte Sewell