-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructure the code, create installable package (#17)
* Restructure the folder. Change the year in the license file * Restructure the folder. * Fix failing ci. * fix wrong path * fix mypy options
- Loading branch information
Showing
28 changed files
with
307 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: code-lints | ||
on: [push] | ||
|
||
jobs: | ||
build: | ||
strategy: | ||
matrix: | ||
os: ['ubuntu-latest'] | ||
python-version: [3.8] | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v1 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -e game[lint] | ||
- name: flake8 | ||
run: flake8 game/ | ||
- name: mypy | ||
run: cd game && mypy . --check-untyped-defs | ||
- name: codestyle | ||
run: pycodestyle game |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Mathematico | ||
|
||
A classic game, for unlimited number of players. Each player keeps track of their own board | ||
and after 25 random choices of numbers, the score is calculated for each player and the highest score wins. | ||
|
||
|
||
## Rules of the Game | ||
|
||
The game of Mathematico is played on a *5x5 grid*. In each round, a card with a number is drawn | ||
from the deck consisting of cards with numbers in range 1-13, with 4 copies of each, and players | ||
are obliged to fill the number in one of the empty cells on their board. When the boards are full, | ||
resulting scores are computed (see below) for each board, and the player with the highest score wins. | ||
|
||
### Scoring System | ||
|
||
For each line, row and two longest diagonals, the points are computed based on the following table | ||
and are summed across all rows, columns and diagonals. In addition, if the score in a diagonal | ||
is non-zero, the player is awarded *10 bonus points* for each such diagonal. | ||
|
||
*The numbers in the rows, columns and diagonals can be in __ANY__ order.* | ||
|
||
|
||
| Rule | Example | Points | ||
|--------------------------------- | -------------- | ------- | ||
| One pair | 1 2 3 4 1 | 10 | ||
| Two pairs | 1 2 2 3 1 | 20 | ||
| Three of a kind | 5 6 7 7 7 | 40 | ||
| Full House | 1 1 2 2 2 | 80 | ||
| Four of a kind (not number 1) | 1 2 2 2 2 | 160 | ||
| Four ones | 1 1 5 1 1 | 200 | ||
| Straight | 5 7 9 8 6 | 50 | ||
| Three 1s and two 13s | 1 13 1 13 1 | 100 | ||
| Numbers 1, 10, 11, 12, 13 | 12 11 13 1 10 | 150 | ||
|
||
|
||
For each row, column and diagonal, only the highest score is applied, i.e. it is forbidden | ||
to combine two scoring rules for one line. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from .game import Arena, Board, Mathematico, Player | ||
from .players import HumanPlayer, RandomPlayer, SimulationPlayer | ||
|
||
|
||
__all__ = [ | ||
"Arena", "Board", "Mathematico", "Player", | ||
"HumanPlayer", "RandomPlayer", "SimulationPlayer" | ||
] |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 6 additions & 3 deletions
9
src/mathematico/game/_utils.py → game/mathematico/game/_utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import time | ||
from typing import List, Any | ||
|
||
from .player import Player | ||
from ._mathematico import Mathematico | ||
|
||
|
||
class Arena: | ||
""" | ||
This class allows simulating multiple rounds of the game Mathematico. | ||
Mehods | ||
------ | ||
reset: reset the results so far | ||
add_player: add a player to the arena | ||
run: run the simulation | ||
""" | ||
|
||
def __init__(self): | ||
self.players: List[Player] = [] | ||
self.results: List[List[int]] = [] | ||
|
||
def reset(self): | ||
"""Clear the previous results, keep the players.""" | ||
for player_results in self.results: | ||
player_results.clear() | ||
|
||
def add_player(self, player: Player): | ||
"""Add new player to the arena.""" | ||
self.players.append(player) | ||
self.results.append([]) | ||
|
||
def run(self, rounds: int = 100, verbose: bool = True, seed: Any = None): | ||
""" | ||
Repeatedly play the game of Mathematico. | ||
Play Mathematico for the specified number of rounds and | ||
record the statistics (final score) for each of the players. | ||
Arguments | ||
--------- | ||
rounds: number of rounds to play | ||
verbose: if True, print the elapsed time, also passed | ||
to each round | ||
seed: the seed to play the same game from | ||
Returns | ||
------- | ||
result: 2d list, `results[idx]` is the list of scores | ||
obtained by `idx`-th player | ||
""" | ||
start = time.time() | ||
|
||
for _ in range(rounds): | ||
# initialize a new game | ||
game = Mathematico(seed=seed) | ||
for player in self.players: | ||
player.reset() | ||
game.add_player(player) | ||
|
||
# play the game and collect rewards | ||
results = game.play(verbose=False) | ||
for idx, result in enumerate(results): | ||
self.results[idx].append(result) | ||
|
||
if verbose: | ||
total_time = time.time() - start | ||
print(f"Steps run: {rounds}\tElapsed time: {total_time}") | ||
|
||
return self.results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from ._human_player import HumanPlayer | ||
from ._random_player import RandomPlayer | ||
from ._random_simulations import SimulationPlayer | ||
|
||
|
||
__all__ = ["RandomPlayer", "HumanPlayer", "SimulationPlayer"] |
Oops, something went wrong.