Advertisement
Guest User

Untitled

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