Creative Commons License Foxbond's Repo

/**(c) 2016 Michał (Foxbond) Chraniuk */
#include <stdio.h>
#include <stdlib.h>

int map[10][10];

void prepareMap() {
	int i, xi;
	for (i = 0; i < 10; i++) {
		for (xi = 0; xi < 10; xi++) {
			map[i][xi] = 0;
		}
	}
}

int placeObstacle(int x, int y) {
	if (map[y][x] != 0) {
		return 0;
	}
	map[y][x] = 3;
	return 1;
}

void shoot(int x, int y) {
	int i, xi;

	//right
	for (xi = x + 1; xi < 10; xi++) {
		if (map[y][xi] == 3) break;
		map[y][xi] = 2;
	}

	//left
	for (xi = x - 1; xi >= 0; xi--) {
		if (map[y][xi] == 3) break;
		map[y][xi] = 2;
	}

	//down
	for (i = y+1; i < 10; i++) {
		if (map[i][x] == 3) break;

		map[i][x] = 2;
	}

	//up
	for (i = y - 1; i >= 0; i--) {
		if (map[i][x] == 3) break;

		map[i][x] = 2;
	}

	//SE
	for (i = y + 1, xi = x + 1; i < 10, xi < 10; i++, xi++) {
		if (map[i][xi] == 3) break;

		map[i][xi] = 2;
	}

	//SW
	for (i = y + 1, xi = x - 1; i < 10, xi >= 0; i++, xi--) {
		if (map[i][xi] == 3) break;

		map[i][xi] = 2;
	}

	//NW
	for (i = y - 1, xi = x - 1; i >= 0, xi >= 0; i--, xi--) {
		if (map[i][xi] == 3) break;
		map[i][xi] = 2;
	}

	//NE
	for (i = y - 1, xi = x + 1; i >= 0, xi < 10; i--, xi++) {
		if (map[i][xi] == 3) break;

		map[i][xi] = 2;
	}
}

int placePlayer(int x, int y) {
	if (map[y][x] != 0) {
		return 0;
	}

	map[y][x] = 1;
	shoot(x, y);
	return 1;
}



void initMap() {
	placeObstacle(2, 1);
	placeObstacle(2, 2);
	placeObstacle(0, 0);
	placeObstacle(0, 0);
	placeObstacle(0, 0);
	placeObstacle(0, 0);
	placeObstacle(0, 0);
	placeObstacle(0, 0);

	placePlayer(9, 9);
	placePlayer(6, 4);
	placePlayer(0, 0);
}

void changeColor(char color) {
	char temp[10];
	sprintf(temp, "COLOR 0%c\0", color);
	//system(temp);
}

void displayMap() {
	int i, xi;
	char obstacle = 219;
	char empty = 176;
	char player = 'P';
	char shoot = 248;
	char green = '2';
	char red = '4';
	char blue = '1';
	char gray = '8';
	char standard = '7';

	for (i = 0; i < 10; i++) {
		for (xi = 0; xi < 10; xi++) {
			if (map[i][xi] == 0) {
				changeColor(green);
				printf("%c", empty);
				changeColor(standard);
			} else if (map[i][xi] == 1) {
				changeColor(blue);
				printf("%c", player);
				changeColor(standard);
			} else if (map[i][xi] == 2) {
				changeColor(red);
				printf("%c", shoot);
				changeColor(standard);
			} else if (map[i][xi] == 3) {
				changeColor(gray);
				printf("%c", obstacle);
				changeColor(standard);
			} else {
				printf("!");
			}
		}
		printf("\n");
	}
	printf("\n");
}

void addPlayer(int deep) {
	int i, xi;
	if (deep < 1) {
		return;
	}

	int placed = 0;
	for (i = 0; i < 10; i++) {
		for (xi = 0; xi < 10; xi++) {
			placed = placePlayer(xi, i);
			if (placed) break;
		}
		if (placed) break;
	}

	if (placed) {
		displayMap();
		addPlayer(--deep);
	}
	else {
		printf("Nie mozna ustawic wiecej graczy, ostateczna plansza:\n");
		displayMap();
	}
}

int main() {
	int n;
	prepareMap();

	initMap();

	displayMap();

	printf("Ile graczy ustawic? ");
	scanf("%d", &n);

	addPlayer(n);

	getch();
	return 0;
}

> Back