Рекомендация: прочитайте задачу! Выполните ее и сравните полученный результат с картинкой CMD
Если вы полностью уверены что не можете осилить эту задачу, советую вам просмотреть код и полностью в нем разобраться! Протестировать все в отладчике!
Задача написать игру Мосрчкой бой без использования ООП
#pragma once
#include <iostream>
#include <time.h>
#include <string>
#include <Windows.h>
#include <conio.h>
#include "clocale"
#include <fstream>
#include <io.h>
#include <fcntl.h>
using namespace std;
void Warning_Welcome();
void Print_Map(char ** &arr, int row, int col);
char ** Create_Map(char ** &arr, int row, int col);
string ** Create_Map(string ** &arr, int row, int col);
void Delete_New(char ** &arr, int row);
void Delete_New(string** &arr, int row);
void Map(char ** &arr, int row, int col);
int Convert_S_One(string s);
int Convert_S_Two(string s);
void Create_Ship_User(char ** &arr, int row, int col, int Ship_Count, int Cell_Counter);
void Ship_Outline(char ** &arr, int row, int col, int SET);
void Create_Ship_Rand(char ** &arr, int row, int col);
void Dead_Ship_Counter(char ** &arr_pc, char ** &arr_Battle, int I, int J);
void Dead_Ship_Counter_PC(char ** &arr, int i, int j);
int Percentage_Win(char ** &arr, int row, int col);
int Dead_Ship_Counter_User(char ** &arr, int i, int j);
void Read_File_Cout(string name_file);
void Write_File(string Write_Str, string name_file);
void Write_File_ADD_Statistic(string name_file, string Name_Player, int Stroke_Counter);
void Wins_Display();
#define STOP_Console system("pause");
#define Clear_Console system("CLS");
struct File_Data {
string Player_Move;
string Read_Data;
};
//Functions.cpp
#include "H.h"
void Warning_Welcome() { // WELCOME
cout << "This game is written under windows 10 console!" << endl;
system("color 0A");
STOP_Console
Clear_Console
system("color 0C");
cout << "S ";
Sleep(300);
cout << "E ";
Sleep(300);
cout << "A ";
Sleep(300);
cout << "|| ";
cout << "|| ";
cout << "|| ";
Sleep(300);
cout << "B ";
Sleep(300);
cout << "A ";
Sleep(300);
cout << "T ";
Sleep(300);
cout << "T ";
Sleep(300);
cout << "L ";
Sleep(300);
cout << "E ";
Sleep(300);
cout << endl << endl;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
for (int z = 0; z < 2; z++) {
for (int i = 0; i < 15; i++) {
SetConsoleTextAttribute(hStdOut, i);
cout << "L O A D" << endl;
cout << "\t\t |= = | |= = | |= = | \n";
cout << "\t\t __ | __ | __ | __ | __ | __ | _ \n";
cout << "\t\t __ | ___________________ | ___ \n";
cout << "\t\t __ | __[]__[]__[]__[]__[]__[]__ | ___ \n";
cout << "\t\t| ................................ / \n";
cout << "\t\t \............................... / \n";
Sleep(200);
Clear_Console
}
}
STOP_Console
Clear_Console
system("color 0A");
cout << endl;
}
void Wins_Display() {
int GO = 0;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
srand(time(NULL));
while (1) {
GO++;
SetConsoleTextAttribute(hStdOut, GO);
cout << rand() % 2;
if (GO == 15)
GO = 0;
}
}
int Percentage_Win(char ** &arr, int row, int col) {
const int Number_Squares = 20; //Squares
int count = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (arr[i][j] == 'X') {
count += 5;
}
}
}
return count;
}
void Dead_Ship_Counter(char ** &arr_pc,char ** &arr_Battle, int i, int j) { //Analyzing dead ship or not
int tmp = 0;
if (arr_pc[i + 1][j] == char(254))
tmp = 1;
if (arr_pc[i - 1][j] == char(254))
tmp = 1;
if (arr_pc[i][j + 1] == char(254))
tmp = 1;
if (arr_pc[i][j - 1] == char(254))
tmp = 1;
if (i < 10)
if (arr_pc[i + 1][j] == 'X' && arr_pc[i + 2][j] == char(254))
tmp = 1;
if (i > 0)
if (arr_pc[i - 1][j] == 'X' && arr_pc[i - 2][j] == char(254))
tmp = 1;
if (j < 10)
if (arr_pc[i][j + 1] == 'X' && arr_pc[i][j + 2] == char(254))
tmp = 1;
if (j > 0)
if (arr_pc[i][j - 1] == 'X' && arr_pc[i][j - 2] == char(254))
tmp = 1;
if (i < 9)
if (arr_pc[i + 1][j] == 'X' && arr_pc[i + 2][j] == 'X' && arr_pc[i + 3][j] == char(254))
tmp = 1;
if (i > 1)
if (arr_pc[i - 1][j] == 'X' && arr_pc[i - 2][j] == 'X' && arr_pc[i - 3][j] == char(254))
tmp = 1;
if (j < 9)
if (arr_pc[i][j + 1] == 'X' && arr_pc[i][j + 2] == 'X' && arr_pc[i][j + 3] == char(254))
tmp = 1;
if (j > 1)
if (arr_pc[i][j - 1] == 'X' && arr_pc[i][j - 2] == 'X' && arr_pc[i][j - 3] == char(254))
tmp = 1;
if (tmp == 0) {
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
cout << "*********Admiral !!! Congratulations, you have sunk the ship!*********" << endl;
Sleep(1500);
SetConsoleTextAttribute(hStdOut, 10);
const int row = 12, col = 12;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (arr_Battle[i][j] == 'X') {
if (arr_Battle[i - 1][j] != char(254) && arr_Battle[i - 1][j] != '#' && arr_Battle[i - 1][j] != 'X')
arr_Battle[i - 1][j] = 'O';
if (arr_Battle[i + 1][j] != char(254) && arr_Battle[i + 1][j] != '#' && arr_Battle[i + 1][j] != 'X')
arr_Battle[i + 1][j] = 'O';
if (arr_Battle[i - 1][j - 1] != char(254) && arr_Battle[i - 1][j - 1] != '#' && arr_Battle[i - 1][j - 1] != 'X')
arr_Battle[i - 1][j - 1] = 'O';
if (arr_Battle[i + 1][j + 1] != char(254) && arr_Battle[i + 1][j + 1] != '#' && arr_Battle[i + 1][j + 1] != 'X')
arr_Battle[i + 1][j + 1] = 'O';
if (arr_Battle[i][j + 1] != char(254) && arr_Battle[i][j + 1] != '#' && arr_Battle[i][j + 1] != 'X')
arr_Battle[i][j + 1] = 'O';
if (arr_Battle[i][j - 1] != char(254) && arr_Battle[i][j - 1] != '#' && arr_Battle[i][j - 1] != 'X')
arr_Battle[i][j - 1] = 'O';
if (arr_Battle[i - 1][j + 1] != char(254) && arr_Battle[i - 1][j + 1] != '#')
arr_Battle[i - 1][j + 1] = 'O';
if (arr_Battle[i + 1][j - 1] != char(254) && arr_Battle[i + 1][j - 1] != '#')
arr_Battle[i + 1][j - 1] = 'O';
}
}
}
}
}
int Dead_Ship_Counter_User(char ** &arr, int i, int j) { //Analyzing dead ship or not
int tmp = 0;
if (arr[i + 1][j] == char(254))
tmp = 1;
if (arr[i - 1][j] == char(254))
tmp = 1;
if (arr[i][j + 1] == char(254))
tmp = 1;
if (arr[i][j - 1] == char(254))
tmp = 1;
if (i < 10)
if (arr[i + 1][j] == 'X' && arr[i + 2][j] == char(254))
tmp = 1;
if (i > 0)
if (arr[i - 1][j] == 'X' && arr[i - 2][j] == char(254))
tmp = 1;
if (j < 10)
if (arr[i][j + 1] == 'X' && arr[i][j + 2] == char(254))
tmp = 1;
if (j > 0)
if (arr[i][j - 1] == 'X' && arr[i][j - 2] == char(254))
tmp = 1;
if (i < 9)
if (arr[i + 1][j] == 'X' && arr[i + 2][j] == 'X' && arr[i + 3][j] == char(254))
tmp = 1;
if (i > 1)
if (arr[i - 1][j] == 'X' && arr[i - 2][j] == 'X' && arr[i - 3][j] == char(254))
tmp = 1;
if (j < 9)
if (arr[i][j + 1] == 'X' && arr[i][j + 2] == 'X' && arr[i][j + 3] == char(254))
tmp = 1;
if (j > 1)
if (arr[i][j - 1] == 'X' && arr[i][j - 2] == 'X' && arr[i][j - 3] == char(254))
tmp = 1;
if (tmp == 0) {
return 0;
}
else
return -1;
}
void Dead_Ship_Counter_PC(char ** &arr, int i, int j) { //Analyzing dead ship or not PC
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
int tmp = 0;
if (arr[i + 1][j] == char(254))
tmp = 1;
if (arr[i - 1][j] == char(254))
tmp = 1;
if (arr[i][j + 1] == char(254))
tmp = 1;
if (arr[i][j - 1] == char(254))
tmp = 1;
if (i < 10)
if (arr[i + 1][j] == 'X' && arr[i + 2][j] == char(254))
tmp = 1;
if (i > 0)
if (arr[i - 1][j] == 'X' && arr[i - 2][j] == char(254))
tmp = 1;
if (j < 10)
if (arr[i][j + 1] == 'X' && arr[i][j + 2] == char(254))
tmp = 1;
if (j > 0)
if (arr[i][j - 1] == 'X' && arr[i][j - 2] == char(254))
tmp = 1;
if (i < 9)
if (arr[i + 1][j] == 'X' && arr[i + 2][j] == 'X' && arr[i + 3][j] == char(254))
tmp = 1;
if (i > 1)
if (arr[i - 1][j] == 'X' && arr[i - 2][j] == 'X' && arr[i - 3][j] == char(254))
tmp = 1;
if (j < 9)
if (arr[i][j + 1] == 'X' && arr[i][j + 2] == 'X' && arr[i][j + 3] == char(254))
tmp = 1;
if (j > 1)
if (arr[i][j - 1] == 'X' && arr[i][j - 2] == 'X' && arr[i][j - 3] == char(254))
tmp = 1;
if (tmp == 0) {
cout << "*********Admiral !!! The computer sank our ship!*********" << endl;
Sleep(1500);
SetConsoleTextAttribute(hStdOut, 10);
const int row = 12, col = 12;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (arr[i][j] == 'X') {
if (arr[i - 1][j] != char(254) && arr[i - 1][j] != '#' && arr[i - 1][j] != 'X')
arr[i - 1][j] = 'O';
if (arr[i + 1][j] != char(254) && arr[i + 1][j] != '#' && arr[i + 1][j] != 'X')
arr[i + 1][j] = 'O';
if (arr[i - 1][j - 1] != char(254) && arr[i - 1][j - 1] != '#' && arr[i - 1][j - 1] != 'X')
arr[i - 1][j - 1] = 'O';
if (arr[i + 1][j + 1] != char(254) && arr[i + 1][j + 1] != '#' && arr[i + 1][j + 1] != 'X')
arr[i + 1][j + 1] = 'O';
if (arr[i][j + 1] != char(254) && arr[i][j + 1] != '#' && arr[i][j + 1] != 'X')
arr[i][j + 1] = 'O';
if (arr[i][j - 1] != char(254) && arr[i][j - 1] != '#' && arr[i][j - 1] != 'X')
arr[i][j - 1] = 'O';
if (arr[i - 1][j + 1] != char(254) && arr[i - 1][j + 1] != '#')
arr[i - 1][j + 1] = 'O';
if (arr[i + 1][j - 1] != char(254) && arr[i + 1][j - 1] != '#')
arr[i + 1][j - 1] = 'O';
}
}
}
}
}
void Create_Ship_User(char ** &arr, int row, int col, int Ship_Count, int Cell_Counter) { //User Ship INPUT
Print_Map(arr, row, col);
cout << "Comrade General, put the ship on " << Cell_Counter << " decks, we have " << Ship_Count << "! GO ==>>" << endl;
STOP_Console
Clear_Console
string User_Input;
bool flag1 = true, flag2 = false;
int I = 0, J = 0;
for (int i = 0; i < Cell_Counter; i++) {
Print_Map(arr, row, col);
cout << "Enter the coordinates (Example A1):" << endl;
cin >> User_Input;
cin.ignore();
if (User_Input.size() > 0) {
I = Convert_S_One(User_Input);
J = Convert_S_Two(User_Input);
if (I != -1 && J != -1 && flag1 == true && arr[I][J] != '~') { // One
arr[I][J] = char(254);
flag2 = true;
flag1 = false;
}
else if (I != -1 && J != -1 && flag2 == true && arr[I][J] != '~') {
if (arr[I + 1][J] == char(254) || arr[I - 1][J] == char(254) || arr[I][J + 1] == char(254) || arr[I][J - 1] == char(254)) {
if (arr[I - 1][J + 1] == char(254) || arr[I - 1][J - 1] == char(254)) {
cout << "invalid input" << endl;
Cell_Counter++;
Sleep(1500);
}
else if (arr[I + 1][J + 1] == char(254) || arr[I + 1][J - 1] == char(254)) {
cout << "invalid input" << endl;
Cell_Counter++;
Sleep(1500);
}
else {
if (arr[I][J] != char(254) && arr[I][J] != '~') {
arr[I][J] = char(254);
}
else {
cout << "invalid input" << endl;
Cell_Counter++;
Sleep(1500);
}
}
}
else {
cout << "invalid input" << endl;
Cell_Counter++;
Sleep(1500);
}
}
else {
cout << "invalid input" << endl;
Cell_Counter++;
Sleep(1500);
}
Clear_Console
}
else
{
cout << "invalid input" << endl;
Cell_Counter++;
Sleep(1500);
Clear_Console
}
}
}
void Create_Ship_Rand(char ** &arr, int row, int col) { // Functions RAND SHIP
int I, J, count;
bool flag = true;
while(flag == true) {//SHIP 4
I = 0;
J = 0;
count = 0;
I = rand() % 10 + 1;
J = rand() % 10 + 1;
if (arr[I][J] != '~' && arr[I][J] != 254 && I != J) {
if (J > 3 && arr[I][J - 1] != 254 && arr[I][J - 2] != 254 && arr[I][J - 3] != 254 && flag == true) {
arr[I][J] = 254;
arr[I][J - 1] = 254;
arr[I][J - 2] = 254;
arr[I][J - 3] = 254;
flag = false;
}
if (I < 7 && arr[I + 1][J] != 254 && arr[I + 2][J] != 254 && arr[I + 3][J] != 254 && flag == true) {
arr[I][J] = 254;
arr[I + 1][J] = 254;
arr[I + 2][J] = 254;
arr[I + 3][J] = 254;
flag = false;
}
if (I > 3 && arr[I - 1][J] != 254 && arr[I - 2][J] != 254 && arr[I - 3][J] != 254 && flag == true) {
arr[I][J] = 254;
arr[I - 1][J] = 254;
arr[I - 2][J] = 254;
arr[I - 3][J] = 254;
flag = false;
}
if (J < 7 && arr[I][J + 1] != 254 && arr[I][J + 2] != 254 && arr[I][J + 3] != 254 && flag == true) {
arr[I][J] = 254;
arr[I][J + 1] = 254;
arr[I][J + 2] = 254;
arr[I][J + 3] = 254;
flag = false;
}
Ship_Outline(arr, row, col, 0);
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
{
if (arr[i][j] == char(254))
count++;
}
}
if (count == 4) {
break;
}
else {
flag = true;
}
}
flag = true;
count = 0;
while(flag == true) {//SHIP 3
count = 0;
I = 0;
J = 0;
flag = true;
I = rand() % 10 + 1;
J = rand() % 10 + 1;
if (arr[I][J] != '~' && arr[I][J] != 254 && I != J) {
if (J > 2 && arr[I][J - 1] != '~' && arr[I][J - 2] != '~' && flag == true) {
arr[I][J] = 254;
arr[I][J - 1] = 254;
arr[I][J - 2] = 254;
flag = false;
}
if (I < 8 && arr[I + 1][J] != '~' && arr[I + 2][J] != '~' && flag == true) {
arr[I][J] = 254;
arr[I + 1][J] = 254;
arr[I + 2][J] = 254;
flag = false;
}
if (I > 2 && arr[I - 1][J] != '~' && arr[I - 2][J] != '~' && flag == true) {
arr[I][J] = 254;
arr[I - 1][J] = 254;
arr[I - 2][J] = 254;
flag = false;
}
if (J < 8 && arr[I][J + 1] != '~' && arr[I][J + 2] != '~' && flag == true) {
arr[I][J] = 254;
arr[I][J + 1] = 254;
arr[I][J + 2] = 254;
flag = false;
}
Ship_Outline(arr, row, col, 0);
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
{
if (arr[i][j] == char(254))
count++;
}
}
if (count == 10){
break;
}
else {
flag = true;
}
}
flag = true;
count = 0;
while (flag == true) {//SHIP 2
count = 0;
I = 0;
J = 0;
flag = true;
I = rand() % 10 + 1;
J = rand() % 10 + 1;
if (arr[I][J] != '~' && arr[I][J] != 254 && I != J) {
if (J > 2 && arr[I][J - 1] != '~' && flag == true) {
arr[I][J] = 254;
arr[I][J - 1] = 254;
flag = false;
}
if (I < 8 && arr[I + 1][J] != '~' && flag == true) {
arr[I][J] = 254;
arr[I + 1][J] = 254;
flag = false;
}
if (I > 2 && arr[I - 1][J] != '~' && flag == true) {
arr[I][J] = 254;
arr[I - 1][J] = 254;
flag = false;
}
if (J < 8 && arr[I][J + 1] != '~' && flag == true) {
arr[I][J] = 254;
arr[I][J + 1] = 254;
flag = false;
}
Ship_Outline(arr, row, col, 0);
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
{
if (arr[i][j] == char(254))
count++;
}
}
if (count == 16) {
break;
}
else {
flag = true;
}
}
flag = true;
count = 0;
while (flag == true) {//SHIP 1
count = 0;
I = 0;
J = 0;
flag = true;
I = rand() % 10 + 1;
J = rand() % 10 + 1;
if (arr[I][J] != '~' && arr[I][J] != 254) {
arr[I][J] = 254;
Ship_Outline(arr, row, col, 0);
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
{
if (arr[i][j] == char(254))
count++;
}
}
if (count == 20) {
break;
}
else {
flag = true;
}
}
Ship_Outline(arr, row, col, 1);
}
void Ship_Outline(char ** &arr, int row, int col, int SET) { // SET = If 1 then draw the whole field
if (SET != 1) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (arr[i][j] == char(254)) {
if (arr[i - 1][j] != char(254) && arr[i - 1][j] != '#')
arr[i - 1][j] = '~';
if (arr[i + 1][j] != char(254) && arr[i + 1][j] != '#')
arr[i + 1][j] = '~';
if (arr[i - 1][j - 1] != char(254) && arr[i - 1][j - 1] != '#')
arr[i - 1][j - 1] = '~';
if (arr[i + 1][j + 1] != char(254) && arr[i + 1][j + 1] != '#')
arr[i + 1][j + 1] = '~';
if (arr[i][j + 1] != char(254) && arr[i][j + 1] != '#')
arr[i][j + 1] = '~';
if (arr[i][j - 1] != char(254) && arr[i][j - 1] != '#')
arr[i][j - 1] = '~';
if (arr[i - 1][j + 1] != char(254) && arr[i - 1][j + 1] != '#')
arr[i - 1][j + 1] = '~';
if (arr[i + 1][j - 1] != char(254) && arr[i + 1][j - 1] != '#')
arr[i + 1][j - 1] = '~';
}
}
}
}
if (SET == 1) {
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (arr[i][j] == ' ')
{
arr[i][j] = '~';
}
}
}
}
}
int Convert_S_One(string s) { // I
switch (s[0])
{
case 'A': return 1; break;
case 'B': return 2; break;
case 'C': return 3; break;
case 'D': return 4; break;
case 'E': return 5; break;
case 'F': return 6; break;
case 'G': return 7; break;
case 'H': return 8; break;
case 'I': return 9; break;
case 'J': return 10; break;
case 'a': return 1; break;
case 'b': return 2; break;
case 'c': return 3; break;
case 'd': return 4; break;
case 'e': return 5; break;
case 'f': return 6; break;
case 'g': return 7; break;
case 'h': return 8; break;
case 'i': return 9; break;
case 'j': return 10; break;
default:
return -1;
break;
}
}
int Convert_S_Two(string s) { // J
int Number_Ten = 0;
if (s.size() >= 3) {
string s1 = s.substr(1);
if(s1.find_first_not_of("0123456789") == string::npos)
Number_Ten = stoi(s1);
if (Number_Ten == 10)
return 10;
else
return -1;
}
else
{
switch (s[1])
{
case '1': return 1; break;
case '2': return 2; break;
case '3': return 3; break;
case '4': return 4; break;
case '5': return 5; break;
case '6': return 6; break;
case '7': return 7; break;
case '8': return 8; break;
case '9': return 9; break;
default:
return -1;
break;
}
}
}
void Read_File_Cout(string name_file) {
ifstream file_read; //FSTREAM
File_Data a; // STRUCT
file_read.open(name_file);
if (!file_read.is_open()) { // read
cout << "error open file" << endl;
}
else
{
string s;
while (!file_read.eof()) {
getline(file_read, s);
cout << s << endl;
}
}
file_read.close();
}
void Write_File_ADD_Statistic(string name_file, string Name_Player, int Stroke_Counter) { //BOARD ADD FILE
ofstream file_write;
File_Data a;
file_write.open(name_file, ofstream::app); //Add to file
if (!file_write.is_open()) { // write
cout << "error open file" << endl;
}
else
{
file_write << Name_Player << " ==>> destroyed the enemy for the number of moves = " << Stroke_Counter;
file_write << endl;
}
file_write.close();
}
void Write_File(string Write_Str, string name_file) { //BOARD ADD FILE
ofstream file_write;
File_Data a;
file_write.open(name_file, ofstream::app); //Add to file
if (!file_write.is_open()) { // write
cout << "error open file" << endl;
}
else
{
file_write << Write_Str;
file_write << endl;
}
file_write.close();
}
void Map(char ** &arr, int row, int col) { // MAP
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (i == 0 || i == row - 1 || j == 0 || j == col - 1)
arr[i][j] = '#';
else
arr[i][j] = ' ';
}
}
}
void Print_Map(char ** &arr, int row, int col) { // Display
int count = 65;
for (int i = 1; i < col - 1; i++) {
if (i == 1)
cout << " ";
cout << i << " ";
}
cout << endl;
for (int i = 0; i < row; i++) {
if (i > 0 && i < row - 1) {
cout << char(count) << " ";
count++;
}
for (int j = 0; j < col; j++) {
if (i == 0 && j == 0 || i == row - 1 && j == 0)
cout << " ";
cout << arr[i][j] << " ";
}
cout << endl;
}
}
char ** Create_Map(char ** &arr, int row, int col) { // ARRAY CREATE
arr = new char *[row]();
for (int i = 0; i < row; i++)
arr[i] = new char[col]();
return arr;
}
string ** Create_Map(string ** &arr, int row, int col) { // ARRAY CREATE
arr = new string *[row]();
for (int i = 0; i < row; i++)
arr[i] = new string[col]();
return arr;
}
void Delete_New(char** &arr, int row) { // DELETE MATRIX
for (int i = 0; i < row; i++)
delete[] arr[i];
delete[] arr;
}
void Delete_New(string** &arr, int row) { // DELETE MATRIX
for (int i = 0; i < row; i++)
delete[] arr[i];
delete[] arr;
}
//MAIN
#include "H.h"
void main() {
SetConsoleTitle("SEA BATTLE ([-_-])");
system("MODE CON: COLS=70 LINES=40");
srand(time(NULL));
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdOut, 10); // Color green
char **arr;
char **arr_pc;
char **arr_Battle;
int row = 12, col = 12, User_Input = 0, I = 0, J = 0, C_I = 0, C_J = 0, Stroke_Сounter = 0, x_check = 0;
int Count_User = 0, Count_Computer = 0;
string insert, Cordinate_Kick, Name_User, Complexity;
bool GameOver = true;
bool flag = true;
ofstream file_write;
ifstream file_read_Help;
File_Data a;
//------------------------------------------------------------------------------------------------------------------------------
do {
cout << "Enter your name: [MAX = 10]" << endl;
cin >> Name_User;
if (Name_User.size() > 10 || Name_User.size() < 1) {
cout << "error name" << endl;
STOP_Console
Clear_Console
}
} while (Name_User.size() > 10 || Name_User.size() < 1);
do { // insert Complexity
cout << "Choose your opponent's difficulty!\n"
<< "1.Easy!\n"
<< "2.Heavy\n";
cin >> Complexity;
cin.ignore();
if (Complexity.size() == 1) {
if (Complexity.find_first_not_of("0123456789") == string::npos) { //CHECK
x_check = stoi(Complexity);
}
else {
cout << "error input" << endl;
system("color 0C");
Sleep(1500);
system("color 0A");
}
if (x_check < 1 || x_check > 2) {
cout << "error input" << endl;
system("color 0C");
Sleep(1500);
system("color 0A");
}
}
else {
cout << "error input" << endl;
system("color 0C");
Sleep(1500);
system("color 0A");
}
} while (Complexity.size() > 1 || Complexity.size() <= 0 || x_check < 1 || x_check > 2);
Warning_Welcome();
//------------------------------------------------------------------------------------------------------------------------------
/*Create Player Field*/
//------------------------------------------------------------------------------------------------------------------------------
Create_Map(arr, row, col);
Map(arr, row, col);
//------------------------------------------------------------------------------------------------------------------------------
/*User puts ships on the map*/
//------------------------------------------------------------------------------------------------------------------------------
do {
cout << "Install ships manually enter 1:" << endl;
cout << "Set ships RAND enter 2:" << endl;
cin >> insert;
cin.ignore();
if (insert.size() == 1) {
if (insert.find_first_not_of("0123456789") == string::npos) { //CHECK
User_Input = stoi(insert);
}
else {
cout << "error input" << endl;
system("color 0C");
Sleep(1500);
system("color 0A");
}
}
else {
cout << "error input!" << endl;
system("color 0C");
Sleep(1500);
system("color 0A");
}
} while (User_Input > 2 || User_Input == 0);
if (User_Input == 1) {
Clear_Console
Create_Ship_User(arr, row, col, 1, 4);
Ship_Outline(arr, row, col, 0);
Create_Ship_User(arr, row, col, 2, 3);
Ship_Outline(arr, row, col, 0);
Create_Ship_User(arr, row, col, 1, 3);
Ship_Outline(arr, row, col, 0);
Create_Ship_User(arr, row, col, 3, 2);
Ship_Outline(arr, row, col, 0);
Create_Ship_User(arr, row, col, 2, 2); // USER SHIP
Ship_Outline(arr, row, col, 0);
Create_Ship_User(arr, row, col, 1, 2);
Ship_Outline(arr, row, col, 0);
Create_Ship_User(arr, row, col, 4, 1);
Ship_Outline(arr, row, col, 0);
Create_Ship_User(arr, row, col, 3, 1);
Ship_Outline(arr, row, col, 0);
Create_Ship_User(arr, row, col, 2, 1);
Ship_Outline(arr, row, col, 0);
Create_Ship_User(arr, row, col, 1, 1);
Ship_Outline(arr, row, col, 0);
Ship_Outline(arr, row, col, 1);
}
if (User_Input == 2) {
Clear_Console
Create_Ship_Rand(arr, row, col); // USER RAND SHIP
Print_Map(arr, row, col);
}
//------------------------------------------------------------------------------------------------------------------------------
/*Create PC Field*/
Create_Map(arr_pc, row, col);
Map(arr_pc, row, col);
Create_Ship_Rand(arr_pc, row, col);
//------------------------------------------------------------------------------------------------------------------------------
/*The computer puts the ships on the map*/
//------------------------------------------------------------------------------------------------------------------------------
/*Creating a battlefield*/
Create_Map(arr_Battle, row, col);
Map(arr_Battle, row, col);
//------------------------------------------------------------------------------------------------------------------------------
cout << "Your card is ready for battle! All ships are randomly placed !!" << endl;
SetConsoleTextAttribute(hStdOut, 10);
STOP_Console
fstream clear_file("Cordinates_Player.txt", ios::out);
clear_file.close();
while (GameOver == true) {
system("color 0A");
do {
Clear_Console
Print_Map(arr, row, col);
Print_Map(arr_Battle, row, col);
cout << "[SMS] = Help call, type [H]" << endl;
cout << "[SMS] = Player move, type [M]" << endl;
cout << "[SMS] = The number of your moves >> " << Stroke_Сounter << endl;
cout << "[SMS] = The enemy is destroyed by the number of percent = " << Percentage_Win(arr_pc, row, col) <<"%" << endl; //The percentage of the destruction of the enemy
cout << Name_User << " View statistics enter [S]" << endl;
cout << Name_User << " Enter the coordinates to strike:" << endl;
cin >> Cordinate_Kick;
a.Player_Move = Cordinate_Kick;
Write_File(a.Player_Move, "Cordinates_Player.txt"); //write input user
//-----------------------------------------------------------------------------
/*Player Move read*/
if (Cordinate_Kick == "M") { //Player Move
Read_File_Cout("Cordinates_Player.txt");
STOP_Console
Clear_Console
continue;
}
//-----------------------------------------------------------------------------
/*Statistics read*/
if (Cordinate_Kick == "S") { //Statstic
Read_File_Cout("Statistics.txt");
STOP_Console
Clear_Console
continue;
}
//-----------------------------------------------------------------------------
/*Read HELP*/
if (Cordinate_Kick == "H") { //HELP
Read_File_Cout("Help.txt");
STOP_Console
Clear_Console
continue;
}
//-----------------------------------------------------------------------------
cin.ignore(); //BUFER CLEAR
I = Convert_S_One(Cordinate_Kick);
J = Convert_S_Two(Cordinate_Kick);
if (I != -1 && J != -1) {
if (arr_Battle[I][J] != '+' && arr_Battle[I][J] != 'X') {
if (arr_pc[I][J] == char(254)) {
arr_pc[I][J] = 'X';
arr_Battle[I][J] = 'X';
Dead_Ship_Counter(arr_pc, arr_Battle, I, J); // Проверка умер корабль или нет!
cout << "***Admiral !!! Hit the ship !!!***" << endl;
system("color 0A");
Sleep(1500);
system("color 0C");
}
else {
arr_Battle[I][J] = 'O';
arr_pc[I][J] = 'O';
cout << "***Admiral !!! We did not hit the ship!***" << endl;
system("color 0A");
Sleep(1500);
system("color 0C");
}
}
else {
cout << "Admiral!!! You have already shot at this place!!!" << endl;
system("color 0A");
Sleep(1500);
system("color 0C");
}
//-----------------------------------------------------------------------------
/*PC SHOT*/
if (flag == true) {
bool check_shot = true;
while (check_shot == true) {
C_I = rand() % 10 + 1;
C_J = rand() % 10 + 1;
if (arr[C_I][C_J] == 'X' || arr[C_I][C_J] == 'O')
check_shot = true;
else
check_shot = false;
if (arr[C_I][C_J] == '#')
check_shot = true;
if (C_I < 1 || C_I > 10 && C_J < 1 || C_J > 10)
check_shot = true;
}
}
if (arr[C_I][C_J] == char(254)) {
arr[C_I][C_J] = 'X';
cout << "The computer hit us! =(-_+)= Admiral!!!" << endl;
Dead_Ship_Counter_PC(arr, C_I, C_J);
system("color 0A");
Sleep(1500);
system("color 0C");
if (x_check == 2) {
int Value_Dead = 0;
bool Bool_Flag = true;
Value_Dead = Dead_Ship_Counter_User(arr, C_I, C_J);
if (Value_Dead == -1) { // HARD
if (arr[C_I + 1][C_J] == char(254) && Bool_Flag == true) {
C_I += 1;
flag = false;
Bool_Flag = false;
}
if (arr[C_I - 1][C_J] == char(254) && Bool_Flag == true) {
C_I -= 1;
flag = false;
Bool_Flag = false;
}
if (arr[C_I][C_J + 1] == char(254) && Bool_Flag == true) {
C_J += 1;
flag = false;
Bool_Flag = false;
}
if (arr[C_I][C_J - 1] == char(254) && Bool_Flag == true) {
C_J -= 1;
flag = false;
Bool_Flag = false;
}
if (arr[C_I + 1][C_J] == 'X' && arr[C_I + 2][C_J] == char(254) && Bool_Flag == true) {
C_I += 2;
flag = false;
Bool_Flag = false;
}
if (arr[C_I - 1][C_J] == 'X' && arr[C_I - 2][C_J] == char(254) && Bool_Flag == true) {
C_I -= 2;
flag = false;
Bool_Flag = false;
}
if (arr[C_I][C_J + 1] == 'X' && arr[C_I][C_J + 2] == char(254) && Bool_Flag == true) {
C_J += 2;
flag = false;
Bool_Flag = false;
}
if (arr[C_I][C_J - 1] == 'X' && arr[C_I][C_J - 2] == char(254) && Bool_Flag == true) {
C_J -= 2;
flag = false;
Bool_Flag = false;
}
}
else
flag = true;
}
}
else {
if(arr[C_I][C_J] != 'X')
arr[C_I][C_J] = 'O';
cout << "Computer missed =(-_+)= Admiral!!!" << endl;
system("color 0A");
Sleep(1500);
system("color 0C");
flag = true;
}
}
else {
cout << "error inpur " << endl;
STOP_Console
Clear_Console
}
} while (I == -1 || J == -1);
for (int s = 0; s < row; s++) {
for (int d = 0; d < col; d++) {
if (arr[s][d] == char(254)) {
Count_User = 1;
}
if (arr_pc[s][d] == char(254)) {
Count_Computer = 1;
}
}
}
if (GameOver != false) {
Stroke_Сounter++;
}
if (Count_Computer != 1) {
cout << "ADMIRAL WINS!!!!" << endl;
SetConsoleTextAttribute(hStdOut, 10); // Color green
GameOver = false;
Sleep(500);
Wins_Display();
}
if (Count_User != 1) {
cout << "Computer WINS!!!!" << endl;
SetConsoleTextAttribute(hStdOut, 10); // Color green
GameOver = false;
Sleep(500);
Wins_Display();
}
if (Count_User != 1 && Count_Computer != 1) {
cout << "DRAW!!!!" << endl;
SetConsoleTextAttribute(hStdOut, 10); // Color green
GameOver = false;
Sleep(500);
Wins_Display();
}
//-----------------------------------------------------------------------------
/*Statistics write*/
int Stat = 0;
Stat = Percentage_Win(arr_pc, row, col);
if (Stat == 100) {
Write_File_ADD_Statistic("Statistics.txt", Name_User, Stroke_Сounter);
}
Count_User = 0;
Count_Computer = 0;
}
Delete_New(arr, row);
Delete_New(arr_pc, row);
Delete_New(arr_Battle, row);
STOP_Console
}