Hi, I just started learning programming couple of days ago and this is an awesome site to learn it from. I have managed to get to lesson 29 at the beginners section and this is the first time I try to write my own code. First I made a function for checking for win situation, which is quite robust but atleast I have found a way to make it work. Then I made a difficulty selection possibility by changing the number 1000000 in the randomize function to a variable.
But now I'm having trouble making a function for ending game/playing again after winning. When I select yes then everything seems to work (not sure if only seems so), when I press random letters it properly asks for new input, but when I select no then by some mysterious reason the bPlayAgain false value does not exit the do while loop in main function.
Oh, and if anyone has good ideas what more to try to improve in this program, share them

PS. You can change the value of iShuffles in case 1 in SelectDifficulty function to 1 to quickly test the program...
#include <iostream>
#include <ctime>
enum EMove { keUp = 'w',
keDown = 's',
keLeft = 'a',
keRight = 'd'};
// Function declarations
void InitializeBoard(char[4][4]);
void PrintBoard(char[4][4]);
void LocateSpace(int&, int&, char [4][4]);
void Randomize(char[4][4]);
void Move(char[4][4], const EMove);
void CheckVictory(char[4][4]);
void SelectDifficulty();
void PlayAgain();
int iVictory = 0;
int iShuffles;
bool bPlayAgain = false;
char cPlayAgain;
int main() {
using namespace std;
char caaBoard[4][4];
do {
SelectDifficulty();
InitializeBoard(caaBoard);
Randomize(caaBoard);
do {
PrintBoard(caaBoard);
cout << endl << "w = Up, s = Down, a = Left, d = Right, enter = confirm" << endl;
char cNextMove;
cin >> cNextMove;
EMove eNextMove = (EMove)cNextMove;
Move(caaBoard, eNextMove);
cout << endl;
CheckVictory(caaBoard);
} while (iVictory != 16);
PlayAgain();
} while (bPlayAgain = true);
cout << "Thanks For Playing" << endl;
return EXIT_SUCCESS;
}
void PlayAgain() {
using namespace std;
bool bYesNoSelected;
bPlayAgain = false;
do {
cout << "Victory!" << endl << "Play again? (y)/(n)" << endl;
cin >> cPlayAgain;
switch (cPlayAgain) {
case 'y':
{
bPlayAgain = true;
bYesNoSelected = true;
break;
}
case 'n':
{
bPlayAgain = false;
bYesNoSelected = true;
break;
}
default:
{
cout << "Invalid input! (y)/(n)" << endl;
bPlayAgain = false;
bYesNoSelected = false;
break;
}
}
} while (bYesNoSelected != true);
}
void SelectDifficulty() {
using namespace std;
char cDifficulty;
bool bDifSelected = 0;
do {
cDifficulty = 0;
cout << "Select difficulty: Easy(1),Medium(2),Hard(3)?" << endl;
cin >> cDifficulty;
cout << endl;
switch (cDifficulty) {
case '1':
{
iShuffles = 10;
bDifSelected = true;
break;
}
case '2':
{
iShuffles = 75;
bDifSelected = true;
break;
}
case '3':
{
iShuffles = 1000000;
bDifSelected = true;
break;
}
default:
{
cout << "Invalid input! Posslible inputs: 1, 2, 3." << endl;
bDifSelected = false;
break;
}
}
} while (bDifSelected != true);
}
void CheckVictory(char caaBoard[4][4]) {
const char kcaaWin[][4] = {
{'1', '2', '3', '4'},
{'5', '6', '7', '8'},
{'9', 'A', 'B', 'C'},
{'D', 'E', 'F', ' '}
};
iVictory = 0;
for (int iRow = 0; iRow < 4; ++iRow) {
for (int iCol = 0; iCol < 4; ++iCol) {
if (caaBoard[iRow][iCol] == kcaaWin[iRow][iCol]) {
++iVictory;
}
}
}
}
void InitializeBoard(char caaBoard[4][4]) {
const char kcaaInitial[4][4] = {
{'1', '2', '3', '4'},
{'5', '6', '7', '8'},
{'9', 'A', 'B', 'C'},
{'D', 'E', 'F', ' '}
};
for (int iRow = 0; iRow < 4; ++iRow) {
for (int iCol = 0; iCol < 4; ++iCol) {
caaBoard[iRow][iCol] = kcaaInitial[iRow][iCol];
}
}
}
void PrintBoard(char caaBoard[4][4]) {
using namespace std;
for (int iRow = 0; iRow < 4; ++iRow) {
for (int iCol = 0; iCol < 4; ++iCol) {
cout << caaBoard[iRow][iCol];
}
cout << endl;
}
}
void LocateSpace(int& irRow, int& irCol, char caaBoard[4][4]) {
for (int iRow = 0; iRow < 4; ++iRow) {
for (int iCol = 0; iCol < 4; ++iCol) {
if (caaBoard[iRow][iCol] == ' ') {
irRow = iRow;
irCol = iCol;
}
}
}
}
void Randomize(char caaBoard[4][4]) {
using namespace std;
srand((unsigned int)time(0));
for (int iIndex = 0; iIndex < iShuffles; ++iIndex) {
const int kiNextMove = (rand() % 4);
switch (kiNextMove) {
case 0:
{
Move(caaBoard, keUp);
break;
}
case 1:
{
Move(caaBoard, keDown);
break;
}
case 2:
{
Move(caaBoard, keLeft);
break;
}
case 3:
{
Move(caaBoard, keRight);
break;
}
}
}
}
void Move(char caaBoard[4][4], const EMove keMove) {
int iRowSpace;
int iColSpace;
LocateSpace(iRowSpace, iColSpace, caaBoard);
int iRowMove(iRowSpace);
int iColMove(iColSpace);
switch (keMove) {
case keUp:
{
iRowMove = iRowSpace + 1;
break;
}
case keDown:
{
iRowMove = iRowSpace - 1;
break;
}
case keLeft:
{
iColMove = iColSpace + 1;
break;
}
case keRight:
{
iColMove = iColSpace - 1;
break;
}
}
// Make sure that the square to be moved is in bounds
if (iRowMove >= 0 && iRowMove < 4 && iColMove >= 0 && iColMove < 4) {
caaBoard[iRowSpace][iColSpace] = caaBoard[iRowMove][iColMove];
caaBoard[iRowMove][iColMove] = ' ';
}
}