PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Tetr0s/Board.cs

https://gitlab.com/GordonClanProgramming/Tetr0s
C# | 394 lines | 357 code | 26 blank | 11 comment | 38 complexity | 01e0170e0c9ca4c6da5e88f6effaecae MD5 | raw file
  1. //Evan Gordon
  2. /*
  3. * This clas file defines what a point is, as well as containing and managing all of the data related to the game board
  4. */
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.IO;
  11. using System.Windows.Controls;
  12. using System.Windows.Media;
  13. namespace Tetr0s
  14. {
  15. enum Shape{SQUARE, LBLOCK, RLBLOCK, LINE, TBLOCK, ZBLOCK, RZBLOCK};//used for definine which shape a group will take
  16. class Point
  17. {
  18. public int x = -1, y = -1;
  19. public Point()
  20. {
  21. x = -1;
  22. y = -1;
  23. }
  24. public Point(int x, int y)
  25. {
  26. this.x = x;
  27. this.y = y;
  28. }
  29. }
  30. class Board
  31. {
  32. private Tile[,] board = new Tile[10, 18];
  33. private string[,] fileNames = new string[1, 6];
  34. private string path;
  35. private bool gameOver = false;
  36. public bool GameOver
  37. {
  38. get { return gameOver; }
  39. }
  40. private Random rndNum = new Random();
  41. Queue<TileShape> shapeStack = new Queue<TileShape>();// add in logic later
  42. TileShape currentTileShape = new TileShape();
  43. public Board(Canvas c)
  44. {
  45. gameOver = false;
  46. for (int i = 0; i < board.GetLength(0); i++)
  47. {
  48. for (int j = 0; j < board.GetLength(1); j++)
  49. {
  50. board[i, j] = new Tile();
  51. }
  52. }
  53. //this is for showing next elements
  54. //shapeStack.Enqueue(new TileShape());
  55. }
  56. public string setPath(string s)
  57. {
  58. return System.IO.Path.Combine(Environment.CurrentDirectory, s);
  59. }
  60. public string setColor(Shape s)
  61. {
  62. switch (s)
  63. {
  64. case Shape.SQUARE:
  65. return "basicBlockYellow.png";
  66. case Shape.LBLOCK:
  67. return "basicBlockOrange.png";
  68. case Shape.RLBLOCK:
  69. return "basicBlockBlue.png";
  70. case Shape.LINE:
  71. return "basicBlockLightBlue.png";
  72. case Shape.TBLOCK:
  73. return "basicBlockPurple.png";
  74. case Shape.ZBLOCK:
  75. return "basicBlockRed.png";
  76. case Shape.RZBLOCK:
  77. return "basicBlockGreen.png";
  78. }
  79. return "";//maybe have error throw?
  80. }
  81. public void spawn(Canvas c)
  82. {
  83. Shape s = (Shape)rndNum.Next(0, 6); // add back in later
  84. currentTileShape.spawn(s, generateRotation(s), rndNum.Next(0, 10), ref board);
  85. path = setPath(setColor(currentTileShape.CurrShape));
  86. Point[] arr = currentTileShape.getLocationsArray();
  87. foreach (Point po in arr)
  88. {
  89. if (!board[po.x, po.y].IsEmpty)//check to make sure all locations of tileShape are empty
  90. {
  91. gameOver = true;
  92. return;
  93. }
  94. board[po.x, po.y] = new Tile(path, po);//spawn new block
  95. }
  96. foreach (Point po in arr)//this is seperate so that it only shows up if all points are valid
  97. {
  98. c.Children.Add(board[po.x, po.y].getImage());
  99. }
  100. }
  101. public int generateRotation(Shape s)
  102. {
  103. int rotationNum = 0;
  104. switch (s)
  105. {
  106. case Shape.SQUARE:
  107. //no rand needed because it has no rotations
  108. break;
  109. case Shape.LBLOCK:
  110. rotationNum = rndNum.Next(0, 4);
  111. break;
  112. case Shape.RLBLOCK:
  113. rotationNum = rndNum.Next(0, 4);
  114. break;
  115. case Shape.LINE:
  116. rotationNum = rndNum.Next(0, 2);
  117. break;
  118. case Shape.TBLOCK:
  119. rotationNum = rndNum.Next(0, 4);
  120. break;
  121. case Shape.ZBLOCK:
  122. rotationNum = rndNum.Next(0, 2);
  123. break;
  124. case Shape.RZBLOCK:
  125. rotationNum = rndNum.Next(0, 2);
  126. break;
  127. }
  128. return rotationNum;
  129. }
  130. public bool rotate(Turn t, Canvas c)
  131. {
  132. Point[] prevLoc = currentTileShape.getLocationsArray();
  133. if (currentTileShape.rotate(t, ref board))
  134. {
  135. foreach (Point po in prevLoc)//remove old location
  136. {
  137. c.Children.Remove(board[po.x, po.y].getImage());
  138. board[po.x, po.y] = new Tile();
  139. }
  140. Point[] newLoc = currentTileShape.getLocationsArray();
  141. foreach (Point po in newLoc)
  142. {
  143. board[po.x, po.y] = new Tile(path, po);
  144. board[po.x, po.y].setImagePosition(po);
  145. c.Children.Add(board[po.x, po.y].getImage());
  146. }
  147. return true;
  148. }
  149. else
  150. {
  151. //play invalid sound
  152. return false;
  153. }
  154. }
  155. public bool move(Direction d, Canvas c)
  156. {
  157. Point[] prevLoc = currentTileShape.getLocationsArray();
  158. if (currentTileShape.move(d, ref board))
  159. {
  160. foreach (Point po in prevLoc)//remove old location
  161. {
  162. c.Children.Remove(board[po.x, po.y].getImage());
  163. board[po.x, po.y] = new Tile();
  164. }
  165. Point[] newLoc = currentTileShape.getLocationsArray();
  166. foreach (Point po in newLoc)
  167. {
  168. board[po.x, po.y] = new Tile(path, po);
  169. board[po.x, po.y].setImagePosition(po);
  170. c.Children.Add(board[po.x, po.y].getImage());
  171. }
  172. return true;
  173. }
  174. else
  175. {
  176. //play invalid sound
  177. return false;
  178. }
  179. }
  180. public void maxLower(Canvas c)
  181. {
  182. Point[] arr = currentTileShape.getLocationsArray();
  183. foreach (Point po in arr)//remove images
  184. {
  185. c.Children.Remove(board[po.x, po.y].getImage());
  186. board[po.x, po.y] = new Tile();
  187. }
  188. while (!currentTileShape.gravity(ref board)) ;//move until collision
  189. Point[] newPositions = currentTileShape.getLocationsArray();
  190. foreach (Point po in newPositions)
  191. {
  192. board[po.x, po.y] = new Tile(path, po);
  193. board[po.x, po.y].setImagePosition(po);//accepts indexes
  194. c.Children.Add(board[po.x, po.y].getImage());
  195. }
  196. }
  197. public int update(Canvas c, Point[] arr)//returns value from removeFullRows
  198. {
  199. if (!currentTileShape.gravity(ref board))
  200. {
  201. foreach (Point po in arr)
  202. {
  203. board[po.x, po.y] = new Tile();//empty prev tile
  204. }
  205. Point[] newPositions = currentTileShape.getLocationsArray();
  206. foreach(Point po in newPositions)
  207. {
  208. board[po.x, po.y] = new Tile(path ,po);
  209. board[po.x, po.y].setImagePosition(po);//accepts indexes
  210. }
  211. }
  212. else//collision occured so spawn new shape
  213. {
  214. foreach (Point po in arr)
  215. {
  216. c.Children.Add(board[po.x, po.y].getImage());//let old tile stick around
  217. board[po.x, po.y].IsActive = false;
  218. board[po.x, po.y].IsEmpty = false;
  219. }
  220. spawn(c);
  221. if (!gameOver)
  222. {
  223. Point[] newPositions = currentTileShape.getLocationsArray();
  224. foreach (Point po in newPositions)
  225. {
  226. c.Children.Remove(board[po.x, po.y].getImage());//remove new tiles from canvas so draw func can re-add it
  227. }
  228. }
  229. }
  230. return removeFullRows(c);
  231. }
  232. public int removeFullRows(Canvas c)//returns number of rows that were deleted
  233. {
  234. int result = 0;
  235. for (int j = 0; j < board.GetLength(1); j++)
  236. {
  237. if (!board[0, j].IsEmpty && !board[0, j].IsActive)
  238. {
  239. bool fullRow = true;
  240. for (int i = 1; i < board.GetLength(0); i++)
  241. {
  242. if (board[i, j].IsEmpty || board[i, j].IsActive)
  243. {
  244. fullRow = false;
  245. }
  246. }
  247. if (fullRow)
  248. {
  249. result++;
  250. deleteRow(j, c);//call delete row
  251. }
  252. }
  253. }
  254. return result;
  255. }
  256. public void deleteRow(int rIndex, Canvas c)
  257. {
  258. for (int i = 0; i < board.GetLength(0); i++)
  259. {
  260. c.Children.Remove(board[i, rIndex].getImage());
  261. board[i, rIndex] = new Tile();
  262. }
  263. if(rIndex != 0)//no need to move row -1 down (wara)
  264. {
  265. for (int j = rIndex - 1; j >= 0; j--)
  266. {
  267. for (int i = 0; i < board.GetLength(0); i++)
  268. {
  269. if (!board[i, j].IsEmpty && !board[i, j].IsActive)
  270. {
  271. c.Children.Remove(board[i, j].getImage());
  272. board[i, j + 1] = board[i, j];
  273. Point temp = new Point();
  274. temp.x = i;
  275. temp.y = j + 1;
  276. board[i, j + 1].setImagePosition(temp);
  277. board[i, j] = new Tile();
  278. c.Children.Add(board[i, j + 1].getImage());
  279. //lol delete from canvas
  280. }
  281. }
  282. }
  283. }
  284. }
  285. public int draw(Canvas c)
  286. {
  287. Point[] positions = currentTileShape.getLocationsArray();
  288. foreach (Point po in positions)
  289. {
  290. c.Children.Remove(board[po.x, po.y].getImage());
  291. }
  292. int r = update(c, positions);
  293. if (!gameOver)
  294. {
  295. positions = currentTileShape.getLocationsArray();
  296. foreach (Point po in positions)
  297. {
  298. c.Children.Add(board[po.x, po.y].getImage());
  299. }
  300. }
  301. return r;
  302. }
  303. public void save(ref StreamWriter writer)
  304. {
  305. for (int j = 0; j < board.GetLength(1); j++)
  306. {
  307. string result = "";
  308. for (int i = 0; i < board.GetLength(0); i++)
  309. {
  310. if (!board[i, j].IsEmpty && !board[i, j].IsActive)
  311. {
  312. result += "1";
  313. }
  314. else
  315. {
  316. result += "0";
  317. }
  318. if (i < board.GetLength(0) - 1)
  319. {
  320. result += ",";
  321. }
  322. }
  323. writer.WriteLine(result);
  324. }
  325. }
  326. public void clear(Canvas c)
  327. {
  328. for(int i = 0; i < board.GetLength(0); i++)
  329. {
  330. for (int j = 0; j < board.GetLength(1); j++)
  331. {
  332. if (!board[i, j].IsEmpty)
  333. {
  334. c.Children.Remove(board[i, j].getImage());
  335. board[i, j] = new Tile();
  336. }
  337. }
  338. }
  339. }
  340. public void load(ref StreamReader reader, Canvas c)
  341. {
  342. //http://stackoverflow.com/questions/7425963/how-to-read-values-from-a-comma-separated-file
  343. string line = "";
  344. int i = 0, j = 0;
  345. path = setPath(setColor(Shape.LINE));
  346. while ((line = reader.ReadLine()) != null)
  347. {
  348. line.Trim();
  349. string[] tiles = line.Split(',');
  350. for (i = 0; i < board.GetLength(0); i++)
  351. {
  352. if(tiles[i] == "1")
  353. {
  354. board[i, j] = new Tile(path, new Point(i, j));
  355. board[i, j].IsActive = false;
  356. board[i, j].IsEmpty = false;
  357. c.Children.Add(board[i, j].getImage());
  358. }
  359. else
  360. {
  361. board[i,j] = new Tile();
  362. }
  363. }
  364. j++;
  365. }
  366. }
  367. }
  368. }