#include <iostream>
#include <windows.h>
usingnamespace std;
// Functions
void GameBegin(int selector);
void NewGame();
void LoadGame();
void CreateGameBoard();
void PlayGame();
// Global Variables
enum { MENU, PLAYING };
char cTable[14][14];
int main()
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
cout << "Welcome to";
SetConsoleTextAttribute(hConsole, 207);
cout << " ANCIENT WARFARE" << endl;
SetConsoleTextAttribute(hConsole, 7);
cout << "A C++ Program developed by Justin B------\n";
cout << "\n(If you don't know how to play, see the Readme file)\n";
cout << "This game requires 2+ people to play.\nPlease select an option, then press enter:\n";
bool bBadEntry = false;
int iMenuSelect;
do
{
if(bBadEntry == true)
cout << "\nBad input! Try again:\n";
cout << "1. New Game\n2. Load Game\n3. Exit\n";
cin >> iMenuSelect;
if(iMenuSelect <= 3 && iMenuSelect >= 1)
{
bBadEntry = false;
GameBegin(iMenuSelect);
}
else
bBadEntry = true;
} while(bBadEntry == true);
}
void GameBegin(int select)
{
switch(select)
{
case 1:
NewGame();
break;
case 2:
LoadGame();
break;
case 3:
exit(1);
break;
}
}
void NewGame()
{
int iNumPlayers;
CreateGameBoard();
do
{
cout << "\nHow many players will there be? (Up to four)\n";
cin >> iNumPlayers;
if(iNumPlayers > 4 || iNumPlayers < 1)
cout << "\nInvalid number of players. Please re-enter.\n";
} while(iNumPlayers > 4 || iNumPlayers < 1);
switch(iNumPlayers)
{
case 4:
cTable[7][14] = 6;
case 3:
cTable[7][0] = 5;
case 2:
cTable[14][7] = 4;
case 1:
cTable[0][7] = 3;
break;
}
}
void LoadGame()
{
}
void CreateGameBoard()
{
int min = 1;
int max = 10;
int iRandomNumber;
int iRandomNumber2;
int playOrReshuffle;
int iHiddenTable[14][14];
for(int i = 0; i < 15; i++) // Creates the hidden table, which will serve to map the real table
{
iRandomNumber = min + rand() % max;
for(int j = 0; j < 15; j++)
{
iRandomNumber = min + rand() % max;
iHiddenTable[i][j] = iRandomNumber2 = min + rand() % max;
}
}
for(int i = 0; i < 15; i++) // Creates the real table, based on the randomized hidden table.
{ // If the hidden table int is 1, it creates water. Otherwise, it
for(int j = 0; j < 15; j++) // creates land.
{
if(iHiddenTable[i][j] != 1)
cTable[i][j] = 254;
else
cTable[i][j] = 177;
}
}
cTable[0][7] = 5; // Creates the players' bases. 5 = Club
cTable[14][7] = 6; // 6 = Spade
for(int i = 0; i < 15; i++)
{
cout << endl;
for(int j = 0; j < 15; j++)
cout << cTable[i][j];
}
cout << "\n1. Play this map?\n2. Or reshuffle and make a new map?\n";
cin >> playOrReshuffle;
if(playOrReshuffle == 2)
CreateGameBoard();
}
void PlayGame()
{
cout << "\nPlayer One, it is your turn";
}
(Note that PlayGame() isn't finished)
When I run this code, and enter the sequence "1" followed by "1" (New Game > Play This Map?), it says "Ancient Warfare.exe has stopped working" and I get the error in the compiler "Process terminated with status -1073741510 (0 minutes, 12 seconds)" (time varies, of course). Does anyone know what I'm doing wrong?
This one was trickey but it appears to be a scope issue when you return from your "CreateGame()" function. Declaring 'iHiddenTable[14][14]' on line 105 to be static should fix the crash.
The loops in CreateGameBoard() are going out of bounds, treating cTable and iHiddenTable as arrays of 15x15 elements when they are actually 14x14. It would be alot cleaner if you use constants to define the board size. E.g.,
1 2 3 4 5 6 7 8
#include <iostream>
#define BoardWidth 15
#define BoardHeight 15
...
int iHiddenTable[BoardWidth][BoardHeight];
for (int y = 0; y < BoardHeight; y++)
for (int x = 0; x < BoardWidth; x++)
Also, cTable should probably be type unsigned char. If you move to a compiler where char is signed by default then you will have truncation problems, particularly with this line: