-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Main loop for Python bots, example Python bot
- Loading branch information
Showing
9 changed files
with
124 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,4 @@ | ||
{ | ||
"nick": "SchwarzeneggerBot", | ||
"cmd": [".venv/bin/python3", "bots/schwarzenegger_bot.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from gomori import * | ||
import sys | ||
|
||
class SchwarzeneggerBot(Bot): | ||
def new_game(self, color: Color): | ||
pass | ||
|
||
def play_first_turn(self, cards: List[Card]) -> Card: | ||
print("I was elected to lead, not to read. Number 3!", file=sys.stderr) | ||
return cards[2] | ||
|
||
def play_turn( | ||
self, | ||
cards: List[Card], | ||
board: Board, | ||
cards_won_by_opponent: CardsSet | ||
) -> PlayTurnResponse: | ||
ctp = CardToPlay(card=cards[2], i=0, j=0) | ||
return PlayTurnResponse([ctp]) | ||
|
||
run_bot(SchwarzeneggerBot()) |
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,69 @@ | ||
use gomori::{Board, Card, CardsSet, Color, Field, PlayTurnResponse}; | ||
use gomori_bot_utils::Bot; | ||
use pyo3::{pyfunction, types::PyDict, Py, PyObject, Python}; | ||
|
||
struct PythonBot { | ||
bot: PyObject, | ||
} | ||
|
||
// TODO: Re-evaluate this whole design | ||
impl gomori_bot_utils::Bot for PythonBot { | ||
fn new_game(&mut self, color: Color) { | ||
Python::with_gil(|py| { | ||
let kwargs = PyDict::new(py); | ||
kwargs | ||
.set_item("color", Py::new(py, color).unwrap()) | ||
.unwrap(); | ||
self.bot | ||
.call_method(py, "new_game", (), Some(kwargs)) | ||
.expect("Call to new_game() failed"); | ||
}) | ||
} | ||
|
||
fn play_first_turn(&mut self, cards: [Card; 5]) -> Card { | ||
Python::with_gil(|py| { | ||
let kwargs = PyDict::new(py); | ||
kwargs | ||
.set_item("cards", cards.map(|card| Py::new(py, card).unwrap())) | ||
.unwrap(); | ||
self.bot | ||
.call_method(py, "play_first_turn", (), Some(kwargs)) | ||
.expect("Call to play_first_turn() failed") | ||
.extract(py) | ||
.expect("play_first_turn() returned wrong type") | ||
}) | ||
} | ||
|
||
fn play_turn( | ||
&mut self, | ||
cards: [Card; 5], | ||
fields: Vec<Field>, | ||
cards_won_by_opponent: CardsSet, | ||
) -> PlayTurnResponse { | ||
Python::with_gil(|py| { | ||
let kwargs = PyDict::new(py); | ||
kwargs | ||
.set_item("cards", cards.map(|card| Py::new(py, card).unwrap())) | ||
.unwrap(); | ||
kwargs | ||
.set_item("board", Py::new(py, Board::new(&fields)).unwrap()) | ||
.unwrap(); | ||
kwargs | ||
.set_item( | ||
"cards_won_by_opponent", | ||
Py::new(py, cards_won_by_opponent).unwrap(), | ||
) | ||
.unwrap(); | ||
self.bot | ||
.call_method(py, "play_turn", (), Some(kwargs)) | ||
.expect("Call to play_turn() failed") | ||
.extract(py) | ||
.expect("play_turn() returned wrong type") | ||
}) | ||
} | ||
} | ||
|
||
#[pyfunction] | ||
pub fn run_bot(bot: PyObject) { | ||
PythonBot { bot }.run().unwrap() | ||
} |
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