猫史档案馆


【Python作品分享】井字格游戏【作品秀】

用户:咸味煎鱼(工作室招人)咸味煎鱼(工作室招人)查看:0 回复:0 评论:0 创建时间:2021-02-09T14:01:17


【作品展示】

center_image

 

【作品介绍】

尝试将三点连成一线

 

【作品源代码】

import turtle
import random

mypen = turtle.Pen()
myscreen = turtle.Screen()

mypen.hideturtle()
mypen.pensize(5)
mypen.penup()
mypen.goto(-150,50)
mypen.pendown()
mypen.forward(300)

mypen.penup()
mypen.goto(-150, -50)
mypen.pendown()
mypen.forward(300)

mypen.penup()
mypen.goto(-50, 150)
mypen.right(90)
mypen.pendown()
mypen.forward(300)

mypen.penup()
mypen.goto(50, 150)
mypen.pendown()
mypen.forward(300)

a = 0
b = 0
i = 0
ping_ju = 0
winner = 0
com = 0
game = 0 
place_1 = 0
place_2 = 0
place_3 = 0
place_4 = 0
place_5 = 0
place_6 = 0
place_7 = 0
place_8 = 0
place_9 = 0
while i < 1:
    if place_1 == 2 and place_2 == 2 and place_3 == 2:
        winner = 2
        break
    elif place_4 == 2 and place_5 == 2 and place_6 == 2:
        winner = 2
        break
    elif place_7 == 2 and place_8 == 2 and place_9 == 2:
        winner = 2
        break
    elif place_1 == 2 and place_4 == 2 and place_7 == 2:
        winner = 2
        break
    elif place_2 == 2 and place_5 == 2 and place_8 == 2:
        winner = 2
        break
    elif place_3 == 2 and place_6 == 2 and place_9 == 2:
        winner = 2
        break
    elif place_1 == 2 and place_5 == 2 and place_9 == 2:
        winner = 2
        break
    elif place_3 == 2 and place_5 == 2 and place_7 == 2:
        winner = 2
        break

    if place_1 != 0 and place_2 != 0  and place_3 != 0 and place_4 != 0 and place_5 != 0 and place_6 != 0 and place_7 != 0 and place_8 != 0 and place_9 != 0:
        ping_ju = 1
        break
    else:
        print(" 1 | 2 | 3 ")
        print("---+---+---")
        print(" 4 | 5 | 6 ")
        print("---+---+---")
        print(" 7 | 8 | 9 ")
        player = float(turtle.textinput("位置", "请输入您想画的所在的位置(若输入小数则自动向下取整)"))
        b = 0
        if 1 <= player and player <= 9:
            player = int(player)

            if player == 1 and place_1 != 0:
                print("这个地方已经画过了!")
            elif player == 2 and place_2 != 0:
                print("这个地方已经画过了!")
            elif player == 3 and place_3 != 0:
                print("这个地方已经画过了!")
            elif player == 4 and place_4 != 0:
                print("这个地方已经画过了!")
            elif player == 5 and place_5 != 0:
                print("这个地方已经画过了!")
            elif player == 6 and place_6 != 0:
                print("这个地方已经画过了!")
            elif player == 7 and place_7 != 0:
                print("这个地方已经画过了!")
            elif player == 8 and place_8 != 0:
                print("这个地方已经画过了!")
            elif player == 9 and place_9 != 0:
                print("这个地方已经画过了!")
            else:
                print("您选择的位置是",player)
                mypen.penup()
                if player == 1:
                    place_1 = 1
                    mypen.goto(-100,100)
                    b = 1
                elif player == 2:
                    place_2 = 1
                    mypen.goto(0, 100)
                    b = 1
                elif player == 3:
                    place_3 = 1
                    mypen.goto(100, 100)
                    b = 1
                elif player == 4:
                    place_4 = 1
                    mypen.goto(-100, 0)
                    b = 1
                elif player == 5:
                    place_5 = 1
                    mypen.goto(0, 0)
                    b = 1
                elif player == 6:
                    place_6 = 1
                    mypen.goto(100, 0)
                    b = 1
                elif player == 7:
                    place_7 = 1
                    mypen.goto(-100, -100)
                    b = 1
                elif player == 8:
                    place_8 = 1
                    mypen.goto(0, -100)
                    b = 1
                else:
                    place_9 = 1
                    mypen.goto(100, -100)
                    b = 1

                mypen.pendown()
                mypen.pencolor("green")
                mypen.dot(80)
                mypen.pencolor("white")
                mypen.dot(75)

                if place_1 == 1 and place_2 == 1 and place_3 == 1:
                    winner = 1
                    break
                elif place_4 == 1 and place_5 == 1 and place_6 == 1:
                    winner = 1
                    break
                elif place_7 == 1 and place_8 == 1 and place_9 == 1:
                    winner = 1
                    break
                elif place_1 == 1 and place_4 == 1 and place_7 == 1:
                    winner = 1
                    break
                elif place_2 == 1 and place_5 == 1 and place_8 == 1:
                    winner = 1
                    break
                elif place_3 == 1 and place_6 == 1 and place_9 == 1:
                    winner = 1
                    break
                elif place_1 == 1 and place_5 == 1 and place_9 == 1:
                    winner = 1
                    break
                elif place_3 == 1 and place_5 == 1 and place_7 == 1:
                    winner = 1
                    break

                if place_1 != 0 and place_2 != 0  and place_3 != 0 and place_4 != 0 and place_5 != 0 and place_6 != 0 and place_7 != 0 and place_8 != 0 and place_9 != 0:
                    ping_ju = 1
                    break
                else:
                    if b == 1:
                        while a < 1:
                            mypen.penup()
                            if com == 1 and place_1 == 0:
                                place_1 = 2
                                mypen.goto(-100, 100)
                                break
                            elif com == 2 and place_2 == 0:
                                place_2 = 2
                                mypen.goto(0, 100)
                                break
                            elif com == 3 and place_3 == 0:
                                place_3 = 2
                                mypen.goto(100, 100)
                                break
                            elif com == 4 and place_4 == 0:
                                place_4 = 2
                                mypen.goto(-100, 0)
                                break
                            elif com == 5 and place_5 == 0:
                                place_5 = 2
                                mypen.goto(0, 0)
                                break
                            elif com == 6 and place_6 == 0:
                                place_6 = 2
                                mypen.goto(100, 0)
                                mypen.pendown()
                                break
                            elif com == 7 and place_7 == 0:
                                place_7 = 2
                                mypen.goto(-100, -100)
                                break
                            elif com == 8 and place_8 == 0:
                                place_8 = 2
                                mypen.goto(0, -100)
                                break
                            elif com == 9 and place_9 == 0:
                                place_9 = 2
                                mypen.goto(100, -100)
                                break
                            else:
                                com = random.randint(1, 9)
                        print("*******************")
                        print("电脑选择的位置是", com)
                        mypen.pendown()
                        mypen.pencolor("red")
                        mypen.dot(80)
                        mypen.pencolor("white")
                        mypen.dot(75)
                        game = game + 1
                        print("-------------------")
        else:
            print("请输入大于0小于10的数!若输入小数则自动向下取整!")

print("本次一共游玩了",game + 1,"局")
if ping_ju == 1:
    print("本次游玩结果是:平局")
if winner == 1:
    print("本次游玩结果是:玩家胜利")
elif winner == 2:
    print("本次游玩结果是:电脑胜利")
    print("不要气馁,再接再厉")

turtle.done()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页