tic tac toe与python中的数字

时间:2018-02-08 09:32:29

标签: python

我有H.W就是这样 Tic-Tac-Toe有数字。显示3 x 3的棋盘,玩家1获取奇数1, 3,5,7,9和玩家2取偶数0,2,4,6,8。玩家轮流写下他们的 数字。奇怪的数字开始了。仅使用每个号码一次。第一个完成一条线的人 最多可以增加15个。该行可以包含奇数和偶数 。

我直到这里

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


def tic_tac_toe ():

 print ('|' ,board[0],'|',board[1] ,'|', board[2],'|')

 print ('--------------------')

 print ('|' ,board[3],'|',board[4] ,'|', board[5],'|')

 print ('--------------------')

 print ('|' ,board[6],'|',board[7] ,'|', board[8],'|')


def move(x1,x2):

 board[x2] = x1

     tic_tac_toe()



def odd (x):

    while  (x%2==0):

        x = int(input ('enter an odd number')

        move(x,x2)

    if (x%2!=0):

        move (x ,x2)        



def even (x) :

     while  (x%2!=0):

        x = int(input ('enter an even number')

        move(x,x2)


     if (x%2==0):

        move (x ,x2)        


def winner ():

    if (board[0]+board [1]+board[2]==15 or

        board[0]+board [3]+board[6]==15 or

        board[1]+board [4]+board[7]==15 or

        board[3]+board [4]+board[5]==15 or

        board[2]+board [5]+board[8]==15 or

        board[6]+board [7]+board[8]==15):

        print ('you are the winner')

def turn(s):

    print ('its '+ s +' turn')

    x = int (input ('enter the number: '))
    x1 = int (input ('enter the places number: '))            


print('Tic Tac Toe')

print ('player A should enter even numbers only'+' and player B should enter odd 
numbers only')

print ('the player with the ood numbers start')

tic_tac_toe ()

while (true):

    turn(B)

    odd(x1)
    break    

我的问题现在我想制作一个功能,每次玩家输入一个数字时检查是否有赢家,我希望它知道输入的数字和已经存在的数字之间的差异(职位数量)  我是编程新手,所以如果代码有很多错误,请原谅我

2 个答案:

答案 0 :(得分:1)

试试这个:

我添加了一个板日志数组来监控哪些位置包含用户输入,然后在获胜者函数中交叉引用所述数组以验证获胜标准

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

player = 'a' #with this we'll know which player's turn it is

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

def move(x1,x2):
    board[x2] = x1
    boardLog[x2] = 1
    tic_tac_toe()

def odd (x, x2):
    while  (x%2==0):
        x = int(input ('enter an odd number'))
    #Nothing here because if we get out of the while is because it's a valid number (we're not checking numbers out of range or anything)
    move (x ,x2)      

def even (x ,x2) :
    while  (x%2!=0):
        x = int(input ('enter an even number'))
    #Same here
    move (x ,x2)        

def winner():
    if (boardLog[0] + boardLog[1] + boardLog[2] == 3):
      if (board[0]+board [1]+board[2]==15):
          print ('you are the winner')
          return True
    if (boardLog[0] + boardLog[3] + boardLog[6] == 3):
      if (board[0]+board [3]+board[6]==15):
          print ('you are the winner')
          return True
    if (boardLog[1] + boardLog[4] + boardLog[7] == 3):
      if (board[1]+board [4]+board[7]==15):
          print ('you are the winner')
          return True
    if (boardLog[3] + boardLog[4] + boardLog[5] == 3):
      if (board[3]+board [4]+board[5]==15):
          print ('you are the winner')
          return True
    if (boardLog[2] + boardLog[5] + boardLog[8] == 3):
      if (board[2]+board [5]+board[8]==15):
          print ('you are the winner')
          return True
    if (boardLog[6] + boardLog[7] + boardLog[8] == 3):
      if (board[6]+board [7]+board[8]==15):
          print ('you are the winner')
          return True

    else: return False

def turn(s):
    print ('its '+ s +' turn')
    x = int (input ('enter the number: '))
    x1 = int (input ('enter the places number: '))
    if player == 'a':
        even(x, x1)
    else: odd(x, x1)          

print('Tic Tac Toe')
print ('player A should enter even numbers only'+' and player B should enter odd numbers only')
print ('the player with the ood numbers start')
tic_tac_toe ()
while (True):
    turn(player)
    if winner(): break
    else:
        if player == 'a': player = 'b'

答案 1 :(得分:0)

我认为这接近你想要的。注意评论。也知道我没有测试过这个,我刚刚纠正了你的代码。如有任何问题,请随时提出。

board = [0, 0, 0,
         0, 0, 0,
         0, 0, 0]
player = 'a' #with this we'll know which player's turn it is

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

def move(x1,x2):
    board[x2] = x1
    tic_tac_toe()

def odd (x, x2):
    while  (x%2==0):
        x = int(input ('enter an odd number'))
    #Nothing here because if we get out of the while is because it's a valid number (we're not checking numbers out of range or anything)
    move (x ,x2)      

def even (x ,x2) :
    while  (x%2!=0):
        x = int(input ('enter an even number'))
    #Same here
    move (x ,x2)        

def winner ():
    if (board[0]+board [1]+board[2]==15 or
        board[0]+board [3]+board[6]==15 or
        board[1]+board [4]+board[7]==15 or
        board[3]+board [4]+board[5]==15 or
        board[2]+board [5]+board[8]==15 or
        board[6]+board [7]+board[8]==15):
        print ('you are the winner')
        return true #To know if we need to stop the game
    else: return false

def turn(s):
    print ('its '+ s +' turn')
    x = int (input ('enter the number: '))
    x1 = int (input ('enter the places number: '))
    if player == 'a':
        even(x, x1)
    else: odd(x, x1)          

print('Tic Tac Toe')
print ('player A should enter even numbers only'+' and player B should enter odd numbers only')
print ('the player with the ood numbers start')
tic_tac_toe ()
while (true):
    turn(player)
    if winner(): break
    else:
        if player == 'a': player = 'b'
        else: player = 'a'