Some thoughts.
1. Indentation. Getting into the habit of maintaining good indentation will save you a lot of grief in the long run.
SourceForge.net: Indentation - cpwiki
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int const row_array = 5; //constants should be in caps (but is a personal preference not a rule)
int const column_array = 5;
int mines = 0;
int value;
char hidden_easy_game_board[row_array][column_array];
int easy_game_board[row_array][column_array]; //array global variable
int entered_user_row = -1; //the row integer and col integer the user enters
int entered_user_col = -1;
void mines_placement_easy();
void create_easy_board();
void hidden_easy_board();
int amountturns = 0;
int amountmines = 0;
int correctguesses = 0;
int guessesremaining = 20;
bool mine = false;
bool notmine = false;
int main()
{
mines_placement_easy();
create_easy_board(); //delete this line after
do {
hidden_easy_board(); //this calls hidden easy board which is the 5by5 ? board.
cout << endl;
cout << "Please enter the row you would like to select 1-5: ";
cin >> entered_user_row;
entered_user_row -= 1; //if user picks 1 it means 0 in the array ,and 2 means 1 and so on and so fourth will make sense later.
cout << endl;
cout << "Please enter the col you would like to select 1-5: ";
cin >> entered_user_col;
entered_user_col -= 1; //if user picks 1 it means 0 in the area and 2 means 1 and so on and so fourth will make sense later.
cout << endl;
if (easy_game_board[entered_user_row][entered_user_col] == 1) {
cout << "well done you hit a bomb" << endl;
cout << endl;
} else if (easy_game_board[entered_user_row][entered_user_col] == 0) {
cout << "Woops you missed!" << endl;
cout << endl;
}
guessesremaining--;
} while (guessesremaining > 0);
//_getch();
return 0;
}
void mines_placement_easy()
{
//setting mines
do {
int row_array; //declaring variables
int column_array;
do {
row_array = rand() % 5; //initialising the declared variables with a random number POSITION with the modulus 5 woo!
column_array = rand() % 5;
} while (easy_game_board[row_array][column_array] != 0);
easy_game_board[row_array][column_array] = 1;
mines++;
} while (mines < 10);
}
void create_easy_board()
{
//creating 5by5 board.
for (int row = 0; row < 5; row++) { //creating table using for loop
for (int column = 0; column < 5; column++) {
cout << easy_game_board[row][column] << " "; // delete this line after
}
cout << endl; // because each row is on a seperate line we have to endline!
}
}
void hidden_easy_board()
{
char hidden_easy_game_board[row_array][column_array]; //array values!
char hidden_char = '?';
int row;
int column;
for (row = 0; row < 5; row++) { //creating table using for loop
for (column = 0; column < 5; column++) {
hidden_easy_game_board[row][column] = hidden_char; //sets all grid to question mark
if ((row == entered_user_row) && (column == entered_user_col)) {
if (easy_game_board[entered_user_row][entered_user_col] == 1) {
hidden_easy_game_board[entered_user_row][entered_user_col] = 'b';
} else if (easy_game_board[entered_user_row][entered_user_col] == 0) {
hidden_easy_game_board[entered_user_row][entered_user_col] = 'e';
}
} else {
hidden_easy_game_board[row][column] = hidden_char; //sets all grid to question mark
}
cout << hidden_easy_game_board[row][column] << " "; //printing out array table!
}
cout << endl; // because each row is on a seperate line we have to endline!
}
}
2. Compiler warnings
Compile with as many warnings enabled as you can.
Code:
$ g++ -Wall -Wshadow foo.cpp
foo.cpp: In function ‘void mines_placement_easy()’:
foo.cpp:63:9: warning: declaration of ‘row_array’ shadows a global declaration [-Wshadow]
foo.cpp:5:11: warning: shadowed declaration is here [-Wshadow]
foo.cpp:64:9: warning: declaration of ‘column_array’ shadows a global declaration [-Wshadow]
foo.cpp:6:11: warning: shadowed declaration is here [-Wshadow]
foo.cpp: In function ‘void hidden_easy_board()’:
foo.cpp:88:54: warning: declaration of ‘hidden_easy_game_board’ shadows a global declaration [-Wshadow]
foo.cpp:9:6: warning: shadowed declaration is here [-Wshadow]
You're using the same name in two different contexts. Are you sure you're referring to the one you think you are?
3. so when I get this to run the first time around it works perfectly
At the moment, your global variables like
char hidden_easy_game_board[row_array][column_array];
are implicitly declared as being
Code:
char hidden_easy_game_board[row_array][column_array] = { { 0 } };
That is, they're filled with zeros.
When you call something like mines_placement_easy(), it would be a good idea to put a loop at the start of this function to explicitly initialise the array to be all zeros before doing anything else. This would give you the consistency between runs.