2023 ECM1400 Coursework - Battleships
Project description
ECM1400 Battleships
Introduction
A python package (availible on PyPi) to simulate a Battleships game against an AI in both a command line interface and a Flask GUI.
Coursework Self assessment
All features required by specification are complete (including testing & logging). Note: main.py uses classes to avoid global variables.
Extensions
Entry Screen
Users can select game settings (size, difficulty, storm mode) on an Entry page. main.html now has a menu button so games can be replayed.
HTML Template Sunken ships
Sunken ship positions for either the AI or Player to the HTML code, and it colours those tiles green.
Storm mode
When activated, the player's board shifts one space each turn (as suggested by specification). Since the main.html's grid isn't persistent this had to be in javascript
Packaging
- Packaged with PyPi
- Detailed documentation from sphinx
AI attacking
There are 5 difficulties code here 0. Pure Random Guessing
- Random but it won't guess same space twice
- Guessing a random position around hits where the ship is still unsunk. If no unsunk hits it guesses randomly
- Guesses in a line following unsunk hits (more accurate method)
- Guesses in a line if there are unsunk hits, if not it calculates all possible ship placements in the board given what's already guessed. Then chooses the tile that has the highest probability of a ship
Wrote a script to score AIs against themselves.
Prerequisites
- Flask must be installed
pip install Flask
- numpy must be installed
pip install numpy
- For testing pytest must be installed
pip install pytest
- Pytest plugins must be installed
pip install pytest-depends
pip install pytest-cov
- Python version must be ≥ 3.10 (pip package requires ≥ 3.11) due to
|
symbol in typehinting
Version used in running = Python 3.11.4
Installation and Getting Started
There are 3 modes to run the game:
- Run the game_engine file as a module for a CLI game against yourself
- Run the mp_game_engine file for a CLI game against an AI
- Run the main file for a full GUI game. Once you have run this program navigate to http://127.0.0.1:5000 on a browser of choice.
If you are using the Flask (main.py) application then on my version you do not need to enter via /placement. The script will automatically render the correct webpage and handle logic (entry, placement or main) depending on your game status.
There are two ways to install the package:
pip (recommended)
- In a new venv or for the whole computer run
pip install battleships-edf1101
- This will automatically install Flask and numpy
- You can then run
python -m battleships.main
orpython -m battleships.game_engine
orpython -m battleships.mp_game_engine
to run each mode
Manually
- Get this repository's folder (clone from github, or download otherwise)
- Open it either either with a virtual environment (useful if using PyCharm) and install Flask and numpy manually as shown in prerequisites
- Or you can install Flask and numpy for the whole PC
- To open files you can cd to the battleships directory (the source code folder not the project root) and run
python -m main
(note if you are running it from within the source code directory you don't need to have battleships. prefix) - Alternatively you could open them manually in a file browser and run them
Developer Documentation
All documentation for source code can be found here
In general
- Components.py: Contains the basic functions for creating Battleships games
- game_engine.py: Contains functions to run a single player game and to run a single player game
- mp_game_engine.py: Contains functions needed for multiplayer games (against AI)
- main.py: Contains the framework needed for a Flask GUI game
- advanced_ai.py: Contains the functions needed for the extension 5 versions of AI
- ai_comparison.py: Contains functions to compare different versions of AI difficulties
Testing
If pytest & plugins are installed then you can test in 2 ways.
Note scripts like game_engine.py, mp_game_engine and main.py don't have full coverage as some functions require input statements which my unittests were not intended to mock
- Use an IDE like PyCharm to run the tests manually
- Navigate to the tests folder in the project (from the root directory it would be
cd tests
then runpytest
)
pylint
pylint can be run by going to either the root of the project or the source code folder in the terminal then running
pylint battleships
Logging
Logs are taken for error events and for general game actions.
Logs can be found at the working directory for python, this will be different depending on how you installed the package.
Details
License
MIT License: found here
Authors
- Student 730003140
Acknowledgments
This coursework is for the University of Exeter.
Thank you to the Computer Science department.
Source
Code hosted (may be public or private depending on time of reading) on GitHub Packaged at : PyPi
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file battleships-edf1101-1.8.tar.gz
.
File metadata
- Download URL: battleships-edf1101-1.8.tar.gz
- Upload date:
- Size: 118.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0bdd16c45f2269067a5b607560f8be2267e885d76b79c08db37418ae5d8069a5 |
|
MD5 | 1cdab880a4f6c58424b42c7ccc32d571 |
|
BLAKE2b-256 | f7a2471f03c6734bc410c07853c007423b965864b88641c3463cc896fa838d5a |