For those who don't want to manually do it, the source can be downloaded here.
First, change the form size to 528, 442.
Then, add 3 buttons. Size: 136, 93. Font: Microsoft Sans Serif, size 10, bold.
Make the buttons say "Rock", "Paper", and "Scissors".
Add a textbox, make it multiline, and change the size to 388, 181. Make it readonly. I changed the BackColor to ButtonHighlight so it will still be white as readonly.
Here is what my GUI looks like; please note your's can look different if you wish, and none of the above is required.
First, let's make our Variables. Click the "Rock" button, and above the event, type/paste the following:
string prps = "", airps = ""; // player's rockpaperscissors, AI's rockpaperscissors Random uir = new Random(); // UI's randomizer
prps is the variable we will use to store our choice of rock paper or scissors.
aiprs is the variable we will use to store the AI's choice of rock paper or scissors.
air is the randomizer we will use to determine what the AI's pick will be.
Now, inside of the "Rock" button's click event, type/paste the following:
prps = "Rock";
if (textBox1.Text.Equals(""))
textBox1.AppendText("Player Chose: " + prps);
else
textBox1.AppendText("\r\nPlayer chose: " + prps);
AI();
CheckForWinner();
Alright, the first thing that happens here is we are setting prps to Rock, what you selected.
Secondly, we're seeing if the textbox is empty. This if/else is completely due to my OCD about everything have to be neat. Basically, the if/else if saying if the textbox is empty, then don't make a newline (the first press), otherwise, add a newline.
Then it calls the method "AI", which determines what the AI chooses. That method will be found below.
After that, it calls the method "CheckForWinner" which, obviously, just checks who won. This method will also be found below.
Do this for each button, except, for the "Paper" button, change prps = "Rock"; to prps = "Paper";, and for the "Scissors" button, change it to prps = "Scissors";
Now it's time to do the AI. Rock, Paper, Scissors has very basic AI. We call a random number (1-4), and, depending on what the random number was, it assigns a value to airps.
Please type/paste the following AFTER the last button event you do:
public void AI()
{
int which = air.Next(1, 4);
if (which.Equals(1))
airps = "Rock";
if (which.Equals(2))
airps = "Paper";
if (which.Equals(3))
airps = "Scissors";
textBox1.AppendText("\r\nAI Chose: " + airps);
}
We do 1-4 instead of 1-3 because it never seems to choose 3 unless it's 1-4.
air.Next(1, 4); is seeding a random number between 1-4, then we assign a Rock, Paper, or Scissors value to airps. Then, we add "AI Chose: (what the AI chose)" to the textbox.
So now that we have our choice and the AI's choice, it's time to see who wins! I will not deeply explain this next code, though I will explain one line (all lines are the same, just with slightly different values).
public void CheckForWinner()
{
if (prps.Equals("Rock") && airps.Equals("Rock"))
{
textBox1.AppendText("\r\nPlayer tied with AI!");
if (MessageBox.Show("Tie! Would you like to play again?", "Tie!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
this.Close();
}
else if (prps.Equals("Paper") && airps.Equals("Paper"))
{
textBox1.AppendText("\r\nPlayer tied with AI!");
if (MessageBox.Show("Tie! Would you like to play again?", "Tie!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
this.Close();
}
else if (prps.Equals("Scissors") && airps.Equals("Scissors"))
{
textBox1.AppendText("\r\nPlayer tied with AI!");
if (MessageBox.Show("Tie! Would you like to play again?", "Tie!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
this.Close();
}
else if (prps.Equals("Rock") && airps.Equals("Scissors"))
{
textBox1.AppendText("\r\nPlayer beat AI!");
if (MessageBox.Show("Player Won! Would you like to play again?", "Player Won!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
this.Close();
}
else if (prps.Equals("Paper") && airps.Equals("Rock"))
{
textBox1.AppendText("\r\nPlayer beat AI!");
if (MessageBox.Show("Player won! Would you like to play again?", "Player Won!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
this.Close();
}
else if (prps.Equals("Scissors") && airps.Equals("Paper"))
{
textBox1.AppendText("\r\nPlayer beat AI!");
if (MessageBox.Show("Player won! Would you like to play again?", "Player Won!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
this.Close();
}
else if (prps.Equals("Rock") && airps.Equals("Paper"))
{
textBox1.AppendText("\r\nPlayer lost against AI!");
if (MessageBox.Show("Player lost! Would you like to play again?", "Player Lost!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
this.Close();
}
else if (prps.Equals("Paper") && airps.Equals("Scissors"))
{
textBox1.AppendText("\r\nPlayer lost against AI!");
if (MessageBox.Show("Player lost! Would you like to play again?", "Player Lost!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
this.Close();
}
else if (prps.Equals("Scissors") && airps.Equals("Rock"))
{
textBox1.AppendText("\r\nPlayer lost against AI!");
if (MessageBox.Show("Player lost! Would you like to play again?", "Player Lost!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
this.Close();
}
So, basically, it's seeing what our choice was, what the AI's choice was, and comparing. Then, it tells us if we won, the AI won, or it was a tie.
Then, we ask if we want to play again. If you choose yes, then we just continue, otherwise, it closes.
Thank you for reading, for any additional information about the game, please comment or PM me.




MultiQuote




|