【问题标题】:Winning check and playagain c# tictactoec# tictactoe 中奖并再次游戏
【发布时间】:2015-02-15 23:16:37
【问题描述】:

我有 3 个问题: 1-第一次按下“letsPlay”按钮时,无论我按下哪里都会给我oImage,但之后工作正常。 2-Winner() 方法不起作用……它什么也不做。 3-如何重置我的网格背景,因为我找不到任何结果。

好的,这里是 xaml 代码:

<StackPanel>
    <Grid VerticalAlignment="Top">
        <Button x:Name="letsPlay" Content="Let's Start!" Click="letsPlay_Click"/>
        <TextBlock x:Name="turnText" 
                   HorizontalAlignment="Left" 
                   Margin="302,10,0,0" 
                   TextWrapping="Wrap" 
                   Text="Turn = X" 
                   VerticalAlignment="Top"
                   Foreground="#FFFB0707" FontSize="20"/>
    </Grid>
    <Grid x:Name="theGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Image x:Name="cell9" Grid.Column="2" Grid.Row="2" Source="Images/Logo.png" Tapped="cell9_Tapped"/>
        <Image x:Name="cell8" Grid.Column="1" Grid.Row="2" Source="Images/Logo.png" Tapped="cell8_Tapped"/>
        <Image x:Name="cell7" Grid.Column="0" Grid.Row="2" Source="Images/Logo.png" Tapped="cell7_Tapped"/>
        <Image x:Name="cell4" Grid.Column="0" Grid.Row="1" Source="Images/Logo.png" Tapped="cell4_Tapped"/>
        <Image x:Name="cell1" Grid.Column="0" Grid.Row="0" Source="Images/Logo.png" Tapped="cell1_Tapped"/>
        <Image x:Name="cell5" Grid.Column="1" Grid.Row="1" Source="Images/Logo.png" Tapped="cell5_Tapped"/>
        <Image x:Name="cell2" Grid.Column="1" Grid.Row="0" Source="Images/Logo.png" Tapped="cell2_Tapped"/>
        <Image x:Name="cell6" Grid.Column="2" Grid.Row="1" Source="Images/Logo.png" Tapped="cell6_Tapped"/>
        <Image x:Name="cell3" Grid.Column="2" Grid.Row="0" Source="Images/Logo.png" Tapped="cell3_Tapped"/>
    </Grid>
</StackPanel>

现在运行后面的代码:

bool turn = true;
    int moveCount1 = 0;
    BitmapImage xImage = new BitmapImage(new Uri("ms-appx:///Images/X.png"));
    BitmapImage oImage = new BitmapImage(new Uri("ms-appx:///Images/O.png"));
    BitmapImage nullImage = new BitmapImage(new Uri("ms-appx:///Images/Logo.png"));
    public MainPage()
    {
        this.InitializeComponent();
        this.NavigationCacheMode = NavigationCacheMode.Required;
        theGrid.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        turnText.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        this.Winner();
    }

letsPlay 按钮和 Reset() 方法:

    private void letsPlay_Click(object sender, RoutedEventArgs e)
        {
            Reset();
        }
void Reset()
        {
            theGrid.Visibility = Windows.UI.Xaml.Visibility.Visible;
            turnText.Visibility = Windows.UI.Xaml.Visibility.Visible;
            turn = true;
            turnText.Text = "Turn = X";
            moveCount1 = 0;
            cell1.Source = nullImage;
            cell1.Tapped += cell1_Tapped;
            cell2.Source = nullImage;
            cell2.Tapped += cell2_Tapped;
            cell3.Source = nullImage;
            cell3.Tapped += cell3_Tapped;
            cell4.Source = nullImage;
            cell4.Tapped += cell4_Tapped;
            cell5.Source = nullImage;
            cell5.Tapped += cell5_Tapped;
            cell6.Source = nullImage;
            cell6.Tapped += cell6_Tapped;
            cell7.Source = nullImage;
            cell7.Tapped += cell7_Tapped;
            cell8.Source = nullImage;
            cell8.Tapped += cell8_Tapped;
            cell9.Source = nullImage;
            cell9.Tapped += cell9_Tapped;
        }

这是一个单元格代码示例:

private void cell9_Tapped(object sender, TappedRoutedEventArgs e)
    {
        if (turn)
        {
            cell9.Source = xImage;
            turn = !turn;
            turnText.Text = "Turn = O";
        }
        else
        {
            cell9.Source = oImage;
            turn = !turn;
            turnText.Text = "Turn = X";
        }
        cell9.Tapped -= cell9_Tapped;
        moveCount1++;
    }

最后这是 Winner() 方法:

public void Winner()
    {
        if (
            (cell1.Source == xImage && cell2.Source == cell1.Source && cell3.Source == cell1.Source) ||
            (cell4.Source == xImage && cell5.Source == cell4.Source && cell6.Source == cell4.Source) ||
            (cell7.Source == xImage && cell8.Source == cell7.Source && cell9.Source == cell7.Source) ||
            (cell1.Source == xImage && cell5.Source == cell1.Source && cell9.Source == cell1.Source) ||
            (cell3.Source == xImage && cell5.Source == cell3.Source && cell7.Source == cell3.Source) ||
            (cell1.Source == xImage && cell4.Source == cell1.Source && cell7.Source == cell1.Source) ||
            (cell2.Source == xImage && cell5.Source == cell2.Source && cell8.Source == cell2.Source) ||
            (cell3.Source == xImage && cell6.Source == cell3.Source && cell9.Source == cell3.Source)
            )
        {
            ImageBrush xWins1 = new ImageBrush();
            xWins1.ImageSource = xImage;
            theGrid.Background = xWins1;
        }
        else if (
            (cell1.Source == oImage && cell2.Source == cell1.Source && cell3.Source == cell1.Source) ||
            (cell4.Source == oImage && cell5.Source == cell4.Source && cell6.Source == cell4.Source) ||
            (cell7.Source == oImage && cell8.Source == cell7.Source && cell9.Source == cell7.Source) ||
            (cell1.Source == oImage && cell5.Source == cell1.Source && cell9.Source == cell1.Source) ||
            (cell3.Source == oImage && cell5.Source == cell3.Source && cell7.Source == cell3.Source) ||
            (cell1.Source == oImage && cell4.Source == cell1.Source && cell7.Source == cell1.Source) ||
            (cell2.Source == oImage && cell5.Source == cell2.Source && cell8.Source == cell2.Source) ||
            (cell3.Source == oImage && cell6.Source == cell3.Source && cell9.Source == cell3.Source)
            )
        {
            ImageBrush oWins1 = new ImageBrush();
            oWins1.ImageSource = oImage;
            theGrid.Background = oWins1;
        }
        else if (moveCount1 == 9)
        {
            ImageBrush tie1 = new ImageBrush();
            tie1.ImageSource = nullImage;
            theGrid.Background = tie1;
        }
    }

我将感谢你们提供的任何帮助,如果您需要更多详细信息,请提前询问。

【问题讨论】:

    标签: c# asp.net wpf


    【解决方案1】:

    首先,如果您将单元格作为数组访问并实现checkHorizon(int column)/checkVerticals(int row)/checkCross(bool isRightToLeft) 函数 - 接收数字并检查所选水平/获胜者的垂直/交叉线,并返回一个值。由于我不确定是否有一种简单的方法可以将 gridView 作为 wpf 中的数组进行访问,所以这里有一个 link,如果您手动插入 9 个单元格,则该方法可以从 C# 工作。

    您的第一个问题是关于button OnClick event not firing on the first try. 发生此问题的原因有很多——我记得当我曾经为 ASP.NET 编程时,这类问题与 page_load 和 ASP 页面生命周期有关。看看the equivallent for wpf,,如果它解决了你的问题,请告诉我

    您的第二个问题非常简单。如果我正确理解,您希望您的 Winner() 函数更改图像。我认为问题再次与对象生命周期有关 - 问题是对象已加载到页面中,因此为了查看更改,您必须在更改后刷新页面。

    然后是第三个问题,这可能是前两个问题的解决方案。如何刷新你问的页面?我从Another source 借用了语法,因为我记住了 ASP 的上下文。解决方案是将用户重定向到您的页面,然后网格将首先重新加载。你可以这样做:

    const string pageName = "insert your page name here";
    /*Insert some other code here*/
    public static redirect(string directionName)
    {
        Response.Redirect(directionName+".aspx");
    }
    /* Insert some more code here */
    

    只要gridview发生变化,您只需使用redirect(pageName);

    祝你好运,如果还有其他你无法解决的问题,请告诉我:)

    【讨论】:

    • 我有点理解你在说什么......但我不知道如何使用或实现它们(经验非常低)。因此,如果您可以帮助我使用社交媒体(如果您是免费的),我可以。并感谢您的支持。
    • @ElieSaad 如果你真的需要,我们可以通过 Skype 交谈;然而,在这里交谈有它的好处,当我们遇到困难时其他人可以帮助我们,我也不知道一切:) 但是,如果只是将我在这里写的概念应用到你的项目中 - 我会帮助你。
    • 加我:saad.elie1 当我们讨论完并解决问题后,我们可以在这里展示给他们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多