首先这是我的代码https://github.com/knightKevin2004/Python-2048.git
import random
def game_start(game_list):
x_1, y_1 = random.randint(0, 3), random.randint(0, 3)
x_2, y_2 = random.randint(0, 3), random.randint(0, 3)
if x_1 == x_2 and y_1 == y_2:
return game_start(game_list)
else:
game_list[x_1][y_1] = 2
game_list[x_2][y_2] = 2 ** random.randint(1, 2)
for i in game_list:print(i)
return game_list
def push_right(game_list):
for i in range(3, -1, -1):
for j in range(2, -1, -1):
if game_list[i][j] > game_list[i][j + 1] and game_list[i][j + 1] == 0:
game_list[i][j + 1] = game_list[i][j]
game_list[i][j] = 0
return push_right(game_list)
elif game_list[i][j] == game_list[i][j + 1] and game_list[i][j] != 0:
game_list[i][j + 1] *= 2
game_list[i][j] = 0
return push_right(game_list)
else:
for i in game_list: print(i)
return game_list
def push_left(game_list):
for i in range(4):
for j in range(3):
if game_list[i][j] < game_list[i][j + 1] and game_list[i][j] == 0:
game_list[i][j] = game_list[i][j + 1]
game_list[i][j + 1] = 0
return push_left(game_list)
elif game_list[i][j] == game_list[i][j + 1] and game_list[i][j] != 0:
game_list[i][j] *= 2
game_list[i][j + 1] = 0
return push_left(game_list)
else:
for i in game_list: print(i)
return game_list
def push_up(game_list):
for i in range(3):
for j in range(4):
if game_list[i][j] < game_list[i+1][j] and game_list[i][j] == 0:
game_list[i][j] = game_list[i+1][j]
game_list[i+1][j] = 0
return push_up(game_list)
elif game_list[i][j] == game_list[i+1][j] and game_list[i][j] != 0:
game_list[i][j] *= 2
game_list[i+1][j] = 0
return push_up(game_list)
else:
for i in game_list: print(i)
return game_list
def push_down(game_list):
for i in range(2,-1,-1):
for j in range(3,-1,-1):
if game_list[i][j] > game_list[i+1][j] and game_list[i+1][j] == 0:
game_list[i+1][j] = game_list[i][j]
game_list[i][j] = 0
return push_down(game_list)
elif game_list[i][j] == game_list[i+1][j] and game_list[i+1][j] != 0:
game_list[i+1][j] *= 2
game_list[i][j] = 0
return push_down(game_list)
else:
for i in game_list: print(i)
return game_list
def random_number(game_list):
r_x=random.randint(0, 3)
r_y=random.randint(0, 3)
if game_list[r_x][r_y]==0:
game_list[r_x][r_y]=2
return game_list
else:
return random_number(game_list)
def lost(game_list):
count = len([item for row in game_list for item in row if item == 0])
for i in range(0,3):
for j in range(0,3):
if count==0 and game_list[i][j]!=game_list[i][j+1] and game_list[i][j]!=game_list[i+1][j]:
print("You LOST!!!" * 3)
quit()
else:pass
def win(game_list):
for i in game_list:
for win in i:
if win==2048:
print("You Win!!!"*3)
else:pass
def choose(game_list):
old_game_list = game_list.copy()
h = input("选择:")
if h == "w":
new_game_list=push_up(game_list)
print(old_game_list,new_game_list)
if old_game_list==new_game_list:
return choose(old_game_list)
elif old_game_list!=new_game_list and len([item for row in game_list for item in row if item == 0]) > 0:
return random_number(game_list)
elif h == "s":
new_game_list = push_down(game_list)
print(old_game_list, new_game_list)
if old_game_list == new_game_list:
return choose(old_game_list)
elif old_game_list != new_game_list and len([item for row in game_list for item in row if item == 0]) > 0:
return random_number(game_list)
elif h == "a":
new_game_list = push_left(game_list)
print(old_game_list, new_game_list)
if old_game_list == new_game_list:
return choose(old_game_list)
elif old_game_list != new_game_list and len([item for row in game_list for item in row if item == 0]) > 0:
return random_number(game_list)
elif h == "d":
new_game_list = push_right(game_list)
print(old_game_list, new_game_list)
if old_game_list == new_game_list:
return choose(old_game_list)
elif old_game_list != new_game_list and len([item for row in game_list for item in row if item == 0]) > 0:
return random_number(game_list)
elif h == "q":
quit()
else:
print("What are you doing now?!!")
print("-------------------------------------")
def main():
game_list = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
game_start(game_list)
while True:
win(game_list)
lost(game_list)
choose(game_list)
choose([[2, 3, 4, 5],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
一看就知道,一个字,shi
但是我还是继续写,因为自己菜.
代码写了两天,然后就放那了,最近才动.
我写这篇文章的目的九四想要记录下我学习的观点,想法
今天是2023-12-28 0:24:18
我发现我这代码先不看为什么随机数生成不了.就先看2048移动那部分的代码,我个人觉得两个for后面跟range太傻了.我现在准备代码删光重写.很明显,在这边需要len这个方式更好.但其实这问题不大,因为本来在我的认知里2048就是4*4的格子.
重点 迭代器
迭代器,在我push的过程中,一个数字2+2已经变成4了,但是他还是会被再下面的4相加变成8.很明显2048绝对不允许这种两次相加的出现的.我问了下ai,他说
def move_right_zh(board):
"""
将棋盘上的所有瓷砖向右移动一位,如果遇到相同数值的瓷砖则合并。
"""
new_board = [row[:] for row in board] # 复制棋盘,以免修改原始棋盘
for i in range(len(board)):
# 将每行瓷砖向右移动一位,如果遇到相同数值的瓷砖则合并
merge = False
for j in range(len(board[i]) - 1, 0, -1):
if new_board[i][j] != 0: # 如果瓷砖非空
if new_board[i][j] == new_board[i][j-1] and not merge: # 如果当前瓷砖与左侧瓷砖数值相同且尚未合并
temp = new_board[i][j] # 保存即将合并的瓷砖的值
new_board[i][j] = 0 # 清除即将合并的瓷砖
new_board[i][j-1] = temp * 2 # 合并瓷砖并设置新值
merge = True # 标记已发生合并
else:
new_board[i][j-1] = new_board[i][j] # 将瓷砖向右移动
new_board[i][j] = 0 # 清除刚刚移动的位置
else:
merge = False # 重置合并标记,准备下一次迭代
return new_board
但我还是不懂,我更不懂什么是迭代器.