IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> 井字格游戏Python实现 -> 正文阅读

[游戏开发]井字格游戏Python实现

340行的井字格游戏

import random
from itertools import permutations
def play_again():
    print("你想要在尝试一遍吗(Yes or No)")
    response = input(">").upper()
    if response.startswith("Y"):
        return True
    else:
        return False
def start():
    win = False
    player_moves = []
    computer_moves = []
    attempt = []
    turns = 0
    tic_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 = False
                while not comp_gen:
                    if (2 not in player_moves) and (2 not in computer_moves):
                        computer_moves.append(2)
                        comp()
                        comp_gen = True
                    elif (4 not in player_moves) and (4 not in computer_moves):
                        computer_moves.append(4)
                        comp()
                        comp_gen = True
                    elif (6 not in player_moves) and (6 not in computer_moves):
                        computer_moves.append(6)
                        comp()
                        comp_gen = True
                    elif (8 not in player_moves) and (8 not in computer_moves):
                        computer_moves.append(8)
                        comp()
                        comp_gen = True
                    elif (1 not in player_moves) and (1 not in computer_moves):
                        computer_moves.append(1)
                        comp()
                        comp_gen = True
                    elif (3 not in player_moves) and (3 not in computer_moves):
                        computer_moves.append(3)
                        comp()
                        comp_gen = True
                    elif (5 not in player_moves) and (5 not in computer_moves):
                        computer_moves.append(5)
                        comp()
                        comp_gen = True
                    elif (7 not in player_moves) and (7 not in computer_moves):
                        computer_moves.append(7)
                        comp()
                        comp_gen = True
                    elif (9 not in player_moves) and (9 not in computer_moves):
                        computer_moves.append(9)
                        comp()
                        comp_gen = True
                    else:
                        comp_gen = True
    while 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 += 1
            if "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 = True
                    break
            else:
                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 = True
                            break
                elif 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 = True
                            break
                elif 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 = True
                            break
                elif 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 = True
                            break
                else:
                    for row in tic_tac_screen:
                        print(row)
                    print("Tie Game")
                    win = True

while True:
    start()
    if play_again():
        continue
    else:
        break
  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-11-25 08:26:52  更:2021-11-25 08:28:10 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/27 23:56:48-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码