Loading
submit to reddit
May 21, 2013, 04:27:57 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Welcome!
 
   Forum Home   Help Search Forum Login Register  
Pages: [1]   Go Down
  Send this topic  |  Print  
Author Topic: Fifteen Puzzle  (Read 808 times)
Kreation
Newbie
*
Posts: 8


« on: October 10, 2011, 01:37:47 PM »

I downloaded the code from the lesson and was looking at it to get a better understanding. I even played a game and when I finished it didn't close or anything. I know that's because the infinite loop, but I tried to make a way for it to break out of the loop.

I added this code at the end of the loop:

Code:
if (CheckBoard(caaBoard)) {
cout << "You won!" << endl;
break;
}

This is the function:

Code:
bool CheckBoard(char caaBoard[4][4]) {
const char caaCheck[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) {
if (caaBoard[iRow][iCol] == caaCheck[iRow][iCol]) {
return true;
} else {
return false;
}
}
}
}

The problem with this is, once I put the number 1 in the first spot it's supposed to be, the loop breaks and displays the message. I understand why that happens, but I want to make sure that it is correct.

I also have a solution. Although, I'm not completely certain of it, I'm pretty sure I would have to check each spot of the board individually.

If you could let me know if that is correct or incorrect, that'd be great.

Thanks, Kreation.
Logged
Kreation
Newbie
*
Posts: 8


« Reply #1 on: October 10, 2011, 01:53:01 PM »

I tested this myself and it was exactly what I presumed. I had to check each space specifically.

This is the new function:

Code:
bool CheckBoard(char caaBoard[4][4]) {
const char caaCheck[4][4] = {
{ '1', '2', '3', '4' },
{ '5', '6', '7', '8' },
{ '9', 'A', 'B', 'C' },
{ 'D', 'E', 'F', ' ' }
};

if (caaBoard[0][0] == caaCheck[0][0] &&
caaBoard[0][1] == caaCheck[0][1] &&
caaBoard[0][2] == caaCheck[0][2] &&
caaBoard[0][3] == caaCheck[0][3] &&

caaBoard[1][0] == caaCheck[1][0] &&
caaBoard[1][1] == caaCheck[1][1] &&
caaBoard[1][2] == caaCheck[1][2] &&
caaBoard[1][3] == caaCheck[1][3] &&

caaBoard[2][0] == caaCheck[2][0] &&
caaBoard[2][1] == caaCheck[2][1] &&
caaBoard[2][2] == caaCheck[2][2] &&
caaBoard[2][3] == caaCheck[2][3] &&

caaBoard[3][0] == caaCheck[3][0] &&
caaBoard[3][1] == caaCheck[3][1] &&
caaBoard[3][2] == caaCheck[3][2] &&
caaBoard[3][3] == caaCheck[3][3])
{
return true;
}

return false;
}

But I'd like to know if there is anyway to make it look cleaner, because it looks really long and eh choppy? I don't know, I just think there is a way to make it shorter and more efficient that I don't see.
Logged
Michael Hall
Administrator
Hero Member
*****
Posts: 901



« Reply #2 on: October 10, 2011, 02:01:54 PM »

Yes, I would use your original code and make the following modification to the loops:

Code:
for (int iRow = 0; iRow < 4; ++iRow) {
     for (int iCol = 0; iCol < 4; ++iCol) {
          if (caaBoard[iRow][iCol] != caaCheck[iRow][iCol]) {
               return false;
          }
     }
}
return true;

Try that out.

Mike
Logged
Kreation
Newbie
*
Posts: 8


« Reply #3 on: October 10, 2011, 04:23:04 PM »

Alright, thanks for that. I'll try it out and give some more feedback to how it worked.

EDIT: I tested it, finally(took me forever to freaking win the game lol). It worked, thanks again for clearing that up for me.
« Last Edit: October 10, 2011, 04:42:02 PM by Kreation » Logged
Michael Hall
Administrator
Hero Member
*****
Posts: 901



« Reply #4 on: October 10, 2011, 05:38:54 PM »

Yoiu're welcome. I'm glad that I could help. BTW, I have done a solution video for the fifteen puzzle, if that will help you with testing:

http://xoax.net/puzzle/Fifteen.php

Mike
Logged
Kreation
Newbie
*
Posts: 8


« Reply #5 on: October 11, 2011, 08:10:32 PM »

Yoiu're welcome. I'm glad that I could help. BTW, I have done a solution video for the fifteen puzzle, if that will help you with testing:

http://xoax.net/puzzle/Fifteen.php

Mike

Thanks, but I think it was mainly because I was rushing to finish the game so I could reply back and I got mixed up with my movements a little bit. I'll be sure to check out the video just in case.

EDIT: Very handy video, I did it a little bit. Both get the job done, although yours is most likely quicker. Tongue
« Last Edit: October 11, 2011, 08:13:10 PM by Kreation » Logged
VinQbator
Newbie
*
Posts: 2


« Reply #6 on: January 17, 2012, 03:02:46 PM »

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 Smiley

PS. You can change the value of iShuffles in case 1 in SelectDifficulty function to 1 to quickly test the program...


Code:
#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] = ' ';
}
}
Logged
Pages: [1]   Go Up
  Send this topic  |  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!