A Python implementation of the MNK game (a generalization of tic‑tac‑toe). Play against the computer with three difficulty levels. Developed as part of an academic Programming Fundamentals project.
- Overview
- Features
- Project structure
- Main functions
- AI difficulty levels
- How to run
- Game rules
- Example run
- Requirements
- Contributing
- Author
The objective is to align K pieces in a row (horizontal, vertical, or diagonal) on an M×N board. A human player competes against an AI-controlled opponent. The player who uses symbol X always goes first.
- Configurable board size: M x N and target K
- Three AI difficulty levels: Easy, Normal, Hard
- Automatic detection of wins, losses, and draws
- Text-based interface for the terminal
- Player vs Computer mode
The repository contains a single main script:
FP2425P1.py— full implementation of the game, including the functions listed below.
Main functions
eh_tabuleiro()— validates the board structuretabuleiro_para_str()— converts the board into a string visual representationjogo_mnk()— main function that starts and runs the gameescolhe_posicao_manual()— handles the user's move inputescolhe_posicao_auto()— implements the computer AI
- Easy: Chooses moves adjacent to its own pieces (simple heuristic).
- Normal: Blocks opponent's immediate winning moves and tries to create winning sequences.
- Hard: Simulates future moves to make more strategic decisions (deeper analysis).
- Make sure you have Python 3 installed.
- Run the script from a terminal:
python FP2425P1.pyDuring execution the program will ask for:
- Board dimensions: M, N and K
- Player symbol:
XorO - Computer difficulty level:
Easy,NormalorHard
- A player wins by aligning K consecutive pieces horizontally, vertically, or diagonally.
- The game ends when one player wins or when the board is full (draw).
- The player using
Xmoves first.
Example terminal output:
Welcome to MNK GAME.
The player plays with 'X'.
+---+---+---+
| | | |
+---+---+---+
| | | |
+---+---+---+
| | | |
+---+---+---+
Player's turn. Choose a free position: 5
When prompted, enter the number corresponding to a free cell.
- Python 3.x
- No external libraries required
Contributions are welcome — bug fixes, AI improvements, refactoring, or additional tests. If you'd like, I can:
- Split the single script into modules (for example
board.py,ai.py,cli.py) - Add unit tests
- Improve documentation with more examples
Tell me which option(s) you prefer and I can prepare changes or a patch.
Developed as part of an academic Programming Fundamentals assignment.