Turns are now functioning

This commit is contained in:
Alexander Munch-Hansen 2018-04-14 18:47:38 +02:00
parent 7764a70799
commit 7993da0db7
2 changed files with 381 additions and 280 deletions

View File

@ -2,73 +2,64 @@
# TODO: An issue with the bouncing back things. It appears to do the move and then # TODO: An issue with the bouncing back things. It appears to do the move and then
# it doesn't properly restore the buckets to where they should be. # it doesn't properly restore the buckets to where they should be.
import random
import pygame import pygame
import threading
from board import Board from board import Board
import numpy as np import numpy as np
import time
# --- constants --- (UPPER_CASE names) # --- constants --- (UPPER_CASE names)
SCREEN_WIDTH = 1050
SCREEN_HEIGHT = 400
SPACING = 83.333
#BLACK = ( 0, 0, 0) class Board_painter:
#242 209 107
SAND = (242, 209, 107)
GREEN_FILT = (0,102,0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
SALMON = (250,128,114)
BLACK = (0,0,0)
BROWN = (160,82,45)
LIGHT_GREY = (220,220,220)
num_pieces = 15
FPS = 30
cen = SPACING/2 - 11
t = 5*SPACING - cen-22
m = 7*SPACING+50 - cen-22
STARTING_IDX_P1 = [[cen,0], [cen, 30], [cen, 60], [cen, 90], [cen,120], [SCREEN_WIDTH-cen-22, 0], [SCREEN_WIDTH-cen-22, 30], [t, 378],[t,348],[t,318],[m, 378], [m,348],[m,318],[m,288],[m,258]]
STARTING_IDX_P2 = [[cen, 378], [cen, 348], [cen, 318], [cen, 288], [cen, 258], [SCREEN_WIDTH-cen-22, 378], [SCREEN_WIDTH-cen-22, 348], [t, 0], [t, 30], [t, 60], [m, 0], [m,30],[m,60],[m,90],[m,120]] def __init__(self):
self.SCREEN_WIDTH = 1050
self.SCREEN_HEIGHT = 400
self.SPACING = 83.333
# --- classses --- (CamelCase names) #BLACK = ( 0, 0, 0)
#242 209 107
self.SAND = (242, 209, 107)
self.GREEN_FILT = (0,102,0)
self.WHITE = (255, 255, 255)
self.RED = (255, 0, 0)
self.SALMON = (250,128,114)
self.BLACK = (0,0,0)
self.BROWN = (160,82,45)
self.LIGHT_GREY = (220,220,220)
self.num_pieces = 15
self.FPS = 999
cen = self.SPACING/2 - 11
t = 5*self.SPACING - cen-22
m = 7*self.SPACING+50 - cen-22
self.STARTING_IDX_P1 = [[cen,0], [cen, 30], [cen, 60], [cen, 90], [cen,120], [self.SCREEN_WIDTH-cen-22, 0], [self.SCREEN_WIDTH-cen-22, 30], [t, 378],[t,348],[t,318],[m, 378], [m,348],[m,318],[m,288],[m,258]]
# empty self.STARTING_IDX_P2 = [[cen, 378], [cen, 348], [cen, 318], [cen, 288], [cen, 258], [self.SCREEN_WIDTH-cen-22, 378], [self.SCREEN_WIDTH-cen-22, 348], [t, 0], [t, 30], [t, 60], [m, 0], [m,30],[m,60],[m,90],[m,120]]
# --- functions --- (lower_case names) pygame.init()
# empty self.screen = pygame.display.set_mode((self.SCREEN_WIDTH, self.SCREEN_HEIGHT))
#screen_rect = screen.get_rect()
# --- main --- pygame.display.set_caption("Backgammon")
# - init -
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
#screen_rect = screen.get_rect()
pygame.display.set_caption("Backgammon")
# - objects -
self.all_rects = {-1 : [], 1 : []}
all_rects = {-1 : [], 1 : []} for p in [-1,1]:
x, y = [0, 0]
for p in [-1,1]:
if p == -1: if p == -1:
for idx in STARTING_IDX_P1: for idx in self.STARTING_IDX_P1:
all_rects[p] += [pygame.rect.Rect(idx[0],idx[1], 22, 22)] self.all_rects[p] += [pygame.rect.Rect(idx[0],idx[1], 22, 22)]
if p == 1: if p == 1:
for idx in STARTING_IDX_P2: for idx in self.STARTING_IDX_P2:
all_rects[p] += [pygame.rect.Rect(idx[0],idx[1], 22, 22)] self.all_rects[p] += [pygame.rect.Rect(idx[0],idx[1], 22, 22)]
# for i in range(num_pieces): # for i in range(num_pieces):
# x = x+20 # x = x+20
# all_rects[p] += [pygame.rect.Rect(x,y, 22, 22)] # all_rects[p] += [pygame.rect.Rect(x,y, 22, 22)]
@ -77,35 +68,42 @@ for p in [-1,1]:
all_drag = {-1 : [], 1 : []} self.all_drag = {-1 : [], 1 : []}
all_drag[-1] += [False]*num_pieces self.all_drag[-1] += [False]*self.num_pieces
all_drag[1] += [False]*num_pieces self.all_drag[1] += [False]*self.num_pieces
all_off = {-1 : [], 1 : []} self.all_off = {-1 : [], 1 : []}
all_off[-1] += [[0,0]]*num_pieces self.all_off[-1] += [[0,0]]*self.num_pieces
all_off[1] += [[0,0]]*num_pieces self.all_off[1] += [[0,0]]*self.num_pieces
is_true = False self.is_true = False
# - mainloop -
clock = pygame.time.Clock()
buckets = [[5,-1],[0,0],[0,0],[0,0],[3,1],[0,0],[5,1],[0,0],[0,0],[0,0],[0,0],[2,-1],[5,1],[0,0],[0,0],[0,0],[3,-1],[0,0],[5,-1],[0,0],[0,0],[0,0],[0,0],[2,1]]
running = True
player = 1
roll = []
def switch_player(roll):
roll = roll
player *= -1
def gen_buckets_from_board(board):
self.clock = pygame.time.Clock()
self.buckets = [[5,-1],[0,0],[0,0],[0,0],[3,1],[0,0],[5,1],[0,0],[0,0],[0,0],[0,0],[2,-1],[5,1],[0,0],[0,0],[0,0],[3,-1],[0,0],[5,-1],[0,0],[0,0],[0,0],[0,0],[2,1]]
self.running = True
self.player = -1
self.roll = [random.randrange(1, 7), random.randrange(1, 7)]
print("initial_roll:", self.roll)
self.from_board = None
self.from_buckets = [x for x in self.buckets]
self.from_locat = None
self.total_moves = 0
def switch_player(self):
self.player *= -1
print("CHANGED PLAYER!")
def gen_buckets_from_board(self, board):
meh = [] meh = []
for i in range(13,25): for i in range(13,25):
pin = board[i] pin = board[i]
print(pin) # print(pin)
meh.append([abs(pin), np.sign(pin)]) meh.append([abs(pin), np.sign(pin)])
for i in range(1,13): for i in range(1,13):
pin = board[i] pin = board[i]
@ -114,7 +112,7 @@ def gen_buckets_from_board(board):
return meh return meh
def gen_board_from_buckets(buckets): def gen_board_from_buckets(self, buckets):
board = [] board = []
board.append([0,0]) board.append([0,0])
for i in range(-1,-13,-1): for i in range(-1,-13,-1):
@ -126,16 +124,16 @@ def gen_board_from_buckets(buckets):
return board return board
def move_legal(from_board, buckets, roll): def move_legal(self, from_board, buckets, roll):
board = gen_board_from_buckets(buckets) board = self.gen_board_from_buckets(buckets)
legal_states = Board.calculate_legal_states(from_board, 1, roll) legal_states = Board.calculate_legal_states(from_board, self.player, roll)
# print(legal_states) # print(legal_states)
if board in [list(state) for state in list(legal_states)]: if board in [list(state) for state in list(legal_states)]:
return True return True
return False return False
def find_pin(self, pos):
def find_pin(pos): SPACING = self.SPACING
x,y = pos x,y = pos
x -= 50 if x > 550 else 0 x -= 50 if x > 550 else 0
if y < 175: if y < 175:
@ -146,13 +144,13 @@ def find_pin(pos):
idx = 12+ int(x / SPACING) idx = 12+ int(x / SPACING)
return pin, idx return pin, idx
# Find the y position based on the chosen pin # Find the y position based on the chosen pin
def calc_pos(buckets, chosen): def calc_pos(self, buckets, chosen):
amount = buckets[chosen][0] amount = buckets[chosen][0]
SPACING = self.SPACING
# My math is incredibly off
if chosen > 11: if chosen > 11:
print("Amount at pin:", amount) # print("Amount at pin:", amount)
y = 378 - (30 * amount) y = 378 - (30 * amount)
chosen -= 11 chosen -= 11
x = (SPACING*(chosen-1))+(SPACING/2) x = (SPACING*(chosen-1))+(SPACING/2)
@ -162,44 +160,85 @@ def calc_pos(buckets, chosen):
y = 30 * amount y = 30 * amount
x = (SPACING*(chosen-1))+(SPACING/2) x = (SPACING*(chosen-1))+(SPACING/2)
x += 50 if x > 500 else 0 x += 50 if x > 500 else 0
return x,y return x,y
from_board = None
from_buckets = [x for x in buckets]
from_locat = None
while running:
def calc_move_sets(self, from_board, roll, player):
# board = self.gen_board_from_buckets(buckets)
board = from_board
sets = []
total = 0
for r in roll:
# print("Value of r:",r)
sets.append([Board.calculate_legal_states(board, player, [r,0]), r])
total += r
sets.append([Board.calculate_legal_states(board, player, [total,0]), total])
return sets
def calc_turn(self):
player = self.player
if self.total_moves == 0:
return player * -1
return player
def handle_move(self, from_board, buckets, roll, player):
board = self.gen_board_from_buckets(buckets)
# print("Cur board:",board)
sets = self.calc_move_sets(from_board, roll, player)
for idx, board_set in enumerate(sets):
board_set[0] = list(board_set[0])
# print("My board_set:",board_set)
if board in [list(c) for c in board_set[0]]:
self.total_moves -= board_set[1]
if idx < 2:
# print("Roll object:",self.roll)
self.roll[idx] = 0
else:
self.roll = [0,0]
break
print("Total moves left:",self.total_moves)
# while running:
def paint_board(self):
# - events - # - events -
if self.player != self.calc_turn():
self.switch_player()
self.roll = [random.randrange(1, 7), random.randrange(1, 7)]
self.total_moves = self.roll[0] + self.roll[1]
print("Player:",self.player,"rolled:",self.roll)
player = self.player
rectangles_drag = self.all_drag[player]
rectangles = self.all_rects[player]
offsets = self.all_off[player]
buckets = self.buckets
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
running = False running = False
elif event.type == pygame.MOUSEBUTTONDOWN: elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: if event.button == 1:
rectangles = all_rects[player]
offsets = all_off[player]
meh = [rect.collidepoint(event.pos) for rect in rectangles] meh = [rect.collidepoint(event.pos) for rect in rectangles]
if any(meh): if any(meh):
is_true = np.where(meh)[0][0] is_true = np.where(meh)[0][0]
if any(meh): if any(meh):
print("GETTING CALLED") # print("GETTING CALLED")
rectangles_drag[is_true] = True rectangles_drag[is_true] = True
mouse_x, mouse_y = event.pos mouse_x, mouse_y = event.pos
# Need this to be a deepcopy :< # Need this to be a deepcopy :<
from_buckets = [] self.from_buckets = []
for x in buckets: for x in buckets:
tmp = [] tmp = []
for y in x: for y in x:
tmp.append(y) tmp.append(y)
from_buckets.append(tmp) self.from_buckets.append(tmp)
from_board = [x for x in gen_board_from_buckets(buckets)] self.from_board = [x for x in self.gen_board_from_buckets(buckets)]
# print("From board in mousedown:", from_board) # print("From board in mousedown:", from_board)
pin, idx = find_pin(event.pos) pin, idx = self.find_pin(event.pos)
from_pin = pin from_pin = pin
buckets[idx][0] -= 1 buckets[idx][0] -= 1
@ -209,7 +248,7 @@ while running:
offsets[is_true][0] = rectangles[is_true].x - mouse_x offsets[is_true][0] = rectangles[is_true].x - mouse_x
offsets[is_true][1] = rectangles[is_true].y - mouse_y offsets[is_true][1] = rectangles[is_true].y - mouse_y
from_locat = [rectangles[is_true].x, rectangles[is_true].y] self.from_locat = [rectangles[is_true].x, rectangles[is_true].y]
elif event.type == pygame.MOUSEBUTTONUP: elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1: if event.button == 1:
@ -217,40 +256,57 @@ while running:
if any(meh): if any(meh):
is_true = np.where(meh)[0][0] is_true = np.where(meh)[0][0]
pin, idx = self.find_pin(event.pos)
x, y = self.calc_pos(buckets,idx)
pin, idx = find_pin(event.pos)
x, y = calc_pos(buckets,idx)
buckets[idx][0] += 1 buckets[idx][0] += 1
buckets[idx][1] = player buckets[idx][1] = player
print(from_board) # print(self.from_board)
print("To :",gen_board_from_buckets(buckets)) # print("To :",self.gen_board_from_buckets(buckets))
# print(move_legal(from_board, buckets, [1,2])) # print(move_legal(from_board, buckets, [1,2]))
if move_legal(from_board, buckets, [1,0]):
print("From:",gen_board_from_buckets(from_buckets))
print("WOHO!"*10)
rectangles_drag = all_drag[player]
# if self.move_legal(self.from_board, buckets, self.roll):
sets = self.calc_move_sets(self.from_board, self.roll, player)
pot_board = self.gen_board_from_buckets(buckets)
# print("board:",pot_board)
truth_values = []
for t in sets:
b = [list(c) for c in list(t)[0]]
if pot_board in list(b):
truth_values.append(pot_board in list(b))
print("Truth values:",truth_values)
if any(truth_values):
self.handle_move(self.from_board, buckets, self.roll, player)
# print("From:",self.gen_board_from_buckets(self.from_buckets))
# print("WOHO!"*10)
rectangles_drag[is_true] = False rectangles_drag[is_true] = False
rectangles[is_true].x = x rectangles[is_true].x = x
rectangles[is_true].y = y rectangles[is_true].y = y
else: else:
print("From:",gen_board_from_buckets(from_buckets)) # print("From:",self.gen_board_from_buckets(self.from_buckets))
buckets = [x for x in from_buckets]
rectangles_drag = all_drag[player] self.buckets = []
for x in self.from_buckets:
tmp = []
for y in x:
tmp.append(y)
self.buckets.append(tmp)
rectangles_drag[is_true] = False rectangles_drag[is_true] = False
rectangles[is_true].x = from_locat[0] rectangles[is_true].x = self.from_locat[0]
rectangles[is_true].y = from_locat[1] rectangles[is_true].y = self.from_locat[1]
print("End :",gen_board_from_buckets(buckets))
# print("End :",self.gen_board_from_buckets(buckets))
elif event.type == pygame.MOUSEMOTION: elif event.type == pygame.MOUSEMOTION:
rectangles_drag = all_drag[player]
rectangles = all_rects[player]
offsets = all_off[player]
if any(rectangles_drag): if any(rectangles_drag):
is_true = np.where(rectangles_drag)[0][0] is_true = np.where(rectangles_drag)[0][0]
@ -258,50 +314,94 @@ while running:
rectangles[is_true].x = mouse_x + offsets[is_true][0] rectangles[is_true].x = mouse_x + offsets[is_true][0]
rectangles[is_true].y = mouse_y + offsets[is_true][1] rectangles[is_true].y = mouse_y + offsets[is_true][1]
screen.fill(GREEN_FILT) self.screen.fill(self.GREEN_FILT)
# pygame.draw.polygon(screen, (RED), [[0, 0], [50,0],[25,100]], 2) # pygame.draw.polygon(screen, (RED), [[0, 0], [50,0],[25,100]], 2)
color = LIGHT_GREY color = self.LIGHT_GREY
x = 0 x = 0
y = 150 y = 150
# for _ in range(2): # for _ in range(2):
for i in range(12): for i in range(12):
if x < 500 and x+SPACING > 500: if x < 500 and x+self.SPACING > 500:
x = 550 x = 550
color = SALMON if color == LIGHT_GREY else LIGHT_GREY color = self.SALMON if color == self.LIGHT_GREY else self.LIGHT_GREY
pygame.draw.polygon(screen, color, [[x, 0], [x+SPACING, 0], [(2*x+SPACING)/2, y]]) pygame.draw.polygon(self.screen, color, [[x, 0], [x+self.SPACING, 0], [(2*x+self.SPACING)/2, y]])
x += SPACING x += self.SPACING
# y += 50 # y += 50
x = 0 x = 0
y = 250 y = 250
# for _ in range(2): # for _ in range(2):
color = SALMON if color == LIGHT_GREY else LIGHT_GREY color = self.SALMON if color == self.LIGHT_GREY else self.LIGHT_GREY
for i in range(12): for i in range(12):
if x < 500 and x+SPACING > 500: if x < 500 and x+self.SPACING > 500:
x = 550 x = 550
color = SALMON if color == LIGHT_GREY else LIGHT_GREY color = self.SALMON if color == self.LIGHT_GREY else self.LIGHT_GREY
pygame.draw.polygon(screen, color, [[x, 400], [x+SPACING, 400], [(2*x+SPACING)/2, y]]) pygame.draw.polygon(self.screen, color, [[x, 400], [x+self.SPACING, 400], [(2*x+self.SPACING)/2, y]])
x += SPACING x += self.SPACING
# print(gen_board_from_buckets(buckets)) # print(gen_board_from_buckets(buckets))
pygame.draw.rect(screen, BROWN, pygame.rect.Rect((500, 0, 50, 400))) pygame.draw.rect(self.screen, self.BROWN, pygame.rect.Rect((500, 0, 50, 400)))
for p in [-1,1]: for p in [-1,1]:
for rect in all_rects[p]: for rect in self.all_rects[p]:
pygame.draw.rect(screen, RED if p == -1 else BLACK, rect) pygame.draw.rect(self.screen, self.RED if p == -1 else self.BLACK, rect)
pygame.display.flip() pygame.display.flip()
# - constant game speed / FPS - # - constant game speed / FPS -
clock.tick(FPS) self.clock.tick(self.FPS)
def test(self):
while True:
self.paint_board()
b = Board_painter()
b.test()
# class myThread (threading.Thread):
# def __init__(self, threadID, name, counter):
# threading.Thread.__init__(self)
# self.threadID = threadID
# self.name = name
# self.counter = counter
# def run(self, function):
# print ("Starting " + self.name)
# function()
# print ("Exiting " + self.name)
# def change_player():
# while True:
# time.sleep(5)
# b.switch_player([1,2])
# def keep_painting():
# while True:
# b.paint_board()
# # Create new threads
# thread2 = myThread( keep_painting() )
# thread1 = myThread( change_player() )
# # Start new Threads
# thread1.start()
# thread2.start()
# thread1.join()
# thread2.join()
# print ("Exiting Main Thread")
# # - end -
# pygame.quit()
# - end -
pygame.quit()

View File

@ -284,9 +284,10 @@ class Board:
legal_moves = set() legal_moves = set()
if not Board.any_move_valid(board, player, roll): if not Board.any_move_valid(board, player, roll):
return { board } print("Inside board:",board)
return [ board ]
dice_permutations = list(itertools.permutations(roll)) if roll[0] != roll[1] else [[roll[0]]*4] dice_permutations = list(itertools.permutations(roll)) if roll[0] != roll[1] else [[roll[0]]*4]
print("Dice permuts:",dice_permutations) # print("Dice permuts:",dice_permutations)
for roll in dice_permutations: for roll in dice_permutations:
# Calculate boards resulting from first move # Calculate boards resulting from first move
#print("initial board: ", board) #print("initial board: ", board)