python实现井字棋游戏
作者:Nick_Aaron 发布时间:2022-02-27 15:16:49
标签:python,井字棋
本文实例介绍了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俄罗斯方块游戏集合
python经典小游戏汇总
python微信跳一跳游戏集合
0
投稿
猜你喜欢
- 前言:python虽然是一门'慢语言',但是也有着比较多的性能检测工具来帮助我们优化程序的运行效率。这里总结了五个比较好的p
- 本文实例为大家分享了python实现图书管理系统的具体代码,供大家参考,具体内容如下题目:写一个简单的图书借阅系统
- 一、实现过程终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关转义序列是以ESC开头,即用\033来完成(ESC
- 我们做WEB程序开发过程中,经常会为了表格Table中的cellSpacing,cellpadding等样式的设置发愁,经常没有办法可想了,
- 今天解析DEDECMS时发现deder的MYSQL时间字段,都是用`senddata` int(10) unsigned NOT NULL
- EXEC SQL WHENEVER SQLERROR CONTINUE; sqlglm(msg_buffer, &buf
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 9 - Input Filterin
- 1、你需要通过指定的文本模式去检查字符串的开头或者结尾,比如文件名后缀,URL Scheme 等等。检 查 字 符 串 开 头 或 结 尾
- 本文实例讲述了php查找指定目录下指定大小文件的方法。分享给大家供大家参考。具体实现方法如下:php查找文件大小的原理是遍历目录然后再利用f
- 简介除ide的执行方式外,命令行的方式执行Python脚本是参数化程序执行的一种常见且简单的方法,正确处理命令行参数,可以提供给包含某种参数
- pytorch在Horovod上训练步骤分为以下几步:import torchimport horovod.torch as hvd# In
- 背景: 由于工作需要,现在有这么一个需求,要合并大量的word文档,而且要在不同的目录
- 概述要访问一个变量的内容,可以直接使用其名称。如果该变量是一个数组,可以使用变量名称和关键字或索引的组合来访问其内容。像其他变量一样,使用运
- 今天突然想起这个问题, 就好好搜索整理一下,不过在开始归纳之前,请先来一起做做这个小实验:忽略一切实际的外在情况, 你看了下面的按钮,第一本
- eval()函数eval() 函数用来执行一个字符串表达式,并返回表达式的值。语法eval(expression[, globals[, l
- F()函数F()函数的导入from django.db.models import F为什么要使用F()函数?一个 F()对象代表了一个mo
- 前言ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、f
- 看下面的Java代码,目的是为了当i是3的时候,就不做输出,直接跳到下一个循环。int i = 0; 
- Limit语法:SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offsetL
- 摘要:本文介绍了字符与编码的发展过程,相关概念的正确理解。举例说明了一些实际应用中,编码的实现方法。然后,本文讲述了通常对字符与编码的几种误