Correct field's initialization
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -9,6 +9,7 @@
|
||||
*.user
|
||||
*.userosscache
|
||||
*.sln.docstates
|
||||
build/
|
||||
|
||||
# User-specific files (MonoDevelop/Xamarin Studio)
|
||||
*.userprefs
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
// BattleCap.h : включаемый файл для стандартных системных включаемых файлов
|
||||
// или включаемые файлы для конкретного проекта.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// TODO: установите здесь ссылки на дополнительные заголовки, требующиеся для программы.
|
||||
@@ -1,8 +1,15 @@
|
||||
# CMakeList.txt: проект CMake для BattleCap; включите исходный код и определения,
|
||||
# CMakeList.txt: проект CMake для BattleCap; включите исходный код и определения,
|
||||
# укажите здесь логику для конкретного проекта.
|
||||
#
|
||||
cmake_minimum_required (VERSION 3.8)
|
||||
|
||||
if (WIN32)
|
||||
add_compile_definitions(WINDOWS)
|
||||
else()
|
||||
add_compile_definitions(LINUX)
|
||||
endif()
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
# Включение горячей перезагрузки для компиляторов MSVC, если поддерживается.
|
||||
if (POLICY CMP0141)
|
||||
cmake_policy(SET CMP0141 NEW)
|
||||
@@ -11,8 +18,9 @@ endif()
|
||||
|
||||
project ("BattleCap")
|
||||
|
||||
include_directories(include)
|
||||
# Добавьте источник в исполняемый файл этого проекта.
|
||||
add_executable (BattleCap "BattleCap.cpp" "BattleCap.h")
|
||||
add_executable (BattleCap "src/BattleCap.cpp" "include/BattleCap.h" "include/Soldier.h" "include/Game.h" "include/Infantry.h" "src/Game.cpp" "include/Player.h" "include/stdafx.h")
|
||||
|
||||
if (CMAKE_VERSION VERSION_GREATER 3.12)
|
||||
set_property(TARGET BattleCap PROPERTY CXX_STANDARD 20)
|
||||
|
||||
5
include/BattleCap.h
Normal file
5
include/BattleCap.h
Normal file
@@ -0,0 +1,5 @@
|
||||
// или включаемые файлы для конкретного проекта.
|
||||
|
||||
#pragma once
|
||||
|
||||
// TODO: установите здесь ссылки на дополнительные заголовки, требующиеся для программы.
|
||||
29
include/Game.h
Normal file
29
include/Game.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
#include "Player.h"
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
constexpr auto SIZE = 10;
|
||||
constexpr auto SIZE_FOR_ARRAY = SIZE - 1;
|
||||
|
||||
class Game {
|
||||
private:
|
||||
char field[SIZE][SIZE];
|
||||
std::unique_ptr<Player[]> gamers;
|
||||
public:
|
||||
Game() : gamers(std::make_unique<Player[]>(2)) {
|
||||
for (int i = 0; i < SIZE; ++i) {
|
||||
for (int j = 0; j < SIZE; ++j) {
|
||||
field[i][j] = '*';
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
void info();
|
||||
void init();
|
||||
void play();
|
||||
|
||||
private:
|
||||
void clearScreen();
|
||||
void print_field();
|
||||
};
|
||||
14
include/Infantry.h
Normal file
14
include/Infantry.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include "Soldier.h"
|
||||
class Infantry : public Soldier {
|
||||
public:
|
||||
Infantry() : Soldier() {};
|
||||
int step() override
|
||||
{
|
||||
return 10;
|
||||
}
|
||||
void fire() override
|
||||
{
|
||||
return;
|
||||
}
|
||||
};
|
||||
21
include/Player.h
Normal file
21
include/Player.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "stdafx.h"
|
||||
|
||||
class Player {
|
||||
private:
|
||||
char name;
|
||||
std::vector<std::unique_ptr<Soldier>> army;
|
||||
double score;
|
||||
public:
|
||||
Player() {
|
||||
score = 0.;
|
||||
for (auto i = 0; i < 4; ++i) {
|
||||
army.push_back(std::make_unique<Infantry>());
|
||||
}
|
||||
}
|
||||
std::vector<std::unique_ptr<Soldier>>& get_army() {
|
||||
return army;
|
||||
}
|
||||
};
|
||||
14
include/Soldier.h
Normal file
14
include/Soldier.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
class Soldier {
|
||||
private:
|
||||
double HP;
|
||||
double attack;
|
||||
double defence;
|
||||
char type;
|
||||
protected:
|
||||
Soldier() : HP(100), attack(1), defence(1) {};
|
||||
Soldier(double _HP, double _attack, double _defence) : HP(_HP), attack(_attack), defence(_defence) {};
|
||||
public:
|
||||
virtual int step() = 0;
|
||||
virtual void fire() = 0;
|
||||
};
|
||||
31
include/stdafx.h
Normal file
31
include/stdafx.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <cstdbool>
|
||||
|
||||
#ifdef WINDOWS
|
||||
#include <conio.h>
|
||||
#else
|
||||
#include "curses.h"
|
||||
#endif // WINDOWS
|
||||
|
||||
#include "Soldier.h"
|
||||
#include "Infantry.h"
|
||||
#include "Game.h"
|
||||
#include "Player.h"
|
||||
|
||||
enum KEY_CODES
|
||||
{
|
||||
ESC_KEY = 27,
|
||||
UP_KEY = 119,
|
||||
DOWN_KEY = 115,
|
||||
LEFT_KEY = 97,
|
||||
RIGHT_KEY = 100,
|
||||
ENTER = 13
|
||||
};
|
||||
|
||||
enum CELL_CHARS {
|
||||
EMPTY_CELL = '*',
|
||||
CURSOR_CELL = '?',
|
||||
ENEMY_CELL = 'E',
|
||||
PLAYER_CELL = 'I'
|
||||
};
|
||||
@@ -1,12 +1,13 @@
|
||||
// BattleCap.cpp: определяет точку входа для приложения.
|
||||
//
|
||||
|
||||
#include "BattleCap.h"
|
||||
|
||||
using namespace std;
|
||||
#include "stdafx.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "Hello CMake." << endl;
|
||||
Game game;
|
||||
game.init();
|
||||
game.play();
|
||||
return 0;
|
||||
|
||||
}
|
||||
135
src/Game.cpp
Normal file
135
src/Game.cpp
Normal file
@@ -0,0 +1,135 @@
|
||||
#include "Game.h"
|
||||
|
||||
void Game::clearScreen() {
|
||||
#ifdef _WIN32
|
||||
system("cls");
|
||||
#else
|
||||
system("clear");
|
||||
#endif
|
||||
}
|
||||
|
||||
void Game::print_field() {
|
||||
clearScreen();
|
||||
for (const auto& row : field) {
|
||||
for (char cell : row) {
|
||||
std::cout << cell << ' ';
|
||||
}
|
||||
std::cout << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
void Game::info() {
|
||||
while (true) {
|
||||
clearScreen();
|
||||
|
||||
std::cout << "Main author: ParkSuMin\n"
|
||||
<< "Email: tsettaro@paksurion.ru\n";
|
||||
int key = _getch();
|
||||
if (key == ESC_KEY) { // Escape
|
||||
std::cout << "Exit" << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Game::init() {
|
||||
|
||||
auto isNearPlayer = [this](int x, int y) {
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> 8 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
for (int dy = -1; dy <= 1; ++dy) {
|
||||
for (int dx = -1; dx <= 1; ++dx) {
|
||||
if (dx == 0 && dy == 0) continue; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
int nx = (x + dx + SIZE_FOR_ARRAY + 1) % (SIZE_FOR_ARRAY + 1);
|
||||
int ny = (y + dy + SIZE_FOR_ARRAY + 1) % (SIZE_FOR_ARRAY + 1);
|
||||
|
||||
if (field[ny][nx] == PLAYER_CELL) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
for (const auto& element : gamers[0].get_army()) {
|
||||
int x = 0, y = 0;
|
||||
bool found = false;
|
||||
|
||||
while (true) {
|
||||
int start = 0;
|
||||
int end = SIZE_FOR_ARRAY;
|
||||
int i = rand() % (end - start + 1) + start;
|
||||
int j = rand() % (end - start + 1) + start;
|
||||
if (field[i][j] != PLAYER_CELL && field[i][j] == EMPTY_CELL) {
|
||||
field[i][j] = CURSOR_CELL;
|
||||
x = j;
|
||||
y = i;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!found) return;
|
||||
|
||||
while (true) {
|
||||
clearScreen();
|
||||
print_field();
|
||||
|
||||
int key = _getch();
|
||||
if (key == ESC_KEY) {
|
||||
std::cout << "Exit\n";
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int newX = x, newY = y;
|
||||
|
||||
switch (key) {
|
||||
case UP_KEY: newY = (y == 0) ? SIZE_FOR_ARRAY : y - 1; break;
|
||||
case DOWN_KEY: newY = (y == SIZE_FOR_ARRAY) ? 0 : y + 1; break;
|
||||
case LEFT_KEY: newX = (x == 0) ? SIZE_FOR_ARRAY : x - 1; break;
|
||||
case RIGHT_KEY: newX = (x == SIZE_FOR_ARRAY) ? 0 : x + 1; break;
|
||||
case ENTER: found = false; field[newY][newX] = PLAYER_CELL; break;
|
||||
default: continue;
|
||||
}
|
||||
if (!found) break;
|
||||
if (field[newY][newX] == PLAYER_CELL) {
|
||||
newX = x;
|
||||
newY = y;
|
||||
continue;
|
||||
}
|
||||
field[y][x] = EMPTY_CELL;
|
||||
field[newY][newX] = CURSOR_CELL;
|
||||
x = newX;
|
||||
y = newY;
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& element : gamers[1].get_army()) {
|
||||
int attempts = 0;
|
||||
const int MAX_ATTEMPTS = 100;
|
||||
|
||||
while (attempts < MAX_ATTEMPTS) {
|
||||
int x = rand() % (SIZE_FOR_ARRAY + 1);
|
||||
int y = rand() % (SIZE_FOR_ARRAY + 1);
|
||||
|
||||
if (field[y][x] == EMPTY_CELL && // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>
|
||||
field[y][x] != PLAYER_CELL && // <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (<28><><EFBFBD>. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
||||
!isNearPlayer(x, y)) { // <20><> <20><><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
field[y][x] = ENEMY_CELL;
|
||||
break;
|
||||
}
|
||||
attempts++;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void Game::play() {
|
||||
#ifndef DEBUG
|
||||
print_field();
|
||||
#else
|
||||
return;
|
||||
#endif // !DEBUG
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user