Advertisement
StreetKatya

model

Jun 30th, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Game2048
  6. {
  7. public class Model
  8. {
  9. private int score = 0;
  10. private Map map;
  11. private Random random = new Random();
  12. private bool isGameOver;
  13. private bool moved;
  14.  
  15. public int Score => this.score;
  16.  
  17. public int size => this.map.size;
  18.  
  19. public Model(int size) => this.map = new Map(size);
  20.  
  21. public void Start()
  22. {
  23. this.score = 0;
  24. this.isGameOver = false;
  25. for (int x = 0; x < this.size; ++x)
  26. {
  27. for (int y = 0; y < this.size; ++y)
  28. this.map.Set(x, y, 0);
  29. }
  30. this.AddRandomNumber();
  31. this.AddRandomNumber();
  32. }
  33.  
  34. private void AddRandomNumber()
  35. {
  36. if (this.isGameOver)
  37. return;
  38. List<int[]> source = new List<int[]>();
  39. for (int x = 0; x < this.size; ++x)
  40. {
  41. for (int y = 0; y < this.size; ++y)
  42. {
  43. if (this.GetMap(x, y) == 0)
  44. source.Add(new int[2]{ x, y });
  45. }
  46. }
  47. int index = this.random.Next(0, source.Count<int[]>());
  48. int number = this.random.Next(1, 3) * 2;
  49. this.map.Set(source[index][0], source[index][1], number);
  50. }
  51.  
  52. private void Join(int x, int y, int sx, int sy)
  53. {
  54. if (this.map.Get(x, y) <= 0 || this.map.Get(x + sx, y + sy) != this.map.Get(x, y))
  55. return;
  56. this.map.Set(x + sx, y + sy, this.map.Get(x, y) * 2);
  57. this.score += this.map.Get(x, y) * this.map.Get(x, y);
  58. for (; this.map.Get(x - sx, y - sy) > 0; y -= sy)
  59. {
  60. this.map.Set(x, y, this.map.Get(x - sx, y - sy));
  61. x -= sx;
  62. }
  63. this.map.Set(x, y, 0);
  64. this.moved = true;
  65. }
  66.  
  67. private void Lift(int x, int y, int sx, int sy)
  68. {
  69. if (this.map.Get(x, y) <= 0)
  70. return;
  71. while (this.map.Get(x + sx, y + sy) == 0)
  72. {
  73. this.map.Set(x + sx, y + sy, this.map.Get(x, y));
  74. this.map.Set(x, y, 0);
  75. x += sx;
  76. y += sy;
  77. this.moved = true;
  78. }
  79. }
  80.  
  81. public void Left()
  82. {
  83. this.moved = false;
  84. for (int y = 0; y < this.map.size; ++y)
  85. {
  86. for (int x = 1; x < this.map.size; ++x)
  87. this.Lift(x, y, -1, 0);
  88. }
  89. for (int y = 0; y < this.map.size; ++y)
  90. {
  91. for (int x = 1; x < this.map.size; ++x)
  92. this.Join(x, y, -1, 0);
  93. }
  94. if (!this.moved)
  95. return;
  96. this.AddRandomNumber();
  97. }
  98.  
  99. public void Right()
  100. {
  101. this.moved = false;
  102. for (int y = 0; y < this.map.size; ++y)
  103. {
  104. for (int x = this.map.size - 2; x >= 0; --x)
  105. this.Lift(x, y, 1, 0);
  106. for (int x = this.map.size - 2; x >= 0; --x)
  107. this.Join(x, y, 1, 0);
  108. }
  109. if (!this.moved)
  110. return;
  111. this.AddRandomNumber();
  112. }
  113.  
  114. public void Up()
  115. {
  116. this.moved = false;
  117. for (int x = 0; x < this.map.size; ++x)
  118. {
  119. for (int y = 1; y < this.map.size; ++y)
  120. this.Lift(x, y, 0, -1);
  121. }
  122. for (int x = 0; x < this.map.size; ++x)
  123. {
  124. for (int y = 1; y < this.map.size; ++y)
  125. this.Join(x, y, 0, -1);
  126. }
  127. if (!this.moved)
  128. return;
  129. this.AddRandomNumber();
  130. }
  131.  
  132. public void Down()
  133. {
  134. this.moved = false;
  135. for (int x = 0; x < this.map.size; ++x)
  136. {
  137. for (int y = this.map.size - 2; y >= 0; --y)
  138. this.Lift(x, y, 0, 1);
  139. }
  140. for (int x = 0; x < this.map.size; ++x)
  141. {
  142. for (int y = this.map.size - 2; y >= 0; --y)
  143. this.Join(x, y, 0, 1);
  144. }
  145. if (!this.moved)
  146. return;
  147. this.AddRandomNumber();
  148. }
  149.  
  150. public int GetMap(int x, int y) => this.map.Get(x, y);
  151.  
  152. public bool IsGameOver()
  153. {
  154. if (this.isGameOver)
  155. return this.isGameOver;
  156. for (int x = 0; x < this.map.size; ++x)
  157. {
  158. for (int y = 0; y < this.map.size; ++y)
  159. {
  160. if (this.map.Get(x, y) == 0)
  161. return false;
  162. }
  163. }
  164. for (int x = 0; x < this.map.size; ++x)
  165. {
  166. for (int y = 0; y < this.map.size; ++y)
  167. {
  168. if (this.map.Get(x, y) == this.map.Get(x + 1, y) || this.map.Get(x, y) == this.map.Get(x, y + 1))
  169. return false;
  170. }
  171. }
  172. this.isGameOver = true;
  173. return this.isGameOver;
  174. }
  175. }
  176. }
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement