Edit enemy's logic for step
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <cstdbool>
|
||||
#include <array>
|
||||
|
||||
#ifdef WINDOWS
|
||||
#include <conio.h>
|
||||
|
||||
98
src/Game.cpp
98
src/Game.cpp
@@ -4,6 +4,13 @@ static bool check_lose_win(auto& player) {
|
||||
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) {
|
||||
auto isNearPlayer = [this](int x, int y) {
|
||||
for (int dy = -1; dy <= 1; ++dy) {
|
||||
@@ -28,8 +35,8 @@ void Game::Random(char symbol) {
|
||||
int x = 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;
|
||||
if (symbol == ENEMY_CELL)
|
||||
gamers[1].set_army(x, y);
|
||||
@@ -140,19 +147,16 @@ void Game::init() {
|
||||
|
||||
void Game::play() {
|
||||
|
||||
bool is_selected = false;
|
||||
bool is_active = true;
|
||||
bool is_selected = false;
|
||||
bool unit_selected = false;
|
||||
|
||||
int current_unit_index = 0;
|
||||
auto& player_army = gamers[0].get_army();
|
||||
auto& enemy_army = gamers[1].get_army();
|
||||
|
||||
int cursorX = -1, cursorY = -1;
|
||||
if (!player_army.empty()) {
|
||||
auto [x, y] = player_army[0]->get_coordinates();
|
||||
cursorX = x;
|
||||
cursorY = y;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
if (is_active) {
|
||||
|
||||
@@ -161,11 +165,14 @@ void Game::play() {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
auto [x, y] = player_army[0]->get_coordinates();
|
||||
cursorX = x;
|
||||
cursorY = y;
|
||||
|
||||
while (!is_selected) {
|
||||
clearScreen();
|
||||
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()) {
|
||||
case ESC_KEY: std::cout << "Exit\n"; exit(0);
|
||||
case LEFT_KEY:
|
||||
@@ -212,6 +219,7 @@ void Game::play() {
|
||||
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 RIGHT_KEY: cursorX = (cursorX == SIZE_FOR_ARRAY) ? 0 : cursorX + 1; break;
|
||||
case ENTER: continue;
|
||||
}
|
||||
|
||||
is_moved = true;
|
||||
@@ -223,7 +231,7 @@ void Game::play() {
|
||||
cursorY = cp_y;
|
||||
break;
|
||||
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.);
|
||||
std::swap(field[cursorY][cursorX], field[cp_y][cp_x]);
|
||||
break;
|
||||
@@ -236,12 +244,76 @@ void Game::play() {
|
||||
std::swap(field[cursorY][cursorX], field[cp_y][cp_x]);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
is_active = false;
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user