0

I was finally able to get fifteen to run, however it is outputting seemingly random numbers instead of what could be expected. It also says the input is invalid even when it is the correct input.

#define _XOPEN_SOURCE 500

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
// constants
#define DIM_MIN 3
#define DIM_MAX 9

// board
int board[DIM_MAX][DIM_MAX];

// dimensions
int d;

// prototypes
void clear(void);
void greet(void);
void init(void);
void draw(void);
bool move(int tile);
bool won(void);

int main(int argc, string argv[])
{
// ensure proper usage
if (argc != 2)
{
    printf("Usage: fifteen d\n");
    return 1;
}

// ensure valid dimensions
d = atoi(argv[1]);
if (d < DIM_MIN || d > DIM_MAX)
{
    printf("Board must be between %i x %i and %i x %i, inclusive.\n",
        DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
    return 2;
}

// open log
FILE* file = fopen("log.txt", "w");
if (file == NULL)
{
    return 3;
}

// greet user with instructions
greet();

// initialize the board
init();

// accept moves until game is won
while (true)
{
    // clear the screen
    clear();

    // draw the current state of the board
    draw();

    // log the current state of the board (for testing)
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            fprintf(file, "%i", board[i][j]);
            if (j < d - 1)
            {
                fprintf(file, "|");
            }
        }
        fprintf(file, "\n");
    }
    fflush(file);

    // check for win
    if (won())
    {
        printf("ftw!\n");
        break;
    }

    // prompt for move
    printf("Tile to move: ");
    int tile = GetInt();

    // quit if user inputs 0 (for testing)
    if (tile == 0)
    {
        break;
    }

    // log move (for testing)
    fprintf(file, "%i\n", tile);
    fflush(file);

    // move if possible, else report illegality
    if (!move(tile))
    {
        printf("\nIllegal move.\n");
        usleep(500000);
    }

    // sleep thread for animation's sake
    usleep(500000);
}

// close log
fclose(file);

// success
return 0;
}

/**
* Clears screen using ANSI escape sequences.
*/
void clear(void)
{
printf("\033[2J");
printf("\033[%d;%dH", 0, 0);
}

/**
 * Greets player.
 */
void greet(void)
{
clear();
printf("WELCOME TO GAME OF FIFTEEN\n");
usleep(2000000);
}

void init(void)
{
int d = GetInt();
int b[d][d];
int i, j;
int z = 0;
for (i=0; i<d; i++)
{
for (j=0; j<d;j++)
{

b[i][j] = d*d-1-z;
z++;
}

}

}

/**
 * Prints the board in its current state.
 */
void draw(void)

{
int i, j;
int b[d][d];
 for (i=0; i<d; i++)
{
 for (j=0; j<d; j++)
 {
 if (b[i][j] == 0)
 {
     printf(" _ ");
 }
 else
 {
 printf("%2d ", b[i][j]);
 }
 }
 printf("\n");
}
}

bool move(int tile)
{
int i, j;
int b[d][d];

for (i = 0; i<d; i++)
{
for (j = 0; j<d; j++)

{
if (tile ==b[i][j])
{
if (b[i-1][j] == 0)
{
if (i> 0){
int t = b[i][j];
b[i][j]=b[i-1][j];
t=b[i-1][j];
return true;
}
}
if (j>0)
{
if (b[i][j-1]==0)
{
int t = b[i][j];
b[i][j]=b[i][j-1];
t=b[i][j-1];
return true;
}

}

if (i<d-1)
{
if (b[i+1][j]==0)
{
int t = b[i][j];
b[i][j]=b[i+1][j];
t=b[i+1][j];
return true;
}
}
if (j<d-1)
{
if (b[i][j+1]==0)
{
int t = b[i][j];
b[i][j]=b[i][j+1];
t=b[i][j+1];
return true;
}
}
}
}

}
 return false;
}





bool won(void)
{
    int i;
    int j;
   int x[d][d];
   int b[d][d];

    for(i = 0; i<d; i++) //sets number of columns
{
for (j= 0; j<d; j++) //sets number of rows
{
for (int l = 1; l<d*d-1; l++)
{
x[i][j] = l;  //fills with values
}
if (x[i][j] == b[i][j]) 
{
return true;
}
else
{
return false;
}

}
}
return 0; 
}
2

Remember that you already got d from command line input, so there is no need for

d=GetInt()

in init() function. If on the other hand you are not providing d at command line, that would account for the invalid input error you are getting.

2
  • Even after making this change it still outputs random numbers after a single move. When running my code it will print out the board, but when I input a tile to move it says illegal move and prints out something like this: 28267088 _ 28266512 _ 2 _ 1148784640 32713 -1304358352 32767 1137828828 32713 -1304358816 32767 4 _
    – adjcode
    Nov 19 '16 at 15:11
  • Remember you already defined board[d][d] as a global variable. When you declared b[d][d] in init(), you created another array which was erased from the stack after init() finished executing. But you then try to draw b[d][d] in draw().
    – ronga
    Nov 20 '16 at 8:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .