以下是一个简单的Python版本的开心消消乐游戏代码:
import random
def print_board(board):
print(" ", end="")
for i in range(4):
print(" " + str(i + 1), end="")
print()
for i in range(4):
print(str(i + 1) + " |", end="")
for j in range(4):
print(" " + str(board[i][j]), end="")
print()
def check_win(board):
# 检查每一行
for i in range(4):
if board[i][0] == board[i][1] == board[i][2] == board[i][3] and board[i][0] != 0:
return True
# 检查每一列
for j in range(4):
if board[0][j] == board[1][j] == board[2][j] == board[3][j] and board[0][j] != 0:
return True
# 检查左上到右下的对角线
if board[0][0] == board[1][1] == board[2][2] == board[3][3] and board[0][0] != 0:
return True
# 检查右上到左下的对角线
if board[0][3] == board[1][2] == board[2][1] == board[3][0] and board[0][3] != 0:
return True
return False
def play(board):
move = None
while move is None:
# 获取玩家输入
print_board(board)
print("请输入要操作的行和列,例如:1 1")
move = input().split()
# 检查玩家输入是否合法
if len(move) != 2 or not move[0].isdigit() or not move[1].isdigit() or int(move[0]) > 4 or int(move[1]) > 4:
print("输入错误,请重新输入")
move = None
row, col = int(move[0]), int(move[1])
# 检查操作是否合法
if board[row][col] == 0:
print("操作错误,请重新输入")
move = None
else:
# 更新棋盘
board[row][col] = 0
# 检查是否获胜
if check_win(board):
print("恭喜你,你赢了!")
return
# 打乱剩余的棋子
for i in range(4):
for j in range(4):
if board[i][j] == 1:
board[i][j] = random.randint(1, 5)
print_board(board)
# 重新开始游戏
play(board)
if __name__ == "__main__":
board = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
play(board)
这个版本的开心消消乐游戏是一个简单的命令行版本,玩家可以通过输入行和列的编号来进行操作。游戏的规则是玩家需要消除所有的棋子,当棋盘上的棋子全部消除完