From c08e7fe540ffbee7f7a01ac42e2f6d316d656f3f Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Sat, 14 Apr 2018 14:13:27 +0200 Subject: [PATCH] Few changes to board --- actual_board.py | 189 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 132 insertions(+), 57 deletions(-) diff --git a/actual_board.py b/actual_board.py index 998dca1..f65dbf5 100644 --- a/actual_board.py +++ b/actual_board.py @@ -1,4 +1,9 @@ +# TODO: The bar is just for show at the moment. Home doesn't work either. +# 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. + import pygame +from board import Board import numpy as np # --- constants --- (UPPER_CASE names) @@ -8,8 +13,6 @@ SCREEN_HEIGHT = 400 SPACING = 83.333 -BUCKETS = [[]]*24 - #BLACK = ( 0, 0, 0) #242 209 107 @@ -88,10 +91,50 @@ is_true = False clock = pygame.time.Clock() -buckets = [5,0,0,0,3,0,5,0,0,0,0,2,5,0,0,0,3,0,5,0,0,0,0,2] +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): + meh = [] + for i in range(13,25): + pin = board[i] + print(pin) + meh.append([abs(pin), np.sign(pin)]) + for i in range(1,13): + pin = board[i] + meh.append([abs(pin), np.sign(pin)]) + + return meh + + +def gen_board_from_buckets(buckets): + board = [] + board.append([0,0]) + for i in range(-1,-13,-1): + board.append(buckets[i]) + for i in range(0,12): + board.append(buckets[i]) + board.append([0,0]) + board = [x*y for x,y in board] + + return board + +def move_legal(from_board, buckets, roll): + board = gen_board_from_buckets(buckets) + legal_states = Board.calculate_legal_states(from_board, 1, roll) + # print(legal_states) + if board in [list(state) for state in list(legal_states)]: + return True + return False + + def find_pin(pos): x,y = pos x -= 50 if x > 550 else 0 @@ -105,13 +148,12 @@ def find_pin(pos): # Find the y position based on the chosen pin def calc_pos(buckets, chosen): - amount = buckets[chosen] + amount = buckets[chosen][0] # My math is incredibly off if chosen > 11: - print(amount) + print("Amount at pin:", amount) y = 378 - (30 * amount) - print(y) chosen -= 11 x = (SPACING*(chosen-1))+(SPACING/2) x += 50 if x > 500 else 0 @@ -124,7 +166,9 @@ def calc_pos(buckets, chosen): return x,y - +from_board = None +from_buckets = [x for x in buckets] +from_locat = None while running: # - events - @@ -145,27 +189,63 @@ while running: rectangles_drag[is_true] = True mouse_x, mouse_y = event.pos + # Need this to be a deepcopy :< + from_buckets = [] + for x in buckets: + tmp = [] + for y in x: + tmp.append(y) + from_buckets.append(tmp) + + from_board = [x for x in gen_board_from_buckets(buckets)] + # print("From board in mousedown:", from_board) pin, idx = find_pin(event.pos) - buckets[idx] -= 1 + from_pin = pin + buckets[idx][0] -= 1 + + if buckets[idx][0] == 0: + buckets[idx][1] = 0 offsets[is_true][0] = rectangles[is_true].x - mouse_x offsets[is_true][1] = rectangles[is_true].y - mouse_y + from_locat = [rectangles[is_true].x, rectangles[is_true].y] + elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1: - if is_true is not None: + meh = [rect.collidepoint(event.pos) for rect in rectangles] + if any(meh): + is_true = np.where(meh)[0][0] + - pin, idx = find_pin(event.pos) - print(pin,idx) - print(calc_pos(buckets, idx)) - x, y = calc_pos(buckets,idx) - buckets[idx] += 1 - rectangles_drag = all_drag[player] - rectangles_drag[is_true] = False - rectangles[is_true].x = x - rectangles[is_true].y = y + pin, idx = find_pin(event.pos) + x, y = calc_pos(buckets,idx) + buckets[idx][0] += 1 + buckets[idx][1] = player + + + print(from_board) + print("To :",gen_board_from_buckets(buckets)) + # 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] + rectangles_drag[is_true] = False + rectangles[is_true].x = x + rectangles[is_true].y = y + else: + print("From:",gen_board_from_buckets(from_buckets)) + buckets = [x for x in from_buckets] + rectangles_drag = all_drag[player] + rectangles_drag[is_true] = False + rectangles[is_true].x = from_locat[0] + rectangles[is_true].y = from_locat[1] + print("End :",gen_board_from_buckets(buckets)) + elif event.type == pygame.MOUSEMOTION: rectangles_drag = all_drag[player] @@ -173,59 +253,54 @@ while running: offsets = all_off[player] if any(rectangles_drag): is_true = np.where(rectangles_drag)[0][0] - if any(rectangles_drag): - mouse_x, mouse_y = event.pos - rectangles[is_true].x = mouse_x + offsets[is_true][0] - rectangles[is_true].y = mouse_y + offsets[is_true][1] + + mouse_x, mouse_y = event.pos + rectangles[is_true].x = mouse_x + offsets[is_true][0] + rectangles[is_true].y = mouse_y + offsets[is_true][1] - # - updates (without draws) - - - # empty - - # - draws (without updates) - - - screen.fill(GREEN_FILT) - # pygame.draw.polygon(screen, (RED), [[0, 0], [50,0],[25,100]], 2) + screen.fill(GREEN_FILT) + # pygame.draw.polygon(screen, (RED), [[0, 0], [50,0],[25,100]], 2) - color = LIGHT_GREY - x = 0 - y = 150 - # for _ in range(2): - for i in range(12): - if x < 500 and x+SPACING > 500: - x = 550 - color = SALMON if color == LIGHT_GREY else LIGHT_GREY - pygame.draw.polygon(screen, color, [[x, 0], [x+SPACING, 0], [(2*x+SPACING)/2, y]]) - x += SPACING + color = LIGHT_GREY + x = 0 + y = 150 + # for _ in range(2): + for i in range(12): + if x < 500 and x+SPACING > 500: + x = 550 + color = SALMON if color == LIGHT_GREY else LIGHT_GREY + pygame.draw.polygon(screen, color, [[x, 0], [x+SPACING, 0], [(2*x+SPACING)/2, y]]) + x += SPACING # y += 50 - x = 0 - y = 250 - # for _ in range(2): - color = SALMON if color == LIGHT_GREY else LIGHT_GREY - - for i in range(12): - if x < 500 and x+SPACING > 500: - x = 550 + x = 0 + y = 250 + # for _ in range(2): color = SALMON if color == LIGHT_GREY else LIGHT_GREY - pygame.draw.polygon(screen, color, [[x, 400], [x+SPACING, 400], [(2*x+SPACING)/2, y]]) - x += SPACING + + for i in range(12): + if x < 500 and x+SPACING > 500: + x = 550 + color = SALMON if color == LIGHT_GREY else LIGHT_GREY + pygame.draw.polygon(screen, color, [[x, 400], [x+SPACING, 400], [(2*x+SPACING)/2, y]]) + x += SPACING - pygame.draw.rect(screen, BROWN, pygame.rect.Rect((500, 0, 50, 400))) - for p in [-1,1]: - for rect in all_rects[p]: + # print(gen_board_from_buckets(buckets)) + pygame.draw.rect(screen, BROWN, pygame.rect.Rect((500, 0, 50, 400))) + for p in [-1,1]: + for rect in all_rects[p]: - pygame.draw.rect(screen, RED if p == -1 else BLACK, rect) + pygame.draw.rect(screen, RED if p == -1 else BLACK, rect) - pygame.display.flip() + pygame.display.flip() - # - constant game speed / FPS - + # - constant game speed / FPS - - clock.tick(FPS) + clock.tick(FPS) # - end -