Hi, I've got a puzzle game program consist of 2D array 2 rows x 3 cols.
The professor wants no changes to be made in puzzle.h, which is:
#ifndef _PUZZLE_H
#define _PUZZLE_H
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
// Constants
const int COLS = 3;
const int ROWS = 2;
// Function prototypes
// Your cpp file must have the code for movePuzzle
void movePuzzle(int puzzle[][COLS], char dir, int rowcol);
void initPuzzle(int puzzle[][COLS], unsigned int seed);
void initPuzzle(int puzzle[][COLS], unsigned int seed)
{
/* Initialise the puzzle and scramble it around */
int x, y;
// initialise random number generator using the seed value
srand(seed);
// fill the puzzle
for (y=0; y<ROWS; y++) {
for (x=0; x<COLS; x++) {
puzzle[y][x] = 1 + y * COLS + x;
}
}
// scramble the puzzle
for (y=0; y<=ROWS*COLS; y++) {
if ((rand() % 2) == 0) {
movePuzzle(puzzle, 'v', rand() % COLS);
}
else {
movePuzzle(puzzle, 'h', rand() % ROWS);
}
}
}
#endif
This is my puzzle.cpp which includes puzzle.h
/*****************************************************************************\
This is a C++ program that runs a puzzle game.
The puzzle consist of 2 row x 3 column grid.
Grid filled with numbers 1 to 6 in random order.
The main aim of this puzzle game is to get user swap those number so that
they are in order (with number 1 in top left and 6 in bottom right).
\*****************************************************************************/
#include "puzzle.h"
int main(int argc, char *argv[])
{
int puzzle[ROWS][COLS];
initPuzzle(); ?????
cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "\n";
cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "\n";
cout << "i : " << "to print these instructions" << "\n";
cout << "q : " << "to quit" << "\n";
system("PAUSE");
return 0;
}
void movePuzzle(int puzzle[][COLS], char dir, int rowcol)
{
}
My problem is to call initPuzzle function before any other code.
I need to pass array and seed using command line argument.
I'm new to C++, can someone explain how to do that? (the line with ????)
initPuzzle function suppose to set up the puzzle and randomise it.
The function itself is in the puzzle.h and I can only call it in the puzzle.cpp (no changes can be made to puzzle.h).
Any help would be appreciated.