On line 35 I'm trying to return the int incrd which the user has inputted but i get a window which pops up saying:
Run Time Check failure #3 - the variable "incrd" is being used without being initialized.
I think its because when I initialize incrd its outside the scope of GetMove() but i want to be able to use the function to get the users input so I'm not sure what to do. Any help would be appreciated, thanks.
#include <iostream>
#include <cstdlib> // include things
#include <ctime>
usingnamespace std;
void PrintBoard(int b[8][8]) // define the function to print the board
{
cout << " 1 2 3 4 5 6 7 8" << endl;
for(int i=0;i<8;i++) // loop 8 times for 8 lines
{
cout << " -----------------" << endl;
cout << (i + 1) <<"|";
for(int o=0;o<8;o++) // loop for the 8 elements on the line
{
if (b[i][o] == 1)
{cout << " ";} // display a blank space
if (b[i][o] == 0)
{cout << " ";} // display a blank space
cout << "|";
}
cout << endl; // go to a new line to display the next line of matrix
}
cout << " -----------------" << endl;
}
int GetMove(int incrd)
{
cout << "Type in the co-ordinates of the square " << endl << "you would like to click in the form xy"<< endl<< endl; // gets the players move and makes sure its on the board
while(true)
{
incrd = 1;
cin >> incrd;
if ((10<incrd && incrd < 19) || (20<incrd && incrd<29) || (20<incrd && incrd<29) || (30<incrd && incrd<39) || (40<incrd && incrd<49) || (50<incrd && incrd<59) || (60<incrd && incrd<69) || (70<incrd && incrd<79) || (70<incrd && incrd<79))
{
return incrd;
break;
}
cout <<"invalid input, please enter co-ordinates in the correct form (xy)" << endl;
}
}
int main() // MINESWEEPER text based - input a co-ord in order to click a square just like normal minus the mouse
{ // eg "input a co ordinate"
// 3 4 == [3][4]
// -----------
// | 1X1 | <--- somewhat like that where X = bomb
// | 111 |
int b[8][8]; //declare matrix for board
srand ( (unsignedint)time(NULL) );
int bombs = 0; // control no of bombs
int temp = 1; // temp number for making board
while ( bombs != 10 ) // sets up the minimum number of mobs on the board at once
{
int cx = 0; // x and y co-ords for the bombs location (b array)
int cy = 0;
bombs = 0; // setting bombs to zero
for(int ctl = 0; ctl < 8; ctl += 1) //defining the minesweeper board [8*8]
{
cx = 0;
for(int cnt = 0; cnt < 8; cnt += 1)
{
temp = rand() % 6; // temp = random number between 0 and 5
if ( temp < 1 ) // if temp == 0
{b[cx][cy] = 1;} // then theres a bomb on [cx]cy]
if ( temp > 0 )
{b[cx][cy] = 0;} //if temp != 0 then no bomb on [cx][cy]
if (b[cx][cy] == 1) // if theres a bomb on [cx][cy]
{bombs += 1;} // add one onto the counter
cx += 1; // repeat for the rest of the line
}
cy += 1; // repeat for the next line and so on
} // breaks out of loop if theres enough bombs
}
cout << "---Welcome To Minesweeper--- " << endl;
cout << "Creating Minefield... Created" << endl << endl;
PrintBoard(b);
int incrd;
GetMove(incrd);
cout << "Move: " << incrd;
}
Use mod (%) and integer division (/) to chop the input coord up to simplify the tests (easier to read?) And could even return x and y using out params or a std::pair<> return code?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int GetMove()
{
cout << "Type in the co-ordinates of the square " << endl <<
"you would like to click in the form xy" << endl << endl;
while (true)
{
int incrd = 0;
is >> incrd;
int x = incrd % 10;
int y = incrd / 10;
if ((1 <= x) && (x <= 8) && (1 <= y) && (y <=8))
{
return incrd;
}
cout << "invalid input, please enter co-ordinates in the correct form (xy)" << endl;
}
}