- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我有一个非常基本的游戏板:
B W B W with assigned coordinates of 1 2 3 4
W B W B 5 6 7 8
B W B W 9 10 11 12
这个游戏实际上不是跳棋,但很相似。在游戏开始前,用户各自选择一个棋子(B 或 W)来移除。所以我们将从这样的事情开始:
- W B W
W B W B
B - B W
接下来,用户将能够“跳过”其他用户的棋子。跳跃只能水平或垂直,不能大声对角跳跃。因此,我首先捕获用户选择的棋子的坐标,以及他们跳跃后着陆的目的地坐标:
Bjump = input(BLACK, first select the coordinate of the piece you want to move as well as the coordinate where you would like to land. Please Separate Coordinates with a SINGLE space: ")
用户需要能够选择B @坐标'3',跳过W @ 2,并降落@坐标1。这个过程不仅应该将B block 跳过W block ,还应该删除W block 它跳到了@坐标2。用“-”替换它
给出输出:
B - - W
W B W B
B - B W
我的第一个想法是做这样的事情:
if(Bjump == "3 1"):
if(grid[0][1] == 'W'):
if(grid[0][2] == 'B'):
grid[0][2] = '-'
grid[0][1] = '-'
grid[0][1] = 'B'
else:
print("oops! try again!")
else:
print("oops! You can only jump over W's!")
我的问题是,我不仅必须为每个可能的场景创建一个 if(语句),而且谁知道每个游戏的每一步棋盘会是什么样子?
有人可以帮助我思考如何去做,以及如何使其“通用”,无论游戏有多少种不同的玩法吗?提前致谢!!
最佳答案
被跳过的棋子始终是源和目标 x,y 坐标的平均值。考虑到这一点,您可以编写如下内容:
board = [
['-', 'W', 'B', 'W'],
['W', 'B', 'W', 'B'],
['B', '-', 'B', 'W']
]
def print_board(board):
for row in board:
print(row)
def coords(board, num):
num = int(num) - 1
return num % len(board), num // len(board)
def jump(board, move):
(src_x, src_y), (dst_x, dst_y) = (coords(board, x) for x in move.split())
x_diff = abs(src_x - dst_x)
y_diff = abs(src_y - dst_y)
if sorted([x_diff, y_diff]) != [0, 2]:
print('Oops, invalid coordinates')
return
mid_x = (src_x + dst_x) // 2
mid_y = (src_y + dst_y) // 2
if board[src_y][src_x] == '-':
print('Oops, source cell empty')
if board[dst_y][dst_x] != '-':
print('Oops, target cell occupied')
if board[mid_y][mid_x] == '-':
print('Oops, no piece to jump over')
if board[src_y][src_x] == board[mid_y][mid_x]:
print('Oops, can\'t jump over piece with same color')
board[dst_y][dst_x] = board[src_y][src_x]
board[mid_y][mid_x] = '-'
board[src_y][src_x] = '-'
move = '3 1'
print_board(board)
print('Execute move {}'.format(move))
jump(board, move)
print_board(board)
输出:
['-', 'W', 'B', 'W']
['W', 'B', 'W', 'B']
['B', '-', 'B', 'W']
Execute move 3 1
['B', '-', '-', 'W']
['W', 'B', 'W', 'B']
['B', '-', 'B', 'W']
请注意,以上仅适用于 Python 3。
关于python - (Python) 2D Array Checkers - 如何跳跃并吃掉对手的棋子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40414908/
我正在尝试Checker Framework使用 Java 8 的 Nullness 检查器。当我在以下代码上运行检查器时: import java.util.HashMap; import java
从 IntellijIDEA 运行 Spring 应用程序时出现以下错误。 2017-04-20 18:20:50.645 ERROR 28680 --- [main] o.s.b.d.Logging
我正在使用为我的 HTML 文件启用的 Syntastic。由于我有一个非常大的文件,并且启用了“validator w3”检查器,因此在保存文件时 GVIM 或 VIM 变得非常慢 (:w)。 是否
正如您在标题中看到的,我正在尝试编写一个程序,该程序可以为我解决“晚间标准管测验”。它的一部分。它必须能够在地铁站名称中找到字谜。我在网上找到了很多代码来检查两个单词是否是字谜。但我需要一些不同的东西
以下是如何使用 C++ Core Checker 的说明:C++ Core Guidelines Checkers available for VS 2015 Update 1. 我成功安装了 NuG
我有一张 table : CREATE TABLE MENUPOINT ( id BIGINT NOT NULL, parent BIGINT, name VARCHAR(64), C
我以前用过它,当有人问及从样式表中删除未使用的 CSS 的工具时,我想起了它。 我回到链接,但是它不见了,这是怎么回事?它永远消失了还是有人知道备份?或者仍然可以根据 HTML 检查您的 CSS 并去
我在我的 React 应用程序的控制台中收到这条奇怪的警告消息。 Warning: Failed propType: checker is not a function Check the rende
如标题所述,我使用 bundle 搜索插件查找依赖项漏洞,但在我的项目中找不到任何漏洞。 我用的是IntelliJ IDEA 2022.1.3(旗舰版),我检查了一下,比如把spring-boot-s
为什么在下面的代码中,使用数组切片有效,但使用 Vec 的切片却无效? use rand::{rngs::adapter::ReadRng, RngCore}; use std::io::Read;
看起来 checkZip 函数是在用户的 zip 绑定(bind)到参数之前触发的。有没有更好的方法使用 Angular 工具来完成此任务?我想在用户输入正确的邮政编码后立即启动用户流程的下一步。 H
我指的是nullness-example in the docs (检查框架版本:2.1.14) 当我按照建议运行示例来检查NullnessExampleWithWarnings.java时: jav
我安装了 Eclipse 4.6.2 和插件“Checker Framework Feature”2.1.7。 首次使用该插件时(例如,当我右键单击并将鼠标悬停在上下文菜单项“Checker Fram
我正在尝试检查两个单词是否是变位词,并尝试使用 LinkedList 来做到这一点。为此,首先,我创建了一个名为 LinkedList 的类: class LinkedList { priv
我已经将 Checker Framework v2.1.6 引入到一个 java8 项目中,并修复了所有的 nullness 错误,但我仍然遇到以下错误 Main.java:52: error: [a
abi-compliance-checker 是一个 perl 程序,可以检查 linux 上库的 ABI ( https://lvc.github.io/abi-compliance-checker
我目前正在使用 Alexander Potochkin 的 AspectJ EDTChecker code (帖子底部的相关代码)。 此代码(基于我对 AspectJ 的一点了解)提示在 Swing
我正在开发一个基于多模块 Maven 的项目,其中一个模块包含一些注释处理器,用于其他模块使用的自定义注释。当我将注释处理器模块的依赖项添加到任何其他模块时,该模块的注释将由这些注释处理器处理。 但最
我想使用我自定义的列表来实现列表的常规应用实例: import Control.Monad import Test.QuickCheck import Test.QuickCheck.Checkers
考虑这个 github 存储库。 https://github.com/dfabulich/bazel-checker-framework-bug 它包括一个样本 X.java公然违反 @Nonnul
我是一名优秀的程序员,十分优秀!