backgammon/board.py

111 lines
4.0 KiB
Python
Raw Normal View History

2018-02-05 21:31:34 +00:00
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 = [["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]]
def get_state(self):
return self.state
def switch(self,cur):
return "X" if cur == "O" else "O"
# 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_ == 24:
from_ = 0
roll -= 1
elif from_ == 25:
from_ = 23
roll -= 1
if (from_ < 0 or from_ > 23) or (to < 0 or to > 23):
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:
return True
2018-02-05 22:36:32 +00:00
def find_pieces_for_player(self,sym):
idxs = []
for idx, pip in enumerate(self.state):
if pip[0] == sym:
idxs.append(idx)
return idxs
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
2018-02-05 21:31:34 +00:00
2018-02-05 22:36:32 +00:00
# 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:
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
if self.check_move([from_,to], sym, roll):
legal_moves.append([from_,to])
return legal_moves
2018-02-05 21:31:34 +00:00
def to_s(self):
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())
def move_to_jail(self,sym):
if sym == "O":
self.state[24][1] += 1
self.state[24][0] = "Jail"
else:
self.state[25][1] += 1
self.state[25][0] = "Jail"
def move_thing(self, cur_sym, from_, to):
self.state[from_][1] -= 1
if self.state[from_][1] == 0:
self.state[from_][0] = " "
if self.state[to][0] == self.switch(cur_sym):
self.move_to_jail(self.switch(cur_sym))
self.state[to][1] = 0
self.state[to][0] = cur_sym
self.state[to][1] += 1