A created a new method startGame which will be called in the constructor class and in the PlayAgain event.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RockPaperScissors
{
public partial class rockPaperScissors : Form
{
public rockPaperScissors()
{
InitializeComponent();
startGame();
}//End Rock Paper Scissors
private void startGame()
{
int compChoice;
Random randomNumber = new Random();
compChoice = randomNumber.Next(0, 3);
switch (compChoice)
{
case 0:
pbCompChoice.Image = Properties.Resources.rock;
break;
case 1:
pbCompChoice.Image = Properties.Resources.sillouttepaper;
break;
case 2:
pbCompChoice.Image = Properties.Resources.scirrorssilloutte;
break;
}
}
private void pbRock_Click(object sender, EventArgs e)
{
pbYourChoice.Image = Properties.Resources.rock;
PlayGame();
pbCompChoice.Visible = true;
}
private void pbPaper_Click(object sender, EventArgs e)
{
pbYourChoice.Image = Properties.Resources.sillouttepaper;
PlayGame();
pbCompChoice.Visible = true;
}
private void pbScissors_Click(object sender, EventArgs e)
{
pbYourChoice.Image = Properties.Resources.scirrorssilloutte;
PlayGame();
pbCompChoice.Visible = true;
}//End pbRock Click
public void PlayGame()
{
if ((pbYourChoice == pbPaper && pbCompChoice == pbRock) || (pbYourChoice == pbRock && pbCompChoice == pbScissors) || (pbYourChoice == pbScissors && pbCompChoice == pbPaper))
{
lblWin.Visible = true;
}
else if ((pbYourChoice == pbScissors && pbCompChoice == pbPaper) || (pbYourChoice == pbPaper && pbCompChoice == pbScissors) || (pbYourChoice == pbRock && pbCompChoice == pbPaper))
{
lblLose.Visible = true;
}
else
{
lblPush.Visible = true;
}
bttnPlayAgain.Visible = true;
}
private void bttnPlayAgain_Click(object sender, EventArgs e)
{
//pbYourChoice.Visible = false;
//pbCompChoice.Visible = false;
//new Random();
bttnPlayAgain.Visible = false;
startGame();
}
}
}
jdweng
No comments:
Post a Comment