Loading repository dataโฆ
Loading repository dataโฆ
numanamjad406 / repository
๐ฎ Console N-in-a-Row in C++ from scratch! Custom grid size, configurable win count, mouse-click input, smart AI (wins, blocks, falls back). Built with Windows API โ colors, boxes & cursor control. Human vs Computer. No libraries. Pure C++. ๐
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
First Project | 1st Semester | BS Artificial Intelligence
Built entirely from scratch in C++ (Console Application) using Windows API.
Student ID:BSAI25033
Gomoku (also known as Five in a Row) is a classic two-player strategy board game.
The objective is simple: be the first player to get N of your symbols in a row โ horizontally, vertically, or diagonally.
This version puts you (Human) against a smart Computer AI on a fully customizable board, all rendered live in the Windows console.
------------------
---X--------------
---XO-------------
---XO-------------
---XO------------- โ Computer blocking Human's column!
------------------
Human (X) Turn:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Human (X) is the Winner! โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
| Feature | Details |
|---|---|
| ๐ง AI Opponent | Win โ Block โ Center โ First Empty strategy |
| ๐ฏ Custom Board | You choose rows, columns, and win condition at start |
| ๐ฑ๏ธ Mouse Input | Click directly on the board to place your symbol |
| ๐จ Colored Output | X = Red, O = Cyan, Empty = Gray |
| ๐ Win Detection | All 8 directions: horizontal, vertical, diagonal |
| ๐ค Draw Detection | Detected when board is completely full |
| ๐ฒ Random First Turn | Randomly decides who goes first each game |
windows.h, conio.h)Option 1 โ Visual Studio:
.cpp file content with the game codeF5 (Run)Option 2 โ g++ (MinGW Command Line):
g++ BSAI25033-PROJECT-2-PHASE-2-FIXED.cpp -o gomoku.exe
gomoku.exe
โ ๏ธ Must be compiled for Windows only. Does not support Linux/macOS due to Windows API dependency.
Launch the program
Enter your game settings when prompted:
Enter WIN_COUNT ROWS COLUMNS: 5 10 15
WIN_COUNT = how many in a row to win (e.g., 5 for classic Gomoku)ROWS = number of board rows (max 100)COLUMNS = number of board columns (max 100)The board appears. Click on any - cell to place your symbol (X)
The Computer (O) responds automatically after a short pause
First to get WIN_COUNT in a row wins!
The Computer opponent uses a priority-based greedy strategy:
Priority 1 โโ Can I WIN right now? โ Play winning move
Priority 2 โโ Can Human WIN next turn? โ Block that move
Priority 3 โโ Is the center cell free? โ Take the center
Priority 4 โโ Fallback โ Take any empty cell
This is a Phase 2 implementation. The AI does not use Minimax yet, but it successfully defends and attacks in most scenarios.
๐ Project
โโโ BSAI25033-PROJECT-2-PHASE-2-FIXED.cpp โ Main source file (bug-fixed)
โโโ README.md โ This file
| # | Function | Bug | Fix Applied |
|---|---|---|---|
| 1 | h_left() | c - WIN_COUNT < 0 is off-by-one โ rejects valid moves | Changed to c - (WIN_COUNT - 1) < 0 |
| 2 | v_up() | r - WIN_COUNT < 0 same off-by-one issue | Changed to r - (WIN_COUNT - 1) < 0 |
| 3 | d_rdown() | c - WIN_COUNT < -1 wrong comparison | Changed to c - (WIN_COUNT - 1) < 0 |
| 4 | d_rup() | c - WIN_COUNT < -1 wrong comparison | Changed to c - (WIN_COUNT - 1) < 0 |
| 5 | d_lup() | c + WIN_COUNT > C allows out-of-bounds access | Changed to c + (WIN_COUNT - 1) >= C |
| 6 | Draw Box | gotoRowCol(10, 8) placed text outside the drawn box | Corrected to gotoRowCol(6, 10) to center inside box |
| 7 | AI humanTurn | Hardcoded humanTurn = 0 breaks if turn order changes | Now computed dynamically as (turn + 1) % NOP |
| 8 | Mouse Event | No EventType == MOUSE_EVENT check โ keyboard events could trigger false clicks | Added InputRecord.EventType == MOUSE_EVENT guard |
| 9 | Win checks | Used count == WIN_COUNT counter (misses gaps) | Replaced with direct != P_sym early-exit loop |
| 10 | Board colors | All cells printed in same color | Added per-symbol color: X=Red, O=Cyan, -=Dark Gray |
initialization() // Sets up board, player names, symbols, random first turn
display_board() // Renders the board with colors to console
update_board() // Places a symbol at given row/col
winning() // Checks all 8 directions from last move for a win
is_draw() // Returns true if no empty cell remains
computer_move() // AI: Win > Block > Center > Fallback
turn_change() // Alternates turn between 0 and 1
getRowColbyLeftClick() // Captures mouse left-click position via Windows API
print_box() // Draws a solid-border box for win/draw announcements
โ โ โ
โ โ โ
โ โ โ
All 8 directions are checked from the last placed piece, making detection efficient โ only the affected position is evaluated each turn.
SetConsoleCursorPosition, SetConsoleTextAttribute, ReadConsoleInput)| Field | Info |
|---|---|
| Name | BSAI25033 |
| Program | BS Artificial Intelligence |
| Semester | 1st Semester |
| Project | Project 2 โ Phase 2 |
| Language | C++ (Console, Windows) |
This project was created as an academic submission. Feel free to use it for learning purposes.
Made with โค๏ธ and way too many for loops โ first semester, first project, all from scratch.