基本上我有这个游戏,规则如下:
-2名玩家;
-玩家1从5x5网格(矩阵(中选择一个数字,用0代替1,然后玩家2也这样做(他们轮流做(;
我的问题是,当玩家用他们的数字制作2x2网格时(例如(,或者每个数字0都被1或2取代时(这使它成为平局(,我该如何让它停止:
0 0 0 0 0
0 1 1 2 0
0 1 1 0 0
0 0 0 2 0
0 0 0 0 2
玩家1赢得
我的代码:
grid= [[0 for row in range (5)] for col in range (5)]
for i in range (0,10): (THIS IS THE PART THAT I NEED TO REPLACE)
player1_row = int (input ("Player 1, please enter the number of the row (0-4): "))
player1_col = int (input ("Player 1, please enter the number of the column (0-4): "))
grid [player1_row][player1_col]= 1
for s in grid:
print(*s)
player2_row = int (input ("Player 2, please enter the number of the row (0-4): "))
player2_col = int (input ("Player 2, please enter the number of the column (0-4): "))
grid [player2_row][player2_col]= 2
for s in grid:
print(*s)
这是我迄今为止的代码:
def check_for_win(grid):
for x in range(4):
for y in range(4):
rect = (grid[x][y], grid[x+1][y], grid[x][y+1], grid[x+1][y+1])
if 0 not in rect and 1 not in rect:
# player 2 won
return 2
if 0 not in rect and 2 not in rect:
# player 1 won
return 1
return None
def check_finished(grid):
for row in grid:
if 0 in row:
return False
return True
grid= [[0 for row in range (5)] for col in range (5)]
for i in range (0,50):
player1_row = int (input ("Player 1, please enter the number of the row (0-4): "))
player1_col = int (input ("Player 1, please enter the number of the column (0-4): "))
grid [player1_row][player1_col]= 1
for s in grid:
print(*s)
player2_row = int (input ("Player 2, please enter the number of the row (0-4): "))
player2_col = int (input ("Player 2, please enter the number of the column (0-4): "))
grid [player2_row][player2_col]= 2
for s in grid:
print(*s)
check_for_win(grid)
check_finished(grid)
这是输出:(应该停下来说玩家赢了(
Player 1, please enter the number of the row (0-4): 1
Player 1, please enter the number of the column (0-4): 1
1 1 0 0 0
1 1 0 0 0
0 0 2 0 0
0 0 0 0 2
0 0 0 0 2
Player 2, please enter the number of the row (0-4):
我该怎么办?
您可以编写一个函数来检查其中一个玩家是否获胜,如下所示:
def check_for_win(grid):
for x in range(4):
for y in range(4):
rect = (grid[x][y], grid[x+1][y], grid[x][y+1], grid[x+1][y+1])
if 0 not in rect and 1 not in rect:
# player 2 won
return 2
if 0 not in rect and 2 not in rect:
# player 1 won
return 1
return None
如果玩家1获胜,则此函数返回1
,如果玩家2获胜,则返回2
,否则返回None
。
检查游戏是否完成非常容易:
def check_finished(grid):
for row in grid:
if 0 in row:
return False
return True
这能回答你的问题吗?
我的方法是使用numpy数组:
import numpy as np
def check_for_win(grid):
grid = np.array(grid)
for i in range(grid.shape[0] - 1):
for k in range(grid.shape[1] - 1):
check = grid[i, k]
if check != 0:
if (grid[i:i+2, k:k+2] - check == 0).all():
return check
if not 0 in grid:
return -1
return 0
该函数从左上角开始遍历网格,因此不会检测到两个玩家都有获胜模式的错误棋盘,但会检测到第一个获胜者并返回相应的数字
它有以下返回值:
1
、2
:玩家1、2赢得的游戏0
什么也没发生-1
战平
使用普通python的版本,因此网格是列表列表:
def check_for_win(grid):
for i in range(len(grid[0]) - 1):
for k in range(len(grid) - 1):
check = grid[i][k]
if check != 0:
if all([elmnt == check for line in grid[k:k+2] for elmnt in line[i:i+2]]):
return check
if not 0 in grid:
return -1
return 0
测试(使用numpy版本(:
np.random.seed(42)
grid = np.random.randint(0, 3, [5, 5])
array([[2, 0, 2, 2, 0],
[0, 2, 1, 2, 2],
[2, 2, 0, 2, 1],
[0, 1, 1, 1, 1],
[0, 0, 1, 1, 0]])
check_for_win(grid)
Out: 1
grid[3, 2] = grid[1, 0] = 2
array([[2, 0, 2, 2, 0],
[2, 2, 1, 2, 2],
[2, 2, 0, 2, 1],
[0, 1, 2, 1, 1],
[0, 0, 1, 1, 0]])
check_for_win(grid)
Out: 2
np.random.seed(1)
grid = np.random.randint(0, 3, [5, 5])
array([[1, 0, 0, 1, 1],
[0, 0, 1, 0, 1],
[0, 2, 1, 2, 0],
[2, 1, 2, 0, 0],
[2, 0, 1, 2, 2]])
check_for_win(grid)
Out: 0
np.random.seed(8)
grid = np.random.randint(1, 3, [5, 5])
array([[2, 1, 2, 2, 2],
[1, 1, 2, 1, 2],
[1, 2, 2, 1, 2],
[2, 1, 2, 1, 2],
[2, 2, 2, 2, 1]])
check_for_win(grid)
Out: -1