到目前为止,对于我的代码,我有:
while True:
"""
determines if there is a list of x's or o's in a horizontal row
"""
game = list(input())
if len(game) == 0:
print("We should now check for vertical or diagonal winners!")
elif game[0] == game[1]:
if game[1] == game[2]:
if game[2] == game[3]:
if game[3] == game[4]:
if game[4] == game[5]:
if game[5] == game[6]:
if game[6] == game[7]:
if game[7] == game[8]:
if game[8] == game[9]:
if game[9] == game[10]:
if game[10] == game[11]:
if game[11] == game[12]:
if game[12] == "o":
print("Player o won")
else:
print("Player x won")
else:
del game[0:12]
else:
del game[0:12]
else:
del game[0:12]
else:
del game[0:12]
else:
del game[0:12]
else:
del game[0:12]
else:
del game[0:12]
else:
del game[0:12]
else:
del game[0:12]
else:
del game[0:12]
else:
del game[0:12]
else:
del game[0:12]
我觉得必须有一个更短的方式来写这个。我还想到了一种方法来确定是否有横向赢家。我不确定如何解决垂直或对角线的赢家。我还在第二行用x获胜测试了这个代码,并且它没有打印出x赢了,所以我想知道我的错误在哪里?
非常感谢任何帮助!感谢。
答案 0 :(得分:2)
你非常需要了解正则我的男人!
这似乎很像一个家庭作业问题,但是因为其他人都在回答......我会给你最快的答案:-)。对于这个问题,正则表达式可能比你在纯python中编写的任何东西都快,因为它使用编译的c代码。
您可以使用正则表达式直接从输入字符串中轻松测试水平或垂直匹配。
import re
# find 13 x's or o's in a row that begin some multiple of 13 characters from the beginning of the input string
horizMatch_regex = re.compile("^(.{13})*(xxxxxxxxxxxxx|ooooooooooooo)")
# find 13 x's or o's that appear with exactly 12 characters in between, which corresponds to columns. Requires lookahead (?=)
verticalMatch_regex = re.compile("(x(.{12})(?=x)){12}|(o(.{12})(?=o)){12}")
# slightly trickier - you need 4 separate match groups to test for each possible diagonal. There are a variety of ways to do that, but here's one
diagonalMatch_regex = re.compile("(^(x.{13}){12}x)|(^(o.{13}){12}o)|((x.{11}){13}.$)|((o.{11}){13}.$)")
if horizMatch_regex.search(input_str):
print("We have a horizontal tic tac toe!")
if verticalMatch_regex.search(input_str):
print("We have a vertical tic tac toe!")
if diagonalMatch_regex.search(input_str):
print("We have a diagonal tic tac toe!")
# string with horizontal, vertical, and diagonal tic tac toe's
input_str = "xooooooooooooxxxxxxxxxxxxxxoxoooooooooxxxxxxxxxxxxxoxoooxoooooooxxxxxxxxxxxxxoxoooooxoooooxxxxxxxxxxxxxoxoooooooxoooxxxxxxxxxxxxxoxoooooooooxoxxxxxxxxxxxxxoxooooooooooox"
We have a horizontal tic tac toe!
We have a vertical tic tac toe!
We have a diagonal tic tac toe!
答案 1 :(得分:2)
使用2D list
和一些循环。
instring = 'oooooooooooooxxxxxxxxxxxxoooooooooooooxxxxxxxxxxxxxoooooooooooooxxxxxxxxxxxxxoooooooooooooxxxxxxxxxxxxxoooooooooooooxxxxxxxxxxxxxoooooooooooooxxxxxxxxxxxxxooooooooooooox'
board = []
for x in range(13):
board.append(instring[x::13])
board = list(zip(*board))
如果某一行有获胜者,请打印获胜者:
>>> for row in range(13):
... if len(set(board[row]))-2: print(board[row][0])
...
o
如果某列有获胜者,请打印获胜者:
>>> for row in range(13):
... if len(set(list(zip(*board))[row]))-2: print(board[row][0])
...
如果\
形对角线有赢家,请打印获胜者:
>>> if len(set(board[i][i] for i in range(13)))==1:
... print(board[0][0])
...
如果/
形对角线有赢家,请打印获胜者:
>>> if len(set(board[i][i] for i in range(-1, -14, -1)))==1:
... print(board[0][12])
...