Changed look of board, now using -1 and 1 as players, cleaner, easier and faster

This commit is contained in:
Alexander Munch-Hansen 2018-02-06 23:29:51 +01:00
parent ea9b8d3feb
commit 3c0625ef47
4 changed files with 71 additions and 54 deletions

View File

@ -10,14 +10,14 @@ class Board:
def __init__(self): def __init__(self):
self.cup = Cup() self.cup = Cup()
self.state = [["O",2],[" ",0],[" ",0],[" ",0],[" ",0],["X",5],[" ",0],["X",3],[" ",0],[" ",0],[" ",0],["O",5],["X",5],[" ",0],[" ",0],[" ",0],["O",3],[" ",0],["O",5],[" ",0],[" ",0],[" ",0],[" ",0],["X",2], ["Jail",0], ["Jail",0]] self.state = [0,2,0,0,0,0,-5,0,-3,0,0,0,5,-5,0,0,0,3,0,5,0,0,0,0,-2,0, 0, 0]
def get_state(self): def get_state(self):
return self.state return self.state
def switch(self,cur): def switch(self,cur):
return "X" if cur == "O" else "O" return -1 if cur == 1 else 1
# Remember to handle edge case when we're on the last moves and you may go from position 22 -> 24 on a 6, if you have no pieces behind 22. Simply check if any are behind if you're circle or if any are higher if you are X, then it can be allowed. # Remember to handle edge case when we're on the last moves and you may go from position 22 -> 24 on a 6, if you have no pieces behind 22. Simply check if any are behind if you're circle or if any are higher if you are X, then it can be allowed.
@ -27,27 +27,32 @@ class Board:
def check_move(self, move, sym, roll): def check_move(self, move, sym, roll):
from_ = int(move[0]) from_ = int(move[0])
to = int(move[1]) to = int(move[1])
if from_ == 24: if from_ == 26:
from_ = 0 from_ = 1
roll -= 1 roll -= 1
elif from_ == 25: elif from_ == 27:
from_ = 23 from_ = 24
roll -= 1 roll -= 1
if (from_ < 0 or from_ > 23) or (to < 0 or to > 23): if (from_ < 1 or from_ > 24) or (to < 1 or to > 24):
return False return False
elif (abs(from_ - to) != roll): elif (abs(from_ - to) != roll):
return False return False
elif ((self.state[to][0] == sym or self.state[to][0] == " ") or (self.state[to][0] == self.switch(sym) and self.state[to][1] == 1)) and self.state[from_][0] == sym: elif ((self.state[to] * sym >= 0 or self.state[to] == 0) or (self.state[to] * self.switch(sym) == 1)) and self.state[from_] * sym >= 1:
return True return True
def find_pieces_for_player(self,sym): def find_pieces_for_player(self,sym):
idxs = [] idxs = []
for idx, pip in enumerate(self.state): for idx, pip in enumerate(self.state):
if pip[0] == sym: if pip * sym >= 1:
idxs.append(idx) idxs.append(idx)
return idxs return idxs
def is_winner(self,sym):
for i in range(1,25):
if sym * self.state[i] < 0:
return False
return True
def find_legal_moves(self,sym,roll): def find_legal_moves(self,sym,roll):
# Find all pips with things on them belonging to the player # Find all pips with things on them belonging to the player
@ -57,54 +62,48 @@ class Board:
# Rewrite this, it's shit. # Rewrite this, it's shit.
idxs_with_thing = self.find_pieces_for_player(sym) idxs_with_thing = self.find_pieces_for_player(sym)
legal_moves = [] legal_moves = []
if sym == "O":
for index in idxs_with_thing: for index in idxs_with_thing:
from_ = index from_ = index
to = index+roll to = index+(roll*sym)
if self.check_move([from_,to], sym, roll): if self.check_move([from_,to], sym, roll):
legal_moves.append([from_,to]) legal_moves.append([from_,to])
else:
for index in idxs_with_thing:
from_ = index
to = index-roll
if self.check_move([from_,to], sym, roll):
legal_moves.append([from_,to])
return legal_moves return legal_moves
def to_s(self): def to_s(self):
temp = []
for x in self.state:
if x >= 0:
temp.append(" {}".format(x))
else:
temp.append("{}".format(x))
return """ return """
------------------------------------------ -------------------------------------------------------------
| {22}| {20}| {18}| {16}| {14}| {12}|{48}| {10}| {8}| {6}| {4}| {2}| {0}| |{12}|{11}|{10}|{9}|{8}|{7}|bar 1: {26} |{6}|{5}|{4}|{3}|{2}|{1}|end 1: {0}|
| {23}| {21}| {19}| {17}| {15}| {13}| {49} | {11}| {9}| {7}| {5}| {3}| {1}| |--|--|--|--|--|--|--------|--|--|--|--|--|--|
|--|--|--|--|--|--|----|--|--|--|--|--|--| |{13}|{14}|{15}|{16}|{17}|{18}|bar -1: {27} |{19}|{20}|{21}|{22}|{23}|{24}|end -1: {25}|
| {24}| {26}| {28}| {30}| {32}| {34}|{50}| {36}| {38}| {40}| {42}| {44}| {46}| -------------------------------------------------------------
| {25}| {27}| {29}| {31}| {33}| {35}| {51} | {37}| {39}| {41}| {43}| {45}| {47}| """.format(*temp)
------------------------------------------
""".format(*np.array(self.state).flatten())
def move_to_jail(self,sym): def move_to_jail(self,sym):
if sym == "O": if sym == 1:
self.state[24][1] += 1 self.state[26] += sym
self.state[24][0] = "Jail"
else: else:
self.state[25][1] += 1 self.state[27] += sym
self.state[25][0] = "Jail"
def move_thing(self, cur_sym, from_, to): def move_thing(self, cur_sym, from_, to):
self.state[from_][1] -= 1 self.state[from_] -= cur_sym
if self.state[from_][1] == 0: if self.state[to] * cur_sym <= -1:
self.state[from_][0] = " "
if self.state[to][0] == self.switch(cur_sym):
self.move_to_jail(self.switch(cur_sym)) self.move_to_jail(self.switch(cur_sym))
self.state[to][1] = 0 self.state[to] = 0
self.state[to][0] = cur_sym self.state[to] += cur_sym
self.state[to][1] += 1

15
bot.py
View File

@ -16,19 +16,26 @@ class Bot:
def switch(self,cur): def switch(self,cur):
return "X" if cur == "O" else "O" return -1 if cur == 1 else 1
def get_sym(self):
return self.sym
def do_move(self, roll): def do_move(self, roll):
print(self.board.to_s()) print(self.board.to_s())
print(self.board.find_legal_moves(self.sym, roll[0]))
moves_1 = self.board.find_legal_moves(self.sym,roll[0]) moves_1 = self.board.find_legal_moves(self.sym,roll[0])
move = random.choice(moves_1) move = random.choice(moves_1)
print("{} was picked as move".format(move))
self.board.move_thing(self.sym, int(move[0]), int(move[1])) self.board.move_thing(self.sym, int(move[0]), int(move[1]))
print(self.board.to_s()) print(self.board.to_s())
print(self.board.find_legal_moves(self.sym, roll[1]))
moves_2 = self.board.find_legal_moves(self.sym,roll[1]) moves_2 = self.board.find_legal_moves(self.sym,roll[1])
omove = random.choice(moves_2) move = random.choice(moves_2)
print("{} was picked as move".format(move))
self.board.move_thing(self.sym, int(move[0]), int(move[1])) self.board.move_thing(self.sym, int(move[0]), int(move[1]))

10
game.py
View File

@ -6,17 +6,23 @@ class Game:
def __init__(self): def __init__(self):
self.board = Board() self.board = Board()
self.p1 = Human(self.board, "O") self.p1 = Human(self.board, 1)
self.p2 = Bot(self.board, "X") self.p2 = Bot(self.board, -1)
def play(self): def play(self):
while True: while True:
# print(self.board.to_s()) # print(self.board.to_s())
roll = self.p1.roll() roll = self.p1.roll()
self.p1.do_move(roll) self.p1.do_move(roll)
if self.board.is_winner(self.p1.get_sym()):
print("{} won!".format(self.p1.get_sym()))
break
# print(self.board.to_s()) # print(self.board.to_s())
roll = self.p2.roll() roll = self.p2.roll()
self.p2.do_move(roll) self.p2.do_move(roll)
if self.board.is_winner(self.p2.get_sym()):
print("{} won!".format(self.p2.get_sym()))
break
g = Game() g = Game()
g.play() g.play()

View File

@ -2,6 +2,9 @@ from cup import Cup
class Human: class Human:
global cup
cup = Cup()
def __init__(self, board, sym): def __init__(self, board, sym):
self.cup = Cup() self.cup = Cup()
self.board = board self.board = board
@ -15,8 +18,11 @@ class Human:
def switch(self,cur): def switch(self,cur):
return "X" if cur == "O" else "O" return -1 if cur == 1 else 1
def get_sym(self):
return self.sym
def do_move(self, roll): def do_move(self, roll):
print(self.board.to_s()) print(self.board.to_s())
print(self.board.find_legal_moves(self.sym,roll[0])) print(self.board.find_legal_moves(self.sym,roll[0]))
@ -32,9 +38,8 @@ class Human:
self.board.move_thing(self.sym, int(cmds_1[0]), int(cmds_1[1])) self.board.move_thing(self.sym, int(cmds_1[0]), int(cmds_1[1]))
print(self.board.to_s()) print(self.board.to_s())
print(self.board.find_legal_moves(self.sym,roll[0])) print(self.board.find_legal_moves(self.sym,roll[1]))
print("What to do with the second roll?") print("What to do with the second roll?")
cmds_2 = input().split(",") cmds_2 = input().split(",")
while not self.board.check_move(cmds_2, self.sym, roll[1]): while not self.board.check_move(cmds_2, self.sym, roll[1]):