首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >求解5x5数字游戏的算法

求解5x5数字游戏的算法
EN

Stack Overflow用户
提问于 2022-10-06 01:34:16
回答 1查看 119关注 0票数 0

我一直试图创建一个算法来解决5x5数字游戏(点击规则和评分)。然而,我一直被困在,因为我不知道如何找到最佳位置,以确定数字的去向。到目前为止,我计算的分数,但不能取得进一步的进展。到目前为止,我想出的糟糕代码是:

代码语言:javascript
复制
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)

如果有人能帮我弄清楚如何使算法和压缩我的代码一点点,请帮助我。

EN

回答 1

Stack Overflow用户

发布于 2022-10-06 11:51:37

您对行和列的分数计算已关闭。

让我们以这个板为例:

代码语言:javascript
复制
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

用于获取倍数的代码不考虑数字是否相邻:

代码语言:javascript
复制
for list in board:
#   ...
    i_seven = list.count(7) # this will return 3 although only 2 7's are adjacent

在这种情况下,最好是使用索引遍历您的董事会。看看这个关于访问嵌套列表中的元素的答案。

代码语言:javascript
复制
# 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,因为它们表示你的棋盘上有一个空字段。所以你的算法知道在一个字段上放一个新的数字是合法的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73967852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档