You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
4.8 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. public static class MineSweeperConstants
  6. {
  7. private static Texture2D Cell_Button;
  8. private static Texture2D Cell_Clicked;
  9. private static GUIStyle Button_Style;
  10. /// <summary>
  11. /// Height and Width of a Cell in Pixels
  12. /// </summary>
  13. public static int CELL_SIZE { get { return 20; } }
  14. /// <summary>
  15. /// Texture of Cell when it hasn't been clicked
  16. /// </summary>
  17. public static Texture2D CELL_BUTTON
  18. {
  19. get
  20. {
  21. if (Cell_Button == null)
  22. {
  23. Cell_Button = CreateButtonTexture(Color.white, new Color(0.75f, 0.75f, 0.75f), Color.black);
  24. Debug.Log("Generating CELL_BUTTON");
  25. }
  26. return Cell_Button;
  27. }
  28. }
  29. /// <summary>
  30. /// Texture of Cell when it has been clicked
  31. /// </summary>
  32. public static Texture2D CELL_CLICKED
  33. {
  34. get
  35. {
  36. if (Cell_Clicked == null)
  37. {
  38. Cell_Clicked = CreateButtonTexture(new Color(0.2f, 0.2f, 0.2f), new Color(0.75f, 0.75f, 0.75f), new Color(0.8f, 0.8f, 0.8f));
  39. Debug.Log("Generating CELL_CLicked");
  40. }
  41. return Cell_Clicked;
  42. }
  43. }
  44. /// <summary>
  45. /// Style to use with a Cell_Button
  46. /// </summary>
  47. public static GUIStyle STYLE_CELL_BUTTON
  48. {
  49. get
  50. {
  51. if (Button_Style == null)
  52. {
  53. Debug.Log("Creating Cell Style");
  54. Button_Style = new GUIStyle();
  55. Button_Style.alignment = TextAnchor.MiddleCenter;
  56. Button_Style.normal.background = CELL_BUTTON;
  57. }
  58. return Button_Style;
  59. }
  60. }
  61. private static Texture2D CreateButtonTexture(Color highLight, Color mainColor, Color lowLight)
  62. {
  63. Texture2D retval = new Texture2D(CELL_SIZE, CELL_SIZE);
  64. for (int i = 0; i < CELL_SIZE; i++)
  65. {
  66. for (int j = 0; j < CELL_SIZE; j++)
  67. {
  68. if (i == 0 || j == CELL_SIZE - 1)
  69. {
  70. retval.SetPixel(i, j, highLight);
  71. }
  72. else if (i == CELL_SIZE - 1 || j == 0)
  73. {
  74. retval.SetPixel(i, j, lowLight);
  75. }
  76. else
  77. retval.SetPixel(i, j, mainColor);
  78. }
  79. }
  80. retval.Apply();
  81. return retval;
  82. }
  83. }
  84. public class Board
  85. {
  86. private Cell[,] Cells;
  87. private int MineCount = 10;
  88. public Board(Vector2Int Size)
  89. {
  90. Cells = new Cell[Size.x, Size.y];
  91. for (int i = 0; i < Size.x; i++)
  92. for (int j = 0; j < Size.y; j++)
  93. Cells[i, j] = new Cell(this, new Vector2Int(i, j));
  94. PlaceMines(new Vector2Int(0, 0));
  95. }
  96. public void OnGUI()
  97. {
  98. foreach (Cell cell in Cells)
  99. cell.DrawCell();
  100. }
  101. public void OnClick()
  102. {
  103. Event e = Event.current;
  104. if (e.type == EventType.MouseUp)
  105. {
  106. foreach (Cell cell in Cells)
  107. cell.OnClick(e.button, e.mousePosition);
  108. }
  109. }
  110. public Cell[] AdjacentCells(Vector2Int coords)
  111. {
  112. List<Cell> retVal = new List<Cell>();
  113. for (int i = -1; i <= 1; i++)
  114. {
  115. if (coords.x + i < 0 || coords.x + i >= Cells.GetLength(0))
  116. continue;
  117. for (int j = -1; j <= 1; j++)
  118. {
  119. if (coords.y + j < 0 || coords.y + j >= Cells.GetLength(0))
  120. continue;
  121. if (i == 0 && j == 0)
  122. continue;
  123. retVal.Add(Cells[coords.x + i, coords.y + j]);
  124. }
  125. }
  126. return retVal.ToArray();
  127. }
  128. public void PlaceMines(Vector2Int startPos)
  129. {
  130. int placedMines = 0;
  131. while (placedMines < MineCount)
  132. {
  133. int x = Random.Range(0, Cells.GetLength(0) - 1);
  134. int y = Random.Range(0, Cells.GetLength(1) - 1);
  135. if (!Cells[x, y].containsMine || startPos != new Vector2Int(x,y))
  136. {
  137. Cells[x, y].containsMine = true;
  138. Debug.Log("Mine at: (" + x + ", " + y + ")");
  139. placedMines++;
  140. }
  141. }
  142. }
  143. }
  144. public struct Vector2Int
  145. {
  146. public int x;
  147. public int y;
  148. public Vector2Int(int x, int y)
  149. {
  150. this.x = x;
  151. this.y = y;
  152. }
  153. public override bool Equals(System.Object obj)
  154. {
  155. return obj is Vector2Int && this == (Vector2Int)obj;
  156. }
  157. public override int GetHashCode()
  158. {
  159. return x.GetHashCode() ^ y.GetHashCode();
  160. }
  161. public static bool operator ==(Vector2Int x, Vector2Int y)
  162. {
  163. return x.x == y.x && x.y == y.y;
  164. }
  165. public static bool operator !=(Vector2Int x, Vector2Int y)
  166. {
  167. return !(x == y);
  168. }
  169. }