簡體   English   中英

贏取支票並再次玩C#字樣

[英]Winning check and playagain c# tictactoe

我有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;
        }
    }

謝謝您提供的任何幫助,如果您需要更多詳細信息,請提前詢問。

首先,如果您將單元作為數組訪問並實現checkHorizon(int column) / checkVerticals(int row) / checkCross(bool isRightToLeft)函數,則該代碼看起來會更好得多-該函數可以接收數字並檢查為獲勝者選擇的水平/垂直/交叉線,並返回一個值。 由於我不確定在wpf中是否有一種簡單的方法可以將gridView作為數組進行訪問,因此,如果您手動插入9個單元格,則此鏈接提供了一種適用於C#的方法。

您的第一個問題是有關按鈕OnClick事件在第一次嘗試時未觸發。 發生此問題的原因有很多-當我過去為ASP.NET編程時,我會想到這類問題與page_load和ASP頁面生命周期有關。 看看wpf的等價物,讓我知道它是否解決了您的問題

您的第二個問題非常簡單。 您希望Winner()函數更改圖像(如果我正確獲取此圖像)。 我認為問題還是與對象生命周期有關-問題是對象已全部加載到頁面,因此,要查看更改,必須在更改后刷新頁面

然后是第三個問題,可能是前兩個問題的解決方案。 如何刷新您詢問的頁面? 我從另一個來源借來了語法,因為我從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);

祝您好運,如果您還有其他無法解決的問題,請告訴我:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM