https://github.com/gappeah/fcc-number-guessing-game
This project is part of the FreeCodeCamp Relational Database certification. The objective was to build a PostgreSQL database to store user information and their game results and create a Bash script to handle the game logic and interact with the database.
https://github.com/gappeah/fcc-number-guessing-game
Last synced: over 1 year ago
JSON representation
This project is part of the FreeCodeCamp Relational Database certification. The objective was to build a PostgreSQL database to store user information and their game results and create a Bash script to handle the game logic and interact with the database.
- Host: GitHub
- URL: https://github.com/gappeah/fcc-number-guessing-game
- Owner: gappeah
- Created: 2024-09-05T12:29:18.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-09T15:33:54.000Z (almost 2 years ago)
- Last Synced: 2025-01-07T19:44:29.378Z (over 1 year ago)
- Language: Shell
- Size: 11.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Number Guessing Game
This project is part of the FreeCodeCamp Relational Database certification. The objective was to build a PostgreSQL database to store user information and game results, as well as create a Bash script to handle the game logic and interact with the database.
## Project Structure
- **Database**: The database is managed using PostgreSQL.
- **Script**: A Bash script (`number_guess.sh`) was created to interact with the database by allowing users to play a number guessing game.
### Files
- `number_guess.sql`: Contains SQL commands to create the database, including tables for users and their game results.
- `number_guess.sh`: A Bash script to play the number guessing game, store user data, and track game results.
## Database Schema

The database consists of the following tables:
### `users` Table
| Column Name | Data Type | Constraints |
| ----------- | ------------ | ---------------- |
| `user_id` | SERIAL | PRIMARY KEY |
| `username` | VARCHAR(20) | UNIQUE, NOT NULL |
### `games` Table
| Column Name | Data Type | Constraints |
| ---------------- | --------- | ------------------------------------- |
| `game_id` | SERIAL | PRIMARY KEY |
| `number_guesses` | INTEGER | NOT NULL |
| `user_id` | INTEGER | REFERENCES `users(user_id)` |
## Usage
### Prerequisites
- **PostgreSQL**: Ensure PostgreSQL is installed and running on your machine. Connect to the database using:
```bash
psql --username=freecodecamp --dbname=number_guess
```
- **Database Setup**: The database should be created and populated using the provided `number_guess.sql` file.
### Running the Script
The `number_guess.sh` script allows users to play the number guessing game and saves the results to the database. To run the script:
```bash
./number_guess.sh
```
### Game Play Instructions
1. **Enter Username**: The script will prompt you to enter your username. If it is your first time, the username will be added to the `users` table.
- If you are a returning user, the script will display the number of games you have played and your best game result (least number of guesses).
2. **Guess the Secret Number**: The script will randomly generate a number between 1 and 1000. You will be prompted to guess the number.
- If your guess is too high or too low, you will be prompted to try again.
- The game continues until you correctly guess the number.
3. **Save Game Results**: Once you guess the number, the script will tell you how many attempts it took, and the result will be saved to the database.
### Example Gameplay
```bash
Enter your username:
JohnDoe
Welcome back, JohnDoe! You have played 5 games, and your best game took 3 guesses.
Guess the secret number between 1 and 1000:
500
It's lower than that, guess again:
250
It's higher than that, guess again:
375
It's lower than that, guess again:
312
You guessed it in 4 tries. The secret number was 312. Nice job!
```
### Database Queries
The following information is stored in the database:
- **Usernames**: Stored in the `users` table.
- **Game Results**: Stored in the `games` table, tracking how many guesses each game took and which user played the game.
## Project Completion
### Git Repository
The project folder was initialized as a Git repository:
```bash
git init
```
The repository was ensured to have a `main` branch with at least five commits, using conventional prefixes like `fix:`, `feat:`, `refactor:`, etc.
### Bash Script
The `number_guess.sh` script was developed to handle user input, generate random numbers, and store game results in the database.
## Installation
1. Clone this repository.
2. Set up the database:
- Log in to PostgreSQL:
```bash
psql --username=freecodecamp --dbname=postgres
```
- Run the SQL commands in `number_guess.sql` to create and populate the database:
```sql
\i number_guess.sql
```
3. Make sure the `number_guess.sh` script has execution permissions:
```bash
chmod +x number_guess.sh
```
4. Run the script as described above.