Skip to content

Commit cef4314

Browse files
authored
Merge pull request #3 from adam-rumpf/cmd
Merging command line branch.
2 parents 7c7efb5 + 73c973e commit cef4314

File tree

2 files changed

+76
-9
lines changed

2 files changed

+76
-9
lines changed

README.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Turtle AIs are written in Python and imported into the game by adding submodules
2222

2323
This module was developed for Python 3.8.3.
2424

25-
In an effort to maintain portability it uses only modules from the [Python Standard Library](https://docs.python.org/3/library/), including: `glob`, `inspect`, `math`, `os.path`, `random`, `tkinter`
25+
In an effort to maintain portability it uses only modules from the [Python Standard Library](https://docs.python.org/3/library/), including: `argparse`, `glob`, `inspect`, `math`, `os.path`, `random`, `tkinter`
2626

2727
## Credits
2828

@@ -56,9 +56,34 @@ This section is meant to provide an overview of how the _Combat Turtles_ game wo
5656

5757
## Running the Game
5858

59-
To begin a game of _Combat Turtles_, run the main driver `combatturtles.py` found in the root of this directory and execute the function `combat_turtles()`. This will take you through a series of command line entries where the available turtle AIs and arenas will be displayed and selected.
59+
A game of _Combat Turtles_ can be initiated by running the main driver script `combatturtles.py` found at the root level of this directory.
6060

61-
If you already know ahead of time which AIs and arena you wish to load, you can streamline this process by including their indices as arguments of the `combat_turtles()` function. For example, `combat_turtles(0, 1, 2)` would attempt to begin the game with AI `0` playing against AI `1` in arena `2`. Note that the AI indices are based on the alphabetical order of the AI submodules in the `ai/` folder, and thus these indices may change as you add files to this folder.
61+
The public `combat_turtles()` function within this script begins the game, at which point the user will be taken through a series of text-driven menus to choose from the available turtle AIs and arenas. The keyword arguments of this function can be used to specify AI and arena IDs ahead of time, bypassing the need to select them from a menu. In order, the arguments specify: the player 1 AI, the player 2 AI, and the arena ID, so for example `combat_turtles(1, 5, 3)` would attempt to begin a game with AI `1` (`DirectTurtle` by default) as player 1, AI `5` (`TurretTurtle` by default) as player 2, and taking place in arena `3` (wall with gap by default).
62+
63+
Running `combatturtles.py` from the command line automatically initiates a game. Command line arguments can be used to specify the keyword arguments of the `combat_turtles()` function. The usage is as follows:
64+
65+
```
66+
usage: combatturtles.py [-h] [-v] [-f P1] [-s P2] [-a A] [-c LIM]
67+
68+
Initializes a game of Combat Turtles. Command line arguments can be supplied
69+
to specify player AIs and the arena (see below for details). Excluding any of
70+
these arguments will prompt the user to specify them on startup.
71+
72+
Note that the player AIs are indexed alphabetically, which may cause indices
73+
to change as new modules are added to the ai/ directory.
74+
75+
optional arguments:
76+
-h, --help show this help message and exit
77+
-v, --version show program's version number and exit
78+
-f P1, --first P1 player 1 AI index
79+
-s P2, --second P2 player 2 AI index
80+
-a A, --arena A arena index
81+
-c LIM, --cutoff LIM iteration cutoff (default: unlimited)
82+
83+
See full documentation online at <adam-rumpf.github.io/combat-turtles>.
84+
```
85+
86+
In both cases the turtle AIs are indexed alphabetically beginning with `0`, which may change as more AI modules are added to the `ai/` directory. If a given ID is unspecified or invalid, the user will be asked to specify a value in the text-driven menu.
6287

6388
## Including a Custom AI Submodule
6489

combatturtles.py

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
of Turtle Combat and then starts the game.
55
"""
66

7+
import argparse
78
import inspect
89
import game
910
import ai
@@ -14,6 +15,11 @@ def combat_turtles(tid1=-1, tid2=-1, aid=-1, cutoff=-1):
1415
"""combat_turtles() -> None
1516
Combat Turtles game driver.
1617
18+
If loading this package as a module this function should be used to
19+
initiate a game of Combat Turtles. If running this package as a script (or
20+
from the command line) this function is run automatically, with inputs
21+
given by the command line.
22+
1723
By default this function will present the user with options for the game
1824
setup, including which turtle AI modules to use and which arena layout to
1925
use. Optional keyword arguments can be used to automatically fill these
@@ -164,9 +170,45 @@ def _arena_table(arenas):
164170

165171
#=============================================================================
166172

167-
# Execute function (add optional arguments to streamline process)
168-
# tid1 -- player 1 ID
169-
# tid2 -- player 2 ID
170-
# aid -- arena ID
171-
# cutoff -- game time limit (steps)
172-
combat_turtles()
173+
# Define docstrings for command line usage
174+
_vers = """
175+
Combat Turtles v1.1.0
176+
Copyright (c) 2021 Adam Rumpf <adam-rumpf.github.io>
177+
Released under MIT License <github.com/adam-rumpf/combat-turtles>
178+
"""
179+
_desc = """
180+
Initializes a game of Combat Turtles. Command line arguments can be supplied
181+
to specify player AIs and the arena (see below for details). Excluding any of
182+
these arguments will prompt the user to specify them on startup.
183+
184+
Note that the player AIs are indexed alphabetically, which may cause indices
185+
to change as new modules are added to the ai/ directory.
186+
"""
187+
_epil = ("See full documentation online at " +
188+
"<adam-rumpf.github.io/combat-turtles>.")
189+
190+
# Initialize game (options can be set from command line)
191+
if __name__ == "__main__":
192+
193+
# Initialize argument parser
194+
parser = argparse.ArgumentParser(description=_desc, epilog=_epil,
195+
formatter_class=
196+
argparse.RawDescriptionHelpFormatter)
197+
198+
# Define arguments
199+
parser.add_argument("-v", "--version", action="version", version=_vers)
200+
parser.add_argument("-f", "--first", action="store", default=-1,
201+
type=int, dest="p1", help="player 1 AI index")
202+
parser.add_argument("-s", "--second", action="store", default=-1,
203+
type=int, dest="p2", help="player 2 AI index")
204+
parser.add_argument("-a", "--arena", action="store", default=-1,
205+
type=int, dest="a", help="arena index")
206+
parser.add_argument("-c", "--cutoff", action="store", default=-1,
207+
type=int, dest="lim",
208+
help="iteration cutoff (default: unlimited)")
209+
210+
# Parse command line arguments
211+
args = parser.parse_args()
212+
213+
# Run game
214+
combat_turtles(tid1=args.p1, tid2=args.p2, aid=args.a, cutoff=args.lim)

0 commit comments

Comments
 (0)