kar
kar

Reputation: 3651

Placing choice of rectangle color and message box in a method

Did a simple scissor paper stone game. When you press a button (3 diff buttons one for each choice scissor/ paper/ stone), it returns if you win, lose or draw. It also colors the choice(rectangle) which human and computer chose plus a message to tell if the human won or lost.

The method game.play calculates who won based on its parameter input. 1 is rock, 2 is paper and 3 is scissor. The computer choice is random.

The following code works but find it messy and trying to find a way to place all the coloring in one method and all messaging in one method. Quite confused considering there can be so many options for the coloring and messaging. Following code is for the choice of paper made by human. Do advice. Thank you.

private void button2_Paper(object sender, RoutedEventArgs e) // Human chose Paper
        {
            if (game.play(2) == "Draw")
            {
                rect2.Fill = red;
                MessageBox.Show("It is a draw. both chose Paper");
            }
            else if (game.play(2) == "Win")
            {
                rect2.Fill = green;
                rect1.Fill = yellow;
                MessageBox.Show("Congratulations! Paper beats Rock");
            }
            else if (game.play(2) == "Lose")
            {
                rect2.Fill = green;
                rect3.Fill = yellow;
                MessageBox.Show("You lose. Scissor beats Paper");
            }
            gameReset();
        }  

Upvotes: 0

Views: 109

Answers (1)

ChrisK
ChrisK

Reputation: 1218

If you want to clean up your code, you should try to search for things that look like they were copy and pasted. I assume that you have a similar method to the one you posted here for "Rock" and "Scissor".

In that case you could restructure your Game class to hold a temporary game result, that indicates who has won. Also, you could replace your "string results" with enums.

Here is a small class to illustrate what I mean. Note that I haven't tested this class, so it might return incorrect results

namespace RockPaperScissor.GameEngine
{
    public enum Move
    {
        Rock = 0,
        Paper = 1,
        Scissor = 2
    }

    public enum MoveResult
    {
        PlayerWon,
        ComputerWon,
        Draw
    }

    public class RPSEngine
    {
        public Move ComputerMove { get; set; }
        public Move PlayerMove { get; set; }

        public MoveResult Result
        {
            get
            {
                if(ComputerMove == PlayerMove)
                {
                    return MoveResult.Draw;
                }
                else if(ComputerMove > PlayerMove || (ComputerMove == Move.Rock && PlayerMove == Move.Scissor))
                {
                    return MoveResult.ComputerWon;
                }
                else
                {
                    return MoveResult.PlayerWon;
                }
            }
        }
    }
}

This would allow you to update the colors in a central method, like in this example:

private void RockButton_Click(object sender, RoutedEventArgs e)
{
    engine.PlayerMove = GameEngine.Move.Rock;
    displayResult();
}

private void PaperButton_Click(object sender, RoutedEventArgs e)
{
    engine.PlayerMove = GameEngine.Move.Paper;
    displayResult();
}

private void ScissorButton_Click(object sender, RoutedEventArgs e)
{
    engine.PlayerMove = GameEngine.Move.Scissor;
    displayResult();
}

private void displayResult()
{
    switch(engine.Result)
    {
        case GameEngine.MoveResult.Draw:
            //Display message and change color
            break;

        case GameEngine.MoveResult.PlayerWon:

            break;

        case GameEngine.MoveResult.ComputerWon:

            break;
    }
}

Since you tagged your question as XAML, you could also create something like a ViewModel, that stores the color and a string of the message to display. The NotifyPropertyChanged would then cause your GUI to update.

Upvotes: 0

Related Questions