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

166
main.cpp
View File

@@ -1,11 +1,9 @@
#include <iostream>
#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>
using namespace std;
int hit = 0, timesG = 0;
int y, x, ysvar, xsvar, playgroundx = 0, playgroundy = 0;
float gameVerisin = 4.0;
@@ -39,12 +37,13 @@ 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 }
};
void loadfor();
void userbattle();
void AIbattle();
void SetWindow(int Width, int Height)
{
{
_COORD coord;
coord.X = Width;
coord.Y = Height;
@@ -58,44 +57,40 @@ void SetWindow(int Width, int Height)
HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); // Get Handle
SetConsoleScreenBufferSize(Handle, coord); // Set Buffer Size
SetConsoleWindowInfo(Handle, TRUE, &Rect); // Set Window Size
}
void begin_screen(){
}
void begin_screen()
{
system("color 05");
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 << "@ @" << endl;
cout << "@ @@@@@@/ .@@@ %@@@@@@@*@@@@@@@% @@ @@@@@@ @@@@@# @@ @@ @@* @@@@@@ @" << endl;
cout << "@ @@ @@ @@#@( @@* *@% @@ @@ ,@@ @@ @@ @@* @@ ,@@ @" << endl;
cout << "@ @@@@@@. @@ @@ @@* *@% @@ @@@@@, %@@@@ @@@@@@@@ @@* @@ .@@*@" << endl;
cout << "@ @@ &@& (@@@@@@@ @@* *@% @@ @@ ,@@ @@ @@ @@* @@,, @" << endl;
cout << "@ @@,,#@@. @@ &@@ @@* *@% @@%%%* @@%%%% #@*,@@@ @@ @@ @@* @@ @" << endl;
cout << "@ @" << endl;
cout << "@ By: Azaaxin @" << endl;
cout << "@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@" << endl;
}
int main()
{
string title = ("Azaaxin's Battleship");
SetConsoleTitle(title.c_str());
string title = ("Azaaxin's Battleship");
SetConsoleTitle(title.c_str());
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo;
GetConsoleScreenBufferInfo(hConsole, &ConsoleInfo);
int originalAttrs = ConsoleInfo.wAttributes;
SetWindow(90,50);
SetWindow(90, 50);
begin_screen();
//locale swedish("swedish"); //äöå
//locale::global(swedish); // äöå
//locale swedish("swedish"); //äöå
//locale::global(swedish); // äöå
srand(time(NULL));
char answerONswitch;
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << "[P]lay " << endl;
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
//cout << "[L]åt en bot gissa" << endl;
//cout << "[L]åt en bot gissa" << endl;
cout << "[E]xit " << endl;
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY);
cin >> answerONswitch;
@@ -105,11 +100,13 @@ SetConsoleTitle(title.c_str());
case 'p':
{
userbattle();
break; }
break;
}
case 'L':
case 'l':
{
cout << "Doesn't work!" << endl;
system("pause");
system("cls");
@@ -119,25 +116,18 @@ SetConsoleTitle(title.c_str());
}
}
return 0;
}//main
}
void userbattle()
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo;
GetConsoleScreenBufferInfo(hConsole, &ConsoleInfo);
int originalAttrs = ConsoleInfo.wAttributes;
cout << "Choose the size of the playground " << endl;
cout << "Height of the playground(in rows, between 1-27 rows)? ";
cin >> playgroundy ;
cout << endl<<"Width of the playground(in rows, between 1-27 rows)? ";
cin >> playgroundy;
cout << endl << "Width of the playground(in rows, between 1-27 rows)? ";
cin >> playgroundx;
if (playgroundx >= 28 || playgroundy >= 28)
{
system("cls");
@@ -157,41 +147,36 @@ void userbattle()
system("pause cls");
return userbattle();
}
else{
else
{
y = rand() % playgroundy;
x = rand() % playgroundx;
system("cls");
do{ // början på do-while loop
//cout << x << y<<endl; Skriver ut svaret i fšrvŠg
do { // början på do-while loop
//cout << x << y<<endl; Skriver ut svaret i fšrvŠg
cout << " " << "|";
for (counterVert = 0; playgroundy>counterVert; counterVert++)
for (counterVert = 0; playgroundy > counterVert; counterVert++)
{
if (counterVert > 10)
{
cout << counterVert << " ";
}
else{
else
{
cout << counterVert << " ";
}
}
cout << endl;
// cout << " " << "|123456789" << endl;
for (i = 0; i < playgroundx; i++)
{
if (i < 10)
{
cout << i << " |";
}
else
{
cout << i << "|";
}
for (j = 0; j < playgroundy; j++)
{
@@ -200,14 +185,14 @@ void userbattle()
cout << gameboard[j][i] << " ";
}
else{
else
{
cout << gameboard[j][i] << " ";
}
}
cout << endl;
}
cout << "-----------------" << endl;
cout << "A new shot\n";
cout << "Set Y-Coordinate: ";
@@ -217,7 +202,7 @@ void userbattle()
cin >> ysvar;
cout << endl << endl;
system("cls");
if (x == xsvar && y == ysvar) // checkar svaret på gissningarna
if (x == xsvar && y == ysvar) // checkar svaret på gissningarna
{
hit = 1;
gameboard[ysvar][xsvar] = 42;
@@ -225,10 +210,8 @@ void userbattle()
else if (xsvar == 624)
{
system("cls");
cout << "The answer is:" << x << " & " << y << endl;
cout << "The answer is:" << x << " &" << y << endl;
system("pause cls");
}
else if (xsvar >= playgroundx || ysvar >= playgroundy)
{
@@ -236,16 +219,13 @@ void userbattle()
cout << "Oops, you missed the playground" << endl;
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY);
system("pause");
}
else
{
gameboard[ysvar][xsvar] = 120;
}
timesG++; // hur många gånger
timesG++; // hur många gånger
} while (hit != 1);
cout << "*--------------------------------------------------------------*" << endl;
@@ -256,7 +236,7 @@ void userbattle()
cout << "Chances: " << timesG << endl;
cout << "Difficulty: " << playgroundy << "x" << playgroundx << 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";
cin.get();
cin.get();
@@ -265,16 +245,10 @@ void userbattle()
void AIbattle()
{
int x1 = 0, y1 = 0;
y = rand() % 27 + 1;
x = rand() % 27 + 1;
x = playgroundx;
y = playgroundy;
//do{ // början på do-while loop
//cout << x << y<<endl; Skriver ut svaret i fšrvŠg
cout << " " << "|";
for (counterVert = 0; playgroundx > counterVert; counterVert++)
{
@@ -282,10 +256,10 @@ void AIbattle()
{
cout << counterVert << " ";
}
else{
else
{
cout << counterVert << " ";
}
}
cout << endl;
// cout << " " << "|123456789" << endl;
@@ -294,7 +268,6 @@ void AIbattle()
if (i < 10)
{
cout << i << " |";
}
else
{
@@ -309,41 +282,31 @@ void AIbattle()
cout << gameboard[j][i] << " ";
}
else{
else
{
cout << gameboard[j][i] << " ";
}
}
cout << endl;
}
do{
do {
int totalRandomsize = 0;
totalRandomsize = playgroundx*playgroundy;
totalRandomsize = playgroundx * playgroundy;
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;
gameboard[ysvar][xsvar] = 42;
}
else
{
gameboard[ysvar][xsvar] = 120;
}
} while (x1 == xsvar && y1 == ysvar);
timesG++; // hur många gånger
}
while (x1 == xsvar && y1 == ysvar);
timesG++; // hur många gånger
} while (hit != 1);
cout << "*--------------------------------------------------------------*" << endl;
cout << " Congratulations you won!!" << endl;
cout << "Statistics:" << endl << "------" << endl;
@@ -352,29 +315,8 @@ void AIbattle()
cout << "Chances: " << timesG << endl;
cout << "Difficulty: " << playgroundy << "x" << playgroundx << 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";
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++)
*/