after days of frustration , finally i did it. Though, no one at DIC gave me hints when i needed, i feel a pretty relief.
But as programmer it's nothing compared to skyhawk133, martyr2, kya , no2pencil, NickDMax and other senior contributors.
It's a long way to go, i hope i can get there someday.
thanks
here's the Code:
CODE
//This is a maze program. it navigates through the board finding golden coins and landing on bombs.
#include <iostream>
#include <cassert>
#include <ctime>
using namespace std;
//prints the board
void print(char board[]);
// prototype shuffle function
// shuffles the pieces on the board
void shuffle( char [], int);
const int ROW_SIZE = 5;
const int BOARD_SIZE = ROW_SIZE * ROW_SIZE;
// shuffle function. it shuffles the characters on the board
void shuffle( char input[], int SIZE )
{
assert (SIZE >=0);
int j = BOARD_SIZE;
while ( j > 1)
{
int k = rand() % j;
char temp = input[ j - 1];
input [j - 1] = input[k];
input [k] = temp;
j --;
}
}
/ ********************************************************************************
******************************************************************
********************************************************************************
*******************************************************************
********************************************************************************
*******************************************************************
********************************************************************************
******************************************************************/
int main()
{
// seed of random numbers
srand (time (0));
char board[BOARD_SIZE] = {'X', 'B', 'X', 'B', 'X','G', 'X', 'G', 'G', 'G','X', 'X' , 'G', '_', '_','_', '_', '_', '_', '_','_', '_', '_', '_', '*'};
int Xposition = 0;
// calling shuffle
shuffle ( board , BOARD_SIZE);
// finding the starting point
for ( int count = 0; count < BOARD_SIZE; count ++ )
{
if ( board[count] == '*')
{
board[count] = '_';
board[Xposition] = '*';
break;
}
}
bool playing = true;
int total = 0; // accumulator
while (playing)
{
//cout << endl;
cout << total << " coins found. " << endl;
//cout << endl;
print(board);
char input;
bool okMove = true;
int newPosition;
cout <<"Move (u,d,l,r), or quit (q): ";
cin >> input;
if (input == 'q')
playing = false;
else if (input == 'l') {
newPosition = Xposition - 1;
if (Xposition % ROW_SIZE == 0 || board [newPosition] == 'X')
okMove = false;
}else if (input == 'r') {
newPosition = Xposition + 1;
if (newPosition % ROW_SIZE == 0 || board [newPosition] == 'X')
okMove = false;
}else if (input == 'u') {
newPosition = Xposition - ROW_SIZE;
if (newPosition < 0 || board[ newPosition] == 'X')
okMove = false;
}else if (input == 'd' ) {
newPosition = Xposition + ROW_SIZE;
if (newPosition >= BOARD_SIZE || board[newPosition] == 'X')
okMove = false;
}else
okMove = false;
if (playing)
{
if (okMove)
{
//getting coins and bombs
if ( board [newPosition] == 'B')
{
cout << endl;
cout << endl;
cout << "Sorry you landed in a bomb." << endl;
cout << "game over" << endl;
return 0;
}
else if ( board[newPosition] == 'G')
{
total++;
cout << "congratulations. You picked a golden coin. You have " << total << " points " << endl;
}
board[newPosition] = '*';
board [Xposition] = '_';
Xposition = newPosition;
}else
cout << "Illegal move\n";
}
cout << endl;
cout << endl;
cout << " Your total score is " << total << endl;
}
return 0;
}
//print the maze board
//precondition: board size == BOARD_SIZe
//postcondition: none
void print(char board [])
{
for (int count=0;count < BOARD_SIZE;count++)
{
if (count % ROW_SIZE == 0)
cout << endl;
// passing as the variable by reference
if (board [count] == 'G' || board [count] == 'B')
cout << '?';
else
cout << board[count];
}
cout << endl;
}