Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.45 KB | None | 0 0
  1. #include "battleship.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int length(Tship obj) {
  6. int l = 0;
  7. Tship p = obj;
  8. while (p) { l += 1; p = p->next; }
  9. return l;
  10. }
  11.  
  12. int lengthf(Tfleet obj) {
  13. int l = 0;
  14. Tfleet p = obj;
  15. while (p) { l += 1; p = p->next; }
  16. return l;
  17. }
  18.  
  19. void formShip(int player, int i, int j, int dir, Tship &cur) {
  20. if (player==1) {
  21. if (!dir) {
  22. if (i + 1 < 10 && player1_field[i + 1][j] == 1) { Tship p = new ship; p->x = i; p->y = j; p->next = 0; cur->next = p; formShip(player, i + 1, j, 1, p); if (j + 1 < 10) player1_field[i][j + 1] = 0; }
  23. else if (j + 1 < 10 && player1_field[i][j + 1] == 1) { Tship p = new ship; p->x = i; p->y = j; p->next = 0; cur->next = p; formShip(player, i, j + 1, 2, p); if (i + 1 < 10) player1_field[i+1][j] = 0;}
  24. }
  25. else if (dir == 1) if (i + 1 < 10 && player1_field[i + 1][j] == 1) {
  26. Tship p = new ship; p->x = i; p->y = j; p->next = 0; cur->next = p; formShip(player, i + 1, j, 1, p); if (j + 1 < 10) player1_field[i][j + 1] = 0; if (i + 1 < 10) player1_field[i + 1][j] = 0; if (i + 1 < 10 && j + 1 < 10) player1_field[i + 1][j + 1] = 0;
  27. }
  28. else if (j + 1 < 10 && player1_field[i][j + 1] == 1) {
  29. Tship p = new ship; p->x = i; p->y = j; p->next = 0; cur->next = p; formShip(player, i, j + 1, 2, p); if (j + 1 < 10) player1_field[i][j + 1] = 0; if (i + 1 < 10) player1_field[i + 1][j] = 0; if (i + 1 < 10 && j + 1 < 10) player1_field[i + 1][j + 1] = 0;
  30. }
  31. }
  32. else {
  33. if (!dir) {
  34. if (i + 1 < 10 && player2_field[i + 1][j] == 1) { Tship p = new ship; p->x = i; p->y = j; p->next = 0; cur->next = p; formShip(player, i + 1, j, 1, p); if (j + 1 < 10) player2_field[i][j + 1] = 0; }
  35. else if (j + 1 < 10 && player2_field[i][j + 1] == 1) { Tship p = new ship; p->x = i; p->y = j; p->next = 0; cur->next = p; formShip(player, i, j + 1, 2, p); if (i + 1 < 10) player2_field[i + 1][j] = 0; }
  36. }
  37. else if (dir == 1) if (i + 1 < 10 && player2_field[i + 1][j] == 1) {
  38. Tship p = new ship; p->x = i; p->y = j; p->next = 0; cur->next = p; formShip(player, i + 1, j, 1, p); if (j + 1 < 10) player2_field[i][j + 1] = 0; if (i + 1 < 10) player2_field[i + 1][j] = 0; if (i + 1 < 10 && j + 1 < 10) player2_field[i + 1][j + 1] = 0;
  39. }
  40. else if (j + 1 < 10 && player2_field[i][j + 1] == 1) {
  41. Tship p = new ship; p->x = i; p->y = j; p->next = 0; cur->next = p; formShip(player, i, j + 1, 2, p); if (j + 1 < 10) player2_field[i][j + 1] = 0; if (i + 1 < 10) player2_field[i + 1][j] = 0; if (i + 1 < 10 && j + 1 < 10) player2_field[i + 1][j + 1] = 0;
  42. }
  43. }
  44. }
  45.  
  46. void formFleet(int player) {
  47.  
  48. if (player == 1) {
  49. Tfleet cur = player1;
  50. for (int i = 0; i < 10; ++i)
  51. for (int j = 0; j < 10; ++j)
  52. if (player1_field[i][j] == 0) continue;
  53. else if (player1_field[i][j] == 1)
  54. { Tship p = new ship; p->x = i; p->y = j; p->next = 0; formShip(player, i, j, 0, p); addFleet(cur, p);}
  55. else throw 110;
  56. }
  57. else {
  58. Tfleet cur = player2;
  59. for (int i = 0; i < 10; ++i)
  60. for (int j = 0; j < 10; ++j)
  61. if (player2_field[i][j] == 0) continue;
  62. else if (player2_field[i][j] == 1) { Tship p = new ship; p->x = i; p->y = j; p->next = 0; formShip(player, i, j, 0, p); addFleet(cur, p); }
  63. else throw 120;
  64. }
  65. }
  66.  
  67. void addFleet(Tfleet &cur, Tship sh) {
  68. if (!cur) {
  69. Tfleet p = new fleet;
  70. p->top = sh;
  71. p -> next = 0;
  72. cur = p;
  73. }
  74. else {
  75. Tfleet p = new fleet;
  76. p->top = sh;
  77. p->next = 0;
  78. cur->next = p;
  79. cur = p;
  80. }
  81. }
  82.  
  83. bool check(int player) {
  84. int amount[4] = { 0,0,0,0 };
  85. int a;
  86. if (player == 1) {
  87. Tfleet cur = player1;
  88. while (cur) {
  89. a = length(cur->top) - 1;
  90. if (a<4&&a>=0)
  91. amount[a] += 1;
  92. cur = cur->next;
  93. }
  94. if (amount[3] != 1 || amount[2] != 2 || amount[1] != 3 || amount[0] != 4||lengthf(player1)>10) { cout<<amount[3]<<amount[2]<<amount[1]<<amount[0]<<endl; return false;}
  95. else return true;
  96. }
  97. else {
  98. Tfleet cur = player2;
  99. while (cur) {
  100. a = length(cur->top) - 1;
  101. if (a < 4 && a >= 0)
  102. amount[a] += 1;
  103. cur = cur->next;
  104. }
  105. if (amount[3] != 1 || amount[2] != 2 || amount[1] != 3 || amount[0] != 4||lengthf(player2)>10) {cout<<amount[3]<<amount[2]<<amount[1]<<amount[0]<<endl;return false;}
  106. else return true;
  107. }
  108. }
  109.  
  110. void rebuild(int player) {
  111. if (player == 1) {
  112. Tfleet cur = player1;
  113. Tship p = cur->top;
  114. while (cur) {
  115. p = cur->top;
  116. while (p) {player1_field[p->x][p->y] = 1; p = p->next;
  117. }
  118. cur = cur->next;
  119. }
  120. }
  121. else {
  122. Tfleet cur = player2;
  123. Tship p = cur->top;
  124. while (cur) {
  125. p = cur->top;
  126. while (p) {
  127. player2_field[p->x][p->y] = 1; p = p->next;
  128. }
  129. cur = cur->next;
  130. }
  131. }
  132. }
  133.  
  134. void makeTurn(int &player) {
  135. int x, y;
  136. cout << "Ход игрока " << player << ". Назовите координаты:\n";
  137. show(player%2+1);
  138. cin >> x >> y;
  139. while (x > 10 || x < 1 || y>10 || y < 1) { cout << "Введены неверные координаты. Попробуйте снова.\n";
  140. cin >> x >> y;
  141. }
  142. if (foundShip(player % 2+1,x,y)) {
  143. cout << "Попал!\n";
  144. deleteShip(player%2+1, x, y);
  145. if (player == 1) { if (!lengthf(player2)) cout << "Победа игрока 1! Поздравляем!\n"; cont = 0; }
  146. else { if (!lengthf(player1)) cout << "Победа игрока 2! Поздравляем!\n"; cont = 0; }
  147. }
  148. else {
  149. cout << "Мимо!\n";
  150. player = player % 2 + 1;
  151. }
  152. }
  153.  
  154. bool foundShip(int player, int x, int y) {
  155. if (player == 1) {
  156. Tfleet cur = player1;
  157. Tship p = cur->top;
  158. while (cur) {
  159. p = cur->top;
  160. while (p && !(p->x == x && p->y == y)) {
  161. p = p->next;
  162. }
  163. if (!p) cur = cur->next;
  164. else return true;
  165. }
  166. return false;
  167. }
  168. else
  169. {
  170. Tfleet cur = player2;
  171. Tship p = cur->top;
  172. while (cur) {
  173. p = cur->top;
  174. while (p && !(p->x == x && p->y == y)) {
  175. p = p->next;
  176. }
  177. if (!p) cur = cur->next;
  178. else return true;
  179. }
  180. return false;
  181. }
  182. }
  183.  
  184. void deleteShip(int player, int x, int y) {
  185. if (player == 1) {
  186. Tfleet cur = player1;
  187. Tship p = cur->top;
  188. if (p->x == x && p->y == y) {
  189. cur->top = p->next;
  190. delete p;
  191. player1_field[x][y] = 0;
  192. if (!length(cur->top)) {
  193. player1=cur->next;
  194. delete cur;
  195. cout<<"Убил!\n";
  196. }
  197. }// Если самый первый
  198. else {
  199. while (p->next && !(p->next->x == x && p->next->y == y)) {
  200. p = p->next;
  201. }
  202. if (!p->next) while (cur->next) {
  203. p = cur->next->top;
  204. if (p->x == x && p->y == y) {
  205. cur->next->top = p->next;
  206. delete p;
  207. player1_field[x][y] = 0;
  208. if (!length(cur->next->top)) {
  209. cur->next = cur->next->next;
  210. delete cur->next;
  211. cout<<"Убил!\n";
  212. }
  213. }//Если первый в непервом корабле
  214. else {
  215. while (p->next && !(p->next->x == x && p->next->y == y)) {
  216. p = p->next;
  217. }
  218. if (!p->next) cur = cur->next;
  219. else {
  220. p->next = p->next->next;
  221. delete p->next;
  222. player1_field[x][y] = 0;
  223. if (!length(cur->next->top)) {
  224. cur->next = cur->next->next;
  225. delete cur->next;
  226. cout<<"Убил!\n";
  227. }
  228. }
  229. }//Если непервый в непервом корабле
  230. }//Если не в первом корабле
  231. else {
  232. p->next = p->next->next;
  233. delete p->next;
  234. player1_field[x][y] = 0;
  235. if (!length(cur->top)) {
  236. player1=cur->next;
  237. delete cur;
  238. cout<<"Убил!\n";
  239. }
  240. }
  241. }// Если в первом корабле
  242. }
  243. else {
  244. Tfleet cur = player2;
  245. Tship p = cur->top;
  246. if (p->x == x && p->y == y) {
  247. cur->top = p->next;
  248. delete p;
  249. player2_field[x][y] = 0;
  250. if (!length(cur->top)) {
  251. player2 = cur->next;
  252. delete cur;
  253. cout<<"Убил!\n";
  254. }
  255. }// Если самый первый
  256. else {
  257. while (p->next && !(p->next->x == x && p->next->y == y)) {
  258. p = p->next;
  259. }
  260. if (!p->next) while (cur->next) {
  261. p = cur->next->top;
  262. if (p->x == x && p->y == y) {
  263. cur->next->top = p->next;
  264. delete p;
  265. player2_field[x][y] = 0;
  266. if (!length(cur->next->top)) {
  267. cur->next = cur->next->next;
  268. delete cur->next;
  269. cout<<"Убил!\n";
  270. }
  271. }//Если первый в непервом корабле
  272. else {
  273. while (p->next && !(p->next->x == x && p->next->y == y)) {
  274. p = p->next;
  275. }
  276. if (!p->next) cur = cur->next;
  277. else {
  278. p->next = p->next->next;
  279. delete p->next;
  280. player2_field[x][y] = 0;
  281. if (!length(cur->next->top)) {
  282. cur->next = cur->next->next;
  283. delete cur->next;
  284. cout<<"Убил!\n";
  285. }
  286. }
  287. }//Если непервый в непервом корабле
  288. }//Если не в первом корабле
  289. else {
  290. p->next = p->next->next;
  291. delete p->next;
  292. player2_field[x][y] = 0;
  293. if (!length(cur->top)) {
  294. player2 = cur->next;
  295. delete cur;
  296. cout<<"Убил!\n";
  297. }
  298. }
  299. }// Если в первом корабле
  300. }
  301. }
  302.  
  303. void show(int player) {
  304. if (player==1) {
  305. cout<<"Поле игрока 1\n";
  306. for (int j=0;j<10;j++)
  307. {for (int i=0;i<10;i++)
  308. cout<<player1_field[i][j];
  309. cout<<endl;
  310. }
  311. }
  312. else {
  313. cout<<"Поле игрока 2\n";
  314. for (int j=0;j<10;j++)
  315. {for (int i=0;i<10;i++)
  316. cout<<player2_field[i][j];
  317. cout<<endl;
  318. }
  319. }
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement