Question windows multi form (Scissors, Stone and Paper-Game)

Dreamer

New member
Joined
Mar 26, 2017
Messages
3
Programming Experience
Beginner
I need to make three different forms to load :
1- a form when the results are tie,
2-a form when the computer wins,
3-a form when the user wins,
I am trying now for one form which is; when the results are tie.
However, it does not work tho i used a while loop for -checkresult();- method and I break it when the scores reach 20 .
Can anyone please help me?!
source code:

private void btnPlay_Click(object sender, EventArgs e)
{
Random r = new Random(); //create a new random object r
compChoice = r.Next(3) + 1; //use r to pick a number from 1 to 3
if (compChoice == 1)
{
pbxComputer.Image = Image.FromFile("Scissors.jpg");
}

else if (compChoice == 2)
{
pbxComputer.Image = Image.FromFile("Paper.jpg");

}
else if (compChoice == 3)
{
pbxComputer.Image = Image.FromFile("Stone.jpg");
}

while (userScore != 20 || compScore != 20)
{
checkResult(); // call the checkResult(); method
if (compScore == 20 || userScore == 20)
{
break;
}
}
finish();
}
private void checkResult()
{
lblResult.BackColor = Color.White;
lblUserScore.BackColor = Color.White;
lblComputerScore.BackColor = Color.White;

if (compChoice == userChoice)
{
lblResult.Text = "It's a DRAW!";
compScore += 1;
userScore += 1;
lblResult.ForeColor = Color.Black;
}
else if (compChoice == 1 && userChoice == 2) //scissors / paper
{
lblResult.Text = "Computer WINS!";
compScore += 2;
lblResult.ForeColor = Color.Black;
}
else if (compChoice == 1 && userChoice == 3) //scissors / stone
{
lblResult.Text = "You WIN!";
userScore += 2;
lblResult.BackColor = Color.Red;
lblResult.ForeColor = Color.Yellow;
}
else if (compChoice == 2 && userChoice == 1) //paper / scissors
{
lblResult.Text = "You WIN!";
userScore += 2;
lblResult.BackColor = Color.Red;
lblResult.ForeColor = Color.Yellow;
}
else if (compChoice == 2 && userChoice == 3) //paper / stone
{
lblResult.Text = "Computer WINS!";
compScore += 2;
lblResult.ForeColor = Color.Black;
}
else if (compChoice == 3 && userChoice == 1) //stone /scissors
{
lblResult.Text = "Computer WINs!";
compScore += 2;
lblResult.ForeColor = Color.Black;
}
else if (compChoice == 3 && userChoice == 2) //stone / paper
{
lblResult.Text = "You WIN!";
userScore += 2;
lblResult.BackColor = Color.Red;
lblResult.ForeColor = Color.Yellow;
}
lblComputerScore.Text = "Computer Score \n" + compScore.ToString();
lblUserScore.Text = "User Score \n" + userScore.ToString();
}
private void finish()
{
if(compScore == userScore)
{
//load the Draw form
frmDraw draw = new frmDraw();
draw.ShowDialog();
}
}
 
Firstly, you have made your code very hard to read. Please post code as plain text inside appropriate formatting tags, i.e.

[xcode=c#]your code here[/xcode]

Until we can read your code, it's hard to say exactly what you should be changing but I can't see how a loop would be appropriate. You appear to be performing one turn when you click a Button so you should once each time that happens.
 
screen shot of the (Scissors, Stone, Paper - Game) C# Windows Form Application.

Firstly, you have made your code very hard to read. Please post code as plain text inside appropriate formatting tags, i.e.[xcode=c#]your code here[/xcode]Until we can read your code, it's hard to say exactly what you should be changing but I can't see how a loop would be appropriate. You appear to be performing one turn when you click a Button so you should once each time that happens.
I attached a screenshots of the code , i hope it will be clear to give me some advice on it.
Many thanks.
 

Attachments

  • Screenshot (670).png
    Screenshot (670).png
    95.7 KB · Views: 52
  • Screenshot (672).png
    Screenshot (672).png
    101.3 KB · Views: 45
  • Screenshot (673).png
    Screenshot (673).png
    104 KB · Views: 49
  • Screenshot (674).png
    Screenshot (674).png
    104.2 KB · Views: 50
Last edited:
When I asked that you post your code as plain text in formatting tags, I actually meant post your code as plain text in formatting tags, not as screen shots. If we need to test your code, we can't copy and paste it from a screen shot.
 
When I asked that you post your code as plain text in formatting tags, I actually meant post your code as plain text in formatting tags, not as screen shots. If we need to test your code, we can't copy and paste it from a screen shot.
        int userChoice, compChoice;
        int userScore = 0;
        int compScore = 0;
    
        public frmSPSGame()
        {
            InitializeComponent();
        }


        private void rbnScissors_CheckedChanged(object sender, EventArgs e)
        {
            pbxUser.Image = Image.FromFile("Scissors.jpg");
            userChoice = 1;
        }


        private void rbnPaper_CheckedChanged(object sender, EventArgs e)
        {
            pbxUser.Image = Image.FromFile("Paper.jpg");
            userChoice = 2;
        }


        private void rbnStone_CheckedChanged(object sender, EventArgs e)
        {
            pbxUser.Image = Image.FromFile("Stone.jpg");
            userChoice = 3;
        }


        private void btnPlay_Click(object sender, EventArgs e)
        {
            Random r = new Random(); //create a new random object r
            compChoice = r.Next(3) + 1; //use r to pick a number from 1 to 3
            if (compChoice == 1)
            {
                pbxComputer.Image = Image.FromFile("Scissors.jpg");
            }


            else if (compChoice == 2)
            {
                pbxComputer.Image = Image.FromFile("Paper.jpg");


            }
            else if (compChoice == 3)
            {
                pbxComputer.Image = Image.FromFile("Stone.jpg");
            }
            while(compScore != 20 || userScore != 20 ) //meaning call the checkresult(); method
                //as long as the scores didn't reach 20
            {
                checkResult(); // call the checkResult(); method
                if(compScore == 20 || userScore == 20) //however, if the score reaches 20 
                    //call the finish(); method, and break from the loop 
                {
                    finish();
                    break;
                }
            }
               
              
        }
        private void checkResult()
        {
            lblResult.BackColor = Color.White;
            lblUserScore.BackColor = Color.White;
            lblComputerScore.BackColor = Color.White;
           
                if (compChoice == userChoice)
                {
                    lblResult.Text = "It's a DRAW!";
                    compScore += 1;
                    userScore += 1;
                    lblResult.ForeColor = Color.Black;
                }
                else if (compChoice == 1 && userChoice == 2) //scissors / paper
                {
                    lblResult.Text = "Computer WINS!";
                    compScore += 2;
                    lblResult.ForeColor = Color.Black;
                }
                else if (compChoice == 1 && userChoice == 3) //scissors / stone
                {
                    lblResult.Text = "You WIN!";
                    userScore += 2;
                    lblResult.BackColor = Color.Red;
                    lblResult.ForeColor = Color.Yellow;
                }
                else if (compChoice == 2 && userChoice == 1) //paper / scissors
                {
                    lblResult.Text = "You WIN!";
                    userScore += 2;
                    lblResult.BackColor = Color.Red;
                    lblResult.ForeColor = Color.Yellow;
                }
                else if (compChoice == 2 && userChoice == 3) //paper / stone
                {
                    lblResult.Text = "Computer WINS!";
                    compScore += 2;
                    lblResult.ForeColor = Color.Black;
                }
                else if (compChoice == 3 && userChoice == 1) //stone /scissors
                {
                    lblResult.Text = "Computer WINs!";
                    compScore += 2;
                    lblResult.ForeColor = Color.Black;
                }
                else if (compChoice == 3 && userChoice == 2) //stone / paper
                {
                    lblResult.Text = "You WIN!";
                    userScore += 2;
                    lblResult.BackColor = Color.Red;
                    lblResult.ForeColor = Color.Yellow;
                }
                lblComputerScore.Text = "Computer Score \n" + compScore.ToString();
                lblUserScore.Text = "User Score \n" + userScore.ToString();


        }
       private void finish()
        {
            if(compScore == userScore)
            {
                //load the Draw form
                frmDraw draw = new frmDraw();
                draw.ShowDialog();
            }
        }


        private void lblResult_Click(object sender, EventArgs e)
        {


        }


        private void frmSPSGame_Load(object sender, EventArgs e)
        {
            frmSplash SScreen = new frmSplash();
            SScreen.ShowDialog();
            pbxUser.Image = Image.FromFile("sps1.jpg");
            pbxComputer.Image = Image.FromFile("sps3.jpg");
        }


        private void btnQuit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
 
Back
Top Bottom