本文实例介绍了python实现井字棋游戏的方法,分享给大家,具体内容如下
windows7下python3.4.0编译运行通过。由于采用了cmd调用,所以与Linux不兼容,无法在Linux下运行。
游戏就是井字棋,小键盘上的数字位置对应棋盘位置。
#本游戏python3.4.0下编写调试,只能在windows下运行。 import random import subprocess import 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 None def make_move(the_board, the_letter, the_move): the_board[the_move] = the_letter def get_board_copy(the_board): duplicated_board = [] for i in board: duplicated_board.append(i) return duplicated_board def is_board_full(the_board): for i in range(1, 9): if is_space_free(the_board, i): return False else: return True def 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_move def 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('''▆▅▅▅▆▅▅▅▅▅▅▅▂▅▅▅▆▆▅▅▃▂▆▅▅▅▅▅▅▅▅ ▆▆▆▃▂▆▆▅▃▄▆▅▂▅▆▇▇▆▆▆▄▂▆▆▆▆▆▆▆▆▅ ▆▅▆▅ ▁▅▂▃▅▆▅▂▆▆▇▆▅▆▇▄▂▆▆▆▆▆▆▆▆▅ ▆▅▆▆▅ ▃▆▅▆▅▂▆▇▆▅▅▆▇▅▂▆▆▆▆▆▆▆▆▅ ▆▆▆▆▆▃ ▁▅▆▆▄▂▇▇▆▅▅▆▇▅▁▆▆▆▆▆▆▆▆▅ ▆▅▆▆▃▂▃▁▁▅▆▄▂▇▇▆▅▆▇▇▅▂▆▆▆▅▅▅▅▅▅ ▆▅▆▃▁▅▆▃▁▁▅▅▂▆▇▆▆▇▆▆▄▂▆▅▅▅▅▅▆▆▅ ▆▅▆▄▅▆▆▆▄▂▂▃▃▆▆▇▇▆▆▆▅▂▆▆▆▆▆▆▆▆▆ ▆▅▄▄▄▄▄▄▄▄▃ ▂▅▄▄▃▄▄▄▃▂▅▄▄▅▅▅▅▅▅ ▆▅▂▂▂▂▃▃▃▃▃▂ ▁▃▂▃▃▃▃▂ ▂▃▂▃▃▃▃▃▅ ▆▅▆▆▆▇▇▇▇▆▆▅▂▁▄▆▆▆▄▅▄▂▆▆▆▆▆▆▆▆▅ ▆▅▆▅▆▇▆▆▆▆▆▄▄▄ ▃▆▂▂▅▄▂▆▅▅▆▅▅▆▆▅ ▆▅▅▆▆▇▆▅▆▇▆▄▃▆▂ ▂▃▅▆▄▂▆▅▅▅▅▅▅▆▅ ▆▅▅▆▇▆▅▅▆▇▇▄▃▆▅▂ ▃▆▅▄▂▆▅▅▅▅▅▆▆▅ ▆▅▅▆▇▆▅▆▆▇▆▃▂▆▄▂▂▁▃▆▅▂▆▅▅▆▆▆▆▆▅ ▆▅▆▆▇▆▆▇▇▆▆▄▂▄▁▄▅▂▁▂▅▂▆▅▆▆▆▆▆▆▅ ▆▅▅▆▆▆▇▆▆▆▆▄▁▃▄▆▆▄▂▁▁▂▆▅▅▆▆▆▆▆▅ ▆▅▂▂▂▂▃▂▂▂▂▂▁▃▃▃▃▂▁▁ ▂▂▂▂▂▂▃▄▅ ▆▆▆▆▆▅▅▅▅▅▅▄▁▅▅▅▅▄▅▅▄ ▁▅▆▅▅▅▅▆▆ ▆▆▆▆▆▆▆▆▆▆▆▅▂▆▆▆▆▆▆▆▄▂▃▂▆▆▆▆▅▅▆ ▆▆▆▆▆▆▆▆▆▆▆▅▂▆▆▆▆▆▆▆▄▂▆▂▁▅▆▃▃▆▆ ▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▂▆▅▁▁▃▂▅▆▆ ▆▆▆▆▆▆▆▆▆▆▆▄▃▆▆▆▆▆▆▆▄▃▆▆▄▁ ▅▇▆▅ ▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▃▆▆▄▁▁▁▅▆▅ ▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▃▆▄▂▄▃▁ ▅▆ ▆▆▆▆▆▆▆▆▆▆▆▅▃▆▆▆▆▆▆▆▅▃▅▁▄▆▆▃▁ ▄ ▆▆▆▆▆▆▆▆▆▆▆▅▄▆▆▆▆▆▆▆▄▃▆▅▆▆▆▆▄▃▂''') time.sleep(2) subprocess.call("cls", shell = True) while True: board = [' '] * 10 player_letter, computer_letter = input_player_letter() turn = who_first() print(turn + '先走') time.sleep(1) game_is_playing = True while game_is_playing: if turn == 'player': draw_board(board) move = get_player_move(board) make_move(board, player_letter, move) if is_winner(board, player_letter): draw_board(board) print('恭喜!你赢了。') game_is_playinig = False else: if is_board_full(board): draw_board(board) print('平局!') break else: turn = 'computer' else: move = get_computer_move(board, computer_letter) make_move(board, computer_letter, move) if is_winner(board, computer_letter): draw_board(board) print('电脑胜利,你挂了!') game_is_playing = False else: if is_board_full(board): draw_board(board) print('平局!') break else: turn = 'player' if not is_again(): break
以上就是本文的详细内容,希望对大家的学习有所帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。


Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

AI Hentai Generator
Menjana ai hentai secara percuma.

Artikel Panas

Alat panas

Dreamweaver CS6
Alat pembangunan web visual

SecLists
SecLists ialah rakan penguji keselamatan muktamad. Ia ialah koleksi pelbagai jenis senarai yang kerap digunakan semasa penilaian keselamatan, semuanya di satu tempat. SecLists membantu menjadikan ujian keselamatan lebih cekap dan produktif dengan menyediakan semua senarai yang mungkin diperlukan oleh penguji keselamatan dengan mudah. Jenis senarai termasuk nama pengguna, kata laluan, URL, muatan kabur, corak data sensitif, cangkerang web dan banyak lagi. Penguji hanya boleh menarik repositori ini ke mesin ujian baharu dan dia akan mempunyai akses kepada setiap jenis senarai yang dia perlukan.

Pelayar Peperiksaan Selamat
Pelayar Peperiksaan Selamat ialah persekitaran pelayar selamat untuk mengambil peperiksaan dalam talian dengan selamat. Perisian ini menukar mana-mana komputer menjadi stesen kerja yang selamat. Ia mengawal akses kepada mana-mana utiliti dan menghalang pelajar daripada menggunakan sumber yang tidak dibenarkan.

EditPlus versi Cina retak
Saiz kecil, penyerlahan sintaks, tidak menyokong fungsi gesaan kod

mPDF
mPDF ialah perpustakaan PHP yang boleh menjana fail PDF daripada HTML yang dikodkan UTF-8. Pengarang asal, Ian Back, menulis mPDF untuk mengeluarkan fail PDF "dengan cepat" dari tapak webnya dan mengendalikan bahasa yang berbeza. Ia lebih perlahan dan menghasilkan fail yang lebih besar apabila menggunakan fon Unicode daripada skrip asal seperti HTML2FPDF, tetapi menyokong gaya CSS dsb. dan mempunyai banyak peningkatan. Menyokong hampir semua bahasa, termasuk RTL (Arab dan Ibrani) dan CJK (Cina, Jepun dan Korea). Menyokong elemen peringkat blok bersarang (seperti P, DIV),
