检查相邻的网格坐标是否有包含 uwp 的椭圆
Check if adjacent grid coordinates has an ellipse contained uwp
我开始用 visual studio uwp 构建一个 peg 纸牌游戏。
我在 xaml 应用程序的 c# 端显示了棋盘和棋子。
我正在检查点击球周围的相邻方块是否包含椭圆。
我突出显示了方块以测试在单击球时是否选择了正确的方块。当我弄清楚如何访问相邻的方块并检查其中是否存在球时,我将删除突出显示的方块。
我遇到的问题是如何检查相邻的正方形是否包含椭圆。
我尝试获取边框的名称并检查它是否有子元素,但是当我将它打印到控制台时它似乎没有输出任何内容。
请参阅下面的 mainPage.xaml.cs 和 mainPage.xaml 文件。
我是 uwp 的新手,如果您能提供帮助,我将不胜感激。
提前致谢。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace Solitaire
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
addRowsColumns();
addBorders();
addPieces();
}
private void addRowsColumns()
{
for (int i = 0; i < 9; i++)
{
grdGame.ColumnDefinitions.Add(new ColumnDefinition());
grdGame.RowDefinitions.Add(new RowDefinition());
}
}
private void addBorders()
{
Border brdr;
int iR, iC;
//iR set to 1 to center the board in front of the background
for (iR = 1; iR < 8; iR++)
{//iC set to 1 to center board
for (iC = 1; iC < 8; iC++)
{
brdr = new Border();
//name for getting the position of the peices on the board.
brdr.Name = "square_" + iR.ToString() + "_" + iC.ToString();
//set default colour of border to balck
brdr.Background = new SolidColorBrush(Colors.Black);
// if modulus of iR + iC is 0, then make the square white
if ((iR + iC) % 2 == 0)
{
brdr.Background = new SolidColorBrush(Colors.White);
}
//remove the squares that are not needed for the game
//colour these squares the same colour as the grid background
//@todo need to make these references in the grid non playable.
if ((iR<3&&iC <3)||(iR <3 && iC > 5)|| (iR >5 && iC < 3) || (iR >5 && iC > 5))
{
brdr.Background = new SolidColorBrush(Colors.BurlyWood);
}
brdr.SetValue(Grid.RowProperty, iR);
brdr.SetValue(Grid.ColumnProperty, iC);
brdr.HorizontalAlignment = HorizontalAlignment.Center;
brdr.VerticalAlignment = VerticalAlignment.Center;
//@todo set height and width of squares not hard coded.
brdr.Height = 100;
brdr.Width = 100;
//add squares to the board.
grdGame.Children.Add(brdr);
}
}
}
int _Rows = 8;
private void addPieces()
{
Ellipse myEl;
int iR, iC;
// use R&C to name the objects
for (iR = 1; iR < _Rows; iR++)
{
for (iC = 1; iC < _Rows; iC++)
{//center square no elipse set for the opening move
if (!((iR < 3 && iC < 3)
|| (iR < 3 && iC > 5)
|| (iR > 5 && iC < 3)
|| (iR > 5 && iC > 5)
||(iR==4&&iC==4)))
{
myEl = new Ellipse();
myEl.Name = "el_" + iR + "_" + iC;
myEl.Tag = "peices";
myEl.Fill = new SolidColorBrush(Colors.Silver);
myEl.Height = 40;
myEl.Width = 40;
myEl.SetValue(Grid.RowProperty, iR);
myEl.SetValue(Grid.ColumnProperty, iC);
myEl.Tapped += myEl_Tapped;
grdGame.Children.Add(myEl);
}
}
}
}
private void myEl_Tapped(object sender, TappedRoutedEventArgs e)
{
Ellipse moveMe;
Border possible1, possible2, possible3, possible4;
int toR1,toC1;
Ellipse current = (Ellipse)sender;
Debug.WriteLine(current.Name);
moveMe = current;
//current.Fill = new SolidColorBrush(Colors.Blue);
toR1 = (int)current.GetValue(Grid.RowProperty);
// toR2 = (int)current.GetValue(Grid.RowProperty);
toC1 = (int)current.GetValue(Grid.ColumnProperty);
//brdr1 = (int)brdr.GetValue(Grid.RowProperty);
//toC2 = (int)current.GetValue(Grid.ColumnProperty);
//Print out rows and coloums one above and below, one to left and right.
Debug.WriteLine("Move row 1 : "+toR1+" MOve col 1: "+toC1);
possible1 = new Border();
possible2 = new Border();
possible3 = new Border();
possible4 = new Border();
possible1.SetValue(Grid.RowProperty, toR1);
possible1.SetValue(Grid.ColumnProperty, toC1+1);
possible2.SetValue(Grid.RowProperty, toR1);
possible2.SetValue(Grid.ColumnProperty, toC1 - 1);
possible1.Background = new SolidColorBrush(Colors.Gold);
possible2.Background = new SolidColorBrush(Colors.Gold);
possible3.SetValue(Grid.RowProperty, toR1+1);
possible3.SetValue(Grid.ColumnProperty, toC1);
possible4.SetValue(Grid.RowProperty, toR1-1);
possible4.SetValue(Grid.ColumnProperty, toC1);
possible3.Background = new SolidColorBrush(Colors.Gold);
possible4.Background = new SolidColorBrush(Colors.Gold);
grdGame.Children.Add(possible1);
grdGame.Children.Add(possible2);
grdGame.Children.Add(possible3);
grdGame.Children.Add(possible4);
}
//@todo add tapped event to peices and move them no logic for now
}
}
和 xaml 文件
<Page
x:Class="Solitaire.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Solitaire"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid x:Name="grdGame">
</Grid>
这里有一个方法可以减轻寻找邻居的痛苦:
- 假设您使用
Grid
,其中每个 child 正好占 1 行和 1 列
- 所有 child 都关联到同一个点击事件
- 该方法将 return
bool
和找到的元素(如果有的话)
待办事项:
当一个元素被return编辑时,根据你的情况进一步检查,比如你可以设置并检查Button
的Tag
属性。
XAML:
<Window
x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button
Grid.Row="0"
Grid.Column="0"
Click="Button_Click" />
<Button
Grid.Row="2"
Grid.Column="2"
Click="Button_Click" />
<Button
Grid.Row="2"
Grid.Column="1"
Click="Button_Click" />
<Button
Grid.Row="2"
Grid.Column="0"
Click="Button_Click" />
<Button
Grid.Row="1"
Grid.Column="2"
Click="Button_Click" />
<Button
Grid.Row="0"
Grid.Column="2"
Click="Button_Click" />
<Button
Grid.Row="1"
Grid.Column="0"
Click="Button_Click" />
<Button
Grid.Row="1"
Grid.Column="1"
Click="Button_Click" />
<Button
Grid.Row="0"
Grid.Column="1"
Click="Button_Click" />
</Grid>
</Window>
代码:
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace WpfApp1
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var button = (Button) sender;
// where is the element you want, relative row/column to this one
var relativeX = 1;
var relativeY = 1;
UIElement result;
if (TryGetAdjacent(button, relativeX, relativeY, out result))
{
MessageBox.Show("Yes !");
// TODO further check whether element satisfies some condition
}
}
private bool TryGetAdjacent(Button button, int relativeX, int relativeY, out UIElement result)
{
result = null;
var row = Grid.GetRow(button);
var col = Grid.GetColumn(button);
var parent = (Grid) button.Parent;
var rows = parent.RowDefinitions.Count;
var cols = parent.ColumnDefinitions.Count;
var x = row + relativeX;
var y = col + relativeY;
if (x < 0 || x >= cols || y < 0 || y >= rows)
return false;
foreach (var child in parent.Children.Cast<UIElement>())
{
var col1 = Grid.GetColumn(child);
var row1 = Grid.GetRow(child);
if (row1 != x || col1 != y)
continue;
result = child;
return true;
}
return false;
}
}
}
注意:
抱歉,我手头没有 UWP,所以这是 WPF,它应该开箱即用,因为逻辑非常相似。您可能需要做一些小的调整,但总而言之,您会明白我的简化方法的要点。
我开始用 visual studio uwp 构建一个 peg 纸牌游戏。 我在 xaml 应用程序的 c# 端显示了棋盘和棋子。 我正在检查点击球周围的相邻方块是否包含椭圆。
我突出显示了方块以测试在单击球时是否选择了正确的方块。当我弄清楚如何访问相邻的方块并检查其中是否存在球时,我将删除突出显示的方块。
我遇到的问题是如何检查相邻的正方形是否包含椭圆。
我尝试获取边框的名称并检查它是否有子元素,但是当我将它打印到控制台时它似乎没有输出任何内容。 请参阅下面的 mainPage.xaml.cs 和 mainPage.xaml 文件。 我是 uwp 的新手,如果您能提供帮助,我将不胜感激。
提前致谢。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace Solitaire
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
addRowsColumns();
addBorders();
addPieces();
}
private void addRowsColumns()
{
for (int i = 0; i < 9; i++)
{
grdGame.ColumnDefinitions.Add(new ColumnDefinition());
grdGame.RowDefinitions.Add(new RowDefinition());
}
}
private void addBorders()
{
Border brdr;
int iR, iC;
//iR set to 1 to center the board in front of the background
for (iR = 1; iR < 8; iR++)
{//iC set to 1 to center board
for (iC = 1; iC < 8; iC++)
{
brdr = new Border();
//name for getting the position of the peices on the board.
brdr.Name = "square_" + iR.ToString() + "_" + iC.ToString();
//set default colour of border to balck
brdr.Background = new SolidColorBrush(Colors.Black);
// if modulus of iR + iC is 0, then make the square white
if ((iR + iC) % 2 == 0)
{
brdr.Background = new SolidColorBrush(Colors.White);
}
//remove the squares that are not needed for the game
//colour these squares the same colour as the grid background
//@todo need to make these references in the grid non playable.
if ((iR<3&&iC <3)||(iR <3 && iC > 5)|| (iR >5 && iC < 3) || (iR >5 && iC > 5))
{
brdr.Background = new SolidColorBrush(Colors.BurlyWood);
}
brdr.SetValue(Grid.RowProperty, iR);
brdr.SetValue(Grid.ColumnProperty, iC);
brdr.HorizontalAlignment = HorizontalAlignment.Center;
brdr.VerticalAlignment = VerticalAlignment.Center;
//@todo set height and width of squares not hard coded.
brdr.Height = 100;
brdr.Width = 100;
//add squares to the board.
grdGame.Children.Add(brdr);
}
}
}
int _Rows = 8;
private void addPieces()
{
Ellipse myEl;
int iR, iC;
// use R&C to name the objects
for (iR = 1; iR < _Rows; iR++)
{
for (iC = 1; iC < _Rows; iC++)
{//center square no elipse set for the opening move
if (!((iR < 3 && iC < 3)
|| (iR < 3 && iC > 5)
|| (iR > 5 && iC < 3)
|| (iR > 5 && iC > 5)
||(iR==4&&iC==4)))
{
myEl = new Ellipse();
myEl.Name = "el_" + iR + "_" + iC;
myEl.Tag = "peices";
myEl.Fill = new SolidColorBrush(Colors.Silver);
myEl.Height = 40;
myEl.Width = 40;
myEl.SetValue(Grid.RowProperty, iR);
myEl.SetValue(Grid.ColumnProperty, iC);
myEl.Tapped += myEl_Tapped;
grdGame.Children.Add(myEl);
}
}
}
}
private void myEl_Tapped(object sender, TappedRoutedEventArgs e)
{
Ellipse moveMe;
Border possible1, possible2, possible3, possible4;
int toR1,toC1;
Ellipse current = (Ellipse)sender;
Debug.WriteLine(current.Name);
moveMe = current;
//current.Fill = new SolidColorBrush(Colors.Blue);
toR1 = (int)current.GetValue(Grid.RowProperty);
// toR2 = (int)current.GetValue(Grid.RowProperty);
toC1 = (int)current.GetValue(Grid.ColumnProperty);
//brdr1 = (int)brdr.GetValue(Grid.RowProperty);
//toC2 = (int)current.GetValue(Grid.ColumnProperty);
//Print out rows and coloums one above and below, one to left and right.
Debug.WriteLine("Move row 1 : "+toR1+" MOve col 1: "+toC1);
possible1 = new Border();
possible2 = new Border();
possible3 = new Border();
possible4 = new Border();
possible1.SetValue(Grid.RowProperty, toR1);
possible1.SetValue(Grid.ColumnProperty, toC1+1);
possible2.SetValue(Grid.RowProperty, toR1);
possible2.SetValue(Grid.ColumnProperty, toC1 - 1);
possible1.Background = new SolidColorBrush(Colors.Gold);
possible2.Background = new SolidColorBrush(Colors.Gold);
possible3.SetValue(Grid.RowProperty, toR1+1);
possible3.SetValue(Grid.ColumnProperty, toC1);
possible4.SetValue(Grid.RowProperty, toR1-1);
possible4.SetValue(Grid.ColumnProperty, toC1);
possible3.Background = new SolidColorBrush(Colors.Gold);
possible4.Background = new SolidColorBrush(Colors.Gold);
grdGame.Children.Add(possible1);
grdGame.Children.Add(possible2);
grdGame.Children.Add(possible3);
grdGame.Children.Add(possible4);
}
//@todo add tapped event to peices and move them no logic for now
}
}
和 xaml 文件
<Page
x:Class="Solitaire.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Solitaire"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid x:Name="grdGame">
</Grid>
这里有一个方法可以减轻寻找邻居的痛苦:
- 假设您使用
Grid
,其中每个 child 正好占 1 行和 1 列 - 所有 child 都关联到同一个点击事件
- 该方法将 return
bool
和找到的元素(如果有的话)
待办事项:
当一个元素被return编辑时,根据你的情况进一步检查,比如你可以设置并检查Button
的Tag
属性。
XAML:
<Window
x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button
Grid.Row="0"
Grid.Column="0"
Click="Button_Click" />
<Button
Grid.Row="2"
Grid.Column="2"
Click="Button_Click" />
<Button
Grid.Row="2"
Grid.Column="1"
Click="Button_Click" />
<Button
Grid.Row="2"
Grid.Column="0"
Click="Button_Click" />
<Button
Grid.Row="1"
Grid.Column="2"
Click="Button_Click" />
<Button
Grid.Row="0"
Grid.Column="2"
Click="Button_Click" />
<Button
Grid.Row="1"
Grid.Column="0"
Click="Button_Click" />
<Button
Grid.Row="1"
Grid.Column="1"
Click="Button_Click" />
<Button
Grid.Row="0"
Grid.Column="1"
Click="Button_Click" />
</Grid>
</Window>
代码:
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace WpfApp1
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var button = (Button) sender;
// where is the element you want, relative row/column to this one
var relativeX = 1;
var relativeY = 1;
UIElement result;
if (TryGetAdjacent(button, relativeX, relativeY, out result))
{
MessageBox.Show("Yes !");
// TODO further check whether element satisfies some condition
}
}
private bool TryGetAdjacent(Button button, int relativeX, int relativeY, out UIElement result)
{
result = null;
var row = Grid.GetRow(button);
var col = Grid.GetColumn(button);
var parent = (Grid) button.Parent;
var rows = parent.RowDefinitions.Count;
var cols = parent.ColumnDefinitions.Count;
var x = row + relativeX;
var y = col + relativeY;
if (x < 0 || x >= cols || y < 0 || y >= rows)
return false;
foreach (var child in parent.Children.Cast<UIElement>())
{
var col1 = Grid.GetColumn(child);
var row1 = Grid.GetRow(child);
if (row1 != x || col1 != y)
continue;
result = child;
return true;
}
return false;
}
}
}
注意:
抱歉,我手头没有 UWP,所以这是 WPF,它应该开箱即用,因为逻辑非常相似。您可能需要做一些小的调整,但总而言之,您会明白我的简化方法的要点。