First Game

Heyo - I'm working on my first game and I am stuck at the moment and could use some ideas at how to proceed. I can get the player character to move, but not the pc-controlled char. I'm trying to set it up so that a random # is generated to move the 'R' character after the 'f' has moved, but I can't get it working. Any help would be greatly appreciated.

#include<iostream>
#include<cstdlib>
#include<ctime>
#include <windows.h>
#include <conio.h> // For using get.ch
#include <stdlib.h> // For cls
#pragma comment(lib, "winmm.lib")//Supposedly need this for sound?
using namespace std;

enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };

HANDLE hCon;



int mapArray[10][12];
void rider_movement(int mapArray[10][12]);

int gen_num();
void draw_map(int mapArray[10][12]);
void SetColor(Color c);

void menu(); //Game options ---Start Game




int main()
{
SetColor(YELLOW);
gen_num();
SetColor(BLUE);
//cout << "I should be BLUE";
SetColor(YELLOW);

draw_map(mapArray);


}

int gen_num() //Generates random number to be used in determing Rider movement.
{
srand((unsigned)time(0));
int random_number;
int lowest=1, highest=9;
int range=(highest-lowest)+1;
for(int index=0; index<10; index++){
random_number = lowest + (rand( ) % highest);

/* if(random_number == 5)
{
cout << "A rider is resting this turn..." << endl;
}*/

// cout << random_number << endl;
return random_number;
}

}

void SetColor(Color c) //Function designed to allow color changes easily
{
if(hCon == NULL)
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon, c);
}

void draw_map(int mapArray[10][12]) //Creates map with a multiarray as parameter
{
int x = 1, y = 1;
int height = 10, width = 12;
char input;

do
{
system("cls"); //Clears screen so you don't see the old map state
for(int r = 0; r < height; r++) //Nexted loop that draws map
{
for(int c = 0; c < width; c++)
{
cout << " | ";
if(r == y && c == x) //Places Frodo at 1,1
{
cout << (char)'f'; // Displays Frodo
}
else if(r == 9 && c == 11)
{
cout << (char)'E'; //Displays Exit
}
else if(r == 4 && c == 4)
{
void rider_movement(int mapArray, int random_number);
cout << (char)'R'; //Displays Rider

}
else
{
cout << '*'; // Empty Square
}
}
cout << " | " << endl; //Vertical wall
//if(mapArray[i][j]==1) cout << "E";
}

cout << endl;


input = getch();

switch(input)
{
case '9': y--; x++; break; //move northeast
case '8': y--; break; //move north
case '7': x--; y--; break; //move northwest
case '6': x++; break; //move east
case '5':; //stay still
case '4': x--; break; //move west
case '3': y++; x++; break; //move southwest
case '2': y++; break; //move south
case '1': y++; x--; break; // move southwest
}
//rider_movement(mapArray); Where does this fit into the loop?
}
while(input != 'q'); //can quit the loop by typing 'q'

}

void rider_movement(int mapArray[10][20], int random_number)//takes random_number to determine direction of rider movement
{
int x = 1, y = 1;
gen_num();
switch(random_number)
{
case '9': y--; x++; break; //move northeast
case '8': y--; break; //move north
case '7': x--; y--; break; //move northwest
case '6': x++; break; //move east
case '5':
cout << "All goes silent..." << endl; //Rider is resting
case '4': x--; break; //move west
case '3': y++; x++; break; //move southwest
case '2': y++; break; //move south
case '1': y++; x--; break; // move southwest
}

}
In the last switch statement you need case 9 etc, not case '9'. (no quotes).
Not sure how that could have anything to do with it, but I tried it anyways. No luck.

What should be happening, is following the movement of 'f' ...a random number should be generated which in turn decides where the 'R' moves.


'9' is the character '9', not the number 9 (e.g. on systems with ASCII, it's the value 57).
Did you replace all the other cases in that switch?
In the last switch statement you need case 9 etc,
I did try replacing them all the cases in the second switch without the single quotes. Still no movement from 'R'.
Last edited on
You have other problems as well.

In draw_map(), you are not calling the rider_movement function, you are declaring it (in a different way than you are declaring it near the top of the file).

rider_movement is taking a random_number, but you are calling gen_num() then throwing away the return value from gen_num() (so calling gen_num() is pointless).

You still need to change the literal numbers with their decimal values inside rider_movement().

Your for() loop inside gen_num() only executes once since you have a return statement in its body. Not sure why you are looping there in the first place.

The way your code is architected the rider can only ever be one space away from the point (1, 1) in any of the 8 directions since rider_movement() always sets x and y to 1.


Topic archived. No new replies allowed.