Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Forms;
  8. using WpfApplication1;
  9. using MessageBox = System.Windows.Forms.MessageBox;
  10.  
  11. namespace WpfApplication1
  12. {
  13. public enum Gestures
  14. {
  15. NONE, X, O
  16. }
  17.  
  18. class GameClass
  19. {
  20. private MainWindow m_window = null;
  21. private const int BOARD_SIZE = 3;
  22. private int[,] m_matrix;
  23.  
  24. public GameClass(MainWindow window)
  25. {
  26. m_window = window;
  27.  
  28. m_matrix = new int[BOARD_SIZE, BOARD_SIZE];
  29.  
  30. for (int i = 0; i < BOARD_SIZE; i++)
  31. {
  32. for (int j = 0; j < BOARD_SIZE; j++)
  33. {
  34. m_matrix[i, j] = -1;
  35. }
  36. }
  37. }
  38.  
  39. private void Reset()
  40. {
  41. for (int i = 0; i < BOARD_SIZE; i++)
  42. {
  43. for (int j = 0; j < BOARD_SIZE; j++)
  44. {
  45. m_matrix[i, j] = -1;
  46. }
  47. }
  48. }
  49.  
  50. private bool Set(Gestures gesture, Point position)
  51. {
  52. int x = (int)position.X;
  53. int y = (int)position.Y;
  54.  
  55. if (m_matrix[x, y] == -1)
  56. {
  57. m_matrix[x, y] = (int)gesture;
  58.  
  59. if(gesture == Gestures.X)
  60. {
  61. m_window.drawX(position);
  62. }
  63. else
  64. {
  65. m_window.drawO(position);
  66. }
  67.  
  68. return true;
  69. }
  70. else
  71. {
  72. return false;
  73. }
  74. }
  75.  
  76. private Gestures CheckWon(Gestures gesture)
  77. {
  78. for (int i = 0; i < BOARD_SIZE; i++)
  79. {
  80. if (m_matrix[i, 0] == (int)gesture && m_matrix[i, 1] == (int)gesture && m_matrix[i, 2] == (int)gesture)
  81. {
  82. return gesture;
  83. }
  84. }
  85.  
  86. for (int i = 0; i < BOARD_SIZE; i++)
  87. {
  88. if (m_matrix[0, i] == (int)gesture && m_matrix[1, i] == (int)gesture && m_matrix[2, i] == (int)gesture)
  89. {
  90. return gesture;
  91. }
  92. }
  93.  
  94. if (m_matrix[0, 0] == (int)gesture && m_matrix[1, 1] == (int)gesture && m_matrix[2, 2] == (int)gesture)
  95. {
  96. return gesture;
  97. }
  98.  
  99. if (m_matrix[2, 0] == (int)gesture && m_matrix[1, 1] == (int)gesture && m_matrix[0, 2] == (int)gesture)
  100. {
  101. return gesture;
  102. }
  103.  
  104. return Gestures.NONE;
  105. }
  106.  
  107. private bool CheckLock()
  108. {
  109. for (int i = 0; i < BOARD_SIZE; i++)
  110. {
  111. for (int j = 0; j < BOARD_SIZE; j++)
  112. {
  113. if (m_matrix[i, j] == -1)
  114. {
  115. return false;
  116. }
  117. }
  118. }
  119.  
  120. return true;
  121. }
  122.  
  123. private void PlaceMove(Gestures gesture, Point position)
  124. {
  125. if (Set(gesture, position))
  126. {
  127. if (CheckWon(gesture) == Gestures.X)
  128. {
  129. // Show message mainwindow X won
  130. DialogResult dresult = MessageBox.Show("X won the game! The board will be reset.", "Alert"
  131. , MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
  132. if (dresult == DialogResult.OK)
  133. {
  134. // Do something
  135. Reset();
  136. }
  137. }
  138. else if (CheckWon(gesture) == Gestures.O)
  139. {
  140. // Show message mainwindow O won
  141. DialogResult dresult = MessageBox.Show("Y won the game! The board will be reset.", "Alert"
  142. , MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
  143. if (dresult == DialogResult.OK)
  144. {
  145. // Do something
  146. Reset();
  147. }
  148. }
  149. else
  150. {
  151. if (CheckLock())
  152. {
  153. // Show message mainwindow lock
  154. DialogResult dresult = MessageBox.Show("Nobody wins, a lock is reached! The board will be reset.", "Alert"
  155. , MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
  156. if (dresult == DialogResult.OK)
  157. {
  158. // Do something
  159. Reset();
  160. }
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement