我开始用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>
答案 0 :(得分:0)
这是一种减轻搜索邻居痛苦的方法:
Grid
,其中每个孩子只需1行1列bool
并找到找到的元素(如果有的话)TODO:
返回元素时,请根据您的情况进一步检查,例如,您可以设置并检查Tag
的{{1}}属性。
XAML:
Button
代码:
<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>
注意:强>
对不起,我手头没有UWP所以这是WPF,因为逻辑很相似所以它应该是开箱即用的。您可能需要进行一些调整,但总而言之,您将明白我的简化方法。