Tic tac toe游戏不检查抽奖

时间:2014-05-07 20:08:16

标签: python

您好。我为课堂做了一个tic tac toe游戏(在我们学习pygame之前),我的代码没有检查它一直持续的抽奖。此外,当计算机为拍摄的地点生成数字时,我不知道如何使计算机生成未拍摄的地点。请原谅我的混乱。

import random # import random module

def show(): 
    print board[0],'|',board[1],'|',board[2]    
    print '----------'
    print board[3],'|',board[4],'|',board[5]
    print '----------'
    print board[6],'|',board[7],'|',board[8]

def checkLine(char, spot1, spot2, spot3): #check if line is filled
    if board[spot1] == char and board[spot2] == char and board[spot3] == char: 
        return True

def checkAll(char): # ways you can win
    if checkLine(char, 0, 1, 2): # across first row
        return True
    if checkLine(char, 1, 4, 7): # second column
        return True
    if checkLine(char, 2, 5, 8): # third column 
        return True

    if checkLine(char, 6, 7, 8): # across third row
        return True
    if checkLine(char, 3, 4, 5): # across second row
        return True
    if checkLine(char, 0, 1, 2): # across first row
        return True

    if checkLine(char, 2, 4, 6): # diagonal 
        return True
    if checkLine(char, 0, 4, 8): # diagonal
        return True
    if checkLine(char, 0, 3, 6): # first column
        return True

def play_again(): # asks user if they want to play again 
        reset = input("Play again? Yes(1) No(2): ") # ask fi they want to play again 

        if reset == 1: # if yes run game again 
            game()
        else: # quit program 
            print "Thanks for playing Good-bye!"
            quit 

def computer_o(): # if computer is playing as 'x'

        random.seed() 
        computer_moves = random.randint(0,8) # computer finds number to use 
        print "COMPUTER: ", computer_moves

        if board[computer_moves] != 'x' and board[computer_moves] != 'o':
            board [computer_moves] = 'o' # place 'o' if space is empty 
        else:
            print "Spot taken"  
        show()   # display board 

def computer_x(): # if computer is playing as 'o'


        random.seed() 
        computer_moves = random.randint(0,8) # computer finds number to use
        print "COMPUTER: ", computer_moves

        if board[computer_moves] != 'x' and board[computer_moves] != 'o':
            board [computer_moves] = 'x' # place 'o' if spaece is empty 
        else:
            print "Spot taken" 
        show()  # display board




def player_x (): # if player wants ot play as 'x' 



        player_moves = input("PLAYER: ") #ask user where they would like to move

        if board[player_moves] != 'x' and board[player_moves] != 'o': 
            board[player_moves] = 'x' # place 'x' if space is empty 
        else:
            print "Spot taken" 
        show() # display board

def player_o():

        player_moves = input("PLAYER: ") # ask user where they would like to move

        if board[player_moves] != 'x' and board[player_moves] != 'o': 
            board[player_moves] = 'o' # place 'o' if space is empty 
        else:
            print "Spot taken" 
        show() # display board 


def game():

    # rest board for next game 

    board[0] = 0
    board[1] = 1
    board[2] = 2
    board[3] = 3
    board[4] = 4
    board[5] = 5
    board[6] = 6
    board[7] = 7
    board[8] = 8





    choice = raw_input("X OR O: ") # ask user for choice 
    choice = choice.lower() # change choice to lowercase if it's in uppercase
    print

    if choice == 'x': 
        print name, "IS X"
        print 
        print "COMPUTER IS O"
        print 

    elif choice == 'o':
        print name, "IS O"
        print 
        print "COMPUTER IS X"
        print


    who_goes_first = random.randint(0,1) # decide who goes first 

    if who_goes_first == 0:
        print name, "GOES FIRST!"
        print
        print
        print "BOARD"
        print 
        show()
        print
    elif who_goes_first == 1:
        print "COMPUTER GOES FIRST!"
        print
        print
        print "BOARD"
        print
        show()
        print



    if who_goes_first == 0 and choice == 'x':

        while checkAll('x') != True and checkAll ('o') != True: # while no one is winning

            player_x() # player goes first as x 
            computer_o() # computer goes second as o 

        if checkAll('x') == True:  # if x wins
            print " X WINS "
            play_again() # aks user if they want to play again 

        elif checkAll('o') == True: # if o wins
            print "O WINS"
            play_again() # ask if they want to play again 

        else:
            print "DRAW"
            play_again() # ask if they want to play again 

    elif who_goes_first == 0 and choice == 'o':

        while checkAll('x') != True and checkAll ('o') != True: # while no one is winning 

            player_o() # player goes first as o 
            computer_x() # computer goes second as x 

        if checkAll('x') == True: # if x wins 
            print " X WINS "
            play_again()

        elif checkAll('o') == True: # if o wins 
            print "O WINS"
            play_again() # ask if the user wants to again 

        else:
            print "DRAW"
            play_again() # ask if the user was to go again 


    elif who_goes_first == 1 and choice == 'x':

            while checkAll('x') != True and checkAll ('o') != True: # while no one is winning

                computer_o() # computer goes first as o 
                player_x() # player goes second as x 

            if checkAll('x') == True: # if x wins 
                print " X WINS "
                play_again() # ask if they want to play again

            elif checkAll('o') == True: # if o wins
                print "O WINS"
                play_again() # ask if they want to play again

            else:
                print "DRAW"
                play_again() # ask if they want to play again

    elif who_goes_first == 1 and choice == 'o': 

            while checkAll('x') != True and checkAll ('o') != True: # while no one is winning

                computer_x() # computer goes first as x 
                player_o() # player goes second as o 

            if checkAll('x') == True: # if x wins 
                print " X WINS "
                play_again() # ask if they want to play again 

            elif checkAll('o') == True: # if o wins 
                print "O WINS"
                play_again() # ask if they want to play again 

            else:
                print "DRAW"
                play_again() # ask if they want to play again




board = [0,1,2,
         3,4,5,
         6,7,8]



print "WELCOME TO TIC TAC TOE!"
print "-----------------------"
print

name = raw_input("Enter your name: ")
print
print "Welcome,", name, "!"
print



choice = raw_input("X OR O: ")
choice = choice.lower()
print

if choice == 'x':
    print name, "IS X"
    print 
    print "COMPUTER IS O"
    print 

elif choice == 'o':
    print name, "IS O"
    print 
    print "COMPUTER IS X"
    print


who_goes_first = random.randint(0,1)  # decides whos going first

if who_goes_first == 0:
    print name, "GOES FIRST!"
    print
    print
    print "BOARD"
    print 
    show()
    print
elif who_goes_first == 1:
    print "COMPUTER GOES FIRST!"
    print
    print
    print "BOARD"
    print
    show()
    print


if who_goes_first == 0 and choice == 'x':

    while checkAll('x') != True and checkAll ('o') != True: # while no one is winning 

        player_x() #player goes first as x 
        computer_o() # computer goes second as o 

    if checkAll('x') == True:
        print " X WINS "
        play_again() # ask if they want to play again 

    elif checkAll('o') == True:
        print "O WINS"
        play_again() # ask if they want to play again 

    else:
        print "DRAW"
        play_again() # asj if they want to play again 

elif who_goes_first == 0 and choice == 'o':

    while checkAll('x') != True and checkAll ('o') != True: # while no one is winning 

        player_o() # player goes first as o 
        computer_x() # computer goes second as x 

    if checkAll('x') == True:
        print " X WINS "
        play_again() # ask if they want to play again 

    elif checkAll('o') == True:
        print "O WINS"
        play_again() # ask if they want to play again 

    else:
        print "DRAW"
        play_again() # aks if they want to play again


elif who_goes_first == 1 and choice == 'x':

        while checkAll('x') != True and checkAll ('o') != True:

            computer_o() # computer goes first as o 
            player_x() # player goes second as x 

        if checkAll('x') == True:
            print " X WINS "
            play_again() # ask they want to play again 

        elif checkAll('o') == True:
            print "O WINS"
            play_again() # ask if they want to play again 

        else:
            print "DRAW"
            play_again() # aks if they want to play again 

elif who_goes_first == 1 and choice == 'o':

        while checkAll('x') != True and checkAll ('o') != True:

            computer_x() # computer goes first as x 
            player_o() # player goes second as o 

        if checkAll('x') == True:
            print " X WINS "
            play_again() # ask if they want to play again 

        elif checkAll('o') == True:
            print "O WINS"
            play_again() # if they want to play again 

        else:
            print "DRAW"
            play_again() # if they want to play again

1 个答案:

答案 0 :(得分:1)

您应该检查电路板的未占用元素。

free = []
for i in range(len(board)):
    if board[i] == i:
    free.append(i)

如果没有更多的方格可用且你没有获胜条件,那么它就是平局。您也可以从自由方格中选择计算机的移动。

computer_move = random.choice(free)