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):
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):
return self.state
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.
@ -27,28 +27,33 @@ class Board:
def check_move(self, move, sym, roll):
from_ = int(move[0])
to = int(move[1])
if from_ == 24:
from_ = 0
if from_ == 26:
from_ = 1
roll -= 1
elif from_ == 25:
from_ = 23
elif from_ == 27:
from_ = 24
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
elif (abs(from_ - to) != roll):
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
def find_pieces_for_player(self,sym):
idxs = []
for idx, pip in enumerate(self.state):
if pip[0] == sym:
if pip * sym >= 1:
idxs.append(idx)
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):
# Find all pips with things on them belonging to the player
# Iterate through each index and check if it's a possible move given the roll
@ -57,54 +62,48 @@ class Board:
# Rewrite this, it's shit.
idxs_with_thing = self.find_pieces_for_player(sym)
legal_moves = []
if sym == "O":
for index in idxs_with_thing:
from_ = index
to = index+roll
if self.check_move([from_,to], sym, roll):
legal_moves.append([from_,to])
else:
for index in idxs_with_thing:
from_ = index
to = index-roll
to = index+(roll*sym)
if self.check_move([from_,to], sym, roll):
legal_moves.append([from_,to])
return legal_moves
def to_s(self):
temp = []
for x in self.state:
if x >= 0:
temp.append(" {}".format(x))
else:
temp.append("{}".format(x))
return """
------------------------------------------
| {22}| {20}| {18}| {16}| {14}| {12}|{48}| {10}| {8}| {6}| {4}| {2}| {0}|
| {23}| {21}| {19}| {17}| {15}| {13}| {49} | {11}| {9}| {7}| {5}| {3}| {1}|
|--|--|--|--|--|--|----|--|--|--|--|--|--|
| {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(*np.array(self.state).flatten())
-------------------------------------------------------------
|{12}|{11}|{10}|{9}|{8}|{7}|bar 1: {26} |{6}|{5}|{4}|{3}|{2}|{1}|end 1: {0}|
|--|--|--|--|--|--|--------|--|--|--|--|--|--|
|{13}|{14}|{15}|{16}|{17}|{18}|bar -1: {27} |{19}|{20}|{21}|{22}|{23}|{24}|end -1: {25}|
-------------------------------------------------------------
""".format(*temp)
def move_to_jail(self,sym):
if sym == "O":
self.state[24][1] += 1
self.state[24][0] = "Jail"
if sym == 1:
self.state[26] += sym
else:
self.state[25][1] += 1
self.state[25][0] = "Jail"
self.state[27] += sym
def move_thing(self, cur_sym, from_, to):
self.state[from_][1] -= 1
self.state[from_] -= cur_sym
if self.state[from_][1] == 0:
self.state[from_][0] = " "
if self.state[to][0] == self.switch(cur_sym):
if self.state[to] * cur_sym <= -1:
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][1] += 1
self.state[to] += cur_sym

15
bot.py
View File

@ -16,19 +16,26 @@ class Bot:
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):
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])
move = random.choice(moves_1)
print("{} was picked as move".format(move))
self.board.move_thing(self.sym, int(move[0]), int(move[1]))
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])
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]))

10
game.py
View File

@ -6,17 +6,23 @@ class Game:
def __init__(self):
self.board = Board()
self.p1 = Human(self.board, "O")
self.p2 = Bot(self.board, "X")
self.p1 = Human(self.board, 1)
self.p2 = Bot(self.board, -1)
def play(self):
while True:
# print(self.board.to_s())
roll = self.p1.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())
roll = self.p2.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.play()

View File

@ -2,6 +2,9 @@ from cup import Cup
class Human:
global cup
cup = Cup()
def __init__(self, board, sym):
self.cup = Cup()
self.board = board
@ -15,7 +18,10 @@ class Human:
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):
print(self.board.to_s())
@ -32,9 +38,8 @@ class Human:
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(self.board.find_legal_moves(self.sym,roll[1]))
print("What to do with the second roll?")
cmds_2 = input().split(",")
while not self.board.check_move(cmds_2, self.sym, roll[1]):