使用海龟的Python Tic Tac Toe。 (用户必须在相应的方格中输入数字。)

时间:2014-05-14 02:53:26

标签: python module tic-tac-toe turtle-graphics

为什么refresh2函数没有做任何事情?它应该检查O玩家是否获胜,但它没有这样做。我真的需要帮助才能使第二个刷新功能正常工作。任何帮助将不胜感激。此外,在玩家获胜后,应该在玩家获胜的地方划一条线。它确实如此,但它是非常不准确和倾斜的。如果您有任何答案(星期五),请通过powdercheeze@gmail.com通知我

from turtle import *
from os import *
import math
list1 = {1: (-100,100),2: (0,100), 3: (100,100), 4: (-100,0), 5: (0,0), 6: (100,0), 7: (-100,-100), 8: (0,-100), 9: (100,-100)}
list2 = [];list3 = [];list4 = {1: (-100,100),2: (0,100), 3: (100,100), 4: (-100,0), 5: (0,0), 6: (100,0), 7: (-100,-100), 8: (0,-100), 9: (100,-100)}
t = Turtle();
def turtle1():
    t.home();resetscreen()
    t.speed(10000);t.up();t.backward(150);t.pendown();t.forward(300);t.left(90);t.up();t.forward(1/3*300);t.pendown();t.left(90);t.forward(300);t.right(180)
    t.forward(100);t.left(90);t.forward(100);t.right(180);t.forward(300);t.right(180);
    t.forward(100)
    t.right(90)
    t.forward(100)
    t.right(90)
    t.forward(100)
    t.right(180)
    t.forward(300)
def input1():
    a = int(input("X player, enter a square: "))
    if a in list1:
        t = Turtle();t.up();t.setposition(list1[a]);t.left(90);t.forward(50);t.pendown();t.right(45);
        t.forward(40*math.sqrt(2));
        t.left(180);
        t.forward(80*math.sqrt(2))
        t.left(180)
        t.forward(40*math.sqrt(2))
        t.right(90)
        t.forward(40*math.sqrt(2))
        t.right(180)
        t.forward(80*math.sqrt(2))
        list1.pop(a)
        list2.append(a)
        refresh2() 
    if a not in list1:
        print("Enter a valid square, or one that hasn't been taken yet.")
        input1()
def input2():
    b = int(input("O player, enter a square: "))
    if b in list1:
        c = Turtle();c.up();c.setposition(list1[b]);
        c.down()
        c.circle(47)
        list1.pop(b)
        input1()
        list3.append(b)
        refresh();refresh2() 
    if b not in list1:
        print("Enter a valid square, or one that hasn't been taken yet.")
        input2()
def refresh():
    global list2;global list1;
    if 1 in list2 and 2 in list2 and 3 in list2 or 4 in list2 and 5 in list2 and 6 in list2 or 7 in list2 and 8 in list2 and 9 in list2: 
        print("The X Player Won! ");list2.sort();t.up();t.left(45);t.forward(80*math.sqrt(2));t.setposition(list4[int(list2[1])]);t.down();t.goto(list4[list2[int(len(list2)-1)]])
        z = input("Do you want to play again? ")
        if z == "y" or z == "Y": list2 = [];list1 = list4;turtle1();input1()
    if 1 in list2 and 4 in list2 and 7 in list2 or 2 in list2 and 5 in list2 and 8 in list2 or 3 in list2 and 6 in list2 and 9 in list2:
        print("The X Player Won! ");list2.sort();t.up();t.left(45);t.forward(80*math.sqrt(2));t.setposition(list4[int(list2[1])]);t.down();t.goto(list4[list2[int(len(list2)-1)]])
        z = input("Do you want to play again? ")
        if z == "y" or z == "Y": list2 = [];list1 = list4;turtle1();input1()
    if 1 in list2 and 5 in list2 and 9 in list2 or 3 in list2 and 5 in list2 and 7 in list2:
        print("The X Player Won! ");list2.sort();t.up();t.left(45);t.forward(80*math.sqrt(2));t.setposition(list4[int(list2[1])]);t.down();t.goto(list4[list2[int(len(list2)-1)]])
        z = input("Do you want to play again? ")
        if z == "y" or z == "Y": list2 = [];list1 = list4;turtle1();input1()
    if len(list1) == 0:print("Tie!")
    else:
        input2()
def refresh2():
    if 1 in list2 and 2 in list2 and 3 in list2 or 4 in list2 and 5 in list2 and 6 in list2 or 7 in list2 and 8 in list2 and 9 in list2: 
        print("The O Player Won! ");list2.sort();t.up();t.left(90);t.forward(50);t.setposition(list4[int(list2[1])]);t.down();t.goto(list4[list2[int(len(list2)-1)]])
        z = input("Do you want to play again? ")
        if z == "y" or z == "Y": list2 = [];list1 = list4;turtle1();input1()
    if 1 in list3 and 4 in list3 and 7 in list3 or 2 in list3 and 5 in list3 and 8 in list3 or 3 in list3 and 6 in list3 and 9 in list3:
        print("The O Player Won! ");list3.sort();t.up();t.left(90);t.forward(50);t.setposition(list4[int(list3[1])]);t.down();t.goto(list4[list3[int(len(list3)-1)]])
        z = input("Do you want to play again? ")
        if z == "y" or z == "Y": list3 = [];list1 = list4;turtle1();input1()
    if 1 in list3 and 5 in list3 and 9 in list3 or 3 in list3 and 5 in list3 and 7 in list3:
        print("The O Player Won! ");list3.sort();t.up();t.left(90);t.forward(50);t.setposition(list4[int(list3[1])]);t.down();t.goto(list4[list3[int(len(list3)-1)]])
        z = input("Do you want to play again? ")
        if z == "y" or z == "Y": list3 = [];list1 = list4;turtle1();input1()
    if len(list1) == 0:print("Tie!")
turtle1()
while True:
    input1()
    input2()

1 个答案:

答案 0 :(得分:0)

#-------------------------------------------------------------------------------
# Name:        TIC-TAC-TOE
#-------------------------------------------------------------------------------
import turtle
turtle.speed(0)

def MakeCircle():
    turtle.pencolor("black")
    turtle.circle(12)
    turtle.pencolor("white")

def MakeCross():
    turtle.pencolor("black")
    turtle.lt(45)
    turtle.forward(19)
    turtle.rt(180)
    turtle.forward(9.5)
    turtle.rt(90)
    turtle.forward(9.5)
    turtle.rt(180)
    turtle.forward(19)
    turtle.pencolor("white")

def MakeTable():
    turtle.forward (90)
    turtle.rt(180)
    turtle.forward(60)
    turtle.lt(90)
    turtle.forward(60)
    turtle.lt(180)
    turtle.forward(90)
    turtle.lt(180)
    turtle.forward(60)
    turtle.lt(90)
    turtle.forward(60)
    turtle.lt(180)
    turtle.forward(90)
    turtle.lt(180)
    turtle.forward(60)
    turtle.lt(90)
    turtle.forward(60)
    turtle.lt(180)
    turtle.forward(90)
    turtle.pencolor("white")
    turtle.goto(45,-15)

def PlayFirstCircle():
    play=raw_input(" CIRCLE : Where do you want to play? : ")
    i=1
    if play == "A":
        play = (4,15)
    elif play == "B":
        play = (4,-15)
    elif play == "C":
        play = (4,-45)
    elif play == "D":
        play = (34,15)
    elif play == "E":
        play = (34,-15)
    elif play == "F":
        play = (34,-45)
    elif play == "G":
        play = (64,15)
    elif play == "H":
        play = (64,-15)
    elif play == "I":
        play = (64,-45)
    elif play == "END":
        print " GAME FINISHES : Cross Wins!"
        i=0
        turtle.bye()
    turtle.goto(play)
    MakeCircle()
    return i

def PlayCircle():
    play=raw_input(" CIRCLE : Where do you want to play? : ")
    i=1
    if play == "A":
        play = (14,3)
    elif play == "B":
        play = (14,-27)
    elif play == "C":
        play = (14,-57)
    elif play == "D":
        play = (44,3)
    elif play == "E":
        play = (44,-23)
    elif play == "F":
        play = (44,-57)
    elif play == "G":
        play = (74,3)
    elif play == "H":
        play = (74,-27)
    elif play == "I":
        play = (74,-57)
    elif play == "END":
        print " GAME FINISHES : Cross Wins!"
        i=0
        turtle.bye()
    turtle.goto(play)
    MakeCircle()
    return i

def PlayCross():
    play=raw_input(" CROSS : Where do you want to play? : ")
    i=1
    if play == "A":
        play = (10,10)
    elif play == "B":
        play = (10,-20)
    elif play == "C":
        play = (10,-50)
    elif play == "D":
        play = (40,10)
    elif play == "E":
        play = (40,-20)
    elif play == "F":
        play = (40,-50)
    elif play == "G":
        play = (70,10)
    elif play == "H":
        play = (70,-20)
    elif play == "I":
        play = (70,-50)
    elif play == "END":
        print " GAME FINISHES : Circle Wins!"
        i=0
        turtle.bye()
    turtle.goto(play)
    MakeCross()
    return i

print "\n TIC - TAC - TOE "
print "\n The objective of the game is to complete a line of 3 circles(O) "
print " or crosses (X), before your opponent. (2 Player Game) "
print " Circle plays first. If a player has won, enter 'END' in input. "
print
print " Play according to this : "
print " A D G "
print " B E H "
print " C F I "

MakeTable()
PlayFirstCircle()
turtle.seth(0)
PlayCross()
turtle.seth(0)
PlayCircle()
turtle.seth(0)
PlayCross()
turtle.seth(0)
PlayCircle()
turtle.seth(0)
PlayCross()
turtle.seth(0)
PlayCircle()
turtle.seth(0)
PlayCross()
turtle.seth(0)
PlayCircle()
turtle.seth(0)