Not sure if this is beginner, but...

I am making a sudoku type game and its a console app, so I am coding the arrows to move the cursor. The cursor is going to be simply changing the color of the target location, but I am printing the entire board at once.

Like this:
1
2
3
4
int GameBoard[9][9];
//Here is the code that handles arrow key pressing
//and the code that handles moving the cursor.
cout<<GameBoard;


Any suggestions so that I don't have to write 81 if statements to cover every possibility?

Thanks.
Last edited on
Why do you want to write 81 if-statements. You know the start position of your cursor and you know the "range of your board" (0 to 8 on x-axis and 0 to 8 on y-axis). If you hit a arrow button you can calculate the new postion of the cursor.

e.g.:
<-- eq. -1 on x-axis; --> eq. +1 on x-axis

So you must only watch out that you don't leave your board. Do you understand?
Last edited on
Right, but I want to have every number white except the one where my cursor is, which I want yellow. I know how to change text color, but I don't know how to change one part, say GameBoard[6][3] to yellow without changing the whole thing. I mean, I could write the 81 if statements and have it output different each time, but I don't want to have to do that.
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
int X=1, Y=1; //Will start at square [1][1]

int Square[10][10];
bool ConstSquare[10][10];

/*Some stuff here to make constant numbers for some squares*/

while(true){
  /*Display goes here*/

    char Choice=getch();
    switch(Choice){
        case 72: //Up arrow key
            if(Y<9){
                Y+=1;
            }
            break;
        case 80: //Down arrow key
            if(Y>1){
                Y-=1;
            }
            break;
        case 77: //Right arrow key
            if(X<9){
                X+=1;
            }
            break;
        case 75: //left arrow key
            if(X>1){
                X-=1;
            }
            break;

        case 1:
            if(ConstSquare[X][Y]){
                Square[X][Y]=1;
        }
        break;
        case 2:
            if(ConstSquare[X][Y]){
                Square[X][Y]=2;
        }
        break;
        case 3:
            if(ConstSquare[X][Y]){
                Square[X][Y]=3;
        }
        break;
        case 4:
            if(ConstSquare[X][Y]){
                Square[X][Y]=4;
        }
        break;
        case 5:
            if(ConstSquare[X][Y]){
                Square[X][Y]=5;
        }
        break;
        case 6:
            if(ConstSquare[X][Y]){
                Square[X][Y]=6;
        }
        break;
        case 7:
            if(ConstSquare[X][Y]){
                Square[X][Y]=7;
        }
        break;
        case 8:
            if(ConstSquare[X][Y]){
                Square[X][Y]=8;
        }
        break;
        case 9:
            if(ConstSquare[X][Y]){
                Square[X][Y]=9;
        }
        break;
    }
}


Hope it helps. The display would be harder...
Last edited on
@samrux
Those aren't ascii codes for the arrow keys (they dont have ascii codes)
Last edited on
No, but you can find all the win32 API codes for them here:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx
Topic archived. No new replies allowed.