本文實例介紹了python實現井字棋游戲的方法,分享給大家,具體內容如下
windows7下python3.4.0編譯運行通過。由于采用了cmd調用,所以與Linux不兼容,無法在Linux下運行。
游戲就是井字棋,小鍵盤上的數字位置對應棋盤位置。
#本游戲python3.4.0下編寫調試,只能在windows下運行。import randomimport subprocessimport time#定義函數def draw_board(the_board): subprocess.call("cls", shell = True) print(' -------/n' + ' |' + the_board[9] + '|' + the_board[8] + '|' + the_board[7] + '|/n' + ' -------/n' + ' |' + the_board[6] + '|' + the_board[5] + '|' + the_board[4] + '|/n' + ' -------/n' + ' |' + the_board[3] + '|' + the_board[2] + '|' + the_board[1] + '|/n' + ' -------')def input_player_letter(): letter = ' ' while not (letter == 'X' or letter == 'O'): print('請選擇X或O作棋子:', end = '') letter = input().upper() if letter == 'X': return ['X', 'O'] else: return ['O', 'X']def who_first(): if 1 == random.randint(1, 2): return 'computer' else: return 'player'def is_again(): print('再一次?(Yes or No)') return input().lower().startswith('y')def is_space_free(the_board, move): return the_board[move] == ' 'def choose_random_from_list(the_board, move_from_list): possible_moves = [] for i in move_from_list: if is_space_free(the_board, i): possible_moves.append(i) if len(possible_moves) != 0: return random.choice(possible_moves) else: return Nonedef make_move(the_board, the_letter, the_move): the_board[the_move] = the_letterdef get_board_copy(the_board): duplicated_board = [] for i in board: duplicated_board.append(i) return duplicated_boarddef is_board_full(the_board): for i in range(1, 9): if is_space_free(the_board, i): return False else: return Truedef get_player_move(the_board): the_move = 0 while the_move not in list(range(1, 9)) or not is_space_free(the_board, the_move): print('請輸入走步:', end = '') the_move = int(input()) return the_movedef is_winner(the_board, the_letter): return (the_board[1] == the_letter and the_board[2] == the_letter and the_board[3] == the_letter) or (the_board[4] == the_letter and the_board[5] == the_letter and the_board[6] == the_letter) or (the_board[7] == the_letter and the_board[8] == the_letter and the_board[9] == the_letter) or (the_board[1] == the_letter and the_board[5] == the_letter and the_board[9] == the_letter) or (the_board[2] == the_letter and the_board[5] == the_letter and the_board[8] == the_letter) or (the_board[3] == the_letter and the_board[5] == the_letter and the_board[7] == the_letter) or (the_board[1] == the_letter and the_board[4] == the_letter and the_board[7] == the_letter) or (the_board[2] == the_letter and the_board[5] == the_letter and the_board[8] == the_letter) or (the_board[3] == the_letter and the_board[6] == the_letter and the_board[9] == the_letter)def get_computer_move(the_board, computer_letter): global player_letter global move if player_letter == 'X': computer_letter = 'O' else: player_letter = 'O' computer_letter = 'X' #虛擬棋盤查看是否自己可一步得勝 for i in range(1,9): copy = get_board_copy(board) if is_space_free(board, i): make_move(copy, computer_letter, i) if is_winner(copy, computer_letter): return i #虛擬棋盤查看是否對手可一步得勝 for i in range(1,9): if is_space_free(board, i): copy = get_board_copy(board) make_move(copy, player_letter, i) if is_winner(copy, player_letter): return i move = choose_random_from_list(board, [1, 3, 7, 9]) if move != 0: return move if is_space_free(board, 5): return 5 return choose_random_from_list(board, [2, 4, 6, 8, 7])print('歡迎玩 井字棋 游戲!')time.sleep(1)print('''
主站蜘蛛池模板:
朝阳市|
比如县|
吴江市|
永寿县|
蒙阴县|
策勒县|
富宁县|
瑞昌市|
石泉县|
凤台县|
武宁县|
定南县|
湘阴县|
格尔木市|
临泉县|
德州市|
榆林市|
延长县|
宜阳县|
广饶县|
宣汉县|
兴山县|
高雄县|
咸宁市|
从江县|
瓦房店市|
刚察县|
朝阳县|
白城市|
高唐县|
桐梓县|
马公市|
永宁县|
丰顺县|
湛江市|
枞阳县|
永顺县|
澳门|
尚义县|
毕节市|
松阳县|