我一直试图创建一个算法来解决5x5数字游戏(点击规则和评分)。然而,我一直被困在,因为我不知道如何找到最佳位置,以确定数字的去向。到目前为止,我计算的分数,但不能取得进一步的进展。到目前为止,我想出的糟糕代码是:
import numpy as np
import random
# i could use an array to represent board but im more familiar with nested lists
# when i get the vertical score i do have to represent it as a array to do that math
board = [[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
# gets the score horizontally
def getHorizontalScore(board):
score = 0
# i know it is inefficient but this is what i came up with inorder to figure this out
for list in board:
i_zero = list.count(0)
i_one = list.count(1)
i_two = list.count(2)
i_three = list.count(3)
i_four = list.count(4)
i_five = list.count(5)
i_six = list.count(6)
i_seven = list.count(7)
i_eight = list.count(8)
i_nine = list.count(9)
i_ten = list.count(10)
if i_zero > 1:
score += 0 * i_zero
if i_one > 1:
score += i_one
if i_two > 1:
score += i_two * 2
if i_three > 1:
score += i_three * 3
if i_four > 1:
score += i_four * 4
if i_five > 1:
score += i_five * 5
if i_six > 1:
score += i_six * 6
if i_seven > 1:
score += i_seven * 7
if i_eight > 1:
score += i_eight * 8
if i_nine > 1:
score += i_nine * 9
if i_ten > 1:
score += i_ten * 10
return score
# gets the score verticaly (hopefully)
def getVerticalScore(board):
# represent board as an array
board = np.asarray(board)
#score
score = 0
# i know it is inefficient but this is what i came up with inorder to figure this out
# board.T is so useful praise numpy for adding this feature
for column in board.T:
i_one = list(column).count(1)
i_two = list(column).count(2)
i_three = list(column).count(3)
i_four = list(column).count(4)
i_five = list(column).count(5)
i_six = list(column).count(6)
i_seven = list(column).count(7)
i_eight = list(column).count(8)
i_nine = list(column).count(9)
i_ten = list(column).count(10)
if i_one > 1:
score += i_one
if i_two > 1:
score += i_two * 2
if i_three > 1:
score += i_three * 3
if i_four > 1:
score += i_four * 4
if i_five > 1:
score += i_five * 5
if i_six > 1:
score += i_six * 6
if i_seven > 1:
score += i_seven * 7
if i_eight > 1:
score += i_eight * 8
if i_nine > 1:
score += i_nine * 9
if i_ten > 1:
score += i_ten * 10
return score
def getScore(board):
return getVerticalScore(board) + getHorizontalScore(board)
def displayBoard(board):
for list in board:
print(list)如果有人能帮我弄清楚如何使算法和压缩我的代码一点点,请帮助我。
发布于 2022-10-06 11:51:37
您对行和列的分数计算已关闭。
让我们以这个板为例:
board = [[7, 7, 2, 3, 7], # 14
[6, 6, 2, 3, 3], # 18
[2, 4, 2, 8, 8], # 16
[1, 10, 9, 5, 8], # 0
[10, 10, 10, 5, 8]] # 30
# 0 20 6 16 24
# total score = 144如果我使用您的getScore()函数,我将得到89而不是144
用于获取倍数的代码不考虑数字是否相邻:
for list in board:
# ...
i_seven = list.count(7) # this will return 3 although only 2 7's are adjacent在这种情况下,最好是使用索引遍历您的董事会。看看这个关于访问嵌套列表中的元素的答案。
# Getting each element per row
for row in board:
for pos in range(5):
row_num = row[pos]
# Getting each element per column
for col in range(5):
for pos in range(5):
col_num = board[pos][col]你不需要数0,因为它们表示你的棋盘上有一个空字段。所以你的算法知道在一个字段上放一个新的数字是合法的。
https://stackoverflow.com/questions/73967852
复制相似问题