def main(): print "What is the start location?" start_x_coor, start_y_coor = tools.get_input_coordinates(width, height) print "What is the end location?" unique_end_coor = False while not unique_end_coor: end_x_coor, end_y_coor = tools.get_input_coordinates(width, height) if (start_x_coor == end_x_coor) and (start_y_coor == end_y_coor): print "Same as start. Enter new end location:" unique_end_coor = False else: unique_end_coor = True # initialize start/end board = [['.' for x in range(height)] for x in range(width)] board[start_x_coor][start_y_coor] = 'S' board[end_x_coor][end_y_coor] = 'E' # initialize board w/ moves board_with_moves = [['.' for x in range(height)] for x in range(width)] board_with_moves[start_x_coor][start_y_coor] = [] board_with_moves_set = [['.' for x in range(height)] for x in range(width)] board_with_moves_set[start_x_coor][start_y_coor] = set([]) continue_loop = True current_step = 0 while continue_loop: continue_loop = False for y in range(len(board[0])): for x in range(len(board)): if board_with_moves[x][y] != '.' and len(board_with_moves[x][y]) == current_step: modified = draw_knight_move(board_with_moves, board_with_moves_set, x, y) if modified: continue_loop = True current_step += 1 # print out percentage more to go through if current_step % 30 == 0: print 'About ', ( current_step / 10.24), '% done.' # shortest move found! move_to_end = board_with_moves[end_x_coor][end_y_coor] for num_move in range(1, len(move_to_end)): x, y = tools.string_to_coor(move_to_end[num_move]) board[x][y] = num_move tools.print_board(board) print 'Sequence of moves: ' for num_move in range(0, len(move_to_end)): x, y = tools.string_to_coor(move_to_end[num_move]) print '(' , x, ', ', y, ')' print '(' , end_x_coor, ', ', end_y_coor, ')'
def main(): print "What is the start location?" start_x_coor, start_y_coor = tools.get_input_coordinates(width, height) print "What is the end location?" unique_end_coor = False while not unique_end_coor: end_x_coor, end_y_coor = tools.get_input_coordinates(width, height) if (start_x_coor == end_x_coor) and (start_y_coor == end_y_coor): print "Same as start. Enter new end location:" unique_end_coor = False else: unique_end_coor = True # initialize start/end points board = [['.' for x in range(height)] for x in range(width)] board[start_x_coor][start_y_coor] = 'S' board[end_x_coor][end_y_coor] = 'E' # initialize to memorize order of moves in each spot board_with_moves = [['.' for x in range(height)] for x in range(width)] board_with_moves[start_x_coor][start_y_coor] = [] # continue loop until we hit step in which nothing changes. continue_loop = True current_step = 0 while continue_loop: continue_loop = False for y in range(len(board[0])): for x in range(len(board)): if board_with_moves[x][y] != '.' and len(board_with_moves[x][y]) == current_step: modified = draw_knight_move(board_with_moves, x, y) if modified: continue_loop = True # shortest move found! We can now print results and exit. if board_with_moves[end_x_coor][end_y_coor] != '.': move_to_end = board_with_moves[end_x_coor][end_y_coor] # print the move number in each spot for num_move in range(1, len(move_to_end)): x, y = tools.string_to_coor(move_to_end[num_move]) board[x][y] = num_move tools.print_board(board) # print sequence of move print 'Sequence of moves: ' for num_move in range(0, len(move_to_end)): x, y = tools.string_to_coor(move_to_end[num_move]) print '(' , x, ', ', y, ')' print '(' , end_x_coor, ', ', end_y_coor, ')' exit() current_step += 1
def main(): print "What is the start location?" start_x_coor, start_y_coor = tools.get_input_coordinates(width, height) # make sure end location not same as start location print "What is the end location?" unique_end_coor = False while not unique_end_coor: end_x_coor, end_y_coor = tools.get_input_coordinates(width, height) if (start_x_coor == end_x_coor) and (start_y_coor == end_y_coor): print "Same as start. Enter new end location:" unique_end_coor = False else: unique_end_coor = True # initialize start/end points board = [['.' for x in range(height)] for x in range(width)] board[start_x_coor][start_y_coor] = 'S' board[end_x_coor][end_y_coor] = 'E' # print initial version tools.print_board(board) # initialize current_x_coor = start_x_coor current_y_coor = start_y_coor # start getting coordinates, and check validity of them in each move while not (current_x_coor == end_x_coor and current_y_coor == end_y_coor): next_x_coor, next_y_coor = tools.get_input_coordinates(width, height) if tools.is_valid_knight_move(current_x_coor, current_y_coor, next_x_coor, next_y_coor): # if over start, replace with 'S' if current_x_coor == start_x_coor and current_y_coor == start_y_coor: board[current_x_coor][current_y_coor] = 'S' else: board[current_x_coor][current_y_coor] = '.' # mark current location board[next_x_coor][next_y_coor] = 'K' current_x_coor = next_x_coor current_y_coor = next_y_coor tools.print_board(board) else: print 'Not a valid knight move.'
def main(): print 'reading:', TRAIN_FILE for rid, delta, start_board, stop_board in readBoards(TRAIN_FILE, includeStartBoard=True): print "\n***", "id:", rid, "delta:", delta,"***\n" print "START BOARD" print_board(start_board) start_rows = board_rows_str(start_board) print print "STOP BOARD" print_board(stop_board) stop_rows = board_rows_str(stop_board) print for row in zip(start_rows, stop_rows): print row print if delta == 1: print "DELTA==1" print "id:", rid, ataviseBoard(stop_board, start_board)
def main(): set_play_board(init_board()) is_game_running = True set_player(random.choice(["X", "O"])) tools.intro() while is_game_running: if game_sts == 0: print("Available game modes: 1 - Human-Human, 2 - Human-AI, 3 - AI-AI or \"exit\" to terminate.") mode = input("Select the game mode: ") if mode == "exit": tools.outro() break elif tools.is_mode_correct(mode): set_game_sts(int(mode)) else: print('\033[31m', "The game mod you selected is invalid, please select 1, 2 or 3!", '\033[0m') continue # ============ HUMAN-HUMAN ============== elif game_sts == 1: tools.clear_console() win = tools.has_won(play_board) full = tools.is_full(play_board) if win[0]: set_message(win[3]) elif full[0]: set_message(full[1]) tools.print_board(play_board, win[2]) tools.show_message(message) if win[0] or full[0]: reset_game() continue move = input(f"Now moves \"{player}\": ") move = move.upper() if move == "EXIT": tools.outro() break elif mess := mark(play_board, move, player): set_message(mess[1]) else: continue # ============ HUMAN-AI ============== elif game_sts == 2: if player == "X": set_player_ai("O") else: set_player_ai("X") if level_hum_ai == "": print("There are available game levels for Human-AI: \"easy\", \"medium\" or \"hard\".") level = input("Select a game level: ") level = level.upper() if level == "EXIT": tools.outro() break elif tools.is_level_correct(level): set_level_hum_ai(level) else: print('\033[31m', "Write the level you want to play: \"easy\", \"medium\" or \"hard\"!", '\033[0m') continue elif tools.is_level_correct(level_hum_ai): tools.clear_console() win = tools.has_won(play_board) full = tools.is_full(play_board) if win[0]: set_message(win[3]) elif full[0]: set_message(full[1]) tools.print_board(play_board, win[2]) tools.show_message(message) if win[0] or full[0]: reset_game() continue move = input(f"Now your move \"{player}\": ") move = move.upper() if move == "EXIT": tools.outro() break else: mess_human = mark(play_board, move, player) set_message(mess_human[1]) win = tools.has_won(play_board) full = tools.is_full(play_board) if mess_human[0] and not win[0] and not full[0]: ai.get_ai_move(level_hum_ai, play_board, player_ai, player)
elif game_sts == 3: if player == "X": set_player_ai("O") else: set_player_ai("X") set_level_hum_ai("HARD") win = tools.has_won(play_board) full = tools.is_full(play_board) if win[0]: set_message(win[3]) elif full[0]: set_message(full[1]) if not win[0] and not full[0]: ai.get_ai_move("MEDIUM", play_board, player, player_ai) tools.clear_console() tools.print_board(play_board, win[2]) time.sleep(1) win = tools.has_won(play_board) full = tools.is_full(play_board) if win[0]: set_message(win[3]) elif full[0]: set_message(full[1]) if not win[0] and not full[0]: ai.get_ai_move(level_hum_ai, play_board, player_ai, player) tools.clear_console() tools.print_board(play_board, win[2]) tools.show_message(message) time.sleep(1) else: tools.clear_console()
def main(): special_board = import_special_board() width = len(special_board) height = len(special_board[0]) tools.print_board(special_board) # get list of teleport spots teleport_list = [] for y in range(len(special_board[0])): for x in range(len(special_board)): if special_board[x][y] == 'T': teleport_list.append(tools.coor_to_string(x, y)) # get input for data. We don't want to start on an element for simplicity. print "What is the start location?" valid_start_coor = False while not valid_start_coor: start_x_coor, start_y_coor = tools.get_input_coordinates(width, height) element = special_board[start_x_coor][start_y_coor] if element == 'W' or element == 'R' or element == 'B' or element == 'T' or element == 'L': print "Start has element. Enter new start location:" valid_start_coor = False else: valid_start_coor = True print "What is the end location?" unique_end_coor = False while not unique_end_coor: end_x_coor, end_y_coor = tools.get_input_coordinates(width, height) element = special_board[end_x_coor][end_y_coor] if (start_x_coor == end_x_coor) and (start_y_coor == end_y_coor): print "Same as start. Enter new end location:" unique_end_coor = False elif element == 'W' or element == 'R' or element == 'B' or element == 'T' or element == 'L': print "End has element. Enter new end location:" unique_end_coor = False else: unique_end_coor = True # mark start/end location board = [['.' for x in range(height)] for x in range(width)] board[start_x_coor][start_y_coor] = 'S' board[end_x_coor][end_y_coor] = 'E' # initialize board storing moves board_with_moves = [['.' for x in range(height)] for x in range(width)] board_with_moves[start_x_coor][start_y_coor] = [] # store number of moves. Important because of our elements board_with_num_moves = [['.' for x in range(height)] for x in range(width)] board_with_num_moves[start_x_coor][start_y_coor] = 0 continue_loop = True current_step = 0 teleport_done = False while continue_loop: continue_loop = False for y in range(len(board[0])): for x in range(len(board)): if board_with_moves[x][y] != '.' and board_with_num_moves[x][y] == current_step: modified = draw_knight_move(board_with_moves, board_with_num_moves, special_board, x, y, current_step + 1, width, height) if modified: continue_loop = True # shortest move found! if board_with_moves[end_x_coor][end_y_coor] != '.': move_to_end = board_with_moves[end_x_coor][end_y_coor] special_board_to_print = [row[:] for row in special_board] for num_move in range(1, len(move_to_end)): x, y = tools.string_to_coor(move_to_end[num_move]) special_board_to_print[start_x_coor][start_y_coor] = 'S' special_board_to_print[end_x_coor][end_y_coor] = 'E' special_board_to_print[x][y] = board_with_num_moves[x][y] tools.print_board(special_board_to_print) print 'Sequence of moves: ' for num_move in range(0, len(move_to_end)): x, y = tools.string_to_coor(move_to_end[num_move]) print '(' , x, ', ', y, ')' print '(' , end_x_coor, ', ', end_y_coor, ')' # we want to check if sequence of moves are all valid valid_knight_moves = True for idx in range(1, len(move_to_end)): prev_x, prev_y = tools.string_to_coor(move_to_end[idx - 1]) x, y = tools.string_to_coor(move_to_end[idx]) if not tools.is_valid_knight_move(prev_x, prev_y, x, y) and move_to_end[idx] not in teleport_list: print 'Invalid' valid_knight_moves = False break if valid_knight_moves: print '--- All valid knight moves' else: print '--- Invalid knight moves' exit() current_step += 1 # check if anything has landed on teleport locations. # because we care about shortest moves, if we land on one, then move all other moves to teleport. if not teleport_done: for teleport_str in teleport_list: t_x, t_y = tools.string_to_coor(teleport_str) if board_with_moves[t_x][t_y] != '.': for teleport_str in teleport_list: t_x_, t_y_ = tools.string_to_coor(teleport_str) if board_with_moves[t_x_][t_y_] == '.': board_with_moves[t_x_][t_y_] = board_with_moves[t_x][t_y][:] board_with_num_moves[t_x_][t_y_] = board_with_num_moves[t_x][t_y] break