代码展示:
def set_chess(x,y,color):
if board[x][y] != ' ':
print('该位置已有棋子')
return False
else:
board[x][y] = color
for i in board:
print(i)
return True
def check_win(board):
for list_str in board:
if ''.join(list_str).find('O'*5) != -1:
print('白棋获胜')
return True
elif ''.join(list_str).find('X'*5) != -1:
print('黑棋获胜')
return True
else:
return False
def check_win_all(board):
board_c = [[] for line in range(29)]
for x in range(15):
for y in range(15):
board_c[x+y].append(board[x][y])
board_d = [[] for line in range(29)]
for x in range(15):
for y in range(15):
board_d[x-y].append(board[x][y])
return check_win(board) or check_win([list(l) for l in zip(*board)]) or check_win(board_c) or check_win(board_d)
if __name__ == '__main__':
board = [[' ']*15 for line in range(15)]
for i in board:
print(i)
end = False
for i in range(300):
if i%2 == 0:
print('白棋下棋')
while True:
x = int(input('请输入棋子横坐标0-14:'))
y = int(input('请输入棋子纵坐标0-14:'))
if set_chess(x,y,'O'):
if check_win_all(board):
end = True
break
else:
break
else:
continue
if end:
break
else:
print('黑棋下棋')
while True:
x = int(input('请输入棋子横坐标0-14:'))
y = int(input('请输入棋子纵坐标0-14:'))
if set_chess(x,y,'X'):
if check_win_all(board):
end = True
break
else:
break
else:
continue
if end:
break
成品展示:
如果重复,将会提示重新落子: