Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include<stdio.h>
  4. #include<conio.h>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. class plansza {
  10. public:
  11. vector<vector<int>> mat;
  12. bool prevMove; //0 for player A ...... 1 for player B;
  13. int A_wins, B_wins;
  14. char A_sign, B_sign;
  15. int width, height;
  16.  
  17. plansza(int width = 7, int height = 6 ,int A_wins=0,int B_wins=0, bool prevMove = 0, char A_sign = 'A', char B_sign = 'B')
  18. :width(width), height(height),A_wins(A_wins),B_wins(B_wins), prevMove(prevMove), A_sign(A_sign), B_sign(B_sign)
  19. {
  20. mat.resize(width, vector<int>(height, 0));
  21. }
  22.  
  23. ~plansza() {}
  24.  
  25. void A_add() {
  26. int col;
  27. do {
  28. cout << "[Team A]Please select column from 1 to 7 : ";
  29. cin >> col;
  30. } while (col < 1 || col>7);
  31. int p = 0;
  32. col--;
  33. for (size_t i = 0; i < mat[col].size(); i++) {
  34. if (mat[col][i] != 0) {
  35. p++;
  36. continue;
  37. };
  38. }
  39.  
  40. if (p != mat[col].size()) {
  41. mat[col][p] = 1;
  42. prevMove = 0;
  43. }
  44. else {
  45. A_add();
  46. }
  47.  
  48.  
  49. }
  50. //mat[j][i] => mat[col][row] => mat[w][h]
  51.  
  52. void B_add() {
  53. int col;
  54. do {
  55. cout << "[Team B]Please select column from 1 to 7 : ";
  56. cin >> col;
  57. } while (col < 1 || col>7);
  58. int p = 0;
  59. col--;
  60. for (size_t i = 0; i < mat[col].size(); i++) {
  61. if (mat[col][i] != 0) {
  62. p++;
  63. continue;
  64. };
  65. }
  66.  
  67. if (p != mat[col].size()) {
  68. mat[col][p] = 2;
  69. prevMove = 1;
  70. }
  71. else {
  72. B_add();
  73. }
  74.  
  75.  
  76. }
  77.  
  78. void symbolChoosing() {
  79. cout << "Team A: Choose your symbol" << endl;
  80. cin >> A_sign;
  81. cout << "Team B: Choose your symbol" << endl;
  82. cin >> B_sign;
  83. }
  84.  
  85.  
  86. void printPlansza() {
  87. cout<<"AWINS:"<<A_wins<<" "<<"BWINS:"<<B_wins<<endl;
  88. cout << "--------------------------" << endl;
  89. for (int i = height-1; i >= 0; i--) {
  90. for (int j = 0; j < width; j++) {
  91. if (mat[j][i] != 0) {
  92. if (mat[j][i] == 1) {
  93. cout << A_sign << " ";
  94. }
  95. else if (mat[j][i] == 2) {
  96. cout << B_sign << " ";
  97. }
  98. }
  99. else cout << " " << " ";
  100. }
  101. cout << endl;
  102. }
  103. cout << "--------------------------" << endl;
  104. }
  105. //KAŻDA KOLUMNA TO ODDZIELNY VECTOR
  106. //PLANSZA TO VECTOR ZŁOŻONY Z VECTORÓW KOLUMN
  107.  
  108.  
  109. // MOŻE PO PROSTU ZROBIE CHECKA CHECKB ALE JESZCZE NIE WIEM, CZY WARTO UŻYWAĆ PREVMOVE?
  110.  
  111. bool checkPattern(bool prevMove, vector<int> mat) {
  112. vector<int> D;
  113. if (prevMove) {
  114. D = { 2,2,2,2 };
  115. }
  116. else {
  117. D = { 1,1,1,1 };
  118. }
  119.  
  120. auto res_ = search(begin(mat), end(mat), begin(D), end(D)); //Check vertical
  121. if (res_ == end(mat))
  122. {
  123. return 0;
  124. }
  125. else
  126. {
  127. if (prevMove) {
  128. B_wins++;
  129. return 1;
  130. }
  131. else {
  132. A_wins++;
  133. return 1;
  134. }
  135.  
  136. std::cout << "Found it\n"; //znalazłem
  137. }
  138.  
  139. }
  140.  
  141. bool checkWin() {
  142. vector<int> a, b, c;
  143. int k_, r_;
  144. int k_copy, r_copy;
  145.  
  146.  
  147. for (int j = 0; j < height; j++) {
  148.  
  149. for (int i = 0; i < width; i++) {//KAŻDA KOLUMNA TO WEKTOR W KTÓRYM SZUKAMY PATTERNU
  150.  
  151. if (checkPattern(prevMove, mat[i])) {
  152.  
  153. return 1;
  154. }
  155.  
  156. b.push_back(mat[i][j]);
  157.  
  158. }
  159. if (checkPattern(prevMove, b)) {
  160.  
  161. return 1;
  162. }
  163. b.clear();
  164.  
  165. }
  166.  
  167.  
  168. for (int r = 0; r < height - 1; r++) {
  169. k_ = 0;
  170. r_copy = r;
  171. while (r_copy >= 0 && k_ < width) {
  172. a.push_back(mat[k_][r_copy]);
  173. k_++;
  174. r_copy--;
  175. }
  176. if (a.size() >= 4) {
  177. if (checkPattern(prevMove, a)) {
  178.  
  179. return 1;
  180. }
  181. }
  182. a.clear();
  183. }
  184. for (int k = 1; k <= width - 1; k++) {
  185. r_ = height - 1;
  186. k_copy = k;
  187. while (k_copy < width - 1 && r_ >= 0) {
  188. a.push_back(mat[r_][k_copy]);
  189. r_--;
  190. k_copy++;
  191. }
  192. if (a.size() >= 4) {
  193. if (checkPattern(prevMove, a)) {
  194.  
  195. return 1;
  196. }
  197. }
  198. a.clear();
  199. }
  200.  
  201. for (int r = 0; r < height - 1; r++) {
  202. k_ = width - 1;
  203. r_copy = r;
  204. while (r_copy >= 0 && k_ >= 0) {
  205. a.push_back(mat[k_][r_copy]);
  206. k_--;
  207. r_copy--;
  208. }
  209. if (a.size() >= 4) {
  210. if (checkPattern(prevMove, a)) {
  211.  
  212. return 1;
  213. }
  214. }
  215. a.clear();
  216. }
  217. for (int k = width - 2; k >= 0; k--) {
  218. r_ = height - 1;
  219. k_copy = k;
  220. while (k_copy >= 0 && r_ >= 0) {
  221. a.push_back(mat[r_][k_copy]);
  222. r_--;
  223. k_copy--;
  224. }
  225. if (a.size() >= 4) {
  226. if (checkPattern(prevMove, a)) {
  227.  
  228. return 1;
  229. }
  230. }
  231. b.clear();
  232. }
  233. return 0;
  234. };// checkiwn()
  235. };
  236.  
  237.  
  238.  
  239. //mat[j][i] => mat[col][row] => mat[w][h]
  240.  
  241.  
  242.  
  243. void clear() {
  244.  
  245. std::cout << "\x1B[2J\x1B[H";
  246. }
  247.  
  248. int main() {
  249. plansza mat;
  250. mat.symbolChoosing();
  251. clear();
  252. while (1) {
  253.  
  254. mat.printPlansza();
  255. mat.A_add();
  256. clear();
  257.  
  258. if (mat.checkWin()) {
  259. mat = plansza(mat.width, mat.height, mat.A_wins, mat.B_wins, 0,mat.A_sign,mat.B_sign);
  260. }
  261. cout << endl;
  262. mat.printPlansza();
  263. mat.B_add();
  264. clear();
  265.  
  266. if (mat.checkWin()) {
  267.  
  268. mat = plansza(mat.width,mat.height,mat.A_wins,mat.B_wins,0, mat.A_sign, mat.B_sign);
  269. }
  270. cout << endl;
  271.  
  272.  
  273.  
  274. };
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement