This commit is contained in:
2020-03-06 11:23:24 +01:00
committed by GitHub
parent 97c7e4f9c4
commit e5b89f804e

262
main.cpp
View File

@@ -1,11 +1,9 @@
#include <iostream> #include <iostream>
#include <stdlib.h> #include <stdlib.h>
#include <ctime> // gör det möjligt att använda "random" funktionen i programmet #include <ctime> // gör det möjligt att använda "random" funktionen i programmet
#include <windows.h> #include <windows.h>
using namespace std; using namespace std;
int hit = 0, timesG = 0; int hit = 0, timesG = 0;
int y, x, ysvar, xsvar, playgroundx = 0, playgroundy = 0; int y, x, ysvar, xsvar, playgroundx = 0, playgroundy = 0;
float gameVerisin = 4.0; float gameVerisin = 4.0;
@@ -13,7 +11,7 @@ int i = 0, j = 0, counterVert;
const int width = 27; const int width = 27;
const int height = 27; const int height = 27;
char gameboard[width][height] = { char gameboard[width][height] = {
{ 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 }, // 32 = mellanslag (ASCII) sparat i vektor/array { 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 }, // 32 = mellanslag (ASCII) sparat i vektor/array
{ 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 }, { 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 },
{ 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 }, { 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 },
{ 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 }, { 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 },
@@ -39,105 +37,97 @@ char gameboard[width][height] = {
{ 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 }, { 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 },
{ 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 }, { 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 },
{ 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 }, { 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 },
{ 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 } }; { 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 }
};
void loadfor(); void loadfor();
void userbattle(); void userbattle();
void AIbattle(); void AIbattle();
void SetWindow(int Width, int Height) void SetWindow(int Width, int Height)
{ {
_COORD coord; _COORD coord;
coord.X = Width; coord.X = Width;
coord.Y = Height; coord.Y = Height;
_SMALL_RECT Rect; _SMALL_RECT Rect;
Rect.Top = 0; Rect.Top = 0;
Rect.Left = 0; Rect.Left = 0;
Rect.Bottom = Height - 1; Rect.Bottom = Height - 1;
Rect.Right = Width - 1; Rect.Right = Width - 1;
HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); // Get Handle HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); // Get Handle
SetConsoleScreenBufferSize(Handle, coord); // Set Buffer Size SetConsoleScreenBufferSize(Handle, coord); // Set Buffer Size
SetConsoleWindowInfo(Handle, TRUE, &Rect); // Set Window Size SetConsoleWindowInfo(Handle, TRUE, &Rect); // Set Window Size
} }
void begin_screen(){ void begin_screen()
system("color 05"); {
cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl; system("color 05");
cout<<"@ @"<<endl; cout << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" << endl;
cout<<"@ @@@@@@/ .@@@ %@@@@@@@*@@@@@@@% @@ @@@@@@ @@@@@# @@ @@ @@* @@@@@@ @"<<endl; cout << "@ @" << endl;
cout<<"@ @@ @@ @@#@( @@* *@% @@ @@ ,@@ @@ @@ @@* @@ ,@@ @"<<endl; cout << "@ @@@@@@/ .@@@ %@@@@@@@*@@@@@@@% @@ @@@@@@ @@@@@# @@ @@ @@* @@@@@@ @" << endl;
cout<<"@ @@@@@@. @@ @@ @@* *@% @@ @@@@@, %@@@@ @@@@@@@@ @@* @@ .@@* @"<<endl; cout << "@ @@ @@ @@#@( @@* *@% @@ @@ ,@@ @@ @@ @@* @@ ,@@ @" << endl;
cout<<"@ @@ &@& (@@@@@@@ @@* *@% @@ @@ ,@@ @@ @@ @@* @@,, @"<<endl; cout << "@ @@@@@@. @@ @@ @@* *@% @@ @@@@@, %@@@@ @@@@@@@@ @@* @@ .@@*@" << endl;
cout<<"@ @@,,#@@. @@ &@@ @@* *@% @@%%%* @@%%%% #@*,@@@ @@ @@ @@* @@ @"<<endl; cout << "@ @@ &@& (@@@@@@@ @@* *@% @@ @@ ,@@ @@ @@ @@* @@,, @" << endl;
cout<<"@ @"<<endl; cout << "@ @@,,#@@. @@ &@@ @@* *@% @@%%%* @@%%%% #@*,@@@ @@ @@ @@* @@ @" << endl;
cout<<"@ By: Azaaxin @"<<endl; cout << "@ @" << endl;
cout<<"@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@"<<endl; cout << "@ By: Azaaxin @" << endl;
cout << "@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@" << endl;
} }
int main() int main()
{ {
string title = ("Azaaxin's Battleship"); string title = ("Azaaxin's Battleship");
SetConsoleTitle(title.c_str()); SetConsoleTitle(title.c_str());
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hConsole, &ConsoleInfo);
int originalAttrs = ConsoleInfo.wAttributes;
CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo; SetWindow(90, 50);
GetConsoleScreenBufferInfo(hConsole, &ConsoleInfo); begin_screen();
int originalAttrs = ConsoleInfo.wAttributes; //locale swedish("swedish"); //äöå
//locale::global(swedish); // äöå
SetWindow(90,50);
begin_screen();
//locale swedish("swedish"); //äöå
//locale::global(swedish); // äöå
srand(time(NULL)); srand(time(NULL));
char answerONswitch; char answerONswitch;
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN); SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << "[P]lay " << endl; cout << "[P]lay " << endl;
SetConsoleTextAttribute(hConsole, FOREGROUND_RED); SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
//cout << "[L]åt en bot gissa" << endl; //cout << "[L]åt en bot gissa" << endl;
cout << "[E]xit " << endl; cout << "[E]xit " << endl;
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY); SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY);
cin >> answerONswitch; cin >> answerONswitch;
switch (answerONswitch) switch (answerONswitch)
{ {
case 'P': case 'P':
case 'p': case 'p':
{ {
userbattle(); userbattle();
break; } break;
case 'L': }
case 'l': case 'L':
case 'l':
{ {
cout << "Doesn't work!" << endl;
system("pause"); cout << "Doesn't work!" << endl;
system("cls"); system("pause");
return main(); system("cls");
//AIbattle(); return main();
break; //AIbattle();
} break;
}
} }
return 0; return 0;
}
}//main
void userbattle() void userbattle()
{ {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo;
CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo; GetConsoleScreenBufferInfo(hConsole, &ConsoleInfo);
GetConsoleScreenBufferInfo(hConsole, &ConsoleInfo); int originalAttrs = ConsoleInfo.wAttributes;
int originalAttrs = ConsoleInfo.wAttributes;
cout << "Choose the size of the playground " << endl; cout << "Choose the size of the playground " << endl;
cout << "Height of the playground(in rows, between 1-27 rows)? "; cout << "Height of the playground(in rows, between 1-27 rows)? ";
cin >> playgroundy ; cin >> playgroundy;
cout << endl<<"Width of the playground(in rows, between 1-27 rows)? "; cout << endl << "Width of the playground(in rows, between 1-27 rows)? ";
cin >> playgroundx; cin >> playgroundx;
if (playgroundx >= 28 || playgroundy >= 28) if (playgroundx >= 28 || playgroundy >= 28)
{ {
system("cls"); system("cls");
@@ -157,41 +147,36 @@ void userbattle()
system("pause cls"); system("pause cls");
return userbattle(); return userbattle();
} }
else
else{ {
y = rand() % playgroundy; y = rand() % playgroundy;
x = rand() % playgroundx; x = rand() % playgroundx;
system("cls"); system("cls");
do{ // början på do-while loop do { // början på do-while loop
//cout << x << y<<endl; Skriver ut svaret i fšrvŠg
//cout << x << y<<endl; Skriver ut svaret i fšrvŠg
cout << " " << "|"; cout << " " << "|";
for (counterVert = 0; playgroundy>counterVert; counterVert++) for (counterVert = 0; playgroundy > counterVert; counterVert++)
{ {
if (counterVert > 10) if (counterVert > 10)
{ {
cout << counterVert << " "; cout << counterVert << " ";
} }
else{ else
{
cout << counterVert << " "; cout << counterVert << " ";
} }
} }
cout << endl; cout << endl;
// cout << " " << "|123456789" << endl;
for (i = 0; i < playgroundx; i++) for (i = 0; i < playgroundx; i++)
{ {
if (i < 10) if (i < 10)
{ {
cout << i << " |"; cout << i << " |";
} }
else else
{ {
cout << i << "|"; cout << i << "|";
} }
for (j = 0; j < playgroundy; j++) for (j = 0; j < playgroundy; j++)
{ {
@@ -200,14 +185,14 @@ void userbattle()
cout << gameboard[j][i] << " "; cout << gameboard[j][i] << " ";
} }
else{ else
{
cout << gameboard[j][i] << " "; cout << gameboard[j][i] << " ";
} }
} }
cout << endl; cout << endl;
} }
cout << "-----------------" << endl; cout << "-----------------" << endl;
cout << "A new shot\n"; cout << "A new shot\n";
cout << "Set Y-Coordinate: "; cout << "Set Y-Coordinate: ";
@@ -217,7 +202,7 @@ void userbattle()
cin >> ysvar; cin >> ysvar;
cout << endl << endl; cout << endl << endl;
system("cls"); system("cls");
if (x == xsvar && y == ysvar) // checkar svaret på gissningarna if (x == xsvar && y == ysvar) // checkar svaret på gissningarna
{ {
hit = 1; hit = 1;
gameboard[ysvar][xsvar] = 42; gameboard[ysvar][xsvar] = 42;
@@ -225,27 +210,22 @@ void userbattle()
else if (xsvar == 624) else if (xsvar == 624)
{ {
system("cls"); system("cls");
cout << "The answer is:" << x << " & " << y << endl; cout << "The answer is:" << x << " &" << y << endl;
system("pause cls"); system("pause cls");
} }
else if (xsvar >= playgroundx || ysvar >= playgroundy) else if (xsvar >= playgroundx || ysvar >= playgroundy)
{ {
SetConsoleTextAttribute(hConsole, FOREGROUND_RED); SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << "Oops, you missed the playground" << endl; cout << "Oops, you missed the playground" << endl;
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY); SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY);
system("pause"); system("pause");
} }
else else
{ {
gameboard[ysvar][xsvar] = 120; gameboard[ysvar][xsvar] = 120;
} }
timesG++; // hur många gånger timesG++; // hur många gånger
} while (hit != 1); } while (hit != 1);
cout << "*--------------------------------------------------------------*" << endl; cout << "*--------------------------------------------------------------*" << endl;
@@ -256,7 +236,7 @@ void userbattle()
cout << "Chances: " << timesG << endl; cout << "Chances: " << timesG << endl;
cout << "Difficulty: " << playgroundy << "x" << playgroundx << endl; cout << "Difficulty: " << playgroundy << "x" << playgroundx << endl;
cout << "*--------------------------------------------------------------*" << endl; cout << "*--------------------------------------------------------------*" << endl;
cout << "This is the first English version of the game, created by Azaaxin"<<endl<<"version:" << gameVerisin<<endl; cout << "This is the first English version of the game, created by Azaaxin" << endl << "version:" << gameVerisin << endl;
cout << endl << "Press any key to exit! :D"; cout << endl << "Press any key to exit! :D";
cin.get(); cin.get();
cin.get(); cin.get();
@@ -265,16 +245,10 @@ void userbattle()
void AIbattle() void AIbattle()
{ {
int x1 = 0, y1 = 0; int x1 = 0, y1 = 0;
y = rand() % 27 + 1; y = rand() % 27 + 1;
x = rand() % 27 + 1; x = rand() % 27 + 1;
x = playgroundx; x = playgroundx;
y = playgroundy; y = playgroundy;
//do{ // början på do-while loop
//cout << x << y<<endl; Skriver ut svaret i fšrvŠg
cout << " " << "|"; cout << " " << "|";
for (counterVert = 0; playgroundx > counterVert; counterVert++) for (counterVert = 0; playgroundx > counterVert; counterVert++)
{ {
@@ -282,10 +256,10 @@ void AIbattle()
{ {
cout << counterVert << " "; cout << counterVert << " ";
} }
else{ else
{
cout << counterVert << " "; cout << counterVert << " ";
} }
} }
cout << endl; cout << endl;
// cout << " " << "|123456789" << endl; // cout << " " << "|123456789" << endl;
@@ -294,7 +268,6 @@ void AIbattle()
if (i < 10) if (i < 10)
{ {
cout << i << " |"; cout << i << " |";
} }
else else
{ {
@@ -309,72 +282,41 @@ void AIbattle()
cout << gameboard[j][i] << " "; cout << gameboard[j][i] << " ";
} }
else{ else
{
cout << gameboard[j][i] << " "; cout << gameboard[j][i] << " ";
} }
} }
cout << endl; cout << endl;
} }
do {
do{
int totalRandomsize = 0; int totalRandomsize = 0;
totalRandomsize = playgroundx*playgroundy; totalRandomsize = playgroundx * playgroundy;
for (int o = 0; x == x1 && y == y1; o++) for (int o = 0; x == x1 && y == y1; o++)
{ {
if (x1 == xsvar && y1 == ysvar) // checkar svaret på gissningarna if (x1 == xsvar && y1 == ysvar) // checkar svaret på gissningarna
{ {
hit = 1; hit = 1;
gameboard[ysvar][xsvar] = 42; gameboard[ysvar][xsvar] = 42;
} }
else else
{ {
gameboard[ysvar][xsvar] = 120; gameboard[ysvar][xsvar] = 120;
} }
}
} while (x1 == xsvar && y1 == ysvar); while (x1 == xsvar && y1 == ysvar);
timesG++; // hur många gånger
timesG++; // hur många gånger
} while (hit != 1); } while (hit != 1);
cout << "*--------------------------------------------------------------*" << endl;
cout << "*--------------------------------------------------------------*" << endl; cout << " Congratulations you won!!" << endl;
cout << " Congratulations you won!!" << endl; cout << "Statistics:" << endl << "------" << endl;
cout << "Statistics:" << endl << "------" << endl; cout << "X-Answer: " << x << endl;
cout << "X-Answer: " << x << endl; cout << "Y-Answer: " << y << endl;
cout << "Y-Answer: " << y << endl; cout << "Chances: " << timesG << endl;
cout << "Chances: " << timesG << endl; cout << "Difficulty: " << playgroundy << "x" << playgroundx << endl;
cout << "Difficulty: " << playgroundy << "x" << playgroundx << endl; cout << "*--------------------------------------------------------------*" << endl;
cout << "*--------------------------------------------------------------*" << endl; cout << "This is the first English version of the game, created by Azaaxin" << endl << "version:" << gameVerisin << endl;
cout << "This is the first English version of the game, created by Azaaxin"<<endl<<"version:"<< gameVerisin<<endl; cout << endl << "Press any key to exit! :D";
cout << endl << "Press any key to exit! :D"; cin.get();
cin.get(); cin.get();
cin.get();
} }
/*void loadfor()
{
cout << " " << "|";
for (counterVert = 0; playgroundx > counterVert; counterVert++)
{
if (counterVert > 10)
{
cout << counterVert << " ";
}
else{
cout << counterVert << " ";
}
}
cout << endl;
// cout << " " << "|123456789" << endl;
for (int i = 0; i < playgroundy; i++)
*/