写一个开心消消乐的代码
时间: 2023-10-07 22:11:30 浏览: 127
好的,以下是一个简单的开心消消乐的代码:
```
import random
board = [[random.randint(1, 5) for j in range(10)] for i in range(10)] # 生成一个随机的10x10的棋盘
def print_board(board):
for row in board:
print(row)
def check_horizontally(board, row, col, color):
if col < 0 or col > 7:
return False
if board[row][col] != color:
return False
if board[row][col+1] != color:
return False
if board[row][col+2] != color:
return False
if board[row][col+3] != color:
return False
return True
def check_vertically(board, row, col, color):
if row < 0 or row > 7:
return False
if board[row][col] != color:
return False
if board[row+1][col] != color:
return False
if board[row+2][col] != color:
return False
if board[row+3][col] != color:
return False
return True
def check_win(board, row, col, color):
if check_horizontally(board, row, col, color):
return True
if check_vertically(board, row, col, color):
return True
return False
def remove_pieces(board, row, col, color):
if check_horizontally(board, row, col, color):
board[row][col] = 0
board[row][col+1] = 0
board[row][col+2] = 0
board[row][col+3] = 0
if check_vertically(board, row, col, color):
board[row][col] = 0
board[row+1][col] = 0
board[row+2][col] = 0
board[row+3][col] = 0
def update_board(board):
for row in range(len(board)):
for col in range(len(board[row])):
if board[row][col] == 0:
board[row][col] = random.randint(1, 5)
def play_game():
while True:
print_board(board)
row = int(input("Enter row: "))
col = int(input("Enter col: "))
color = board[row][col]
if color == 0:
print("No piece there!")
continue
remove_pieces(board, row, col, color)
if check_win(board, row, col, color):
print("You win!")
break
update_board(board)
play_game()
```
请注意,这只是一个简单的代码示例,可能存在一些 bug。
阅读全文