|
| 1 | +# 🎮 Mastermind Game |
| 2 | + |
| 3 | +Welcome to **Mastermind**, a logic-based code-breaking game implemented in vanilla JavaScript. The player must guess a hidden sequence of colors within a limited time and number of turns, using feedback on each guess to deduce the solution. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 📁 Project Structure |
| 8 | + |
| 9 | +This project is organized into modular JavaScript files, each responsible for a specific aspect of the game logic and UI. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +### `game.js` |
| 14 | +The main game controller and initializer. |
| 15 | +It defines the `game` object, which holds all relevant game state and methods, and initializes the game based on user input and selected level. |
| 16 | + |
| 17 | +#### Responsibilities: |
| 18 | +- Managing game state (name, level, timer, colors, guesses, turns). |
| 19 | +- Orchestrating game flow: starting the game, handling turns, win/loss conditions. |
| 20 | +- Calling external module functions like `startTimer`, `countResults`, and `createBoard`. |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +### `user.js` |
| 25 | +Handles user interaction outside of the core gameplay. |
| 26 | + |
| 27 | +#### Functions: |
| 28 | +- `modalHandlers()`: Displays and hides modal windows for confirmation (e.g., exit game). |
| 29 | +- `newUser(game)`: Stores a new user in `localStorage` if not already stored. |
| 30 | +- `records()`: Displays the user's best time record (in seconds). |
| 31 | + |
| 32 | +--- |
| 33 | + |
| 34 | +### `level.js` |
| 35 | +Sets the difficulty level and corresponding game configurations. |
| 36 | + |
| 37 | +#### Functions: |
| 38 | +- `setLevel(game, id)`: Assigns a numeric difficulty based on user selection. |
| 39 | +- `setColors(game)`: Configures the list of colors available for each difficulty level. |
| 40 | +- `setComputerColors(game)`: Randomly selects a hidden sequence of 4 unique color indexes. |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +### `timer.js` |
| 45 | +Manages the countdown timer throughout the game. |
| 46 | + |
| 47 | +#### Functions: |
| 48 | +- `startTimer(game, duration)`: Initializes and starts the timer. |
| 49 | +- `tickTimer(game)`: Decreases the timer by one second and checks for timeout. |
| 50 | +- `renderTimer(game)`: Updates the on-screen timer display. |
| 51 | +- `getElapsedTime(game)`: Calculates the time elapsed since the start of the game. |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +### `logic.js` |
| 56 | +Handles the core game logic for checking guesses against the hidden sequence. |
| 57 | + |
| 58 | +#### Functions: |
| 59 | +- `countResults(game)`: Compares the player's guesses to the correct sequence and counts exact (`bulls`) and color-only (`cows`) matches. |
| 60 | +- `showResults(game, bulls, cows)`: Visually indicates the result of each guess using colored markers. |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +### `end-game.js` |
| 65 | +Manages end-game outcomes (win or loss). |
| 66 | + |
| 67 | +#### Functions: |
| 68 | +- `winner(game)`: Checks and stores the user's best time if a new record is achieved, and redirects to the win screen. |
| 69 | +- `failure()`: Redirects to the failure screen after a short delay. |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +### `board.js` |
| 74 | +Handles rendering the game board and user input for guesses. |
| 75 | + |
| 76 | +#### Functions: |
| 77 | +- `createBoard(game)`: Dynamically creates a new row of guess and result slots for the current turn. |
| 78 | +- `createButtons(game)`: Displays color buttons based on the current difficulty. |
| 79 | +- `setButtonEvent(game)`: Registers click events for color buttons, records guesses, and transitions between turns. |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +## 🧠 Game Rules |
| 84 | + |
| 85 | +- The computer selects a hidden sequence of 4 unique colors based on the chosen difficulty. |
| 86 | +- The player must guess the sequence within 10 turns and before time runs out. |
| 87 | +- After each guess: |
| 88 | + - A red marker indicates a correct color in the correct position. |
| 89 | + - A black marker indicates a correct color in the wrong position. |
| 90 | +- Win the game by guessing all 4 colors in the correct order. |
| 91 | + |
| 92 | +--- |
| 93 | + |
| 94 | +## 🗂 Data Persistence |
| 95 | + |
| 96 | +- Player names and best scores are saved to `localStorage`. |
| 97 | +- Records are shown on demand and updated when a better score is achieved. |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +## 🛠 Technologies Used |
| 102 | + |
| 103 | +- HTML5 / CSS3 |
| 104 | +- JavaScript (ES6 Modules) |
| 105 | +- DOM Manipulation |
| 106 | +- `localStorage` for data persistence |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +## 📄 License |
| 111 | + |
| 112 | +This project is released under the [MIT License](LICENSE). |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +## 👩💻 Developed by |
| 117 | + |
| 118 | +Efrat Bdil |
0 commit comments