Awesome tutorial thanks Arek +rep
- Codecall
- → Viewing Profile: Reputation: Siten0308
Check out our Community Blogs
Community Stats
- Group Advanced Member
- Active Posts 297
- Profile Views 5352
- Member Title CC Addict
- Age 34 years old
- Birthday September 16, 1984
-
Gender
Not Telling
-
Biography
Network Engineer, inspired to become Software Developer, father to be and husband
-
Location
California, USA
-
Interests
Programming, WoW, CoD4
-
Occupation
Network Engineer
#503983 Writing SQL commands in C# - Part 1 - INSERT into table
Posted by Siten0308
on 29 August 2009 - 08:39 PM
#458860 Rock Paper Scissors!!
Posted by Siten0308
on 06 May 2009 - 10:37 AM
Hello Everyone,
Got a request from my mentor to post this, so I am, and i think everyone could benefit from this since it has a lot of cool tricks to it such as using the list, soundclip class, picture boxes, and a lot of if statements:
here I just getting my fields ready, I need string and int for each to display some numbers and show a message to put into a list, but this will come together later on
first method is for the computer choice, i had the computer choose a random number between 0-3 using Randomnumber.Next(), and if computer chooses 0, then do this from the result method, sorry if its a little confusing having a method inside a method or not good practice, just let me know and I can fix it up.
here is the results method:
Last thing in the results method is you can see that the last 3 if statements decide who wins and who lost, including if its a tie (in this game you can tie).
I hope this is a great learning experience to anyone, the only thing that I didnt go over in detail was the sound, you wouold just use the Soudplayer class which you can see above and how to embed the code you can easily google it to find the answer however if you wish to get the info please let me know and I can forward you screen shots on how to embed a picture/audio file. Also for everyones information because i didnt know this either, when you embed or use any audio file, only WAV files only, no mp3 or avi, it wont work. Thank you for reading and enjoy the game, or as i like to say "GAME ON!!"
Got a request from my mentor to post this, so I am, and i think everyone could benefit from this since it has a lot of cool tricks to it such as using the list, soundclip class, picture boxes, and a lot of if statements:
here I just getting my fields ready, I need string and int for each to display some numbers and show a message to put into a list, but this will come together later on
public string playerchoice { get; set; } public string computerchoice { get; set; } public string winnerresults { get; set; } public string collectdata { get; set; } public int playerscore = 0; public string playerScore; public int computerscore = 0; public string computerScore; System.Drawing.Bitmap paper = rockpaperscissors.Properties.Resources.paper; System.Drawing.Bitmap rock = rockpaperscissors.Properties.Resources.rock; System.Drawing.Bitmap scissors = rockpaperscissors.Properties.Resources.scissors; List<string> ListResults = new List<string>();
first method is for the computer choice, i had the computer choose a random number between 0-3 using Randomnumber.Next(), and if computer chooses 0, then do this from the result method, sorry if its a little confusing having a method inside a method or not good practice, just let me know and I can fix it up.
public void computerchoicestart() { Random RandomNumber = new Random(); int x = RandomNumber.Next(0, 3); if (x == 0) { piccomputer.Image = new Bitmap(rock); computerchoice = "Rock"; } if (x == 1) { piccomputer.Image = new Bitmap(paper); computerchoice = "Paper"; } if (x == 2) { piccomputer.Image = new Bitmap(scissors); computerchoice = "Scissors"; } results(); }
here is the results method:
public void results() { if (computerchoice == "Rock" && playerchoice == "Rock" || computerchoice == "Scissors" && playerchoice == "Scissors" || computerchoice == "Paper" && playerchoice == "Paper") { MessageBox.Show("Tie game, this wont count, do again : P"); winnerresults = "Tie"; picplayer.Image = null; piccomputer.Image = null; } if (computerchoice == "Paper" && playerchoice == "Rock" || computerchoice == "Scissors" && playerchoice == "Paper" || computerchoice == "Rock" && playerchoice == "Scissors") { MessageBox.Show("Computer Wins"); winnerresults = "Computer"; picplayer.Image = null; piccomputer.Image = null; computerscore++; computerScore = Convert.ToString(computerscore); txt_computer.Text = computerScore; } if (playerchoice == "Paper" && computerchoice == "Rock" || playerchoice == "Scissors" && computerchoice == "Paper" || playerchoice == "Rock" && computerchoice == "Scissors") { MessageBox.Show("You Win"); winnerresults = "Player"; picplayer.Image = null; piccomputer.Image = null; playerscore++; playerScore = Convert.ToString(playerscore); txt_player.Text = playerScore; } int i = 3; ListViewItem LVI = new ListViewItem(); LVI.Text = playerchoice; LVI.SubItems.Add(computerchoice); LVI.SubItems.Add(winnerresults); lstscore.Items.Add(LVI); if (LVI.Index > i) { if (playerscore > computerscore) { SoundPlayer cheer = new SoundPlayer(rockpaperscissors.Resources.Resource1.crowdapplause); cheer.Play(); MessageBox.Show("Player Wins: )\nPlayer Score: " + playerscore + "\nComputer Score: " + computerscore); } if (playerscore < computerscore) { SoundPlayer boo = new SoundPlayer(rockpaperscissors.Resources.Resource1.boo3); boo.Play(); MessageBox.Show("Computer Wins, YOU LOSER!! \nPlayer Score: " + playerscore + "\nComputer Score: " + computerscore); } if (playerscore == computerscore) { MessageBox.Show("Tie Game : P do it again"); } lstscore.Items.Clear(); playerscore = 0; txt_player.Text = "0"; computerscore = 0; txt_computer.Text = "0"; } }
Last thing in the results method is you can see that the last 3 if statements decide who wins and who lost, including if its a tie (in this game you can tie).
I hope this is a great learning experience to anyone, the only thing that I didnt go over in detail was the sound, you wouold just use the Soudplayer class which you can see above and how to embed the code you can easily google it to find the answer however if you wish to get the info please let me know and I can forward you screen shots on how to embed a picture/audio file. Also for everyones information because i didnt know this either, when you embed or use any audio file, only WAV files only, no mp3 or avi, it wont work. Thank you for reading and enjoy the game, or as i like to say "GAME ON!!"
Attached Files
-
rockpaperscissors.zip 887.33KB 1434 downloads
- 4
#367237 using goto/if else and user input in C#
Posted by Siten0308
on 24 July 2008 - 02:04 PM
Hello,
Please bare with me as this is my first little tutorial and first time explaining it, please XAV or Jordan or whoever else if i dont make sense or mistaken something, please correct, this is also great to learn any additional info I have missed.
Ok so here is the program, the purpose of this is to ask the user for input then it shoots back out user input but also asks if you want to repeat the program without restarting the program. So first is variable and variable type, string and the strings are inputfirst = first name which asks you: Console.WriteLine("Hello what is your First name?");
inputfirst = Console.ReadLine();
The second user input is the inputlast = user last name = inputlast = Console.ReadLine();
Console.WriteLine("Hello " + inputfirst + inputlast);
but also you see another string called "choice" which happens to be between the word decide: but also you see another word at the beginning called begin: notice it does not have a ; but a : which i hope jordan or Xav or someone can explain in detail but i would say just think of the decide/begin as a bookmarks in a book to reference or go back to your place to re-read etc. so moving on after it tells your first and last name it then asks you a choice which here comes the if and else statements, if choice (<-- string declared earlier) is equal (==) to the word "continue" or someone types it in as to what they want, then perform whatever is inside {} which is but if the user types quit then do what it says in {}, the last thing is what ever is the user inputs then do this. Now the goto as i said is like book marks and it has a part to play in the if/if/else, so if this happens, the goto (self explanatory) this book mark of the book (or program) but make sure you have the : instead of ; which i was having trouble with in the beginning. The last thing is System.Threading.Thread.Sleep(3000); which is thanks to Xav for telling me that means just end the program in .... uhh... is that seconds or miliseconds? .. Well i hope this helps out
Please bare with me as this is my first little tutorial and first time explaining it, please XAV or Jordan or whoever else if i dont make sense or mistaken something, please correct, this is also great to learn any additional info I have missed.
namespace ConsoleApplication1 { class Program { static void Main() { string inputfirst = ""; string inputlast = ""; string choice = ""; begin: Console.WriteLine("Hello what is your First name?"); inputfirst = Console.ReadLine(); Console.WriteLine("and what is your last name?"); inputlast = Console.ReadLine(); Console.WriteLine("Hello " + inputfirst + inputlast); decide: Console.WriteLine("what would you like to do? continue or quit?"); choice = Console.ReadLine(); if (choice == "continue") { goto begin; } if (choice == "quit") { Console.WriteLine("Good Bye"); System.Threading.Thread.Sleep(3000); } else { Console.WriteLine("please enter continue or quit"); goto decide; } } } }
Ok so here is the program, the purpose of this is to ask the user for input then it shoots back out user input but also asks if you want to repeat the program without restarting the program. So first is variable and variable type, string and the strings are inputfirst = first name which asks you: Console.WriteLine("Hello what is your First name?");
inputfirst = Console.ReadLine();
The second user input is the inputlast = user last name = inputlast = Console.ReadLine();
Console.WriteLine("Hello " + inputfirst + inputlast);
but also you see another string called "choice" which happens to be between the word decide: but also you see another word at the beginning called begin: notice it does not have a ; but a : which i hope jordan or Xav or someone can explain in detail but i would say just think of the decide/begin as a bookmarks in a book to reference or go back to your place to re-read etc. so moving on after it tells your first and last name it then asks you a choice which here comes the if and else statements, if choice (<-- string declared earlier) is equal (==) to the word "continue" or someone types it in as to what they want, then perform whatever is inside {} which is but if the user types quit then do what it says in {}, the last thing is what ever is the user inputs then do this. Now the goto as i said is like book marks and it has a part to play in the if/if/else, so if this happens, the goto (self explanatory) this book mark of the book (or program) but make sure you have the : instead of ; which i was having trouble with in the beginning. The last thing is System.Threading.Thread.Sleep(3000); which is thanks to Xav for telling me that means just end the program in .... uhh... is that seconds or miliseconds? .. Well i hope this helps out
- 1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download
- Codecall
- → Viewing Profile: Reputation: Siten0308
- Privacy Policy
- Guidelines ·