@Nazarko03

Как решить ошибки в коде?

Ошибки:
78 19 C:\Users\mrori\OneDrive\Документы\main.cpp [Error] 'Direction' is not a class or namespace
97 12 C:\Users\mrori\OneDrive\Документы\main.cpp [Error] 'Direction' is not a class or namespace
101 12 C:\Users\mrori\OneDrive\Документы\main.cpp [Error] 'Direction' is not a class or namespace
// ConsoleApplication10.cpp : Defines the entry point for the console application.
//


#include <clocale>
#include <conio.h>
#include <stdlib.h>
#include <iostream>
#include <ctime>


using namespace std;

// enum çáåð³ãຠíàïðÿìêè: ãîðèçîíòàëüíèé, âåðòèêàëüíèé
enum Direction{H, V};

char numbers[10] = { '0','1','2','3','4','5','6','7','8','9'};
// êàðòà ðàçì³ðîì â 13 ñòð³÷îê òà 29 ñòîâáö³â
const int s = 13, c = 29;
char map[s][c] = {
		"  0123456789     0123456789 ",
		" #----------#   #----------#",
		"0|          |  0|          |",
		"1|          |  1|          |",
		"2|          |  2|          |",
		"3|          |  3|          |",
		"4|          |  4|          |",
		"5|          |  5|          |",
		"6|          |  6|          |",
		"7|          |  7|          |",
		"8|          |  8|          |",
		"9|          |  9|          |",
		" #----------#   #----------#" };

void MapInit(char map[s][c]);
void Show(char map[s][c]);
int Input(char& , char&);
void Test();
int CheckEnding();


// Êëàñ Áîéîâîãî ïîëÿ
class BattleField
{
public:
	int Ships[10][10]; // ìàñèâ êîðàáë³â
	int Hits[10][10]; // ìàñèâ âèñòð³ë³â

	void ShipsInit(bool autoPlace)
	{
		// ²í³ö³àë³çàö³ÿ ìàñèâ³â hits ³ ships
		for (int i = 0; i < 10; i++)
		{
			for (int j = 0; j< 10; j++)
			{
				Ships[i][j] = 1;
				Hits[i][j] = 0;
			}
		}
		// Ðîçñòàíîâêà êîðàáë³â
		Set(4, autoPlace);
		Set(3, autoPlace);
		Set(3, autoPlace);
		Set(2, autoPlace);
		Set(2, autoPlace);
		Set(2, autoPlace);
		Set(1, autoPlace);
		Set(1, autoPlace);
		Set(1, autoPlace);
		Set(1, autoPlace);
	}

	void Set (int deck, bool automatically)
	{
		int my = deck - 1;
		bool isset = 0;
		int s,c;
		Direction dir = Direction::H;
		while (isset == 0) // ïåðåâ³ðêà ÷è âäàëîñÿ ðîçì³ñòèòè êîðàáåëü
		{
			if (automatically)
			{
				dir = static_cast<Direction>(rand()%2); // âèáèðàºìî íàïðÿì

				s = rand()%10; //âèïàäêîâèì ÷èíîì âèçíà÷àþòüñÿ êîîðäèíàòè
				c = rand()%10;
			}
			else
			{
				char tempDir;
				cout << "Íàïðÿì H/V: ";
				cin >> tempDir;
				switch (tempDir)
				{
				case 'H':
				case 'h':
					dir = Direction::H;
					break;
				case 'V':
				case 'v':
					dir = Direction::V;
					break;
				}

				cout << "Ðÿäîê: ";
				cin >> s;

				cout << "Ñòîâáåöü: ";
				cin >> c;
			}

			int e = 0;
			switch (dir)
			{
			case H:
				if (Ships[s][c+deck-1] == 1)
				{
					e = ShipPlace(s,c,dir,deck); // ïåðåâ³ðêà ñóì³æíèõ êë³òèí
					if (e == 0)
					{
						for (int i = 0; i < deck; i++)
						{
							Ships[s][c+i] = 2; // ðîçì³ùóºìî êîðàáåëü â ìàñèâ³ ships
						}
						isset = 1;
					}
				}
				break;
			case V:
				if (Ships[s+deck-1][c] == 1)
				{
					e = ShipPlace(s,c,dir,deck);
					if (e == 0)
					{
						for (int i = 0; i < deck; i++)
						{
							Ships[s+i][c] = 2;
						}
						isset = 1;
					}
				}
				break;
			} 
		} 
		MapInit(map);
		Show(map);
		//system("pause");
		//system("cls");
	}

	/*
	Ôóíêö³ÿ ïåðåâ³ðÿº, ÷è ìîæíà â äàí³é êîîðäèíàò³ [s] [c]
	ðîçì³ñòèòè êîðàáåëü ç ïàëóáàìè deck.
	Ó êîìåíòàðÿõ ïîêàçàíî ÿê³ êë³òèíè ïåðåâ³ðÿþòüñÿ ïðè óñòàíîâö³
	÷îòèðèïàëóáíîìó êîðàáëÿ â [4] [3]
	*/
	int ShipPlace(int s, int c, Direction dir, int deck)
	{
		int e = 0;
		switch (dir)
		{
		case H:
			if (Ships[s-1][c-1] == 2)
			{
				e = 1;
			}
			if (Ships[s-1][c+deck] == 2)
			{
				e = 1;
			}
			if (Ships[s+1][c-1] == 2)
			{
				e = 1;
			}
			if (Ships[s+1][c+deck] == 2)
			{
				e = 1;
			}

			if (Ships[s][c-1] == 2)
			{
				e = 1;
			}
			if (Ships[s][c+deck] == 2)
			{
				e = 1;
			}
			for (int i = 0; i < deck; i++)
			{
				if (Ships[s-1][c+i] == 2)
				{
					e = 1;
				}
				if (Ships[s+1][c+i] == 2)
				{
					e = 1;
				}
			}
			break;
		case V:
			if (Ships[s-1][c-1] == 2)
			{
				e = 1;
			}
			if (Ships[s-1][c+1] == 2)
			{
				e = 1;
			}
			if (Ships[s+deck][c-1] == 2)
			{
				e = 1;
			}
			if (Ships[s+deck][c+1] == 2)
			{
				e = 1;
			}

			if (Ships[s-1][c] == 2)
			{
				e = 1;
			}
			if (Ships[s+deck][c] == 2)
			{
				e = 1;
			}
			for (int i = 0; i < deck; i++)
			{
				if (Ships[s+i][c-1] == 2)
				{
					e = 1;
				}
				if (Ships[s+i][c+1] == 2)
				{
					e = 1;
				}
			}
			break;
		}
		return e;
	}
};

class Player : public BattleField
{
public:
	bool DefeatFlag; // çì³ííà â ÿê³é çáåð³ãàþòüñÿ äàí³ ïðî ïîðàçêó

	Player (): DefeatFlag(0)	{} // êîíñòðóêòîð

	// Õ³ä êîìï'þòåðà: âíîñèìî çì³íè â ìàñèâè human.ships ³ computer.hits
	void Turn(Player& enemy)
	{
		bool flag = 0;
		while (flag == 0)
		{
			int character = rand()%10; // Êîîðäèíàòè çà ÿêèìè áóäå ñòð³ëÿòè êîìï'þòåð
			int digit = rand()%10;     // âèáèðàþòüñÿ âèïàäêîâî
			if (Hits[character][digit] != 1) // ïåðåâ³ðêà: âèáèðàâ ÷è âæå êîìï'þòåð ö³ êîîðäèíàòè
			{
				Hits[character][digit] = 1;
				flag = 1;
				if (enemy.Ships[character][digit] == 2)
				{
					enemy.Ships[character][digit] = 3;
				}
			}
		}
	}

	// Õ³ä ãðàâöÿ: âíîñèìî çì³íè â ìàñèâè computer.ships ³ human.hits
	void Turn(Player& enemy, int character, int digit)
	{
		Hits[character][digit] = 1;
		if (enemy.Ships[character][digit] == 2)
		{
			enemy.Ships[character][digit] = 3;
		}
	}
};

Player player1;
Player player2;

// äîáàâëåííÿ â ìàñèâ map ³íôîðìàö³¿ ³ç human.ships.
void MapInit(char map[s][c])
{
	for (int i=0;i < 10; i++)
	{
		for (int j=0; j < 10; j++)
		{
			if (player1.Ships[i][j] == 2)
				map[i+2][j+2] = 'Ê';
		}
	}
}

/*
Ñïî÷àòêó â ìàñèâ maps çàíîñèòüñÿ ³íôîðìàö³ÿ ïðî ïîñòð³ëè çðîáëåíèõ
ïðîòèâíèêàìè.
Ïîò³ì â³äáóâàºòüñÿ âèâåäåííÿ íà åêðàí map. ïðè öüîìó âèêîðèñòîâóþòüñÿ
ìàñèâè ships ³ hits êëàñó player.
*/
void Show(char map[s][c])
{
	for (int i=0; i<10; i++)
	{
		for (int j=0; j<10; j++)
		{
			/* Çàïîâíþºìî ïîëå ãðàâöÿ */
			if (player2.Hits[i][j] == 1 && player1.Ships[i][j] == 3)
			{
				map[i+2][j+2] = 'Õ';
			}
			else if (player2.Hits[i][j] == 1 && player1.Ships[i][j] != 3)
			{
				map[i+2][j+2] = 'Î';
			}

			/* Çàïîâíþºìî ïîëå êîìï'þòåðà */
			if (player2.Ships[i][j] == 3 && player1.Hits[i][j] == 1)
			{
				map[i+2][j+2+15] = 'Õ';
			}
			else if (player2.Ships[i][j] != 3 && player1.Hits[i][j] == 1)
			{
				map[i+2][j+2+15] = 'O';
			}
		} 
	}

	// Ïîñèìâîëüíî¿ âèâîä ìàñèâó map íà åêðàí
	for (int i = 0; i < s; i++)
	{
		for (int j = 0; j < c; j++)
		{
			cout << map[i][j];
		}
		cout << "\n";
	}
}

// ââåäåííÿ êîîðäèíàò
int Input(char& character, char& digit)
{
	character = _getche(); // ââåäåííÿ "ãîðèçîíòàëüíî¿" êîîðäèíàòè
	int match = 0;
	for (int i = 0; i < 10; i++)
		if (numbers[i] == character)
		{
			match = 1;
			character = i;
		}
		if (match == 0)	
			return 1;
		cout << '-';
		match = 0;
		digit = _getche(); // ââåäåííÿ "âåðòèêàëüíî¿" êîîðäèíàòè
		for (int i = 0; i < 10; i++)
			if (numbers[i] == digit)
			{
				match = 1;
				digit = i;
			}
			if (match == 0)
				return 1;
			return 0;	
}

void Test()
{
	cout << endl;
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			cout << player1.Hits[i][j];
		}
		cout << "  ";
		for (int j = 0; j < 10; j++)
		{
			cout << player2.Hits[i][j];
		}
		cout << endl;
	}
	cout << endl;
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			cout << player1.Ships[i][j];
		}
		cout << "  ";
		for (int j = 0; j < 10; j++)
		{
			cout << player2.Ships[i][j];
		}
		cout << endl;
	}
}

// Ó ôóíêö³¿ âñòàíîâëþþòüñÿ ³ ïåðåâ³ðÿþòüñÿ ïðàïîðè defeat_flag ãðàâö³â.
int CheckEnding()
{
	int flag = 0;
	int human_flag = 0;
	int computer_flag = 0;
	for (int i=0; i<10;i++)
	{
		for (int j=0; j<10;j++)
		{
			if (player1.Ships[i][j] == 2)
				human_flag = 1; // ó êîðèñòóâà÷à ùå çàëèøèëèñÿ íåóøêîäæåí³ êîðàáë³
			if (player2.Ships[i][j] == 2)
				computer_flag = 1; // ó êîìï'þòåðà ùå çàëèøèëèñÿ íåóøêîäæåí³ êîðàáë³
		}
	}
	if (human_flag == 0)
		flag = 2;
	if (computer_flag == 0)
		flag = 1;
	if (flag == 1)
	{
		cout << "Ïåðåì³ã ãðàâåöü!" << endl;
		return 2;
	}
	if (flag == 2)
	{
		cout << "Ïåðåì³ã êîìï'þòåð!" << endl;
		return 2;
	}
}

int main()
{
	setlocale(LC_CTYPE, "Russian");
	int message = 0; // çì³ííà çáåð³ãຠêîäè ïîâ³äîìëåíü
	
	// âñòàíîâëåííÿ ïî÷àòêîâãî çíà÷åííÿ ãåíåðàòîðà âèïàäêîâèõ ÷èñåë
	srand( static_cast<unsigned int>(time(NULL)) );

	char temp;
	cout << "Ðîçòàøîâóâàòè àâòîìàòè÷íî? y/n: ";
	cin >> temp;
	bool autoPlace = temp == 'y' ? true : false;

	player1.ShipsInit(autoPlace); // ³í³ö³àë³çàö³ÿ ìàñèâ³â ëþäèíè
	player2.ShipsInit(true); // ³í³ö³àë³çàö³ÿ ìàñèâ³â êîìïþòåðà
	MapInit(map); // äîáàâëåííÿ â ìàñèâ map ³íôîðìàö³¿ ³ç human.ships

	

/*
	message ìîæå ïðèéìàòè íàñòóïí³ çíà÷åííÿ:
	0 - âñå ãóä
	1 - êîðèñòóâà÷ íàòèñíóâ íåâ³ðíó êëàâ³øó
	2 - õòîñü ïåðåì³ã, ê³íåöü ãðè
	*/
	while (message != 2)
	{
		int user_input = 0;
		system("cls"); 
		Show(map);   
		if (message == 1)
		{
			cout << "Íåäîïóñòèìå çíà÷åííÿ!" << endl;
		}
		message = 0;   // îáíóëåííÿ message

		char character, digit;
		user_input = Input(character, digit);
		if (user_input == 1)
		{
			message = 1;
			continue; 
		}
		player1.Turn(player2, character, digit);
		player2.Turn(player1);
		message = CheckEnding();
	}
	system("pause");
	return 0;
}
  • Вопрос задан
  • 49 просмотров
Пригласить эксперта
Ответы на вопрос 1
@res2001
Developer, ex-admin
Посмотрите, как правильно обращаться с перечислениями: https://en.cppreference.com/w/cpp/language/enum
Тот вариант обращения к перечислению, который вы используете в коде называется Scoped enumerations. Чтоб его использовать вы должны объявить Direction так: enum class Direction {....
Если же перечисление объявлять так как у вас, то обращаться к нему нужно без указания Direction.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы