- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个 2 人棋盘游戏(例如 connect 4),棋盘大小为h,w。我想使用 hw 大小的位板检查获胜条件。
在棋盘大小固定的国际象棋游戏中,位盘通常用某种 64 位整数表示。当 h 和 w 不是常量并且可能非常大(假设 30*30)时,位板是个好主意吗?如果是这样,C/C++ 中处理大位板的任何数据类型是否保持其性能?
由于我目前正在研究 python,所以也很感谢使用这种语言的解决方案! :)
提前致谢
最佳答案
我刚才写这段代码只是为了玩弄游戏概念。不涉及智能行为。只是随机移动来演示游戏。我想这对您来说并不重要,因为您只是想快速检查获胜条件。这个实现速度很快,因为我尽力避免 for 循环并仅使用内置的 python/numpy 函数(使用一些技巧)。
import numpy as np
row_size = 6
col_size = 7
symbols = {1:'A', -1:'B', 0:' '}
def was_winning_move(S, P, current_row_idx,current_col_idx):
#****** Column Win ******
current_col = S[:,current_col_idx]
P_idx= np.where(current_col== P)[0]
#if the difference between indexes are one, that means they are consecutive.
#we need at least 4 consecutive index. So 3 Ture value
is_idx_consecutive = sum(np.diff(P_idx)==1)>=3
if is_idx_consecutive:
return True
#****** Column Win ******
current_row = S[current_row_idx,:]
P_idx= np.where(current_row== P)[0]
is_idx_consecutive = sum(np.diff(P_idx)==1)>=3
if is_idx_consecutive:
return True
#****** Diag Win ******
offeset_from_diag = current_col_idx - current_row_idx
current_diag = S.diagonal(offeset_from_diag)
P_idx= np.where(current_diag== P)[0]
is_idx_consecutive = sum(np.diff(P_idx)==1)>=3
if is_idx_consecutive:
return True
#****** off-Diag Win ******
#here 1) reverse rows, 2)find new index, 3)find offest and proceed as diag
reversed_rows = S[::-1,:] #1
new_row_idx = row_size - 1 - current_row_idx #2
offeset_from_diag = current_col_idx - new_row_idx #3
current_off_diag = reversed_rows.diagonal(offeset_from_diag)
P_idx= np.where(current_off_diag== P)[0]
is_idx_consecutive = sum(np.diff(P_idx)==1)>=3
if is_idx_consecutive:
return True
return False
def move_at_random(S,P):
selected_col_idx = np.random.permutation(range(col_size))[0]
#print selected_col_idx
#we should fill in matrix from bottom to top. So find the last filled row in col and fill the upper row
last_filled_row = np.where(S[:,selected_col_idx] != 0)[0]
#it is possible that there is no filled array. like the begining of the game
#in this case we start with last row e.g row : -1
if last_filled_row.size != 0:
current_row_idx = last_filled_row[0] - 1
else:
current_row_idx = -1
#print 'col[{0}], row[{1}]'.format(selected_col,current_row)
S[current_row_idx, selected_col_idx] = P
return (S,current_row_idx,selected_col_idx)
def move_still_possible(S):
return not (S[S==0].size == 0)
def print_game_state(S):
B = np.copy(S).astype(object)
for n in [-1, 0, 1]:
B[B==n] = symbols[n]
print B
def play_game():
#initiate game state
game_state = np.zeros((6,7),dtype=int)
player = 1
mvcntr = 1
no_winner_yet = True
while no_winner_yet and move_still_possible(game_state):
#get player symbol
name = symbols[player]
game_state, current_row, current_col = move_at_random(game_state, player)
#print '******',player,(current_row, current_col)
#print current game state
print_game_state(game_state)
#check if the move was a winning move
if was_winning_move(game_state,player,current_row, current_col):
print 'player %s wins after %d moves' % (name, mvcntr)
no_winner_yet = False
# switch player and increase move counter
player *= -1
mvcntr += 1
if no_winner_yet:
print 'game ended in a draw'
player = 0
return game_state,player,mvcntr
if __name__ == '__main__':
S, P, mvcntr = play_game()
有问题可以私信我
更新:说明:
在每次移动时,查看通过当前单元格的列、行、对角线和次对角线,并找到具有当前符号的连续单元格。避免扫描整个电路板。
在每个方向提取细胞:
专栏:
current_col = S[:,current_col_idx]
行:
current_row = S[current_row_idx,:]
对角线:从中找到所需对角线的偏移量主对角线:
diag_offset = current_col_idx - current_row_idx
current_diag = S.diagonal(offset)
非对角线:
反转矩阵的行:
S_reversed_rows = S[::-1,:]
找到新矩阵中的行索引
new_row_idx = row_size - 1 - current_row_idx
current_offdiag = S.diagonal(offset)
关于python - 如何处理非常大的位板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30319489/
我们将 Azure Boards(与敏捷流程相关的项目)定义为“功能”>“史诗”>“任务”>“用户故事”。 在我们的Azure Boards(Boards >Board)中,它仅显示Epic和Feat
我正在编写一个 C++ 井字游戏,这是我目前拥有的: #include using namespace std; int main() { board *b; b->draw();
这是一个足够简单的问题。 看完documentation for ion-pane它指出: A simple container that fits content, with no side eff
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 4年前关闭。 Improve this
我正在用 javascript 对 arduino 开发板进行编程。我正在尝试使用 johnny-five 库连接多个 arduino 板。我关注了johnny-five documentation我
在我的 Java 类(class)中,我们正在学习《Java 基础知识》一书的第 4 章。我正在做项目 4-11,它是一个黑色和红色的棋盘格,但是我得到随机颜色,我试图按照本书教我们使用 ColorP
我正在制作一个数独板 GUI,它应该看起来像这样 http://www.sudoku.4thewww.com/Grids/grid.jpg 由于某种原因,它只显示最后一个 3*3 板。如果有人能告诉我
我正在开发一款带有二维阵列(游戏板)的新游戏。每个单元格/图 block 都有一定数量的点。 我想实现的是一个算法能找到核心最高的最短路径。 所以我首先实现了 Dijkstra 算法(下面的源代码)来
更新:(2015-10-16)[已解决!]-使用trigger()并通过slice()限制为50个引脚固定。 非常感谢Abhas Tandon通过提供使用 $(this).trigger('click
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 7年前关闭。 Improve this questi
var size = 8; var board = ""; for (var y = 0; y x= (x+y) % 2 = 关于javasc
我正在制作一个简单的游戏,需要我创建一个由用户定义大小的棋盘。 我一直在编写一个函数,该函数应该返回我将在我的游戏中使用的棋盘(矩阵),但我似乎无法让它工作。 我尝试使用嵌套的 for 循环方法在 m
我正在尝试让板模板引擎与 express.js 一起工作。我最初的尝试是这样的: app.register('.html', { compile: function (str, options
我正在测试 Azure Boards Rest API。我目前可以成功创建、删除和获取项目,但我似乎无法在列之间移动它们。 这是我的要求https://{{AzureBoardsToken}}@{{A
我想用 trello api 归档一个板/列表,但我找不到解决方案。 与 https://trello.com/docs/api/list/#post-1-lists-idlist-archiveal
我上传了 sketch到一个 Arduino Uno,它的循环是这样的: void loop(){ Serial.println("Hello, World!"); } 所以,现在,我无法再上
我想要进行一个查询,显示结构 Epic -> 功能 -> 发布 -> 用户故事 -> 任务,以及特定迭代路径下的所有待处理任务 我尝试使用工作项树,但它只显示到 mu 用户故事 我的 Azure De
我在 python 中使用来自 Opencv 的 Charuco 标记。我之前使用的是 Aruco 开发板,我可以选择创建一个带有 id 偏移量(例如:偏移量为 40)的开发板。 from cv2 i
我不知道如何将另一个 View 中的辅助 Anchorpane 设置到主 View 的边界(在 fxml 代码中,它将是名为 holderPane 并且有灰色区域),这样当窗口展开时,它也会随之拉伸(
如何使用包含列、行和堆栈(包含 4、3、2、1)的 3D 通用数组制作一 block 板。 这是我声明的: private int row, col, stack; int[][][] array3D
我是一名优秀的程序员,十分优秀!