- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
#include <iostream>
#include <deque>
#include <time.h>
using namespace std;
int arr[10][10] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 4, 0, 0, 0, 0, 0, 0, 0, 0,
0, 4, 0, 0, 0, 0, 0, 0, 0, 0,
0, 4, 0, 0, 0, 0, 0, 0, 0, 0,
0, 4, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
#define SHIPS 1
struct point{
int x, y;
};
int num = -1, count = 0;
deque<point> ship;
int way[] = {0, 0, 0, 0};
void step(){
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++)
cout << arr[i][j] << ' ' ;
cout << '\n';
}
cout << '\n';
if (num == ship.size()){
memset(way, 0, sizeof(way));
while(!ship.empty()){
arr[ship.front().x + 1][ship.front().y] = 7;
arr[ship.front().x + 1][ship.front().y + 1] = 7;
arr[ship.front().x][ship.front().y + 1] = 7;
arr[ship.front().x + 1][ship.front().y - 1] = 7;
arr[ship.front().x - 1][ship.front().y + 1] = 7;
arr[ship.front().x - 1][ship.front().y - 1] = 7;
arr[ship.front().x][ship.front().y - 1] = 7;
arr[ship.front().x - 1][ship.front().y] = 7;
ship.pop_front();
}
count++;
if (count == SHIPS) {
cout << "Win" << '\n';
exit(0);
}
cout << num << " was killed" << '\n';
num = -1;
}
int x, y;
point tmp;
if (ship.empty()){
x = rand()%10;
y = rand()%10;
while (arr[x][y] == 7){
x = rand()%10;
y = rand()%10;
}
if (arr[x][y] != 0 && arr[x][y] != 7){
num = arr[x][y];
tmp.x = x;
tmp.y = y;
ship.push_front(tmp);
arr[x][y] = 7;
if (x > 0) if (arr[x][y - 1] != 7) way[0] = 1;
if (y > 0) if (arr[x - 1][y] != 7) way[1] = 1;
if (x < 9) if (arr[x][y + 1] != 7) way[2] = 1;
if (y < 9) if (arr[x + 1][y] != 7) way[3] = 1;
step();
}else{
arr[x][y] = 7;
return;
}
}else{
int t = rand()%4;
while (way[t] == 0){
t = rand()%4;
}
switch(t){
case 0:
x = ship.back().x;
y = ship.back().y - 1;
if(arr[x][y] == num){
way[1] = 0;
way[3] = 0;
tmp.x = x;
tmp.y = y;
ship.push_back(tmp);
arr[x][y] = 7;
step();
}else{
arr[x][y] = 7;
way[0] = 0;
return;
}