Advertisement
ada1711

l13 ox extension

Jul 18th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. public partial class Form1 : Form
  2. {
  3.  
  4. bool whosTurn = true; //true - X, false - O
  5. int stalemateCounter = 0;
  6.  
  7.  
  8. public Form1()
  9. {
  10. InitializeComponent();
  11. lblWhosTorn.Text = "X";
  12. }
  13.  
  14. private void InsertSymbol(object button)
  15. {
  16. Button pressedButton = (Button)button;
  17.  
  18. if (whosTurn)
  19. {
  20. pressedButton.Text = "X";
  21. }
  22. else
  23. {
  24. pressedButton.Text = "O";
  25. }
  26.  
  27. pressedButton.Enabled = false;
  28.  
  29. bool wynik = checkIfSomeoneWon();
  30.  
  31.  
  32. if (wynik == true)
  33. {
  34. string winText;
  35. if (whosTurn == true)
  36. {
  37. winText = "Player X won! Replay?";
  38. }
  39. else
  40.  
  41. {
  42.  
  43. winText = "Player 0 won! Replay?";
  44. }
  45.  
  46. //We check if players want to replay the match.
  47. DialogResult answer = MessageBox.Show(winText, "Win", MessageBoxButtons.YesNo);
  48.  
  49. if (answer == DialogResult.Yes)
  50. {
  51. TurnOnAllButtonsAndReset();
  52. return;
  53. }
  54. else
  55. {
  56. Close();
  57. }
  58. }
  59.  
  60. // We check stalemate
  61. if (stalemateCounter == 9)
  62. {
  63.  
  64. DialogResult answer = MessageBox.Show("Stalemate, replay?", "Stalemate", MessageBoxButtons.YesNo);
  65.  
  66. if (answer == DialogResult.Yes)
  67. {
  68. TurnOnAllButtonsAndReset();
  69. return;
  70. }
  71. else
  72. {
  73. Close();
  74. }
  75. }
  76.  
  77.  
  78. whosTurn = !whosTurn; //player switch
  79. if (whosTurn == true)
  80. {
  81. lblWhosTorn.Text = "X";
  82. }
  83. else
  84. {
  85. lblWhosTorn.Text = "O";
  86. }
  87.  
  88. }
  89.  
  90. private void TurnOnAllButtonsAndReset()
  91. {
  92.  
  93.  
  94. btn1.Enabled = true;
  95. btn2.Enabled = true;
  96. btn3.Enabled = true;
  97. btn4.Enabled = true;
  98. btn5.Enabled = true;
  99. btn6.Enabled = true;
  100. btn7.Enabled = true;
  101. btn8.Enabled = true;
  102. btn9.Enabled = true;
  103. btn1.Text = "";
  104. btn2.Text = "";
  105. btn3.Text = "";
  106. btn4.Text = "";
  107. btn5.Text = "";
  108. btn6.Text = "";
  109. btn7.Text = "";
  110. btn8.Text = "";
  111. btn9.Text = "";
  112. stalemateCounter = 0;
  113. whosTurn = true;
  114. lblWhosTorn.Text = "X";
  115. }
  116.  
  117.  
  118. private bool checkIfSomeoneWon()
  119. {
  120.  
  121. if (btn1.Text == btn2.Text && btn2.Text == btn3.Text && btn1.Text != "")
  122.  
  123. {
  124. return true;
  125. }
  126.  
  127. else if (btn4.Text == btn5.Text && btn5.Text == btn6.Text && btn4.Text != "")
  128. {
  129. return true;
  130. }
  131.  
  132. else if (btn7.Text == btn8.Text && btn8.Text == btn9.Text && btn7.Text != "")
  133. {
  134. return true;
  135. }
  136.  
  137. else if (btn1.Text == btn4.Text && btn4.Text == btn7.Text && btn1.Text != "")
  138. {
  139. return true;
  140. }
  141.  
  142. else if (btn2.Text == btn5.Text && btn5.Text == btn8.Text && btn2.Text != "")
  143. {
  144. return true;
  145. }
  146.  
  147. else if (btn3.Text == btn6.Text && btn6.Text == btn9.Text && btn3.Text != "")
  148. {
  149. return true;
  150. }
  151. else if (btn1.Text == btn5.Text && btn5.Text == btn9.Text && btn1.Text != "")
  152. {
  153. return true;
  154. }
  155.  
  156. else if (btn3.Text == btn5.Text && btn5.Text == btn7.Text && btn3.Text != "")
  157. {
  158. return true;
  159. }
  160. else
  161. {
  162. stalemateCounter++;
  163. return false;
  164. }
  165. }
  166.  
  167. private void btn_click(object sender, EventArgs e)
  168. {
  169. InsertSymbol(sender);
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement