From ea9b8d3feb2c71ee1e9114d1d63fc9195411b0bf Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Mon, 5 Feb 2018 23:50:31 +0100 Subject: [PATCH] Now actually has a bot playing, although randomly, it still works --- board.py | 2 +- bot.py | 34 ++++++++++++++++++++++++++++++++++ game.py | 7 ++++--- hooman.py | 3 +++ 4 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 bot.py diff --git a/board.py b/board.py index b4ffee8..1be2f03 100644 --- a/board.py +++ b/board.py @@ -57,7 +57,7 @@ class Board: # Rewrite this, it's shit. idxs_with_thing = self.find_pieces_for_player(sym) - print(idxs_with_thing) + legal_moves = [] if sym == "O": for index in idxs_with_thing: diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..d230d33 --- /dev/null +++ b/bot.py @@ -0,0 +1,34 @@ +from cup import Cup +import random + +class Bot: + + def __init__(self, board, sym): + self.cup = Cup() + self.board = board + self.sym = sym + + def roll(self): + print("{} rolled: ".format(self.sym)) + roll = self.cup.roll() + print(roll) + return roll + + + def switch(self,cur): + return "X" if cur == "O" else "O" + + def do_move(self, roll): + print(self.board.to_s()) + + moves_1 = self.board.find_legal_moves(self.sym,roll[0]) + move = random.choice(moves_1) + self.board.move_thing(self.sym, int(move[0]), int(move[1])) + + print(self.board.to_s()) + + moves_2 = self.board.find_legal_moves(self.sym,roll[1]) + omove = random.choice(moves_2) + self.board.move_thing(self.sym, int(move[0]), int(move[1])) + + diff --git a/game.py b/game.py index c52a398..3622269 100644 --- a/game.py +++ b/game.py @@ -1,19 +1,20 @@ from hooman import Human from board import Board +from bot import Bot class Game: def __init__(self): self.board = Board() self.p1 = Human(self.board, "O") - self.p2 = Human(self.board, "X") + self.p2 = Bot(self.board, "X") def play(self): while True: - print(self.board.to_s()) +# print(self.board.to_s()) roll = self.p1.roll() self.p1.do_move(roll) - print(self.board.to_s()) +# print(self.board.to_s()) roll = self.p2.roll() self.p2.do_move(roll) diff --git a/hooman.py b/hooman.py index f78c8a8..24d2957 100644 --- a/hooman.py +++ b/hooman.py @@ -30,8 +30,11 @@ class Human: cmds_1 = input().split(",") self.board.move_thing(self.sym, int(cmds_1[0]), int(cmds_1[1])) + + print(self.board.to_s()) + print(self.board.find_legal_moves(self.sym,roll[0])) print("What to do with the second roll?") cmds_2 = input().split(",") while not self.board.check_move(cmds_2, self.sym, roll[1]):