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

120
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;
@@ -39,7 +37,8 @@ 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();
@@ -59,7 +58,8 @@ void SetWindow(int Width, int Height)
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"); system("color 05");
cout << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" << endl; cout << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" << endl;
cout << "@ @" << endl; cout << "@ @" << endl;
@@ -77,25 +77,20 @@ 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); 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;
SetWindow(90, 50); SetWindow(90, 50);
begin_screen(); begin_screen();
//locale swedish("swedish"); //äöå //locale swedish("swedish"); //äöå
//locale::global(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;
@@ -105,11 +100,13 @@ SetConsoleTitle(title.c_str());
case 'p': case 'p':
{ {
userbattle(); userbattle();
break; } break;
}
case 'L': case 'L':
case 'l': case 'l':
{ {
cout << "Doesn't work!" << endl; cout << "Doesn't work!" << endl;
system("pause"); system("pause");
system("cls"); system("cls");
@@ -119,25 +116,18 @@ SetConsoleTitle(title.c_str());
} }
} }
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,15 +147,13 @@ 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++)
{ {
@@ -173,25 +161,22 @@ void userbattle()
{ {
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;
@@ -226,9 +211,7 @@ void userbattle()
{ {
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)
{ {
@@ -236,16 +219,13 @@ void userbattle()
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;
@@ -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,41 +282,31 @@ 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;
@@ -356,25 +319,4 @@ void AIbattle()
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++)
*/