Example #1
0
def allMoves(board_state, clr, check_name):
    """ Only for blacks """
    moves=[]
    check_pos = {'x': board_state['board'][clr[0]][check_name]['position']['x'],'y': board_state['board'][clr[0]][check_name]['position']['y']}
    if board_state["board"][clr[0]][check_name]["promoted"] == 0:
        possible_destructions = destruction_is_possible(check_pos, filled_cells(board_state, color=clr[1]), filled_cells(board_state, color=clr[0]), 0)
        if not possible_destructions:
            if clr[0]=='black':
                prob_moves=[{'x':check_pos['x']+1,'y': check_pos['y']+1},
                            {'x':check_pos['x'] - 1,'y':check_pos['y'] + 1}]
            else:
                prob_moves=[{'x':check_pos['x']+1,'y': check_pos['y']-1},
                            {'x':check_pos['x'] - 1,'y':check_pos['y'] - 1}]
           
            for pr_mv in prob_moves:
                if pr_mv not in filled_cells(board_state) and (0 < pr_mv['x'] < 9) and ( 0 < pr_mv['y'] < 9):
                    move={
                            "name":  [clr[0], check_name],
                            "startPos": board_state['board'][clr[0]][check_name]['position'],
                            "finalPos": {
                                "x":  pr_mv['x'],
                                "y":  pr_mv['y'] 
                                },
                            "cutDown": []
                            }
                    moves.append(move)
            moves = [0, moves]
        else:
            moves = allDestructionTurns(clr, check_name, board_state, destruction_is_possible(board_state["board"][clr[0]][check_name]["position"], filled_cells(board_state, color=clr[1]), filled_cells(board_state, color=clr[0]), 0), [], [])
            moves = [1, moves]
    else:
        possible_destructions = destruction_is_possible(check_pos, filled_cells(board_state, color=clr[1]), filled_cells(board_state, color=clr[0]), 1)
        if not possible_destructions:
            prob_moves = [ [(x,y) for x in range(check_pos['x']-1,0,-1) for y in range(check_pos['y']+1, 9) if y==(-x)+check_pos['x']+check_pos['y']],
                           [(x,y) for x in range(check_pos['x']+1,9) for y in range(check_pos['y']+1, 9) if y==x-(check_pos['x']-check_pos['y'])],
                           [(x,y) for x in range(check_pos['x']-1, 0, -1) for y in range(check_pos['y']-1, 0, -1) if y==x-(check_pos['x']-check_pos['y'])],
                           [(x,y) for x in range(check_pos['x']+1, 9) for y in range(check_pos['y']-1,0,-1) if y==(-x)+check_pos['x']+check_pos['y']]
            ]
            for direction in prob_moves:
                for cell in direction:
                    if {'x':cell[0], 'y': cell[1]} in filled_cells(board_state):
                        break
                    else:
                        move={
                            "name":  [clr[0], check_name],
                            "startPos": board_state['board'][clr[0]][check_name]['position'],
                            "finalPos": {
                                "x":  cell[0],
                                "y":  cell[1] 
                                },
                            "cutDown": []
                            }
                        moves.append(move)
            moves = [0, moves]            
        else:
            moves = allDestructionTurns(clr, check_name, board_state, destruction_is_possible(board_state["board"][clr[0]][check_name]["position"], filled_cells(board_state, color=clr[1]), filled_cells(board_state, color=clr[0]), 1), [], [])
            moves = [1, moves]
    return moves
Example #2
0
def target(board_state, cl):
    blacks = filled_cells(board_state, color="black")
    whites= filled_cells(board_state, color="white")
    if len(whites)!=0:
       # if cl[0]=='black':
        return len(blacks)-len(whites)
        #else:
        #    return len(whites)-len(blacks)
    else:
        return 1000    
Example #3
0
def main():
    p1 = Player(name="John")
    p2 = Player(name="Hulio")
    b = Board()
    b.add_player(p1)
    b.add_player(p2)
    b.start()
    _pprint(filled_cells(b.state))
    raw_input()
    destruct_status = False
    prev_check = []
    state = b.state
    promotions = 0
    while state not in ("FIN", ):
    #    for player in (p1, p2):
    #    b.get_state(player)
        event = smart_event_factory(state, colors, destruct_status, prev_check)
        color = 'white' if 'w' == event["name"][0] else 'black'
        if check_the_turn(event, state) == False:
            continue
        else:
            print "VALID"
            if "promoted" in event.keys() and event["promoted"]:
                promotions +=1
            print "PROMOTED CHECKS: ", promotions
            state = check_the_game_state(state, event, colors)
            if event['cutDown'] and destruction_is_possible(event['finalPos'], filled_cells(state, color=colors[1]), filled_cells(state, color=colors[0]), event.get('promoted')):
                destruct_status = True
                prev_check = event['finalPos']
                print 'NEXT DESTRUCT'
                raw_input()
            else:
                colors.reverse()
                destruct_status = False
                prev_check = []
                print 'COLORS REVERSED!'
                print 'FILLED CELLS: ' , len((filled_cells(state)))
                raw_input()
Example #4
0
def allBoardMoves(board_state, clr):  
    possible_moves_dst = []
    possible_moves_no_dst = []    
    for check in filled_cells(board_state, color=clr[0]):
        move = allMoves(board_state, clr, search_check_by_position(board_state["board"], clr[0], check))
        for p_mv in move[1]:
            if move[0]==0:
                possible_moves_no_dst.append(p_mv)    
            else:
                possible_moves_dst.append(p_mv)
    if possible_moves_dst:    
        return possible_moves_dst
    else:
        return possible_moves_no_dst
Example #5
0
def smart_event_factory(board_state, colors, destruct_status, prev_check):
    """ Event factory is smart) """
    whites = filled_cells(board_state, color="white")
    blacks = filled_cells(board_state, color="black")
    current_figures = whites + blacks
    if not destruct_status:
        rand_figure = random.choice(filled_cells(board_state, color=colors[0]))
    else:
        rand_figure = prev_check
    rand_figure_name = ''
    for col in board_state["board"].keys():
        for name in board_state["board"][col].keys():
            if (
                    (board_state["board"][col][name]["position"]["x"] == rand_figure["x"]) and
                    (board_state["board"][col][name]["position"]["y"] == rand_figure["y"]) and
                    (board_state["board"][col][name]["status"] == "alive")
            ):
                rand_figure_name = name
		
    if board_state["board"][colors[0]][rand_figure_name]["promoted"] == 0:
        possible_destructions = destruction_is_possible(rand_figure, filled_cells(board_state, color=colors[1]), filled_cells(board_state, color=colors[0]), 0)
        if not possible_destructions:
            move_modificator = random.choice(['left', 'right'])
            raw_input()
            if rand_figure in whites:
                generated_event = {
                    "name": ['white', rand_figure_name],
                    "startPos": rand_figure,
                    "finalPos": {
                        "x":  rand_figure['x']-1 if move_modificator == 'left' else rand_figure['x'] + 1,
                        "y":  rand_figure['y']-1 if move_modificator == 'right' else rand_figure['y'] - 1
                    },
                    "cutDown": []
                }
            else:
                generated_event = {
                    "name":  ['black', rand_figure_name],
                    "startPos": rand_figure,
                    "finalPos": {
                        "x":  rand_figure['x']+1 if move_modificator == 'left' else rand_figure['x'] - 1,
                        "y":  rand_figure['y']+1 if move_modificator == 'left' else rand_figure['y'] + 1
                    },
                    "cutDown": []
                }

        else:
            attack = random.choice(possible_destructions)
            if rand_figure in whites:
                cutDownCheck = search_check_by_position(board_state["board"], "black", attack['enemy_position'])
                generated_event = {
                        "name": ['white', rand_figure_name],
                        "startPos": rand_figure,
                        "finalPos": {
                            "x":  attack['fin_position']['x'],
                            "y":  attack['fin_position']['y']
                        },
                        "cutDown": [cutDownCheck]
                    }
            else:
                cutDownCheck = search_check_by_position(board_state["board"], "white", attack['enemy_position'])
                generated_event = {
                        "name": ['black', rand_figure_name],
                        "startPos": rand_figure,
                        "finalPos": {
                            "x":  attack['fin_position']['x'],
                            "y":  attack['fin_position']['y']
                        },
                        "cutDown": [cutDownCheck]
                    }
    else:
        upper_left = [(x,y) for x in range(rand_figure['x']-1,0,-1) for y in range(rand_figure['y']+1, 9) if y==(-x)+rand_figure['x']+rand_figure['y']]
        upper_right = [(x,y) for x in range(rand_figure['x']+1,9) for y in range(rand_figure['y']+1, 9) if y==x-(rand_figure['x']-rand_figure['y'])]
        down_left = [(x,y) for x in range(rand_figure['x']-1, 0, -1) for y in range(rand_figure['y']-1, 0, -1) if y==x-(rand_figure['x']-rand_figure['y'])]
        down_right = [(x,y) for x in range(rand_figure['x']+1, 9) for y in range(rand_figure['y']-1,0,-1) if y==(-x)+rand_figure['x']+rand_figure['y']]
        possible_destructions = destruction_is_possible(rand_figure, filled_cells(board_state, color=colors[1]), filled_cells(board_state, color=colors[0]), 1)
        if not possible_destructions:
            free_cells=[]
            while not free_cells:
                move_modificator = random.choice(['upper_left', 'upper_right', 'down_left', 'down_right'])
                for pos in {'upper_left':upper_left,'upper_right':upper_right,'down_left':down_left,'down_right':down_right}[move_modificator]:
                    if {'x':pos[0],'y':pos[1]} in current_figures:
                        break
                    else:
                        free_cells.append(pos)
            print 'FREE CELLS FOR PROMOTED TURN: ', free_cells
            possible_turn = random.choice(free_cells)
            raw_input()
            print 'PROMOTED TURN'
            generated_event = {
                "name": [colors[0], rand_figure_name],
                "startPos": rand_figure,
                "finalPos": {
                    "x": possible_turn[0],
                    "y": possible_turn[1],
                }, 
                "cutDown": []
            
            }
        else:
        #    directions = {'upper_left':upper_left,'upper_right':upper_right,'down_left':down_left,'down_right':down_right}
        #    possible_destructions = {'upper_left':[],'upper_right':[],'down_left':[],'down_right':[]}
        #    possible_positions = {'upper_left':[],'upper_right':[],'down_left':[],'down_right':[]}
        #    for direction in directions:
        #        for pos in directions[direction]:
        #            if {'x':pos[0],'y':pos[1]} in filled_cells(board_state, color=colors[0]):
        #                break
        #            elif {'x':pos[0],'y':pos[1]} in filled_cells(board_state, color=colors[1]):
        #                possible_destructions[direction].append({'x':pos[0],'y':pos[1]})
        #                break
        #        if possible_destructions[direction]:
        #            for av_pos in directions[direction][directions[direction].index([possible_destructions[direction][0]['x'],possible_destructions[direction][0]['y']]):]:
        #                if {'x': av_pos[0], 'y': av_pos[1]} not in current_figures:
        #                    possible_positions[direction].append(av_pos)
        #                else:
        #                    break
        #    for direction in possible_positions:
        #        if possible_positions[direction]:
        #            fin_pos = random.choice(possible_positions[direction])
        #            attack = possible_destructions[direction][0]                    
        #            break
            attack = random.choice(possible_destructions)
                    
            generated_event = {
                "name": [colors[0], rand_figure_name],
                "startPos": rand_figure,
                "finalPos": {
                    "x": attack['fin_position']['x'],
                    "y":attack['fin_position']['y'],
                },
                "cutDown": [search_check_by_position(board_state["board"], colors[1], attack['enemy_position'])]
            }                            
                        
                        
        
    if (board_state["board"][colors[0]][rand_figure_name]["promoted"] == 0) and ((generated_event["name"][0] == 'black' and generated_event["finalPos"]["y"] == 8) or (generated_event["name"][0] == 'white' and generated_event["finalPos"]["y"] == 1)):
        generated_event["promoted"] = 1
        print 'PROMOTION!'

    return generated_event
Example #6
0
def allDestructionTurns(clr, check_name, board_state, available_destructions, destruction_way, final):
    for destruct in available_destructions:
        turn_with_destruct = {
                            "name":  [clr[0], check_name],
                            "startPos": board_state['board'][clr[0]][check_name]['position'],
                            "finalPos": {
                                "x":  destruct["fin_position"]["x"],
                                "y":  destruct["fin_position"]["y"] 
                                },
                            "cutDown": [search_check_by_position(board_state["board"], clr[1], destruct['enemy_position'])]
                            }
        destruction_way_new = deepcopy(destruction_way)
        destruction_way_new.append(turn_with_destruct)        
        board_state_new = deepcopy(board_state)
        board_state_new["board"][clr[0]][check_name]["position"] = destruct["fin_position"]
        board_state_new["board"][clr[1]][search_check_by_position(board_state_new["board"], clr[1], destruct["enemy_position"])]["status"] = "dead"
        if ((clr[0]=="black" and destruct["fin_position"]["y"]==8) or (clr[0]=="white" and destruct["fin_position"]["y"]==1)):
            board_state_new["board"][clr[0]][check_name]["promoted"] = 1 
        available_destructions = destruction_is_possible(board_state_new["board"][clr[0]][check_name]["position"], filled_cells(board_state_new, color=clr[1]), filled_cells(board_state_new, color=clr[0]), board_state_new["board"][clr[0]][check_name]["promoted"])
        if available_destructions:
            allDestructionTurns(clr, check_name, board_state_new, available_destructions, destruction_way_new, final)
        else:
            final.append(destruction_way_new)
    
    return final