my game didnt work

hello. can somebody help me? i want to make puzzle, i do. but after the game over and the player win, i want to print congratulation for the player. but i don't know why it didn't came put, and go back directly to the menu. please kindly help me where's the mistakes. thank you

/*
*
* File : puzzle
*
*/

#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <iostream.h>

#define up 1
#define left 2
#define down 3
#define right 4

#define keyUp 0x048
#define keyDown 0x050
#define keyLeft 0x04b
#define keyRight 0x04d

#define true 1
#define false 0

#define easy 10
#define medium 20
#define hard 30

#define caption " ----------------\n// GAME PUZZLE //\n----------------\n"

#define fieldSize 4

char field[fieldSize][fieldSize];
char field2[fieldSize][fieldSize];
unsigned int seed;
int x, y;

int random(int i);
void initField(int movement);
void move(int direction);
void generateOutput();
int seqCheck();

int random(int i) {
int a;
seed += 5;
srand(seed);

a = (rand() % i) + 1;

return(a);
}

void initField(int movement) {
int direction, dirOld = -1, nMove = movement, xOld, yOld;
int c = 1, i, j;
char temp;

srand(time(NULL));
seed = rand();
x = y = (fieldSize - 1);

for (i = 0; i <= (fieldSize - 1); i++) {
for (j = 0; j <= (fieldSize - 1); j++) {
field[i][j] = field2[i][j] = c;
c++;
}
}
field[fieldSize - 1][fieldSize - 1] = field2[fieldSize - 1][fieldSize - 1] = ' ';

c = 0;
while (c != nMove) {
xOld = x;
yOld = y;
direction = random(4);

if (direction != 0)
{
if ( c != 0)
{
if ((direction+2) % 4 == dirOld)
{
continue;
}
}

switch (direction)
{
case up :
if (y > 0) y--;
else continue;
break;
case left:
if (x > 0) x--;
else continue;
break;
case right :
if (x < fieldSize - 1) x++;
else continue;
break;
case down :
if (y < fieldSize - 1) y++;
else continue;
break;
default :
break;
}

if (x >= 0 && y >= 0 && x <= (fieldSize - 1) && y <= (fieldSize - 1))
{
temp = field[y][x];
field[y][x] = field[yOld][xOld];
field[yOld][xOld] = temp;

c++;
dirOld = direction % 4;
}
else
{
x = xOld;
y = yOld;
}
}
}
}

void move(int direction)
{
int xOld, yOld;
char temp;

xOld = x;
yOld = y;

switch (direction)
{
case up :
if (y > 0) y--;
break;
case left:
if (x > 0) x--;
break;
case right :
if (x < (fieldSize - 1)) x++;
break;
case down :
if (y < (fieldSize - 1)) y++;
break;
default :
break;
}

if (x >= 0 && y >= 0 && x <= (fieldSize - 1) && y <= (fieldSize - 1))
{
temp = field[y][x];
field[y][x] = field[yOld][xOld];
field[yOld][xOld] = temp;
}
else
{
x = xOld;
y = yOld;
}
generateOutput();
}

void generateOutput() {
int i, j, k;
system("cls");
puts(caption);
puts("Press ESC to quit / reset game...");
for(k = 1; k <= fieldSize; k++)
printf("+----");
puts("+");

for (i = 0; i<=(fieldSize - 1); i++)
{
for (j= 0; j<=(fieldSize - 1); j++)
{
if (i == y && j == x)
{
printf("| %c ", field[i][j]);
}
else
{
printf("| %2i ", field[i][j]);
}
}
puts("|");

for(k = 1; k <= fieldSize; k++) printf("+----"); puts("+");
}
}

int seqCheck() {
int c, d;
int match = true;

for (c = 0; c <= (fieldSize - 1); c++)
{
for (d = 0; d <= (fieldSize - 1); d++)
{
if (field[c][d] != field2[c][d])
{
if (match == true)
{
match = false;
}
}
}
}
return(match);
}

void main() {
int i, j, k, level;
char key;

system("cls");
puts(caption);
puts ("Play the puzzle and set it into the correct position...");
puts ("Slide the empty box so that they will form the sequence as follows : \n");
initField(0);
for(k = 1; k <= fieldSize; k++) printf("+----"); puts("+");
for (i = 0; i<=(fieldSize - 1); i++)
{
for (j= 0; j<=(fieldSize - 1); j++)
{
if (i == y && j == x)
{
printf("| %c ", field2[i][j]);
}
else
{
printf("| %2i ", field2[i][j]);
}
}
puts("|");

for(k = 1; k <= fieldSize; k++) printf("+----"); puts("+");
}
puts("Use arrow button to slide empty box...\n");
puts("Press any key to continue the game...");
getch();
for(;;) {
system("cls");
puts("Level : ");
puts("\t1. Easy");
puts("\t2. Medium");
puts("\t3. Hard");
printf("Choose the level you want to play : ");
scanf("%i", &level);

switch (level) {
case 1 :
initField(easy);
break;
case 2 :
initField(medium);
break;
case 3 :
initField(hard);
break;
default :
puts("Error!!Wrong Level");
getch();
continue;
}

system("cls");

generateOutput();

while ((key = getch()) != 27) {
switch(key) {
case keyUp :
move(up);
break;
case keyDown :
move(down);
break;
case keyLeft :
move(left);
break;
case keyRight :
move(right);
break;
}
if (seqCheck() == true) {
puts("\nCONGLATURATION!!! YOU WIN!!!");
break;
}
}
if (key == 27) {
printf("Do you want to quit ?\n['y' yes / 'n' to reset] : ");
if (toupper(getchar()) == 'Y')
break;
else
continue;
} else {
printf("Do you want to play again ? [y/n] : ");
if (toupper(getchar()) == 'N') {
puts("\nThank you for playing and Have a Nice Day!!!");
getch();
break;
}
else continue;
}
}
}
Your game does print congratulations.
I think the problem is that there is a character remaining in the buffer when you break after printing congratulations.

If the player wins, the else part of (key==27) condition is performed.
The getchar() gets the remaining character in the buffer which is not 'N' so the game continues straight away.

I don't know how to clear the buffer though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (key == 27) {
    printf("Do you want to quit ?\n['y' yes / 'n' to reset] : ");
    if (toupper(getchar()) == 'Y')
        break;
    else
        continue;
} 
else {
    printf("Do you want to play again ? [y/n] : ");
    if (toupper(getchar()) == 'N') {
        puts("\nThank you for playing and Have a Nice Day!!!");
        getch();
        break;
    }
    else continue;
}
Last edited on
std::endl; should flush the buffer, it is the main reason I use it over "\n"
Topic archived. No new replies allowed.