制作NxN井字游戏GUI WPF C# [英] Making a NxN tic tac toe GUI wpf c#

查看:67
本文介绍了制作NxN井字游戏GUI WPF C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用WPF c#制作NxN井字游戏.我希望用户输入行数和列数(NxN),并且我的代码将生成那些列数和行数.我无法弄清楚,我将如何动态产生这么多的行和列.我已经发布了我的XAML代码,有没有办法可以循环我的XAML代码?谢谢

 < Grid x:Name ="Container"><!-首先,我要创建N列->< Grid.ColumnDefinitions>< ColumnDefinition Width ="*"/>< ColumnDefinition Width ="*"/>< ColumnDefinition Width ="*"/></Grid.ColumnDefinitions><!-在这里我要排N行->< Grid.RowDefinitions>< RowDefinition Height ="*"/>< RowDefinition Height ="*"/>< RowDefinition Height ="*"/></Grid.RowDefinitions><!-然后在这里我要在N x N列和行中添加按钮的数量-><按钮x:Name ="Button0_0" Grid.Row ="0" Grid.Column ="0" Click ="Button_Click"/><按钮x:Name ="Button0_1" Grid.Row ="0" Grid.Column ="1" Click ="Button_Click"/><按钮x:Name ="Button0_2" Grid.Row ="0" Grid.Column ="2" Click ="Button_Click"/><按钮x:Name ="Button1_0" Grid.Row ="1" Grid.Column ="0" Click ="Button_Click"/><按钮x:Name ="Button1_1" Grid.Row ="1" Grid.Column ="1" Click ="Button_Click"/><按钮x:Name ="Button1_2" Grid.Row ="1" Grid.Column ="2" Click ="Button_Click"/><按钮x:Name ="Button2_0" Grid.Row ="2" Grid.Column ="0" Click ="Button_Click"/><按钮x:Name ="Button2_1" Grid.Row ="2" Grid.Column ="1" Click ="Button_Click"/><按钮x:Name ="Button2_2" Grid.Row ="2" Grid.Column ="2" Click ="Button_Click"/></Grid> 

解决方案

ItemsControl + UniformGrid作为面板是显示矩形游戏板的不错选择.我已经发布了类似的解决方案(

摘要:逻辑和表示法分开.处理数据.让控件根据绑定和提供的模板生成其内容.

I am making a NxN tic tac toe game in WPF c#. I want user to enter Rows and Column count(NxN), and my code will generate those number of columns and rows. I can't figure it out, how will I produce that number of rows and columns dynamically. I have posted my XAML code, is there a way that I can loop my XAML code? thanks

<Grid x:Name="Container">
    <!-- First here i want to make N columns-->
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <!-- Here i want to make N rows-->
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!-- Then here i want to add that number of buttons in N x N columns and rows -->
    <Button x:Name="Button0_0" Grid.Row="0" Grid.Column="0" Click="Button_Click"/>
    <Button x:Name="Button0_1" Grid.Row="0" Grid.Column="1" Click="Button_Click" />
    <Button x:Name="Button0_2" Grid.Row="0" Grid.Column="2" Click="Button_Click"/>

    <Button x:Name="Button1_0" Grid.Row="1" Grid.Column="0" Click="Button_Click"/>
    <Button x:Name="Button1_1" Grid.Row="1" Grid.Column="1" Click="Button_Click"/>
    <Button x:Name="Button1_2" Grid.Row="1" Grid.Column="2" Click="Button_Click"/>

    <Button x:Name="Button2_0" Grid.Row="2" Grid.Column="0" Click="Button_Click"/>
    <Button x:Name="Button2_1" Grid.Row="2" Grid.Column="1" Click="Button_Click"/>
    <Button x:Name="Button2_2" Grid.Row="2" Grid.Column="2" Click="Button_Click"/>                                                  
</Grid>

解决方案

ItemsControl + UniformGrid as a Panel is a good choice to display a rectangular game board. I have already posted similar solution (How to create and use matrix of (color) boxes C# WPF) but it is missing Rows and Columns binding. Here is a more elaborated example for TicTacToe.

Let's create view model classes:

public class BoardCell : INotifyPropertyChanged
{
    private string _sign;
    private bool _canSelect = true;

    public string Sign
    {
        get { return _sign; }
        set
        {
            _sign = value;
            if (value != null)
                CanSelect = false;
            OnPropertyChanged();
        }
    }

    public bool CanSelect
    {
        get { return _canSelect; }
        set
        {
            _canSelect = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class Board
{
    public int Rows { get; set; }
    public int Columns { get; set; }

    private ObservableCollection<BoardCell> _cells;
    public ObservableCollection<BoardCell> Cells
    {
        get
        {
            if (_cells == null)
                _cells = new ObservableCollection<BoardCell>(Enumerable.Range(0, Rows*Columns).Select(i => new BoardCell()));
            return _cells;
        }
    } 
}

and then create a view:

<Grid x:Name="Container">
    <Grid.DataContext>
        <wpfDemos:Board Rows="8" Columns="8"/>
    </Grid.DataContext>
    <ItemsControl x:Name="Board" ItemsSource="{Binding Path=Cells}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate >
                <UniformGrid Rows="{Binding Path=Rows}" Columns="{Binding Path=Columns}"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Content="{Binding Path=Sign}" 
                        IsEnabled="{Binding Path=CanSelect}"
                        Click="CellClick"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

in the code-behind we have one method to allow 2 players make thier moves in turn:

private bool _firstPlayer = true;
private void CellClick(object sender, RoutedEventArgs e)
{
    var cell = (sender as Button).DataContext as BoardCell;
    cell.Sign = _firstPlayer ? "X" : "O";
    _firstPlayer = !_firstPlayer;
    // TODO check winner 
}

to follow MVVM pattern this method should be wrapped into a command (ICommand), moved to Board class and attached to view via Button.Command binding.

summary: separate logic and presentation. work with data. let controls generate their content based on bindings and provided templates.

这篇关于制作NxN井字游戏GUI WPF C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆