Moving character on the map(2d array)

I am trying to make a moving character on the 2d map. When make it move with w, a, s, d keys it is doing well for w and a keys and for s and d it isnt. Where i make mistake!!????

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "pch.h"
#include <iostream>
#include <cstdlib>
#include <conio.h>

char tabla[5][5] = { {'X', 'X', 'X', 'X', 'X'},
		     {'X', 'X', 'X', 'X', 'X'},
		     {'X', 'X', '*', 'X', 'X'},
		     {'X', 'X', 'X', 'X', 'X'},
		     {'X', 'X', 'X', 'X', 'X'} };

// Functions

void printTabla(char _tabla[5][5]);

// Main

int main()
{

	
	char pomeranje;
	
	printTabla(tabla);
	pomeranje = _getch();

		

		for (int i = 0; i < 5; i++) {
			
			for (int k = 0; k < 5; k++) {
				
				if (tabla[i][k] == '*') {

					if (pomeranje == 'w')
						std::swap(tabla[i][k],tabla[i- 1][k]);
					else if (pomeranje == 's')
						std::swap(tabla[i][k], tabla[i + 1][k]);
					else if (pomeranje == 'a')
						std::swap(tabla[i][k], tabla[i][k - 1]);
					else if (pomeranje == 'd')
						std::swap(tabla[i][k], tabla[i][k + 1]);

				}

			}
			system("CLS");
			printTabla(tabla);
		}
	
	// Kraj

	std::cout << std::endl;
	system("PAUSE");
	return 0;
}

//----------------------------------------------------------------------------------------------------------------------------------------------

// Pravljenje funkcija 

void printTabla(char _tabla[5][5]) {
	for (int i = 0; i < 5; i++) {
		for (int j = 0; j < 5; j++)
			std::cout << tabla[i][j] << " ";
		std::cout << std::endl;
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <utility>
#include <algorithm>
#include <cctype>

constexpr int N = 5 ;
using grid_type = char[N][N] ;

enum direction { UP = 'w', DOWN = 's', LEFT = 'a', RIGHT = 'd', QUIT = 'q' };

using point = std::pair<int,int> ;

using std::swap ;

point locate( const grid_type& grid, char ch )
{
    for( int row = 0 ; row < N ; ++row )
        for( int col = 0 ; col < N ; ++col )
            if( grid[row][col] == ch ) return { row, col } ;

    return { -1, -1 } ; // not found
}

bool valid_loc( int x, int y ) { return x >= 0 && x < N && y >= 0 && y < N ; }

void move_up( grid_type& grid, char ch )
{
    const auto [x,y] = locate( grid, ch ) ;
    if( valid_loc( x, y ) && x > 0 ) swap( grid[x][y], grid[x-1][y] ) ;
}

void move_down( grid_type& grid, char ch )
{
    const auto [x,y] = locate( grid, ch ) ;
    if( valid_loc( x, y ) && x != (N-1) ) swap( grid[x][y], grid[x+1][y] ) ;
}

void move_left( grid_type& grid, char ch )
{
    const auto [x,y] = locate( grid, ch ) ;
    if( valid_loc( x, y ) && y != 0 ) swap( grid[x][y], grid[x][y-1] ) ;
}

void move_right( grid_type& grid, char ch )
{
    const auto [x,y] = locate( grid, ch ) ;
    if( valid_loc( x, y ) && y != (N-1) ) swap( grid[x][y], grid[x][y+1] ) ;
}

bool make_move( grid_type& grid, char ch ) // return false if the move is quit
{
    char move ;
    std::cout << "ener move (w/a/s/d/q): " ;
    std::cin >> move ;

    switch( std::tolower(move) )
    {
        case UP: move_up( grid, ch ) ; break ;
        case DOWN: move_down( grid, ch ) ; break ;
        case LEFT: move_left( grid, ch ) ; break ;
        case RIGHT: move_right( grid, ch ) ; break ;

        case QUIT: return false ;

        default: ; // invalid move, ignored
    }

    return true ; // not quit
}

void print( const grid_type& grid )
{
    for( const auto& row : grid )
    {
        for( char c : row ) std::cout << c << ' ' ;
        std::cout << "\n\n" ;
    }
}

int main()
{
    const char star = '*' ;
    grid_type tabla = { {'X', 'X', 'X',  'X', 'X'},
                        {'X', 'X', 'X',  'X', 'X'},
                        {'X', 'X', star, 'X', 'X'},
                        {'X', 'X', 'X',  'X', 'X'},
                        {'X', 'X', 'X',  'X', 'X'} };

   do print(tabla) ; while( make_move( tabla, star ) ) ; // keep moving till user quits
}
@Aleksa03

Expanding a bit on your code, here's my version. I check first that a move does not take you off the grid, then do the swap, and then adjust i or k, accordingly. Pressing 'q', ends the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <cstdlib>
#include <conio.h>

char tabla[5][5] = { {'X', 'X', 'X', 'X', 'X'},
{'X', 'X', 'X', 'X', 'X'},
{'X', 'X', '*', 'X', 'X'},
{'X', 'X', 'X', 'X', 'X'},
{'X', 'X', 'X', 'X', 'X'} };

// Functions

void printTabla(char _tabla[5][5]);

// Main

int main()
{


	char pomeranje;
	int i=2,k=2;
	printTabla(tabla);
	do
	{
	pomeranje = ' ';
	pomeranje = _getch();


	if (pomeranje == 'w')
	{
		if(i-1 >=0)
		{
			std::swap(tabla[i][k],tabla[i - 1][k]);
			i--;
		}
	}
	else if (pomeranje == 's')
	{
		if(i+1 < 5)
		{
			std::swap(tabla[i][k],tabla[i + 1][k]);
			i++;
		}
	}
	else if (pomeranje == 'a')
	{
		if(k-1 >=0)
		{
			std::swap(tabla[i][k],tabla[i][k - 1]);
			k--;
		}
	}
	else if (pomeranje == 'd')
	{
		if(k+1 < 5)
		{
			std::swap(tabla[i][k],tabla[i][k + 1]);
			k++;
		}
	}

	system("CLS");
	printTabla(tabla);
	}while (pomeranje != 'q');


	// Kraj

	std::cout << std::endl;
	system("PAUSE");
	return 0;
}

//----------------------------------------------------------------------------------------------------------------------------------------------

// Pravljenje funkcija 

void printTabla(char _tabla[5][5]) {
	for (int i = 0; i < 5; i++) {
		for (int j = 0; j < 5; j++)
			std::cout << tabla[i][j] << " ";
		std::cout << std::endl;
	}
}
Yet another possibility:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <algorithm>
#include <conio.h>

const int NumRows = 10;
const int NumCols = 10;

int main() {
    char grid[NumRows][NumCols];
    std::fill((char*)grid, (char*)grid + NumRows*NumCols, '.');

    int row = NumRows / 2, col = NumCols / 2;
    grid[row][col] = '#';

    for (bool looping = true; looping; ) {

        for (int r = 0; r < NumRows; ++r) {
            for (int c = 0; c < NumCols; ++c)
                std::cout << grid[r][c] << ' ';
            std::cout << '\n';
        }
        std::cout << '\n';

        int newcol = col, newrow = row;
        bool goodchar = true;
        switch (_getch()) {
        case 'a': case 'A': --newcol; break;
        case 'd': case 'D': ++newcol; break;
        case 's': case 'S': ++newrow; break;
        case 'w': case 'W': --newrow; break;
        case 'q': case 'Q': looping = false; break;
        default: goodchar = false;
        }

        if (goodchar && looping) {
            newrow = newrow < 0 ? newrow + NumRows : newrow % NumRows;
            newcol = newcol < 0 ? newcol + NumCols : newcol % NumCols;
            std::swap(grid[newrow][newcol], grid[row][col]);
            row = newrow;
            col = newcol;
        }
    }
}

Thanks its working.
Topic archived. No new replies allowed.