340行的井字格游戏

import random
from itertools import permutations
def play_again():print("你想要在尝试一遍吗(Yes or No)")response = input(">").upper()if response.startswith("Y"):return Trueelse:return False
def start():win = Falseplayer_moves = []computer_moves = []attempt = []turns = 0tic_tac_screen = [["", "", ""],["", "", ""],["", "", ""]]list_1 = [5]list_2 = [1, 3]list_3 = [5]list_4 = [1, 7]list_5 = [1, 3, 7, 9]list_6 = [3, 9]list_7 = [5]list_8 = [7, 9]list_9 = [5]winning_combinations = {(1, 2, 3), (4, 5, 6), (7, 8, 9), (1, 4, 7), (2, 5, 8), (3, 6, 9), (1, 5, 9), (3, 5, 7)}def comp():computer_check = str(computer_moves[-1])if "1" in computer_check:tic_tac_screen[0][0] = "o"elif "2" in computer_check:tic_tac_screen[0][1] = "o"elif "3" in computer_check:tic_tac_screen[0][2] = "o"elif "4" in computer_check:tic_tac_screen[1][0] = "o"elif "5" in computer_check:tic_tac_screen[1][1] = "o"elif "6" in computer_check:tic_tac_screen[1][2] = "o"elif "7" in computer_check:tic_tac_screen[2][0] = "o"elif "8" in computer_check:tic_tac_screen[2][1] = "o"elif "9" in computer_check:tic_tac_screen[2][2] = "o"def turn_1():if 1 in player_moves:computer_moves.append(random.choice(list_1))comp()elif 2 in player_moves:computer_moves.append(random.choice(list_2))comp()elif 3 in player_moves:computer_moves.append(random.choice(list_3))comp()elif 4 in player_moves:computer_moves.append(random.choice(list_4))comp()elif 5 in player_moves:computer_moves.append(random.choice(list_5))comp()elif 6 in player_moves:computer_moves.append(random.choice(list_6))comp()elif 7 in player_moves:computer_moves.append(random.choice(list_7))comp()elif 8 in player_moves:computer_moves.append(random.choice(list_8))comp()elif 9 in player_moves:computer_moves.append(random.choice(list_9))comp()def comp_game():if (1 in computer_moves) and (2 in computer_moves) and (3 not in player_moves):computer_moves.append(3)comp()elif (1 in computer_moves) and (3 in computer_moves) and (2 not in player_moves):computer_moves.append(2)comp()elif (2 in computer_moves) and (3 in computer_moves) and (1 not in player_moves):computer_moves.append(1)comp()elif (4 in computer_moves) and (5 in computer_moves) and (6 not in player_moves):computer_moves.append(6)comp()elif (4 in computer_moves) and (6 in computer_moves) and (5 not in player_moves):computer_moves.append(5)comp()elif (5 in computer_moves) and (6 in computer_moves) and (4 not in player_moves):computer_moves.append(4)comp()elif (7 in computer_moves) and (8 in computer_moves) and (9 not in player_moves):computer_moves.append(9)comp()elif (7 in computer_moves) and (9 in computer_moves) and (8 not in player_moves):computer_moves.append(8)comp()elif (8 in computer_moves) and (9 in computer_moves) and (7 not in player_moves):computer_moves.append(7)comp()elif (1 in computer_moves) and (4 in computer_moves) and (7 not in player_moves):computer_moves.append(7)comp()elif (1 in computer_moves) and (7 in computer_moves) and (4 not in player_moves):computer_moves.append(4)comp()elif (4 in computer_moves) and (7 in computer_moves) and (1 not in player_moves):computer_moves.append(1)comp()elif (2 in computer_moves) and (5 in computer_moves) and (8 not in player_moves):computer_moves.append(8)comp()elif (2 in computer_moves) and (8 in computer_moves) and (5 not in player_moves):computer_moves.append(5)comp()elif (5 in computer_moves) and (8 in computer_moves) and (2 not in player_moves):computer_moves.append(2)comp()elif (3 in computer_moves) and (6 in computer_moves) and (9 not in player_moves):computer_moves.append(9)comp()elif (3 in computer_moves) and (9 in computer_moves) and (6 not in player_moves):computer_moves.append(6)comp()elif (6 in computer_moves) and (9 in computer_moves) and (3 not in player_moves):computer_moves.append(3)comp()elif (1 in computer_moves) and (5 in computer_moves) and (9 not in player_moves):computer_moves.append(9)comp()elif (1 in computer_moves) and (9 in computer_moves) and (5 not in player_moves):computer_moves.append(5)comp()elif (5 in computer_moves) and (9 in computer_moves) and (1 not in player_moves):computer_moves.append(1)comp()elif (3 in computer_moves) and (5 in computer_moves) and (7 not in player_moves):computer_moves.append(7)comp()elif (3 in computer_moves) and (7 in computer_moves) and (5 not in player_moves):computer_moves.append(5)comp()elif (5 in computer_moves) and (7 in computer_moves) and (3 not in player_moves):computer_moves.append(3)comp()else:if (1 in player_moves) and (2 in player_moves) and (3 not in computer_moves):computer_moves.append(3)comp()elif (1 in player_moves) and (3 in player_moves) and (2 not in computer_moves):computer_moves.append(2)comp()elif (2 in player_moves) and (3 in player_moves) and (1 not in computer_moves):computer_moves.append(1)comp()elif (4 in player_moves) and (5 in player_moves) and (6 not in computer_moves):computer_moves.append(6)comp()elif (4 in player_moves) and (6 in player_moves) and (5 not in computer_moves):computer_moves.append(5)comp()elif (5 in player_moves) and (6 in player_moves) and (4 not in computer_moves):computer_moves.append(4)comp()elif (7 in player_moves) and (8 in player_moves) and (9 not in computer_moves):computer_moves.append(9)comp()elif (7 in player_moves) and (9 in player_moves) and (8 not in computer_moves):computer_moves.append(8)comp()elif (8 in player_moves) and (9 in player_moves) and (7 not in computer_moves):computer_moves.append(7)comp()elif (1 in player_moves) and (4 in player_moves) and (7 not in computer_moves):computer_moves.append(7)comp()elif (1 in player_moves) and (7 in player_moves) and (4 not in computer_moves):computer_moves.append(4)comp()elif (4 in player_moves) and (7 in player_moves) and (1 not in computer_moves):computer_moves.append(1)comp()elif (2 in player_moves) and (5 in player_moves) and (8 not in computer_moves):computer_moves.append(8)comp()elif (2 in player_moves) and (8 in player_moves) and (5 not in computer_moves):computer_moves.append(5)comp()elif (5 in player_moves) and (8 in player_moves) and (2 not in computer_moves):computer_moves.append(2)comp()elif (3 in player_moves) and (6 in player_moves) and (9 not in computer_moves):computer_moves.append(9)comp()elif (3 in player_moves) and (9 in player_moves) and (6 not in computer_moves):computer_moves.append(6)comp()elif (6 in player_moves) and (9 in player_moves) and (3 not in computer_moves):computer_moves.append(3)comp()elif (1 in player_moves) and (5 in player_moves) and (9 not in computer_moves):computer_moves.append(9)comp()elif (1 in player_moves) and (9 in player_moves) and (5 not in computer_moves):computer_moves.append(5)comp()elif (5 in player_moves) and (9 in player_moves) and (1 not in computer_moves):computer_moves.append(1)comp()elif (3 in player_moves) and (5 in player_moves) and (7 not in computer_moves):computer_moves.append(7)comp()elif (3 in player_moves) and (7 in player_moves) and (5 not in computer_moves):computer_moves.append(5)comp()elif (5 in player_moves) and (7 in player_moves) and (3 not in computer_moves):computer_moves.append(3)comp()elif (2 in player_moves) and (6 in player_moves) and (5 not in computer_moves):computer_moves.append(5)comp()elif (2 in player_moves) and (4 in player_moves) and (5 not in computer_moves):computer_moves.append(5)comp()elif (4 in player_moves) and (8 in player_moves) and (5 not in computer_moves):computer_moves.append(5)comp()elif (6 in player_moves) and (8 in player_moves) and (5 not in computer_moves):computer_moves.append(5)comp()elif (1 in player_moves) and (8 in player_moves) and (5 not in computer_moves):computer_moves.append(5)comp()elif (3 in player_moves) and (8 in player_moves) and (5 not in computer_moves):computer_moves.append(5)comp()elif (2 in player_moves) and (7 in player_moves) and (5 not in computer_moves):computer_moves.append(5)comp()elif (2 in player_moves) and (9 in player_moves) and (5 not in computer_moves):computer_moves.append(5)comp()elif (3 in player_moves) and (4 in player_moves) and (5 not in computer_moves):computer_moves.append(5)comp()elif (4 in player_moves) and (9 in player_moves) and (5 not in computer_moves):computer_moves.append(5)comp()elif (1 in player_moves) and (6 in player_moves) and (5 not in computer_moves):computer_moves.append(5)comp()elif (6 in player_moves) and (7 in player_moves) and (5 not in computer_moves):computer_moves.append(5)comp()elif (5 in player_moves) and (1 in player_moves) and (9 in computer_moves) and (7 not in computer_moves):computer_moves.append(7)comp()elif (5 in player_moves) and (3 in player_moves) and (7 in computer_moves) and (9 not in computer_moves):computer_moves.append(9)comp()elif (5 in player_moves) and (9 in player_moves) and (1 in computer_moves) and (3 not in computer_moves):computer_moves.append(3)comp()elif (5 in player_moves) and (7 in player_moves) and (3 in computer_moves) and (1 not in computer_moves):computer_moves.append(1)comp()else:comp_gen = Falsewhile not comp_gen:if (2 not in player_moves) and (2 not in computer_moves):computer_moves.append(2)comp()comp_gen = Trueelif (4 not in player_moves) and (4 not in computer_moves):computer_moves.append(4)comp()comp_gen = Trueelif (6 not in player_moves) and (6 not in computer_moves):computer_moves.append(6)comp()comp_gen = Trueelif (8 not in player_moves) and (8 not in computer_moves):computer_moves.append(8)comp()comp_gen = Trueelif (1 not in player_moves) and (1 not in computer_moves):computer_moves.append(1)comp()comp_gen = Trueelif (3 not in player_moves) and (3 not in computer_moves):computer_moves.append(3)comp()comp_gen = Trueelif (5 not in player_moves) and (5 not in computer_moves):computer_moves.append(5)comp()comp_gen = Trueelif (7 not in player_moves) and (7 not in computer_moves):computer_moves.append(7)comp()comp_gen = Trueelif (9 not in player_moves) and (9 not in computer_moves):computer_moves.append(9)comp()comp_gen = Trueelse:comp_gen = Truewhile not win:tic_tac_screen1 = [["1", "2", "3"],["4", "5", "6"],["7", "8", "9"]]for row in tic_tac_screen1:print(row)print()for row in tic_tac_screen:print(row)attempt = int(input("输入空格对应的数字:"))player_check = str(attempt)if attempt in player_moves:print("这个区域已经被占,再试一次")elif attempt in computer_moves:print("这个区域已经被占,再试一次")elif attempt not in player_moves:player_moves.append(attempt)turns += 1if "1" in player_check:tic_tac_screen[0][0] = "x"elif "2" in player_check:tic_tac_screen[0][1] = "x"elif "3" in player_check:tic_tac_screen[0][2] = "x"elif "4" in player_check:tic_tac_screen[1][0] = "x"elif "5" in player_check:tic_tac_screen[1][1] = "x"elif "6" in player_check:tic_tac_screen[1][2] = "x"elif "7" in player_check:tic_tac_screen[2][0] = "x"elif "8" in player_check:tic_tac_screen[2][1] = "x"elif "9" in player_check:tic_tac_screen[2][2] = "x"for moves in permutations(player_moves, 3):if moves in winning_combinations:for row in tic_tac_screen:print(row)print("你赢了")win = Truebreakelse:if turns == 1:turn_1()for moves in permutations(computer_moves, 3):if moves in winning_combinations:for row in tic_tac_screen:print(row)print("你失败了!")win = Truebreakelif turns == 2:comp_game()for moves in permutations(computer_moves, 3):if moves in winning_combinations:for row in tic_tac_screen:print(row)print("你失败了!")win = Truebreakelif turns == 3:comp_game()for moves in permutations(computer_moves, 3):if moves in winning_combinations:for row in tic_tac_screen:print(row)print("你失败了!")win = Truebreakelif turns == 4:comp_game()for moves in permutations(computer_moves, 3):if moves in winning_combinations:for row in tic_tac_screen:print(row)print("You Lose!")win = Truebreakelse:for row in tic_tac_screen:print(row)print("Tie Game")win = Truewhile True:start()if play_again():continueelse:break

井字格游戏Python实现相关推荐

  1. python井字棋ai_[Python100行系列]-井字棋游戏

    博客:Hzy的博客 | Hzy Blog​hzeyuan.cn一些学习python的小项目,小游戏.python小项目​github.com 话不多说,今天尝试用turtle库来写一个井字棋游戏.1. ...

  2. python井字棋_[Python100行系列]-井字棋游戏

    博客:Hzy的博客 | Hzy Blog​hzeyuan.cn一些学习python的小项目,小游戏.python小项目​github.com 话不多说,今天尝试用turtle库来写一个井字棋游戏.1. ...

  3. Python基础编程案例:简单的井字棋游戏设计与制作

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 前言 python井字棋游戏虽然看上去非常简陋,但是却非常值得学习. 先看怎么玩 ...

  4. python井字棋游戏代码_python实现井字棋游戏

    python实现井字棋游戏 来源:中文源码网    浏览: 次    日期:2018年9月2日 [下载文档:  python实现井字棋游戏.txt ] (友情提示:右键点上行txt文档名->目标 ...

  5. php井字游戏,python实现井字棋游戏

    #本游戏python3.4.0下编写调试,只能在windows下运行. import random import subprocess import time #定义函数 def draw_board ...

  6. python井字棋游戏大作业实验报告_Part 1.2 - 实现一个井字棋游戏的gym环境

    上文已经描述了怎么创建和注册一个自定义的gym环境.但是环境类中的4个函数都是空的,本文将描述怎么实现那4个函数,实现一个完整的井字棋游戏的环境. 游戏规则:两个玩家在3x3的棋盘上,一方执X,一方执 ...

  7. python井字棋游戏开发(人人对战,人机对战,包含源码,逻辑思维流程图)

    需求分析 井字棋是比较便捷休闲娱乐的一种迷你棋,玩法比较简单,只需要一个九宫格棋盘就可以实现两人对战,规则为谁先连成三个棋子的一条线即可获胜.本游戏,需要满足两个主要功能:1.能实现玩家对战:2.能实 ...

  8. C++井字棋游戏,DOS界面版

    据说有一个能保证不败的算法.明天看看先再写个PVC版的. 正题.今天无聊写了个井字棋游戏,顺便逐渐让自己习惯良好的代码风格,放上来给新手学习学习. jzq2.cpp /*N字棋游戏PVP版,DOS版本 ...

  9. C++实现的基于α-β剪枝算法的井字棋游戏

    "井字棋"游戏(又叫"三子棋"),是一款十分经典的益智小游戏,操作简单,娱乐性强.两个玩家,一个打圈(O),一个打叉(X),轮流在3乘3的格上打自己的符号,最先 ...

  10. [文档和源码分享]C++实现的基于α-β剪枝算法的井字棋游戏

    "井字棋"游戏(又叫"三子棋"),是一款十分经典的益智小游戏,操作简单,娱乐性强.两个玩家,一个打圈(O),一个打叉(X),轮流在3乘3的格上打自己的符号,最先 ...

最新文章

  1. intouch负值显示0_excel 应用中计算结果显示为负数,使负数显示为0应如何操作?...
  2. SQL教学思路《图书管理系统》习题二:插入数据
  3. 磁盘加密软件TrueCrypt知识大全(三)之加密非系统分区/设备
  4. 请问为什么像cellspacing=0和bgcolor=red的属性在style里面不起作用呢?
  5. Laravel中一些要记住 的写法
  6. 干货 | Python 标准库之 XML(上)
  7. 过滤特征_机器学习深度研究:特征选择中几个重要的统计学概念
  8. 【报告分享】85后、95后宝妈人群洞察报告.pdf(附下载链接)
  9. kibana报错Request Timeout after 30000ms故障解决
  10. 20155320 Exp3 免杀原理与实践
  11. Maven-Eclipse使用maven创建HelloWorld Java项目,maven常用的命令解析
  12. 基础运维神器:开源的裸金属服务器管理平台RackShift
  13. 独立开发的基于springboot + websocket IM网站聊天系统总结
  14. SAP Fiori Elements SmartLink 创建实例的单步调试
  15. 数据结构入门1(认识数据结构)
  16. 【NOIP模拟】 (11.6) T1 Blash数集
  17. 打造自己的LoRaWAN网关,进阶2:处理异常
  18. 个人开发android如何赚钱
  19. Gopher China 大会福利加码
  20. syslog详解及配置远程发送日志和远程日志分类

热门文章

  1. 华为云云耀云服务器L实例评测|云服务体验实战之Docker使用
  2. 中国矿业大学计算机学院升学,学霸是怎样炼成的 ——访中国矿业大学计算机学院大四国奖获得者李红...
  3. 从坐席到外呼,明道云与品聘云呼叫对接示例
  4. 如何阅读linux源代码,如何阅读Linux源码
  5. 一家服装定制公司从创立到上市,所有步骤及背后的商业模式分享!
  6. 在jsp中渲染隐藏的方法域
  7. Python 之MySql 每日一练 130——同名同性学生名单,并统计人数
  8. 德州学院计算机分数,2017德州学院录取分数线
  9. centos6.8连接WIFI无线上网
  10. 保安日记:番外篇之软件测试(一)