#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
char** ask_map_size(int* n, int* m)
{
char** map;
do {
printf("input map size ex: 3 6 [min: 3 3]\n");
if (scanf("%d %d", n, m) == 2 && *n > 2 && *m > 2) {
break;
}
printf("map size is wrong...");
} while (1);
map = (char**)malloc(*n);
for (int i = 0; i < *n; i++) {
map[i] = (char*)malloc(*m);
for (int j = 0; j < *m; j++) {
map[i][j] = 0x00;
}
}
return map;
}
void free_map(char** map, int n, int m)
{
for (int i = 0; i < n; i++)
free(map[i]);
free(map);
}
void random_mark(char** map, int n, int m)
{
int cnt = 0;
int x, y;
srand(time(NULL));
while (cnt != 3) {
x = rand() % n;
y = rand() % m;
if (map[x][y] != '*') {
map[x][y] = '*';
cnt++;
printf("map[%d][%d] = '*'\n", x, y);
}
}
}
void quize(char** map, int n, int m)
{
int remain_cnt = 3;
int findx, findy;
int x = -1, y = -1;
while (remain_cnt != 0) {
findx = 0;
findy = 0;
printf("input x y\n");
scanf("%d %d", &x, &y);
if (x < 0 || x >= n || y < 0 || y >= m) {
printf("invlid x y value\n");
continue;
}
if (map[x][y] == '*') {
map[x][y] = 0x00;
remain_cnt--;
printf("search ok, remain mark cnt is %d\n", remain_cnt);
continue;
}
for (int i = 0; i < m; i++) {
if (map[x][i] == '*') {
findx = 1;
}
}
for (int i = 0; i < n; i++) {
if (map[i][y] == '*') {
findy = 1;
}
}
printf("result : %s %s\n", findx?"ok":"none", findy?"ok" : "none");
}
printf("congratulation !!\n");
}
int main(void)
{
int map_size_n = 0;
int map_size_m = 0;
char** map = ask_map_size(&map_size_n, &map_size_m);
random_mark(map, map_size_n, map_size_m);
quize(map, map_size_n, map_size_m);
free_map(map, map_size_n, map_size_m);
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: