Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. mt19937 gen(time(0));
  6.  
  7. bool have_moves(vector<vector<int32_t>> &a) {
  8. for (int32_t i = 0; i < 4; ++i) {
  9. for (int32_t j = 0; j < 4; ++j) {
  10. if (a[i][j] == 0) {
  11. return true;
  12. }
  13. }
  14. }
  15. for (int32_t i = 0; i < 3; ++i) {
  16. for (int32_t j = 0; j < 3; ++j) {
  17. if (a[i][j] == a[i][j + 1] || a[i][j] == a[i + 1][j]) {
  18. return true;
  19. }
  20. }
  21. }
  22. return false;
  23. }
  24.  
  25. void add(vector<vector<int32_t>> &a) {
  26. vector<pair<int32_t, int32_t>> zero;
  27. for (int32_t i = 0; i < 4; ++i) {
  28. for (int32_t j = 0; j < 4; ++j) {
  29. if (a[i][j] == 0) {
  30. zero.push_back(make_pair(i, j));
  31. }
  32. }
  33. }
  34. int32_t id = gen() % (zero.size());
  35. int32_t value = gen() % 6;
  36. if (value == 5) {
  37. value = 4;
  38. } else {
  39. value = 2;
  40. }
  41. a[zero[id].first][zero[id].second] = value;
  42. return;
  43. }
  44.  
  45. bool zero_down(vector<vector<int32_t>> &a) {
  46. bool change = false;
  47. for (int32_t j = 0; j < 4; ++j) {
  48. vector<int32_t> id_not_zero;
  49. for (int32_t i = 0; i < 4; ++i) {
  50. if (a[i][j] != 0) {
  51. id_not_zero.push_back(a[i][j]);
  52. }
  53. }
  54. for (int32_t i = 3; i >= 0; --i) {
  55. if (id_not_zero.size() > 0) {
  56. if (a[i][j] != id_not_zero[id_not_zero.size() - 1]) {
  57. change = true;
  58. }
  59. a[i][j] = id_not_zero[id_not_zero.size() - 1];
  60. id_not_zero.pop_back();
  61. } else {
  62. a[i][j] = 0;
  63. }
  64. }
  65. }
  66. return change;
  67. }
  68.  
  69. bool zero_up(vector<vector<int32_t>> &a) {
  70. bool change = false;
  71. for (int32_t j = 0; j < 4; ++j) {
  72. vector<int32_t> id_not_zero;
  73. for (int32_t i = 0; i < 4; ++i) {
  74. if (a[i][j] != 0) {
  75. id_not_zero.push_back(a[i][j]);
  76. }
  77. }
  78. for (int32_t i = 0; i < 4; ++i) {
  79. if (id_not_zero.size() > 0) {
  80. if (a[i][j] != id_not_zero[id_not_zero.size() - 1]) {
  81. change = true;
  82. }
  83. a[i][j] = id_not_zero[0];
  84. reverse(id_not_zero.begin(), id_not_zero.end());
  85. id_not_zero.pop_back();
  86. reverse(id_not_zero.begin(), id_not_zero.end());
  87. } else {
  88. a[i][j] = 0;
  89. }
  90. }
  91. }
  92. return change;
  93. }
  94.  
  95. bool zero_right(vector<vector<int32_t>> &a) {
  96. bool change = false;
  97. for (int32_t i = 0; i < 4; ++i) {
  98. vector<int32_t> id_not_zero;
  99. for (int32_t j = 0; j < 4; ++j) {
  100. if (a[i][j] != 0) {
  101. id_not_zero.push_back(a[i][j]);
  102. }
  103. }
  104. for (int32_t j = 3; j >= 0; --j) {
  105. if (id_not_zero.size() > 0) {
  106. if (a[i][j] != id_not_zero[id_not_zero.size() - 1]) {
  107. change = true;
  108. }
  109. a[i][j] = id_not_zero[id_not_zero.size() - 1];
  110. id_not_zero.pop_back();
  111. } else {
  112. a[i][j] = 0;
  113. }
  114. }
  115. }
  116. return change;
  117. }
  118.  
  119. bool zero_left(vector<vector<int32_t>> &a) {
  120. bool change = false;
  121. for (int32_t i = 0; i < 4; ++i) {
  122. vector<int32_t> id_not_zero;
  123. for (int32_t j = 0; j < 4; ++j) {
  124. if (a[i][j] != 0) {
  125. id_not_zero.push_back(a[i][j]);
  126. }
  127. }
  128. for (int32_t j = 0; j < 4; ++j) {
  129. if (id_not_zero.size() > 0) {
  130. if (a[i][j] != id_not_zero[id_not_zero.size() - 1]) {
  131. change = true;
  132. }
  133. a[i][j] = id_not_zero[0];
  134. reverse(id_not_zero.begin(), id_not_zero.end());
  135. id_not_zero.pop_back();
  136. reverse(id_not_zero.begin(), id_not_zero.end());
  137. } else {
  138. a[i][j] = 0;
  139. }
  140. }
  141. }
  142. return change;
  143. }
  144.  
  145. bool sum_down(vector<vector<int32_t>> &a) {
  146. bool change = zero_down(a);
  147. for (int32_t j = 0; j < 4; ++j) {
  148. for (int32_t i = 3; i > 0; --i) {
  149. if (a[i][j] == a[i - 1][j]) {
  150. if (a[i][j] != 0) {
  151. a[i][j] *= 2;
  152. a[i - 1][j] = 0;
  153. change = true;
  154. }
  155. }
  156. }
  157. }
  158. bool change1 = zero_down(a);
  159. return (change1 || change);
  160. }
  161.  
  162. bool sum_up(vector<vector<int32_t>> &a) {
  163. bool change = zero_up(a);
  164. for (int32_t j = 0; j < 4; ++j) {
  165. for (int32_t i = 0; i < 3; ++i) {
  166. if (a[i][j] == a[i + 1][j]) {
  167. if (a[i][j] != 0) {
  168. a[i][j] *= 2;
  169. a[i + 1][j] = 0;
  170. change = true;
  171. }
  172. }
  173. }
  174. }
  175. bool change1 = zero_up(a);
  176. return (change1 || change);
  177. }
  178.  
  179. bool sum_right(vector<vector<int32_t>> &a) {
  180. bool change = zero_right(a);
  181. for (int32_t i = 0; i < 4; ++i) {
  182. for (int32_t j = 3; j > 0; --j) {
  183. if (a[i][j] == a[i][j - 1]) {
  184. if (a[i][j] != 0) {
  185. a[i][j] *= 2;
  186. a[i][j - 1] = 0;
  187. change = true;
  188. }
  189. }
  190. }
  191. }
  192. bool change1 = zero_right(a);
  193. return (change1 || change);
  194. }
  195.  
  196. bool sum_left(vector<vector<int32_t>> &a) {
  197. bool change = zero_left(a);
  198. for (int32_t i = 0; i < 4; ++i) {
  199. for (int32_t j = 0; j < 3; ++j) {
  200. if (a[i][j] == a[i][j + 1]) {
  201. if (a[i][j] != 0) {
  202. a[i][j] *= 2;
  203. a[i][j + 1] = 0;
  204. change = true;
  205. }
  206. }
  207. }
  208. }
  209. bool change1 = zero_left(a);
  210. return (change1 || change);
  211. }
  212.  
  213. void print(vector<vector<int32_t>> &a, bool cls) {
  214. if (cls) {
  215. system("cls");
  216. }
  217. for (int32_t i = 0; i < 4; ++i) {
  218. for (int32_t j = 0; j < 4; ++j) {
  219. cout << a[i][j] << ' ';
  220. }
  221. cout << "\n";
  222. }
  223. }
  224.  
  225. bool win(vector<vector<int32_t>> &a) {
  226. for (int32_t i = 0; i < 4; ++i) {
  227. for (int32_t j = 0; j < 4; ++j) {
  228. if (a[i][j] >= 2048) {
  229. return true;
  230. }
  231. }
  232. }
  233. return false;
  234. }
  235.  
  236. bool cls = true;
  237. void game(vector<vector<int32_t>> &a) {
  238. print(a, cls);
  239. char c;
  240. cin >> c;
  241. if (c == 'w') {
  242. bool change = sum_up(a);
  243. if (!change) {
  244. cls = false;
  245. system("cls");
  246. cout << "Please, make another move\n";
  247. game(a);
  248. } else {
  249. cls = true;
  250. }
  251. } else if (c == 'a') {
  252. bool change = sum_left(a);
  253. if (!change) {
  254. cls = false;
  255. system("cls");
  256. cout << "Please, make another move\n";
  257. game(a);
  258. } else {
  259. cls = true;
  260. }
  261. } else if (c == 's') {
  262. bool change = sum_down(a);
  263. if (!change) {
  264. cls = false;
  265. system("cls");
  266. cout << "Please, make another move\n";
  267. game(a);
  268. } else {
  269. cls = true;
  270. }
  271. } else if (c == 'd') {
  272. bool change = sum_right(a);
  273. if (!change) {
  274. cls = false;
  275. system("cls");
  276. cout << "Please, make another move\n";
  277. game(a);
  278. } else {
  279. cls = true;
  280. }
  281. }
  282. if (win(a)) {
  283. system("cls");
  284. cout << "You won\n";
  285. cls = false;
  286. game(a);
  287. }
  288. }
  289.  
  290. int main() {
  291. vector<vector<int32_t>> a(4, vector<int32_t> (4, 0));
  292. add(a);
  293. while (have_moves(a)) {
  294. game(a);
  295. add(a);
  296. }
  297. cout << "Lose :-(\n";
  298. return 0;
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement