This repository has been archived on 2026-07-28. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files

75 lines
1.4 KiB
C
Executable File

/*
12.На поле 10х10 клеток установить 5 двухпалубных кораблей. Корабли не соприкасаются.
*/
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#define N 10
#define AMOUNT 5
const char SHIP = '#';
const char VOID = '.';
char matrix[N][N];
void fillMatrix() {
for (int i=0;i<N;i++) {
for (int j=0;j<N;j++) {
matrix[i][j] = VOID;
}
}
}
void printMatrix() {
for (int i=0;i<N;i++) {
for (int j=0;j<N;j++) {
printf("%2c", matrix[i][j]);
}
puts("");
}
}
bool isCanPlace(int x, int y) {
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2;j++) {
if (matrix[abs(x-i)][abs(y-j)] == SHIP)
return false;
}
}
return true;
}
void setShips() {
for (int i = 0; i < AMOUNT; i++) {
int x = rand() % (N - 1);
int y = rand() % (N - 1);
if (isCanPlace(x, y)) {
matrix[x][y] = SHIP;
if (rand() % 2 == 0) {
matrix[x+1][y] = SHIP;
} else {
matrix[x][y+1] = SHIP;
}
} else {
i--;
}
}
}
int main() {
srand(time(0));
fillMatrix();
setShips();
printMatrix();
return 0;
}