根据点击的按钮更改按钮内容

时间:2015-10-14 00:52:24

标签: c#

我正在为一项作业开展一个tic tac toe游戏,而我想要制作游戏的方式似乎不太可能。我认为其他人已经提出了这个问题,但我在网上任何答案中看到的所有代码都比我想做的更先进(或者看起来那样)。

目前,我对所有9个按钮使用了1次点击事件。当玩家1玩游戏时,它将成为玩家2的回合。

因为我对所有按钮使用相同的点击事件,所以我无法弄清楚如何使每个按钮的内容变为“X”或“O”,具体取决于轮到谁。

如果我正在做9个不同的点击事件,我知道该怎么做,但这个任务的重点是使用简单的方法,这让我发疯。

我最初的感觉是应该有一个“this.Content =”X“;”选项等等。

这是我到目前为止的代码(减去一些已经有效的方法。我想要工作的代码行周围有星号,但不知道如何让它们工作。):

public partial class MainWindow : Window
{
    bool turn;
    string playerX;
    string playerO;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void IsTurn ()
    {
        if (turn==true)
        {
            textBlockGameInfo.Text = "It is " + playerO + "'s turn";
            **button_Click.Content = "O";**
            turn = false;
        }
        else if (turn==false)
        {
            textBlockGameInfo.Text = "It is " + playerX + "'s turn";
            **button_Click.Content = "X";**
            turn = true;
        }
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        IsTurn();
    }
}

}

3 个答案:

答案 0 :(得分:0)

您正在寻找的是Button.Text属性。

当玩家1点击按钮时,你会想要

button.Text = "O";

当玩家2点击按钮时:

button.Text = "X";

答案 1 :(得分:0)

您绝对应该将方法中收到的发件人强制转换为Buttonbutton_Click方法中的发件人将是玩家实际点击的按钮。

一旦按下按钮,您就可以根据转弯设置其内容。你的转弯方法可以返回要分配的角色。有点像这样的东西会起作用:

var button = sender as Button;
if (button == null) return;

button.Content = IsTurn();

答案 2 :(得分:0)

尝试遵循@KOTIX的建议,让我的游戏运转起来,但我仍然对按钮状况如何发挥作用感到困惑。我想我只需要回去看看一些事情。

这是我对整个tic tac toe游戏的最终代码。查看最后两种方法,了解button_click的处理和使用方式。

我也认为必须有一个更好的方法来处理胜利者,但这又是另一个问题的另一个问题:)

希望这可以帮助其他人。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TicTacToe
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        Random random = new Random();

        int firstPlayer = 0;
        bool turn;
        bool winner = true;
        string playerX;
        string playerO;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void ResetBoard ()
        {
            //Clear Xs & Os:
            buttonTopLeft.Content = "";
            buttonMiddleTop.Content = "";
            buttonTopRight.Content = "";

            buttonMiddleLeft.Content = "";
            buttonMiddle.Content = "";
            buttonMiddleRight.Content = "";

            buttonLowerLeft.Content = "";
            buttonMiddleBottom.Content = "";
            buttonBottomRight.Content = "";

            //make board playable
            winner = false;

            //reset background colors
            buttonTopLeft.Background = Brushes.LightGray;
            buttonMiddleTop.Background = Brushes.LightGray;
            buttonTopRight.Background = Brushes.LightGray;
            buttonMiddleLeft.Background = Brushes.LightGray;
            buttonMiddle.Background = Brushes.LightGray;
            buttonMiddleRight.Background = Brushes.LightGray;
            buttonLowerLeft.Background = Brushes.LightGray;
            buttonMiddleBottom.Background = Brushes.LightGray;
            buttonBottomRight.Background = Brushes.LightGray;

        }

        private void ChooseFirstPlayer ()
        {
            if (textBoxPlayerO.Text == "Audra")
            {
                turn = false;
                playerX = textBoxPlayerX.Text;
                playerO = textBoxPlayerO.Text;
                textBlockGameInfo.Text = playerO + " goes first";
            }
            else if (textBoxPlayerX.Text == "Audra")
            {
                turn = true;
                playerX = textBoxPlayerX.Text;
                playerO = textBoxPlayerO.Text;
                textBlockGameInfo.Text = playerX + " goes first";
            }
            else
            {
                firstPlayer = random.Next(1, 3);

                if (textBoxPlayerO.Text == "" || textBoxPlayerX.Text == "")
                {
                    textBlockGameInfo.Text = "Please enter both player's names";
                }
                else
                {

                    if (firstPlayer == 1)
                    {
                        turn = true;
                        playerX = textBoxPlayerX.Text;
                        playerO = textBoxPlayerO.Text;
                        textBlockGameInfo.Text = playerX + " goes first";
                    }
                    else if (firstPlayer == 2)
                    {
                        turn = false;
                        playerX = textBoxPlayerX.Text;
                        playerO = textBoxPlayerO.Text;
                        textBlockGameInfo.Text = playerO + " goes first";
                    }
                }
            }

            }

        private void buttonStartGame_Click(object sender, RoutedEventArgs e)
        {
            ResetBoard();
            ChooseFirstPlayer();
        }

        private void IsNoWinner ()
        {
            if (buttonTopLeft.Content != "" && buttonMiddleTop.Content != "" && buttonTopRight.Content != "" &&
                buttonMiddleLeft.Content != "" && buttonMiddle.Content != "" && buttonMiddleRight.Content != "" &&
                buttonLowerLeft.Content != "" && buttonMiddleBottom.Content != "" && buttonBottomRight.Content != "" && winner == false)
            {
                textBlockGameInfo.Text = "It's a draw!";
            }
        }

        private void IsXWinner ()
        {
            if (buttonTopLeft.Content == "X" && buttonMiddleTop.Content == "X" && buttonTopRight.Content == "X")
            {
                textBlockGameInfo.Text = playerX + " is the winner!";
                winner = true;
                buttonTopLeft.Background = Brushes.LightPink;
                buttonMiddleTop.Background = Brushes.LightPink;
                buttonTopRight.Background = Brushes.LightPink;
            }
            else if (buttonMiddleLeft.Content == "X" && buttonMiddle.Content == "X" && buttonMiddleRight.Content =="X")
            {
                textBlockGameInfo.Text = playerX + " is the winner!";
                winner = true;
            }
            else if (buttonLowerLeft.Content == "X" && buttonMiddleBottom.Content == "X" && buttonBottomRight.Content == "X")
            {
                textBlockGameInfo.Text = playerX + " is the winner!";
                winner = true;
                buttonMiddleLeft.Background = Brushes.LightPink;
                buttonMiddle.Background = Brushes.LightPink;
                buttonMiddleRight.Background = Brushes.LightPink;
            }
            else if (buttonTopLeft.Content == "X" && buttonMiddleLeft.Content == "X" && buttonLowerLeft.Content == "X")
            {
                textBlockGameInfo.Text = playerX + " is the winner!";
                winner = true;
                buttonTopLeft.Background = Brushes.LightPink;
                buttonMiddleLeft.Background = Brushes.LightPink;
                buttonLowerLeft.Background = Brushes.LightPink;
            }
            else  if (buttonMiddleTop.Content == "X" && buttonMiddle.Content == "X" && buttonMiddleBottom.Content == "X")
            {
                textBlockGameInfo.Text = playerX + " is the winner!";
                winner = true;
                buttonMiddleTop.Background = Brushes.LightPink;
                buttonMiddle.Background = Brushes.LightPink;
                buttonMiddleBottom.Background = Brushes.LightPink;
            }
            else if (buttonLowerLeft.Content == "X" && buttonMiddleBottom.Content == "X" && buttonBottomRight.Content == "X")
            {
                textBlockGameInfo.Text = playerX + " is the winner!";
                winner = true;
                buttonLowerLeft.Background = Brushes.LightPink;
                buttonMiddleBottom.Background = Brushes.LightPink;
                buttonBottomRight.Background = Brushes.LightPink;
            }
            else if (buttonTopLeft.Content == "X" && buttonMiddle.Content == "X" && buttonBottomRight.Content == "X")
            {
                textBlockGameInfo.Text = playerX + " is the winner!";
                winner = true;
                buttonTopLeft.Background = Brushes.LightPink;
                buttonMiddle.Background = Brushes.LightPink;
                buttonBottomRight.Background = Brushes.LightPink;
            }
            else if (buttonTopRight.Content == "X" && buttonMiddle.Content == "X" && buttonLowerLeft.Content == "X")
            {
                textBlockGameInfo.Text = playerX + " is the winner!";
                winner = true;
                buttonTopRight.Background = Brushes.LightPink;
                buttonMiddle.Background = Brushes.LightPink;
                buttonLowerLeft.Background = Brushes.LightPink;
            }


        }

        private void IsOWinner()
        {
            if (buttonTopLeft.Content == "O" && buttonMiddleTop.Content == "O" && buttonTopRight.Content == "O")
            {
                textBlockGameInfo.Text = playerO + " is the winner!";
                winner = true;
                buttonTopLeft.Background = Brushes.LightPink;
                buttonMiddleTop.Background = Brushes.LightPink;
                buttonTopRight.Background = Brushes.LightPink;
            }
            else if (buttonMiddleLeft.Content == "O" && buttonMiddle.Content == "O" && buttonMiddleRight.Content == "O")
            {
                textBlockGameInfo.Text = playerO + " is the winner!";
                winner = true;
                buttonMiddleLeft.Background = Brushes.LightPink;
                buttonMiddle.Background = Brushes.LightPink;
                buttonMiddleRight.Background = Brushes.LightPink;
            }
            else if (buttonLowerLeft.Content == "O" && buttonMiddleBottom.Content == "O" && buttonBottomRight.Content == "O")
            {
                textBlockGameInfo.Text = playerO + " is the winner!";
                winner = true;
            }
            else if (buttonTopLeft.Content == "O" && buttonMiddleLeft.Content == "O" && buttonLowerLeft.Content == "O")
            {
                textBlockGameInfo.Text = playerO + " is the winner!";
                winner = true;
                buttonTopLeft.Background = Brushes.LightPink;
                buttonMiddleLeft.Background = Brushes.LightPink;
                buttonLowerLeft.Background = Brushes.LightPink;
            }
            else if (buttonMiddleTop.Content == "O" && buttonMiddle.Content == "O" && buttonMiddleBottom.Content == "O")
            {
                textBlockGameInfo.Text = playerO + " is the winner!";
                winner = true;
                buttonMiddleTop.Background = Brushes.LightPink;
                buttonMiddle.Background = Brushes.LightPink;
                buttonMiddleBottom.Background = Brushes.LightPink;
            }
            else if (buttonLowerLeft.Content == "O" && buttonMiddleBottom.Content == "O" && buttonBottomRight.Content == "O")
            {
                textBlockGameInfo.Text = playerO + " is the winner!";
                winner = true;
                buttonLowerLeft.Background = Brushes.LightPink;
                buttonMiddleBottom.Background = Brushes.LightPink;
                buttonBottomRight.Background = Brushes.LightPink;
            }
            else if (buttonTopLeft.Content == "O" && buttonMiddle.Content == "O" && buttonBottomRight.Content == "O")
            {
                textBlockGameInfo.Text = playerO + " is the winner!";
                winner = true;
                buttonTopLeft.Background = Brushes.LightPink;
                buttonMiddle.Background = Brushes.LightPink;
                buttonBottomRight.Background = Brushes.LightPink;
            }
            else if (buttonTopRight.Content == "O" && buttonMiddle.Content == "O" && buttonLowerLeft.Content == "O")
            {
                textBlockGameInfo.Text = playerO + " is the winner!";
                winner = true;
                buttonTopRight.Background = Brushes.LightPink;
                buttonMiddle.Background = Brushes.LightPink;
                buttonLowerLeft.Background = Brushes.LightPink;
            }
        }

        private void PlayButton(Button buttonClicked)
        {
            if (buttonClicked.Content == "")
            {
                if (turn == true)
                {
                    textBlockGameInfo.Text = "It is " + playerO + "'s turn";
                    buttonClicked.Content = "X";
                    turn = false;
                }
                else
                {
                    textBlockGameInfo.Text = "It is " + playerX + "'s turn";
                    buttonClicked.Content = "O";
                    turn = true;
                }
            }

        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            if (winner != true)
            {
                var button = sender as Button;

                PlayButton(button);
                IsXWinner();
                IsOWinner();
                IsNoWinner();
            }
        }
    }
}