Hi guys just wondering if anyone could give me some tips on starting a moveable char function for my maze game.

I wish to create a function that allows me to move the player through the maze shown below. I have a few ideas on how to do this however the methods I am coming up with are slightly long winded.

Just wondering if anyone had any examples or ideas.

Below is the code I have so far

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <conio.h>
#include <Windows.h>
using namespace std;


int col = 1;
int row = 2; 
const int rowm = 20;
const int colm = 20;
char maze[rowm][colm] = 
	{
		{'X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X'},
		{'X','S','X','X','X','X',' ',' ',' ',' ',' ',' ',' ',' ','X','X',' ',' ',' ','X'},
		{'X',' ','X','X','X','X',' ','X','X','X','X','X','X','X','X','X',' ',' ',' ','X'},
		{'X',' ',' ',' ',' ','X',' ',' ',' ',' ',' ',' ',' ',' ',' ','X',' ','X','X','X'},
		{'X',' ','X','X','X','X',' ','X','X','X','X','X','X','X',' ','X',' ',' ',' ','X'},
		{'X',' ','X','X','X','X',' ',' ',' ',' ',' ',' ',' ',' ',' ','X',' ','X',' ','X'},
		{'X',' ','X',' ',' ','X',' ','X','X','X',' ','X','X','X','X','X',' ','X',' ','X'},
		{'X',' ',' ',' ','X','X',' ',' ','X','X',' ',' ',' ',' ',' ',' ',' ','X',' ','X'},
		{'X',' ','X',' ','X','X','X',' ','X',' ',' ',' ',' ','X',' ','X',' ',' ',' ','X'},
		{'X',' ','X',' ',' ',' ',' ',' ','X',' ','X','X',' ','X',' ','X',' ','X','X','X'},
		{'X',' ','X','X','X','X','X',' ','X',' ','X','X',' ','X',' ','X',' ',' ',' ','X'},
		{'X',' ','X','X',' ',' ',' ',' ','X',' ',' ','X',' ','X',' ','X',' ','X',' ','X'},
		{'X',' ',' ','X',' ','X','X','X','X','X',' ','X',' ','X',' ','X',' ','X',' ','X'},
		{'X','X',' ','X',' ','X',' ',' ',' ',' ',' ','X',' ','X',' ','X','X','X',' ','X'},
		{'X','X',' ','X','X','X','X',' ','X',' ','X','X',' ','X',' ',' ',' ','X',' ','X'},
		{'X','X',' ','X','X','X','X',' ','X',' ','X','X',' ','X','X','X','X','X',' ','X'},
		{'X',' ',' ','X',' ',' ','X',' ','X',' ','X','X',' ',' ',' ','X',' ',' ',' ','X'},
		{'X',' ','X','X','X',' ','X',' ','X',' ','X','X','X','X',' ','X',' ','X',' ','X'},
		{'X',' ',' ',' ',' ',' ','X',' ','X',' ',' ',' ',' ',' ',' ','X',' ','X','F','X'},
		{'X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X'}};

void printmaze();
void runmaze(int, int);

void initgame()
{
	printf("Welcome to ze game!");
}
void printmaze()
{
for(int row = 0; row < rowm; row++)
{
for(int col=0; col < colm; col++)
cout << maze[row][col];
cout << "\n";
}
}

void runmaze(int row, int col)
{
if( (row>0 && row<rowm) && (col>0 && col<colm)) {
if( maze[row][col] == 'W' ) return;

if( maze[row][col] == ' ') {
maze[row][col]='*';

runmaze(row, col+1);
runmaze(row, col-1);
runmaze(row-1, col);
runmaze(row+1, col);
}
}
}
void player(char rowm, char colm)
{
	player(rowm ="h", colm
}
int main()
{
	runmaze(2,2);
	printmaze();
	return 0;
}

Regards

Ryan

Hey man!
Here's some really old code from me.
To move player around, just change value of cField[j]. For example, if player is on position i = 4, j = 5 and you want to move left, you want to do this:
cField[j] = ' '; (remove player from previous position) (i = 4; j = 5; )
cField[j-1] = 'X'; (go left) (i = 4; j = 4; )

To get i and j, just use for loop. Go through whole field until you find char that is equal to '$' (in my case symbol for player).

There are 2 ways to draw field. One is to draw whole field every single time the player moves. This will create flickering. My way is a bit different. You have function curpos(y, x); and with it you are able to put cursor on certain position and draw wherever you want in field without having to draw whole field every time, in this case remove the player from old position and put it on new position.


#include <iostream>
#include <conio.h>
#include <time.h>
#include <windows.h>

#define tp 224
#define UP 72
#define LEFT  75
#define RIGHT 77
#define DOWN  80

using namespace std;

void curpos(int y, int x) {
  HANDLE hStdout;
  CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
  hStdout=GetStdHandle(STD_OUTPUT_HANDLE);
  GetConsoleScreenBufferInfo(hStdout, &csbiInfo);
  csbiInfo.dwCursorPosition.X=x;
  csbiInfo.dwCursorPosition.Y=y;
  SetConsoleCursorPosition(hStdout, csbiInfo.dwCursorPosition);
}

void getpos(char ch, int i, int j, int &x, int &y)
{
    if(ch == '$')
    {
        x = i;
        y = j;
    }
}
void draw(char ch, int x, int y)
{
    curpos(x, y);
    switch(ch)
    {
        case 'X':
        cout << (char)219;
        break;

        case '$':
        cout << (char)2;
        break;

        case ' ':
        cout << ' ';
        break;
    }
    curpos(10, 0);
}

void move(char &old_ch, char &new_ch, int x, int y, int i, int j)
{
    old_ch = ' ';
    new_ch = '$';
    draw(old_ch, x, y);
    draw(new_ch, i, j);
}

int main()
{
    char cField[9][16] = {
        "XXXXXXXXXXXXXX\n",
        "X$           X\n",
        "X            X\n",
        "X            X\n",
        "X            X\n",
        "X            X\n",
        "X            X\n",
        "X            X\n",
        "XXXXXXXXXXXXXX\n"};

    int i, j, x, y, k, move2;
    unsigned key, key2;
    enum dir {up, down, left, right};

    for(i=0;i<9;i++)
        for(j=0;j<15;j++)
        draw(cField[i][j], i, j);

    while(key != 27)
    {
        key = getch();
        if (key == tp)
        {
            key2 = getch();
            switch(key2)
            {
                case LEFT:
                    k = left;
                    break;
                case RIGHT:
                    k = right;
                    break;
                case UP:
                    k = up;
                    break;
                case DOWN:
                    k = down;
                    break;
            }
        }
        for(i=0;i<9;i++)
            for(j=0;j<15;j++)
            getpos(cField[i][j], i, j, x, y);

        if(k == up && cField[x-1][y] != 'X')
        {
            move(cField[x][y], cField[x-1][y], x, y, x-1, y);
        }
        else if(k == down && cField[x+1][y] != 'X')
        {
            move(cField[x][y], cField[x+1][y], x, y, x+1, y);
        }
        else if(k == left && cField[x][y-1] != 'X')
        {
            move(cField[x][y], cField[x][y-1], x, y, x, y-1);
        }
        else if(k == right && cField[x][y+1] != 'X')
        {
            move(cField[x][y], cField[x][y+1], x, y, x, y+1);
        }
    }
    return 0;
}
commented: Great advice. Can't thank you enough! +0

Thanks so much. This has helped me immensely.

Yours Sincerely

Ryan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.