Can't get my code to work (grid game)

The K's should move on the grid.. but it wont :(! I'm not sure what I'm doing wrong, but something is obviously wrong with my code. Any insight? Thank you!

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
#include <iostream>
#include <windows.h>
#include <ctime>

using namespace std;

char Map[15][15] ={
"##############",
"# K          #",
"#            #",
"#  K         #",
"#       K    #",
"#            #",
"#            #",
"#            #",
"#     K      #",
"#            #",
"#            #",
"#            #",
"#         K  #",
"##############"
};


bool running = true;
int gamespeed = 100;
int randNum = 0;

int main(int argv, char** argc)
{

    while (running) {
            system ("cls");
        for (int y=0; y<15; y++) {
            cout << Map[y] <<endl;
        }
    for (int y=0; y<15; y++) {
        for (int x=0; x<15; x++)
            {
                switch (Map[y][x])
                {
                    case 'K' :
            randNum = rand() % 2 + 1;

            switch (randNum) {
        case 1:
            if (Map[y-1][x] != '#' && Map [y-1][x] != 'K')
                {
                    Map[y][x] = ' ';
                    Map[y-1][x] = 'K';
                }
            break;

        case 2:
            if (Map[y][x+1] != '#' && Map [y][x+1] != 'K'){
                    Map[y][x] = ' ';
                    Map[y][x+1] = 'K';
        }
        break;

        case 3:
           if (Map[y+1][x] != '#' && Map [y-1][x] != 'K')
                {
                    Map[y][x] = ' ';
                    Map[y+1][x] = 'K';
                }
            break;

        case 4:
            if (Map[y][x-1] != '#' && Map[y][x-1] != 'K' ){
                    Map[y][x] = ' ';
                    Map[y][x-1] = 'K';
            }


      }
    }

    }

    randNum = 0;
    return 0;
}}}
@goflipaburger

Here's your program, with slight corrections. All the 'K's now move, but I'm not checking if another 'K' is at the same location. But, even if they are, after a move or two, they do separate and go their own way.

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
#include <iostream>
#include <windows.h>
#include <ctime>

using namespace std;

char Map[14][15] = {
	"##############",
	"# K          #",
	"#            #",
	"#  K         #",
	"#       K    #",
	"#            #",
	"#            #",
	"#            #",
	"#     K      #",
	"#            #",
	"#            #",
	"#            #",
	"#         K  #",
	"##############"
};

int K_Locations[5][2] = { { 1, 2 }, { 3, 3 }, { 4, 8 }, { 8, 6 }, { 12,10 } };// Locations of 'K's in grid
bool running = true;
int gamespeed = 100;
int randNum = 0;

int main()
{
	int y,x;
	do
	{
		for (int y = 0; y < 5; y++)
		{
			system("cls"); 
			randNum = rand() % 4 + 1;
			switch (randNum)
				{
				case 1:
					if (K_Locations[y][0]-1>0)
					{
						Map[K_Locations[y][0]][K_Locations[y][1]] = ' ';
						K_Locations[y][0] -= 1;
						Map[K_Locations[y][0]][K_Locations[y][1]] = 'K';
					}
					break;
				case 2:
					if (K_Locations[y][0] + 1<13)
					{
						Map[K_Locations[y][0]][K_Locations[y][1]] = ' ';
						K_Locations[y][0] += 1;
						Map[K_Locations[y][0]][K_Locations[y][1]] = 'K';
					}
					break;
				case 3:
					if (K_Locations[y][1] - 1>0)
					{
						Map[K_Locations[y][0]][K_Locations[y][1]] = ' ';
						K_Locations[y][1] -= 1;
						Map[K_Locations[y][0]][K_Locations[y][1]] = 'K';
					}
					break;
				case 4:
					if (K_Locations[y][1] +1 < 15)
					{
						Map[K_Locations[y][0]][K_Locations[y][1]] = ' ';
						K_Locations[y][1] += 1;
						Map[K_Locations[y][0]][K_Locations[y][1]] = 'K';
					}
				}
				for (int a = 0; a < 14; a++)
				{
					cout << Map[a] << endl;
				}
			Sleep(500);
			}
	} while (running);
	return 0;
}
Thank you SO much!!!!! I definitely see how different our versions are. It makes sense now that I can finally see it. Greatly appreciated :)!!!
Topic archived. No new replies allowed.