Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "battleship.h"
- #include <iostream>
- using namespace std;
- int length(Tship obj) {
- int l = 0;
- Tship p = obj;
- while (p) { l += 1; p = p->next; }
- return l;
- }
- int lengthf(Tfleet obj) {
- int l = 0;
- Tfleet p = obj;
- while (p) { l += 1; p = p->next; }
- return l;
- }
- void formShip(int player, int i, int j, int dir, Tship &cur) {
- if (player==1) {
- if (!dir) {
- 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; }
- 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;}
- }
- else if (dir == 1) 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; if (i + 1 < 10) player1_field[i + 1][j] = 0; if (i + 1 < 10 && j + 1 < 10) player1_field[i + 1][j + 1] = 0;
- }
- 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 (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;
- }
- }
- else {
- if (!dir) {
- 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; }
- 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; }
- }
- else if (dir == 1) 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; if (i + 1 < 10) player2_field[i + 1][j] = 0; if (i + 1 < 10 && j + 1 < 10) player2_field[i + 1][j + 1] = 0;
- }
- 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 (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;
- }
- }
- }
- void formFleet(int player) {
- if (player == 1) {
- Tfleet cur = player1;
- for (int i = 0; i < 10; ++i)
- for (int j = 0; j < 10; ++j)
- if (player1_field[i][j] == 0) continue;
- else if (player1_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);}
- else throw 110;
- }
- else {
- Tfleet cur = player2;
- for (int i = 0; i < 10; ++i)
- for (int j = 0; j < 10; ++j)
- if (player2_field[i][j] == 0) continue;
- 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); }
- else throw 120;
- }
- }
- void addFleet(Tfleet &cur, Tship sh) {
- if (!cur) {
- Tfleet p = new fleet;
- p->top = sh;
- p -> next = 0;
- cur = p;
- }
- else {
- Tfleet p = new fleet;
- p->top = sh;
- p->next = 0;
- cur->next = p;
- cur = p;
- }
- }
- bool check(int player) {
- int amount[4] = { 0,0,0,0 };
- int a;
- if (player == 1) {
- Tfleet cur = player1;
- while (cur) {
- a = length(cur->top) - 1;
- if (a<4&&a>=0)
- amount[a] += 1;
- cur = cur->next;
- }
- 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;}
- else return true;
- }
- else {
- Tfleet cur = player2;
- while (cur) {
- a = length(cur->top) - 1;
- if (a < 4 && a >= 0)
- amount[a] += 1;
- cur = cur->next;
- }
- 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;}
- else return true;
- }
- }
- void rebuild(int player) {
- if (player == 1) {
- Tfleet cur = player1;
- Tship p = cur->top;
- while (cur) {
- p = cur->top;
- while (p) {player1_field[p->x][p->y] = 1; p = p->next;
- }
- cur = cur->next;
- }
- }
- else {
- Tfleet cur = player2;
- Tship p = cur->top;
- while (cur) {
- p = cur->top;
- while (p) {
- player2_field[p->x][p->y] = 1; p = p->next;
- }
- cur = cur->next;
- }
- }
- }
- void makeTurn(int &player) {
- int x, y;
- cout << "Ход игрока " << player << ". Назовите координаты:\n";
- show(player%2+1);
- cin >> x >> y;
- while (x > 10 || x < 1 || y>10 || y < 1) { cout << "Введены неверные координаты. Попробуйте снова.\n";
- cin >> x >> y;
- }
- if (foundShip(player % 2+1,x,y)) {
- cout << "Попал!\n";
- deleteShip(player%2+1, x, y);
- if (player == 1) { if (!lengthf(player2)) cout << "Победа игрока 1! Поздравляем!\n"; cont = 0; }
- else { if (!lengthf(player1)) cout << "Победа игрока 2! Поздравляем!\n"; cont = 0; }
- }
- else {
- cout << "Мимо!\n";
- player = player % 2 + 1;
- }
- }
- bool foundShip(int player, int x, int y) {
- if (player == 1) {
- Tfleet cur = player1;
- Tship p = cur->top;
- while (cur) {
- p = cur->top;
- while (p && !(p->x == x && p->y == y)) {
- p = p->next;
- }
- if (!p) cur = cur->next;
- else return true;
- }
- return false;
- }
- else
- {
- Tfleet cur = player2;
- Tship p = cur->top;
- while (cur) {
- p = cur->top;
- while (p && !(p->x == x && p->y == y)) {
- p = p->next;
- }
- if (!p) cur = cur->next;
- else return true;
- }
- return false;
- }
- }
- void deleteShip(int player, int x, int y) {
- if (player == 1) {
- Tfleet cur = player1;
- Tship p = cur->top;
- if (p->x == x && p->y == y) {
- cur->top = p->next;
- delete p;
- player1_field[x][y] = 0;
- if (!length(cur->top)) {
- player1=cur->next;
- delete cur;
- cout<<"Убил!\n";
- }
- }// Если самый первый
- else {
- while (p->next && !(p->next->x == x && p->next->y == y)) {
- p = p->next;
- }
- if (!p->next) while (cur->next) {
- p = cur->next->top;
- if (p->x == x && p->y == y) {
- cur->next->top = p->next;
- delete p;
- player1_field[x][y] = 0;
- if (!length(cur->next->top)) {
- cur->next = cur->next->next;
- delete cur->next;
- cout<<"Убил!\n";
- }
- }//Если первый в непервом корабле
- else {
- while (p->next && !(p->next->x == x && p->next->y == y)) {
- p = p->next;
- }
- if (!p->next) cur = cur->next;
- else {
- p->next = p->next->next;
- delete p->next;
- player1_field[x][y] = 0;
- if (!length(cur->next->top)) {
- cur->next = cur->next->next;
- delete cur->next;
- cout<<"Убил!\n";
- }
- }
- }//Если непервый в непервом корабле
- }//Если не в первом корабле
- else {
- p->next = p->next->next;
- delete p->next;
- player1_field[x][y] = 0;
- if (!length(cur->top)) {
- player1=cur->next;
- delete cur;
- cout<<"Убил!\n";
- }
- }
- }// Если в первом корабле
- }
- else {
- Tfleet cur = player2;
- Tship p = cur->top;
- if (p->x == x && p->y == y) {
- cur->top = p->next;
- delete p;
- player2_field[x][y] = 0;
- if (!length(cur->top)) {
- player2 = cur->next;
- delete cur;
- cout<<"Убил!\n";
- }
- }// Если самый первый
- else {
- while (p->next && !(p->next->x == x && p->next->y == y)) {
- p = p->next;
- }
- if (!p->next) while (cur->next) {
- p = cur->next->top;
- if (p->x == x && p->y == y) {
- cur->next->top = p->next;
- delete p;
- player2_field[x][y] = 0;
- if (!length(cur->next->top)) {
- cur->next = cur->next->next;
- delete cur->next;
- cout<<"Убил!\n";
- }
- }//Если первый в непервом корабле
- else {
- while (p->next && !(p->next->x == x && p->next->y == y)) {
- p = p->next;
- }
- if (!p->next) cur = cur->next;
- else {
- p->next = p->next->next;
- delete p->next;
- player2_field[x][y] = 0;
- if (!length(cur->next->top)) {
- cur->next = cur->next->next;
- delete cur->next;
- cout<<"Убил!\n";
- }
- }
- }//Если непервый в непервом корабле
- }//Если не в первом корабле
- else {
- p->next = p->next->next;
- delete p->next;
- player2_field[x][y] = 0;
- if (!length(cur->top)) {
- player2 = cur->next;
- delete cur;
- cout<<"Убил!\n";
- }
- }
- }// Если в первом корабле
- }
- }
- void show(int player) {
- if (player==1) {
- cout<<"Поле игрока 1\n";
- for (int j=0;j<10;j++)
- {for (int i=0;i<10;i++)
- cout<<player1_field[i][j];
- cout<<endl;
- }
- }
- else {
- cout<<"Поле игрока 2\n";
- for (int j=0;j<10;j++)
- {for (int i=0;i<10;i++)
- cout<<player2_field[i][j];
- cout<<endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement