[英]tic tac toe with numbers in python
我有硬件,這是帶有數字的井字游戲。 顯示一個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
我現在的問題是,我想創建一個函數來檢查玩家每次輸入數字是否有贏家,我希望它知道已輸入的數字與已經存在的數字之間的差(職位),而且我真的是編程新手,如果代碼有很多錯誤,請原諒
嘗試這個:
我添加了一個董事會日志陣列來監視哪些位置包含用戶輸入,然后在獲勝者功能中交叉引用所述陣列以驗證獲勝條件
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'
我認為這接近您想要的。 注意評論。 還知道我還沒有測試過,我只是更正了您的代碼。 有任何問題請隨時詢問我(們。
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'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.