How to code a 2D array rolling algorithm using WASD keyboard control

I'm fairly new and I'm trying to display a 2D array rolling algorithm so that I can continuously rotate the display in UP, DOWN, LEFT, and RIGHT directions. By Continuous rotation, I mean the animation should go on forever until another command is issued. This is what I have so far...

[code]
#include <stdio.h> // getchar(), getc(), fgets(), scanf()
#include <conio.h> // _kbhit(), _getch()
#include <unistd.h>

int main()
{

char cmd = 0;

//program loop
while(cmd != 'x')
{
// this will NOT require ENTER key after the input to make it effective
if(_kbhit()) //if a keyboard hit is detected...
{
cmd = _getch();
printf("%c is pressed...\n", cmd);
}
else
{
printf(".");
usleep(5000);
}

}

}
what is this? Graphics, or text, how you do it will change somewhat though the principles are the same. Rotating graphics you can wrap the image onto a surface and move the surface, while text is going to need console I/O tricks.

you can cycle with %
for example, say you have
int x[100];
int i = 0;
while(true);
{
cout << x[i];
i = (i+1)%100; //0,1,2,...98,99,0,1,2,...
}

the trouble with text is that without third party libraries scrolling left and right is tricksy.
for that, you may need to generate the shifted array and then draw it by rows. In that case the function memmove may be very useful to you.
Last edited on
This is the integer array i want
0010 0010 0010 0010 0010 0010
0010 0010 0010 0010 0010 0010
1111 1111 1111 1111 1111 1111
1111 1111 1111 1111 1111 1111
0010 0010 0010 0010 0010 0010
0010 0010 0010 0010 0010 0010

I want the program to initialize the 6x24 array with the pattern and display a static image on the console screen upon startup. And the six user control commands have to be ‘w’ for upward rotation, ‘s’ for downward rotation, ‘a’ for leftward rotation, ‘d’ for rightward rotation.

and how much do you move per 'frame' ... one letter? or one block of 4 letters?
i dont know, i want the Animation generated at a reasonable speed like usleep(500000); (0.5s)
Before you worry about animation, figure out how to print the array just once.

1
2
3
4
5
6
7
8
void printarray( ? ) {
  ?
}

int main ( ) {
  ? array = ?
  printarray(array);
}


Then figure out how to rotate the array in one direction by a single step.
1
2
3
4
5
6
7
8
9
10
11
12
13
void printarray( ? ) {
  ?
}
void rotateup( ? ) {
  ?
}

int main ( ) {
  ? array = ?
  printarray(array);
  rotateup(array);
  printarray(array);
}


Do the same for down, left and right.

So when you've got 5 functions doing what you want, then you can worry about your animation control loop.

> I want the program to initialize the 6x24 array
Start with something smaller like 3x3
1
2
#define A_WIDTH 3
#define A_HEIGHT 3 

If you start with these, and only use the define names in your code, you can prototype everything with a 3x3 array and change to 6x24 when everything works.

Plus with a 3x3 array, you can initialise it like

1 2 3
4 5 6
7 8 9

It's a lot easier to see whether a rotation has worked, rather than looking at a sea of 0 and 1.
This is what i got but when I run it, it doesn't work and shows me errors in line 11,22 and 34. And I'm having a feeling I'm doing this wrong. I'm totally lost srry

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
#include <stdio.h>	// getchar(), getc(), fgets(), scanf()
#include <conio.h>  // _kbhit(), _getch()
#include <unistd.h>
#define A_WIDTH 24
#define A_HEIGHT 6


int main()
{
	int BigArray[6][24] = 
	{{0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0};
	{0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0};
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
	{0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0};
	{0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0}};
	}
	
	char cmd = 0;

	//program loop	
	while(cmd != 'x')
	{
		
		for(int i = 0; i < x_index; i++)
		{
		for(int j = 0; j < y_index; j++)
		{

		// this will NOT require ENTER key after the input to make it effective
		if(_kbhit())  //if a keyboard hit is detected...
		{
			cmd = _getch();	
			printf("%d , BigArray[i][j], cmd);
		}
		
		}
		
	}

} 
Last edited on
Well you didn't use code tags, so guessing your line number references is impossible.
https://www.cplusplus.com/articles/jEywvCM9/

You didn't start with a small array of unique values so it's a lot easier to debug.

You didn't write any functions to break up the problem into more manageable units.
ok i have used code tags now. so can you tell me what I need to fix in order to function correctly? I'm lost srry
All the inner rows of your array initialisation should end with a comma, not a semicolon.

Your printf is missing a "

You're missing at least one closing }

Even when you've fixed that, you have to press a key 144 times just to print out the array once.
Think how much better off you would be by only having to deal with a 3x3 array as I suggested.


Like this.
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
#include <stdio.h>
#include <stdlib.h>
#define A_WIDTH     3
#define A_HEIGHT    3

void printarray(int array[A_HEIGHT][A_WIDTH]) {
    printf("Current state of array:\n");
    for ( int r = 0 ; r < A_HEIGHT ; r++ ) {
        for ( int c = 0 ; c < A_WIDTH ; c++ ) {
            printf("%d ", array[r][c]);
        }
        printf("\n");
    }
    printf("-----\n");
}
void rotateup(int array[A_HEIGHT][A_WIDTH]) {
    // over to you
}

int main(int argc, char **argv)
{
    int array[A_HEIGHT][A_WIDTH] = {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 9 },
    };
    printarray(array);
    rotateup(array);
    printarray(array);
}



$ gcc foo.c
$ ./a.out 
Current state of array:
1 2 3 
4 5 6 
7 8 9 
-----
Current state of array:
1 2 3 
4 5 6 
7 8 9 
-----


Now over to you to implement rotateup so the 2nd output looks like this

Current state of array:
4 5 6 
7 8 9 
1 2 3 
-----


WHEN you've got all 4 directions working, then you can add your WASD keyboard stuff.

WHEN you're happy everything is working, then you can add your 6x24 array.

It's called incremental development. You need to learn how to do it if you ever want to move beyond the "hello world" stage.

You don't rush madly to the finish line, only to fall at the first hurdle.
Topic archived. No new replies allowed.