initial commit
This commit is contained in:
commit
34264fe8c4
0
__init__.py
Normal file
0
__init__.py
Normal file
78
board.py
Normal file
78
board.py
Normal file
|
@ -0,0 +1,78 @@
|
|||
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
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
10
cup.py
Normal file
10
cup.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
from dice import Dice
|
||||
|
||||
class Cup:
|
||||
|
||||
def __init__(self):
|
||||
self.dice_1 = Dice
|
||||
self.dice_2 = Dice
|
||||
|
||||
def roll(self):
|
||||
return [self.dice_1.roll(), self.dice_2.roll()]
|
5
dice.py
Normal file
5
dice.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
import random
|
||||
|
||||
class Dice:
|
||||
def roll():
|
||||
return random.randrange(1,7)
|
21
game.py
Normal file
21
game.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
from hooman import Human
|
||||
from board import Board
|
||||
|
||||
class Game:
|
||||
|
||||
def __init__(self):
|
||||
self.board = Board()
|
||||
self.p1 = Human(self.board, "O")
|
||||
self.p2 = Human(self.board, "X")
|
||||
|
||||
def play(self):
|
||||
while True:
|
||||
print(self.board.to_s())
|
||||
roll = self.p1.roll()
|
||||
self.p1.do_move(roll)
|
||||
print(self.board.to_s())
|
||||
roll = self.p2.roll()
|
||||
self.p2.do_move(roll)
|
||||
|
||||
g = Game()
|
||||
g.play()
|
44
hooman.py
Normal file
44
hooman.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
from cup import Cup
|
||||
|
||||
class Human:
|
||||
|
||||
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())
|
||||
|
||||
cur_state = self.board.get_state()
|
||||
|
||||
print("What to do with the first roll?")
|
||||
cmds_1 = input().split(",")
|
||||
|
||||
while not self.board.check_move(cmds_1, self.sym, roll[0]):
|
||||
print("Invalid move, try again.")
|
||||
cmds_1 = input().split(",")
|
||||
|
||||
self.board.move_thing(self.sym, int(cmds_1[0]), int(cmds_1[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]):
|
||||
print("Invalid move")
|
||||
cmds_2 = input().split(",")
|
||||
|
||||
|
||||
self.board.move_thing(self.sym, int(cmds_2[0]), int(cmds_2[1]))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user