- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为了学习 C#、XAML,尤其是 MVVM,我开始编写扫雷游戏。我制作的第一个版本没有 MVVM 部分,我在其中使用 C# 代码创建并添加了按钮,而不是通过 MVVM 方式在 XAML 中进行操作。现在我尝试将 MVVM 模式应用到游戏中。
我制作了一个自己的用户控件,其中包含一个代表雷区部分的按钮。这个控件还有一个 ViewModel 和一个 Model 类来存储一些状态数据和处理一些命令。在一个测试项目中,我创建了 4 个自己的用户控件,并尝试将它们放在一个网格中,其中按钮形成一个 2 x 2 按钮的正方形。在后面的代码中,按钮被放在一个 ObservableCollection 对象中。我假设在此对象中,按钮的列出和索引如下:
Button1
Button2
Button3
Button4
但在演示网格中,我希望按钮显示为
Button1 | Button2
--------+--------
Button3 | Button4
问题是:我如何动态地做到这一点?因为在我的测试项目中,我测试了 4 个按钮,但在我想使用它的项目中,按钮的数量可能会有所不同,具体取决于玩家选择的游戏难度。
第二个问题是我如何弄清楚按钮的邻居是什么。因此,如果网格是 4 x 5,包含 20 个按钮。例如,我选择按钮 8,它的相邻按钮编号为 2、3、4、7、9、12、13 和 14。列出这些按钮后,我如何才能到达这些相邻按钮?
我希望我的问题足够清楚。
提前致谢!
最佳答案
您可以使用 ItemsControl 显示您的收藏有它的 ItemsPanel
设置为 Grid
或 UniformGrid
.我有一些ItemsControl
例子 here这可能对您有所帮助,因为我发现 MDSN 的示例不是很有帮助。
A UniformGrid
如果你能绑定(bind)你的 Rows
就最简单了和 Columns
属性到您的 ViewModel
中的属性,但是这需要所有单元格的大小相同,我不记得属性 Rows
是不是和 Columns
是是否参与绑定(bind)系统的 DependencyProperties。
<ItemsControl ItemsSource="{Binding MyCollection}">
<!-- ItemsPanelTemplate -->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="{Binding ColumnCount}"
Rows="{Binding RowCount}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
如果这对您不起作用,您可以使用 Grid
作为 ItemsPanel
并设置 Grid.Row
和 Grid.Column
ItemContainerStyle
中的绑定(bind).
这将要求您在 ObservableCollection
中的每个单元格对象上都有属性说出该单元格所在的行/列,但是我怀疑您无论如何都需要这些来确定点击命令中的相邻单元格之类的东西。
此外,没有内置的方法来绑定(bind) Grid
中的行数/列数。 , 所以我倾向于使用一些 custom attached properties这将动态构建网格的 RowDefinitions
和 ColumnDefinitions
基于绑定(bind)值。
因此,如果您使用网格,您的最终结果可能看起来像这样:
<ItemsControl ItemsSource="{Binding Cells}">
<!-- ItemsPanelTemplate -->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<!-- This is assuming you use the attached properties linked above -->
<Grid local:GridHelpers.RowCount="{Binding Height}"
local:GridHelpers.ColumnCount="{Binding Width}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!-- ItemContainerStyle -->
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Column"
Value="{Binding ColumnIndex}" />
<Setter Property="Grid.Row"
Value="{Binding RowIndex}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
模型/ View 模型看起来像这样
public class GameViewModel
{
// These should be full properties w/ property change notification
// but leaving that out for simplicity right now
public int Height;
public int Width;
public ObservableCollection<CellModel> Cells;
}
public class CellModel
{
// Same thing, full properties w/ property change notification
public int ColumnIndex;
public int RowIndex;
public bool IsVisible;
public bool IsMine;
public int NumberAdjacentMines;
}
关于c# - 为扫雷游戏动态创建一个游戏板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13587311/
1)扫雷 简介 扫雷 是一种经典的单人电脑游戏,最初由微软公司在 1990 年代开发并内置在 Windows 操作系统中。游戏的目标是在一个由方块组成的网格上揭开所有非地雷的方块,而不触发地雷。每个
我正在制作 MineSweeper 的 Angular 版本,但我遇到了事件问题。我的意思是,我有这样的字段组件: import {Component, Input, OnInit} from '@a
我正在尝试制作一个简单的扫雷器,在 n*n 板上埋下 n*n/3 个地雷。地雷用*标记,空格用0标记。(它还不能作为游戏运行:我正在尝试制作扫雷的“答卷”)请注意,我还没有使用过任何有目的的方法。 我
我必须制作一个扫雷 GUI,但我不知道如何使每个按钮都有自己的 mouseAdapter。当我单击某个按钮时,它会更改屏幕上的每个按钮,而不仅仅是我单击的按钮。我可以提供一些关于如何执行此操作的指示吗
我想制作一款扫雷游戏。首先,这适用于按钮。我想我将研究二维数组,并且会有类似 boolean 数组来显示炸弹在哪里,例如,如果 booleanArray[0][4] 为 true,则有炸弹。 现在,我
我遇到了这个奇怪的问题,但我似乎无法弄清楚。我有一个按钮,上面写着“新游戏”。在我玩一次游戏后,然后按“新游戏”返回到 initGame() ,我遇到一个问题,因为我无法右键单击前一个游戏的地雷单元格
我现在面临一个问题,我认为我的 main 正在一遍又一遍地执行一个方法,而不是一次。如果我根据一个例子来解释一下就更好了。我已经能够编写扫雷游戏了。但我把这一切都写在一个 MAIN 类中。这次我尝试再
几个月来我一直在尝试创建自己的扫雷游戏(为了好玩)。唯一让我真正困惑的是如何使用递归函数(flood-fill)来填充游戏中的空白区域。 洪水填充仅部分起作用。它不会从任何单击的节点延伸到右侧或底部节
我正在尝试在 C++11 中使用 Qt 编写扫雷程序。 如果我按下一个带有 0 个炸弹的按钮,我想检查这个按钮周围的按钮以及它们是否也有 0 个炸弹。如果他们有 0 颗炸弹,我想检查按钮。 (图:红色
我想做的是能够“转动”每个相邻的 0,就像普通的扫雷游戏一样。 #include #include #include #include #include int buscamina(); u
有没有办法让某些事件 Action 特定于鼠标左键和右键单击? 我正在创建一个扫雷器图形用户界面,所以当左键单击一个方 block 时,它会被发现,而当它被右键单击时,它会被标记。 我不确定如何从语法
我正在尝试制作一个控制台版本的扫雷。 尽管我目前很努力,但我无法弄清楚扫雷的“洪水填充”部分,如果所选方格的周围邻居不包含炸弹,我们现在必须检查这些相邻方格以找到相邻方格炸弹。 下面的代码适用于所选方
我是论坛新手,因此如果这不符合该网站的标准,我想表示歉意,但是我对 2D 阵列扫雷程序有疑问。 我试图在棋盘上随机放置地雷,棋盘是通过数组构建的。不幸的是,地雷被随机放置在对角线上,而不是整个棋盘上。
我是 Java 编程新手,想寻求您的帮助。我正在尝试使用 Java 开发一个简单的扫雷游戏。但是,我不断收到错误“线程“main”java.lang.ArrayIndexOutOfBoundsExce
我在这段代码上工作了几个小时,终于接近完成了。一旦我弄清楚如何添加通过右键单击标记地雷的功能,我将添加一个地雷计数器和一个计时器。我了解到 JButton 无法识别右键单击,因此我必须使用 Mouse
for (var i = 0; i < 10; i++) { bommen[i] = [ Math.floor(Math.random() * 10), Math.floor(
我正在完成大学的一项作业,我们获得了扫雷游戏的源代码,并且我们有一定的实现要求。其中之一是,所有游戏都是在用户登录后从磁盘读取的,用户可以随时保存游戏进度。我一直在阅读序列化来保存/加载游戏进度,但我
我正在尝试用 Java 制作类似扫雷的游戏,并且我已经完成了其中的大部分工作。我需要帮助的是 FloodFill - http://en.wikipedia.org/wiki/Flood_fill .
我正在尝试为我正在制作的扫雷游戏制作一个函数。该函数的目的是根据给定的 x 和 y(它所在的位置)显示一个元素。这可能不是实现它的最优雅的方法,但我正在为每个标题为 newField 的图 block
这款游戏是扫雷。我试图实现的人工智能将采用已经在计算机上运行的扫雷游戏实例(本例为 Windows 7),获取窗口的矩形尺寸,计算窗口的位置,将其推送到前台在屏幕上,单击左上角的方 block ,然后
我是一名优秀的程序员,十分优秀!