Thread: Tic Tac Toe
View Single Post
Old Mar 25th, 2007, 3:36 PM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
The code has a lot of repetition in that could be factored out. Instead of writing the same code for each label, why not just have all the labels link to the same function, and use the "sender" object to get the right label:
csharp Syntax (Toggle Plain Text)
  1. void Label6Click(object sender, EventArgs e)
  2. {
  3. Label label = (Label)sender;
  4.  
  5. if(player1 == true)
  6. {
  7. label.Text = "X";
  8. player1 = false;
  9. label.Enabled = false;
  10. checkForWinner();
  11. if(player1Win == true)
  12. {
  13. statusLable.Text = "Player 1 Wins";
  14. textBox1.Text = "Player 1 Wins";
  15. }
  16. else
  17. {
  18. statusLable.Text = "Player 2";
  19. textBox1.Text = "Player 2";
  20. }
  21. }
  22. else
  23. {
  24. label.Text = "O";
  25. player1 = true;
  26. label.Enabled = false;
  27. checkForWinner();
  28. if(player2Win == true)
  29. {
  30. statusLable.Text = "Player 2 Wins";
  31. textBox1.Text = "Player 2 Wins";
  32. }
  33. else
  34. {
  35. statusLable.Text = "Player 1";
  36. textBox1.Text = "Player 1";
  37. }
  38. }
  39. }
There are a few other things you could do to cut the amount of code down even further.
Arevos is offline   Reply With Quote