110 lines
3.5 KiB
Python
110 lines
3.5 KiB
Python
from cup import Cup
|
|
import numpy as np
|
|
|
|
class Board:
|
|
|
|
# Remember to handle pushing other pieces to home
|
|
# Also remember that a player can't move backwards and the one player goes from 1-47
|
|
# while the other goes from 47-1
|
|
|
|
|
|
def __init__(self):
|
|
self.cup = Cup()
|
|
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 -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.
|
|
|
|
# Also, the check_move will also fail when you're attempting to leave a jail. A fix of this is of course to check if the from_ = jail and if so, allow some extra stuff!
|
|
|
|
def check_move(self, move, sym, roll):
|
|
from_ = int(move[0])
|
|
to = int(move[1])
|
|
if from_ == 26:
|
|
from_ = 1
|
|
roll -= 1
|
|
elif from_ == 27:
|
|
from_ = 24
|
|
roll -= 1
|
|
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] * 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 * 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
|
|
# If player is O, then check for idx + roll
|
|
# If player is X, then check for idx - roll
|
|
|
|
# Rewrite this, it's shit.
|
|
idxs_with_thing = self.find_pieces_for_player(sym)
|
|
legal_moves = []
|
|
|
|
for index in idxs_with_thing:
|
|
from_ = index
|
|
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 """
|
|
-------------------------------------------------------------
|
|
|{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 == 1:
|
|
self.state[26] += sym
|
|
else:
|
|
self.state[27] += sym
|
|
|
|
def move_thing(self, cur_sym, from_, to):
|
|
|
|
self.state[from_] -= cur_sym
|
|
|
|
if self.state[to] * cur_sym <= -1:
|
|
self.move_to_jail(self.switch(cur_sym))
|
|
self.state[to] = 0
|
|
|
|
self.state[to] += cur_sym
|
|
|
|
|
|
|