2015-10-14 81 views
0

我正在做一个任务的井字游戏,而且我想让游戏看起来不太可行。我想其他人已经问过这个问题,但是我在任何在线答案中看到的所有代码都比我想要做的更先进(或者似乎是这样)。根据点击按钮更改按钮内容

目前,我为所有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(); 
    } 
} 

}

+0

你为什么不改变按钮的文本,或者添加一个图像呢? – Val

+1

正试图做到这一点 - ((Button)sender).Text = turn? “O”:“X”; –

+0

这是WinForms还是WPF? – Lorek

回答

0

你一定要投在方法来接收发送者作为Button。方法button_Click中的发件人将是玩家实际点击的按钮。

一旦作为一个按钮被铸造,您可以根据转弯设置其内容。你的回合方法可能会返回要分配的角色。东西有点像这样的工作:

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

button.Content = IsTurn(); 
+0

“sender as Button”意味着/怎么办?我是这个东西的新品,它绝对显示:/你写的最后一行,“button.Content = IsTurn();”引用我的IsTurn方法。当我不知道要在方法本身中放置什么内容来设置内容时,如何引用即将更改按钮的内容? – Audie

+0

'sender as button'部分会将'object'投射到'Button'。您将可以访问该按钮的属性。为了使用你的'IsTurn()'方法,我建议你重新格式化它以返回想要的字符串而不是'void'。 – KOTIX

0

什么你要找的是Button.Text财产。

当玩家1次点击该按钮,你会想要做

button.Text = "O"; 

,当玩家点击2次按键:

button.Text = "X"; 
+0

按钮没有“文本”属性... – KOTIX

+0

@KOTIX是的,他们这样做。我假设这是一个WinForms应用程序。如果是,则有一个文本属性,https://msdn.microsoft.com/en-us/library/system。windows.forms.button_properties(v = vs.110).aspx – GeMaths

+0

@GeMaths如果不是,那么你可以使用内容 – Hristo

0

试图跟随@KOTIX的建议,我得到了我的比赛的工作,但我有点困惑,按钮的情况如何工作本身出不动。我想我只需要回头审查一些事情。

这是我的整个tic tac toe游戏的最终代码。查看最后2种方法来查看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(); 
      } 
     } 
    } 
} 
+0

如果我的答案帮助你达到目标,我建议你将它标记为答案。谢谢 ! – KOTIX

+0

谢谢你的帮助! – Audie