List of the same 13 characters in a Python string
So far, for my code, I had:
while True:
"""
determines if there is a list of x or o 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]
I feel like there should be a shorter way to write this. I also thought about how to determine if there is a horizontal winner. I'm not sure how to go about a solution for vertical or diagonal winners. I also tested this code with x winning on the second line and it didn't print out x, so I wonder where is my mistake?
Any help is greatly appreciated! Thank.
source to share
You really need to know about my man regex!
This seems rather suspicious as a homework question, but since others answer anyway ... I'll give you the fastest answer :-). Regex will probably be faster than anything you write in pure python for this problem because it uses compiled c code.
You can easily test horizontal or vertical matches directly from the input string using a regular expression.
import re
# find 13 x or o 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 or o 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 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!
source to share
Use 2D list
and multiple loops.
instring = 'oooooooooooooxxxxxxxxxxxxoooooooooooooxxxxxxxxxxxxxoooooooooooooxxxxxxxxxxxxxoooooooooooooxxxxxxxxxxxxxoooooooooooooxxxxxxxxxxxxxoooooooooooooxxxxxxxxxxxxxooooooooooooox' board = [] for x in range(13): board.append(instring[x::13]) board = list(zip(*board))
If the line has a winner, print out the winner:
>>> for row in range(13):
... if len(set(board[row]))-2: print(board[row][0])
...
o
If the column has a winner, print out the winner:
>>> for row in range(13):
... if len(set(list(zip(*board))[row]))-2: print(board[row][0])
...
If the diagonal with the diagonal \
has a winner, print the winner:
>>> if len(set(board[i][i] for i in range(13)))==1:
... print(board[0][0])
...
If the found diagonal /
has a winner, print out the winner:
>>> if len(set(board[i][i] for i in range(-1, -14, -1)))==1:
... print(board[0][12])
...
source to share