Edit enemy's logic for step

This commit is contained in:
2025-08-30 17:24:23 +03:00
parent d4d30ae1fa
commit e63d3030ea
2 changed files with 91 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <iostream> #include <iostream>
#include <cstdbool> #include <cstdbool>
#include <array>
#ifdef WINDOWS #ifdef WINDOWS
#include <conio.h> #include <conio.h>

View File

@@ -4,6 +4,13 @@ static bool check_lose_win(auto& player) {
return player.empty(); return player.empty();
} }
static int Random_step() {
const std::array<KEY_CODES, 5> keys = {
ESC_KEY, UP_KEY, DOWN_KEY, LEFT_KEY, RIGHT_KEY
};
return keys[rand() % 5];
}
void Game::Random(char symbol) { void Game::Random(char symbol) {
auto isNearPlayer = [this](int x, int y) { auto isNearPlayer = [this](int x, int y) {
for (int dy = -1; dy <= 1; ++dy) { for (int dy = -1; dy <= 1; ++dy) {
@@ -28,8 +35,8 @@ void Game::Random(char symbol) {
int x = rand() % (SIZE_FOR_ARRAY + 1); int x = rand() % (SIZE_FOR_ARRAY + 1);
int y = rand() % (SIZE_FOR_ARRAY + 1); int y = rand() % (SIZE_FOR_ARRAY + 1);
if (field[y][x] == EMPTY_CELL && // && !isNearPlayer(x, y)
!isNearPlayer(x, y)) { if (field[y][x] == EMPTY_CELL) {
field[y][x] = symbol; field[y][x] = symbol;
if (symbol == ENEMY_CELL) if (symbol == ENEMY_CELL)
gamers[1].set_army(x, y); gamers[1].set_army(x, y);
@@ -140,19 +147,16 @@ void Game::init() {
void Game::play() { void Game::play() {
bool is_selected = false;
bool is_active = true; bool is_active = true;
bool is_selected = false;
bool unit_selected = false; bool unit_selected = false;
int current_unit_index = 0; int current_unit_index = 0;
auto& player_army = gamers[0].get_army(); auto& player_army = gamers[0].get_army();
auto& enemy_army = gamers[1].get_army();
int cursorX = -1, cursorY = -1; int cursorX = -1, cursorY = -1;
if (!player_army.empty()) {
auto [x, y] = player_army[0]->get_coordinates();
cursorX = x;
cursorY = y;
}
while (true) { while (true) {
if (is_active) { if (is_active) {
@@ -161,11 +165,14 @@ void Game::play() {
exit(0); exit(0);
} }
auto [x, y] = player_army[0]->get_coordinates();
cursorX = x;
cursorY = y;
while (!is_selected) { while (!is_selected) {
clearScreen(); clearScreen();
print_field(cursorX, cursorY); print_field(cursorX, cursorY);
std::cout << "Your turn! Use 'a' or 's' to select a unit, ENTER to confirm, ESC to exit.\n"; std::cout << "Your turn! Use 'a' or 'd' to select a unit, ENTER to confirm, ESC to exit.\n";
switch (_getch()) { switch (_getch()) {
case ESC_KEY: std::cout << "Exit\n"; exit(0); case ESC_KEY: std::cout << "Exit\n"; exit(0);
case LEFT_KEY: case LEFT_KEY:
@@ -207,11 +214,12 @@ void Game::play() {
std::cout << "Let's move!" << std::endl; std::cout << "Let's move!" << std::endl;
switch (_getch()) { switch (_getch()) {
case ESC_KEY: std::cout << "Exit\n"; exit(0); case ESC_KEY: std::cout << "Exit\n"; exit(0);
case UP_KEY: cursorY = (cursorY == 0) ? SIZE_FOR_ARRAY : cursorY - 1; break; case UP_KEY: cursorY = (cursorY == 0) ? SIZE_FOR_ARRAY : cursorY - 1; break;
case DOWN_KEY: cursorY = (cursorY == SIZE_FOR_ARRAY) ? 0 : cursorY + 1; break; case DOWN_KEY: cursorY = (cursorY == SIZE_FOR_ARRAY) ? 0 : cursorY + 1; break;
case LEFT_KEY: cursorX = (cursorX == 0) ? SIZE_FOR_ARRAY : cursorX - 1; break; case LEFT_KEY: cursorX = (cursorX == 0) ? SIZE_FOR_ARRAY : cursorX - 1; break;
case RIGHT_KEY: cursorX = (cursorX == SIZE_FOR_ARRAY) ? 0 : cursorX + 1; break; case RIGHT_KEY: cursorX = (cursorX == SIZE_FOR_ARRAY) ? 0 : cursorX + 1; break;
case ENTER: continue;
} }
is_moved = true; is_moved = true;
@@ -223,7 +231,7 @@ void Game::play() {
cursorY = cp_y; cursorY = cp_y;
break; break;
case ENEMY_CELL: case ENEMY_CELL:
std::cout << "BANG! GOT IT! GOT YOUR POINTS\n"; std::cout << "BANG! GOT IT! GET YOUR POINTS\n";
gamers[0].points(50.); gamers[0].points(50.);
std::swap(field[cursorY][cursorX], field[cp_y][cp_x]); std::swap(field[cursorY][cursorX], field[cp_y][cp_x]);
break; break;
@@ -236,12 +244,76 @@ void Game::play() {
std::swap(field[cursorY][cursorX], field[cp_y][cp_x]); std::swap(field[cursorY][cursorX], field[cp_y][cp_x]);
break; break;
} }
} }
is_active = false;
} }
else { else {
auto steps_for_enemy = [this](char c, int* _x, int* _y) {
int Y = *(_y);
int X = *(_x);
switch (c) {
case 'U':
while (field[X][Y] != PLAYER_CELL && field[X][Y] != FLAG_CELL) {
Y = (Y == 0) ? SIZE_FOR_ARRAY : Y - 1;
if (field[X][Y] == ENEMY_CELL)
return abs(*(_y) - Y);
}
return abs(*(_y)-Y);
case 'D':
while (field[X][Y] != PLAYER_CELL && field[X][Y] != FLAG_CELL) {
Y = (Y == 0) ? SIZE_FOR_ARRAY : Y + 1;
if (field[X][Y] == ENEMY_CELL)
return abs(*(_y)-Y);
}
return abs(*(_y)-Y);
case 'L':
while (field[X][Y] != PLAYER_CELL && field[X][Y] != FLAG_CELL) {
X = (X == 0) ? SIZE_FOR_ARRAY : X - 1;
if (field[X][Y] == ENEMY_CELL)
return abs(*(_x)-X);
}
return abs(*(_x)-X);
case 'R':
while (field[X][Y] != PLAYER_CELL && field[X][Y] != FLAG_CELL) {
X = (X == 0) ? SIZE_FOR_ARRAY : X + 1;
if (field[X][Y] == ENEMY_CELL)
return abs(*(_x)-X);
}
return abs(*(_x)-X);
default:
throw std::runtime_error("Error in method of enemy!");
}
};
if (check_lose_win(enemy_army)) {
std::cout << "YOU WIN\n";
exit(0);
}
int enemy_index = rand() % enemy_army.size();
auto [x, y] = enemy_army[enemy_index].get()->get_coordinates();
const int UP = steps_for_enemy('U', &x, &y);
const int DOWN = steps_for_enemy('D', &x, &y);
const int LEFT = steps_for_enemy('L', &x, &y);
const int RIGHT = steps_for_enemy('R', &x, &y);
const int max_val = std::max({ UP, DOWN, LEFT, RIGHT });
if (max_val == UP) {
cursorY = (cursorY == 0) ? SIZE_FOR_ARRAY : cursorY - 1;
}
else if (max_val == DOWN) {
cursorY = (cursorY == SIZE_FOR_ARRAY) ? 0 : cursorY + 1;
}
else if (max_val == LEFT) {
cursorX = (cursorX == 0) ? SIZE_FOR_ARRAY : cursorX - 1;
}
else if (max_val == RIGHT) {
cursorX = (cursorX == SIZE_FOR_ARRAY) ? 0 : cursorX + 1;
}
// TODO: Logic after enemy's step
} }
} }
} }