python中数字的tic-tac-toe
我有一个H.W,就是这个 用数字抽签。显示一个3 x 3的棋盘,玩家1取奇数1, 3,5,7,9,玩家2取偶数0,2,4,6,8。玩家们轮流写他们的游戏 数字。奇数开始。每个数字只使用一次。第一个完成一行的人 加起来是15个,就是赢家。该行可以有奇数和偶数 . 我一直到这里python中数字的tic-tac-toe,python,Python,我有一个H.W,就是这个 用数字抽签。显示一个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 = [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, 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'
试试这个:
我添加了一个board log数组来监视哪些位置包含用户输入,然后在winner函数中交叉引用所述数组以验证win标准
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'
我会给你一个提示,告诉你一个数字是否已经在黑板上,你可以使用
in
关键字。例如,如果电路板上的数字:打印“数字[{}]已经被使用了。”。格式化(数字)
要回答您的问题,在交换的功能转换后(我想是转换)
,您应该放置赢家()
函数。现在,你应该有一个变量,比如player='a'
,在回合结束并检查是否有赢家后,将其交换给另一个玩家。另外,在odd()
和偶数()
中,即使输入的数字无效,你也会更改棋盘,因为你写了一个额外的移动(x,x2)
函数在if(x%2…
条件之前执行。由于在turn(s)
中输入的变量未返回,因此它们在奇数()和偶数()中未知
函数感谢这确实帮了我很大的忙,但我仍然发现winner函数存在问题,因为它不仅计算玩家输入的数字,而且还计算玩家输入的数字。这就是你声明初始矩阵的方式,我想你希望它从值开始[0,1,2,3,…]