Created
October 2, 2017 12:17
-
-
Save Tachytaenius/939eabd3f6da1df87e852d3d8760b007 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import pygame | |
import random | |
def get_moves(player, opponent, speed): | |
if speed == 0: # No move that can be made will have any effect, so why allow any? | |
return [] | |
valids = [] | |
# Get a piece. | |
for i in range(6): | |
# Check the validity of the spot it would go to. | |
if player[i] + speed <= 15 and player[i] + speed not in player: | |
if player[i] + speed in opponent: | |
if player[i] == 8: # Is the contested position the the shared rosetta? | |
valids.append([i, opponent.index(player[i])]) # Add legal move to list. | |
else: | |
valids.append([i, None]) | |
return valids | |
def choose_move(choices): | |
while True: | |
try: | |
choice = int(input("Which move will you make? [0, " + str(choices) + "]: ")) | |
if 0 <= choice <= choices: | |
return choice | |
raise | |
except: | |
print("Invalid input. Must be an integer within [0, " + str(choices) + "]") | |
board = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] | |
# True = White | |
# False = Black | |
# None = Game over | |
turn = True | |
while turn is not None: | |
roll = random.randint(0, 4) | |
print(turn, board[True], board[False], roll) | |
if turn: | |
moves = get_moves(board[True], board[False], roll) | |
print(moves) | |
if len(moves) == 0: | |
print("White cannot move...") | |
elif len(moves) == 1: | |
print("White only has one valid move.") | |
board[True][moves[0][0]] += roll | |
if moves[0][1] is not None: | |
board[False][moves[0][1]] = 0 | |
else: | |
print("White must choose a piece to move.") | |
move = choose_move(len(moves)) | |
board[True][moves[move][0]] += roll | |
if moves[move][1] is not None: | |
board[False][moves[move][1]] = 0 | |
else: | |
moves = get_moves(board[True], board[False], roll) | |
print(moves) | |
if len(moves) == 0: | |
print("Black cannot move...") | |
elif len(moves) == 1: | |
print("Black only has one valid move.") | |
board[False][moves[0][0]] += roll | |
if moves[0][1] is not None: | |
board[True][moves[0][1]] = 0 | |
else: | |
print("Black must choose a piece to move.") | |
move = choose_move(len(moves)) | |
board[False][moves[move][0]] += roll | |
if moves[move][1] is not None: | |
board[True][moves[move][1]] = 0 | |
if all(piece == 15 for piece in board[True]): | |
turn = None | |
print("White wins!") | |
elif all(piece == 15 for piece in board[False]): | |
turn = None | |
print("Black wins!") | |
else: | |
turn = not turn # Swap turns. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment