这是我的计算机科学最终项目的tictactoe计划。本周我自学了python这个程序,所以请原谅我的代码的可怕性。您可以添加电路板的任何边长,它将正确创建并允许您在该电路板上播放。我的问题是howtowin:
howtowin = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6))
硬编码为3的一侧,这是一块普通的电路板。我需要一个方法来获取边长(num)并创建一个win组合数组。
import math
def tic_tac_toe():
board1 = []
end = False
howtowin = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6))
def choose():
global num
num = input("How large do you want the grid? (input the SIDE length)")
for a in range(num**2):
board1.append(a)
def draw():
x = num
v = 0
for a in range(x):
print
for b in range(x):
print(board1[v]),
v = v + 1
def p1():
n = choose_number()
if board1[n] == 'X' or board1[n] == 'O':
print("\nAre you stupid... Try again")
p1()
else:
board1[n] = 'X'
def p2():
n = choose_number()
if board1[n] == 'X' or board1[n] == 'O':
print("\nAre you stupid... Try again")
p2()
else:
board1[n] = 'O'
def choose_number():
while True:
try:
a = int(input())
if a in board1:
return a
else:
print("\nCan you not count to " + str(num**2) + "? Try again")
except ValueError:
print("\nI don't speak French! Try again")
def check_board():
count = 0
for a in howtowin:
if board1[a[0]] == board1[a[1]] == board1[a[2]] == 'X':
print("\nPlayer 1 Wins!")
print("Congratulations!\n")
return True
if board1[a[0]] == board1[a[1]] == board1[a[2]] == 'O':
print("\nPlayer 2 Wins!")
print("Congratulations!\n")
return True
for a in range(9):
if board1[a] == 'X' or board1[a] == 'O':
count += 1
if count == 9:
print("The game ends in a Tie\n")
return True
choose()
while not end:
draw()
end = check_board()
if end == True:
break
print("\n\nPlayer 1 choose where to place a 'X'")
p1()
print
draw()
end = check_board()
if end == True:
break
print("\n\nPlayer 2 choose where to place a 'O'")
p2()
print
if raw_input("Play again (y/n)\n") == 'y':
print
tic_tac_toe()
else:
print("\nThanks for playing!!!!!!")
tic_tac_toe()
答案 0 :(得分:0)
假设你的网格看起来大小为5:
0 | 1 | 2 | 3 | 4
----+----+----+----+-----
5 | 6 | 7 | 8 | 9
----+----+----+----+-----
10 | 11 | 12 | 13 | 14
----+----+----+----+-----
15 | 16 | 17 | 18 | 19
----+----+----+----+-----
20 | 21 | 22 | 23 | 24
您可以通过以下几个步骤完成此操作:
>>> size = 5
>>> rows = [tuple(range(i * size, (i + 1) * size)) for i in range(size)]
>>> rows
[(0, 1, 2, 3, 4), (5, 6, 7, 8, 9), (10, 11, 12, 13, 14), (15, 16, 17, 18, 19), (20, 21, 22, 23, 24)]
>>> cols = [tuple(range(i, size ** 2 * 1, size)) for i in range(size)]
>>> cols
[(0, 5, 10, 15, 20), (1, 6, 11, 16, 21), (2, 7, 12, 17, 22), (3, 8, 13, 18, 23), (4, 9, 14, 19, 24)]
>>> diags = [tuple(i * (size + 1) for i in range(size)),
tuple(i * (size - 1) for i in range(1, size + 1))]
>>> diags
[(0, 6, 12, 18, 24), (4, 8, 12, 16, 20)]
然后你可以聚合行,列和对角线:
>>> howtowin = tuple(rows + cols + diags)
>>> howtowin
((0, 1, 2, 3, 4), (5, 6, 7, 8, 9), (10, 11, 12, 13, 14), (15, 16, 17, 18, 19), (20, 21, 22, 23, 24), (0, 5, 10, 15, 20), (1, 6, 11, 16, 21), (2, 7, 12, 17, 22), (3, 8, 13, 18, 23), (4, 9, 14, 19, 24), (0, 6, 12, 18, 24), (4, 8, 12, 16, 20))
这适用于任何正尺寸。
请注意:
howtowin
变量实际上是一个包含多个3项元组的元组。tuple(...)
来获取您已使用的格式,但由于您也可以使用列表,因此没有必要。以下代码可以完全按照您使用上一个代码的方式使用。唯一的区别是它使用列表而不是元组:
>>> size = 5
>>> rows = [list(range(i * size, (i + 1) * size)) for i in range(size)]
>>> rows
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]
>>> cols = [list(range(i, size ** 2 * 1, size)) for i in range(size)]
>>> cols
[[0, 5, 10, 15, 20], [1, 6, 11, 16, 21], [2, 7, 12, 17, 22], [3, 8, 13, 18, 23], [4, 9, 14, 19, 24]]
>>> diags = [list(i * (size + 1) for i in range(size)), list(i * (size - 1) for i in range(1, size + 1))]
>>> diags
[[0, 6, 12, 18, 24], [4, 8, 12, 16, 20]]
>>> howtowin = rows + cols + diags
>>> howtowin
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24], [0, 5, 10, 15, 20], [1, 6, 11, 16, 21], [2, 7, 12, 17, 22], [3, 8, 13, 18, 23], [4, 9, 14, 19, 24], [0, 6, 12, 18, 24], [4, 8, 12, 16, 20]]