Advertisement
ada1711

l13 ox basic

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