Sudoku Game

how can i make the cursor skip the tiles that already has a value. and just moves from the blank space when i press the UP/DOWN/LEFT/RIGHT key. and control the input.

this is a project at school and so far i have gone to is 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
void gotoxy(int x, int y){
	HANDLE hConsole;
	COORD cursorLoc;
	std::cout.flush();
	cursorLoc.X = x;
	cursorLoc.Y = y;
	hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hConsole, cursorLoc);
}
void cursor(int x, int y){ //this moves the cursor up down left and right on the whole grid
	while (true){
		while (true){
			gotoxy(x, y);
			if (GetAsyncKeyState(VK_UP) != 0)
			{
				y -= 2;
				Sleep(200);
				gotoxy(x, y);
				if (y < 1)
				{
					y = 17;
					Sleep(200);
					gotoxy(x, y);
				}
				break;
			}
			else if (GetAsyncKeyState(VK_DOWN) != 0)
			{
				y += 2;
				Sleep(200);
				gotoxy(x, y);
				if (y > 17)
				{
					y = 1;
					Sleep(200);
					gotoxy(x, y);
				}
				break;
			}
			else if (GetAsyncKeyState(VK_RIGHT) != 0){
				x += 4;
				Sleep(200);
				gotoxy(x, y);
				if (x > 34)
				{
					x = 2;
					Sleep(200);
					gotoxy(x, y);
				}
				break;
			}
			else if (GetAsyncKeyState(VK_LEFT) != 0){
				x -= 4;
				Sleep(200);
				gotoxy(x, y);
				if (x < 2)
				{
					x = 34;
					Sleep(200);
					gotoxy(x, y);
				}
				break;
			}
		}
	}
}
void pops0(int x, int y, int s0[3][3][3]){ //populates the cells with the given numbers
	gotoxy(x, y);
	for (int i = 0; i < 3; i++){
		for (int j = 0; j < 3; j++){
			for (int k = 0; k < 3; k++){
				if (s0[i][j][k] != 0){
					cout << s0[i][j][k];
					gotoxy(x += 4, y);
				}
				else{
					gotoxy(x += 4, y);
				}
			}
			x = 2;
			gotoxy(x, y += 2);
		}
	}
}
void pops1(int x, int y, int s1[3][3][3]){ //populates the cells with the given numbers
	x = 14;
	y = 1;
	gotoxy(x, y);
	for (int i = 0; i < 3; i++){
		for (int j = 0; j < 3; j++){
			for (int k = 0; k < 3; k++){
				if (s1[i][j][k] != 0){
					cout << s1[i][j][k];
					gotoxy(x += 4, y);
				}
				else{
					gotoxy(x += 4, y);
				}
			}
			x = 14;
			gotoxy(x, y += 2);
		}
	}
}
void pops2(int x, int y, int s0[3][3][3]){ //populates the cells with the given numbers
	x = 26;
	y = 1;
	gotoxy(x, y);
	for (int i = 0; i < 3; i++){
		for (int j = 0; j < 3; j++){
			for (int k = 0; k < 3; k++){
				if (s0[i][j][k] != 0){
					cout << s0[i][j][k];
					gotoxy(x += 4, y);
				}
				else{
					gotoxy(x += 4, y);
				}
			}
			x = 26;
			gotoxy(x, y += 2);
		}
	}
}

int main(){
	int s0[3][3][3] = { { { {5}, {3}, {} }, { {6}, {}, {} }, { {}, {9}, {8} } }, { { {8}, {}, {} }, { {4}, {}, {} }, { {7}, {}, {} } }, { { {}, {6}, {} }, { {}, {}, {} }, { {}, {}, {} } } }; //array for 3, 3 by 3 boxes from left to right containing the values
	int s1[3][3][3] = { { { {}, {7}, {} }, { {1}, {9}, {5} }, { {}, {}, {} } }, { { {}, {6}, {} }, { {8}, {}, {3} }, { {}, {2}, {} } }, { { {}, {}, {} }, { {4}, {1}, {9} }, { {}, {8}, {} } } }; //array for 3, 3 by 3 boxes from left to right containing the values
	int s2[3][3][3] = { { { {}, {}, {} }, { {}, {}, {} }, { {}, {6}, {} } }, { { {}, {}, {3} }, { {}, {}, {1} }, { {}, {}, {6} } }, { { {}, {}, {} }, { {}, {}, { 5 } }, { {}, { 7 }, { 9 } } } }; //array for 3, 3 by 3 boxes from left to right containing the values
	int x = 2, y = 1;
	for (int i = 0; i < 9; i++){ //prints the whole sudoku box
		if ((i % 3) == 0){
			cout << "+---+---+---+---+---+---+---+---+---+\n";
		}
		else{
			cout << "+-----------+-----------+-----------+\n";
		}
		for (int j = 0; j < 1; j++){
			cout << "+   |   |   +   |   |   +   |   |   +\n";
		}
	}
	cout << "+---+---+---+---+---+---+---+---+---+\n";
	pops0(x, y, s0);
	pops1(x, y, s1);
	pops2(x, y, s2);
	cursor(x, y);
	
	system("pause");
Last edited on
I'm sorry, I don't want to read the code.
Please use the tags when writing code and format the code properly.

But in general you have something 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
31
32
33
int map[3 /*Y*/ ][3 /*X*/ ][3 /*y*/][3 /*x*/]; /// represents the structure of the Sudoku field
memset(&map, -1, sizeof(map)); // set all fields to -1
int _Y; // current y position in the large 3x3 field
int _X; // current x position in the large 3x3 field
int _y; // current y position in the small 3x3 field
int _x; // current x position in the small 3x3 field

// ...

/// This function for each direction
void MoveLeft()
{
    for(int i = 0; i < 10; i++) // if no tile is found that has the value -1 go 1 step
    {
        if(--_x < 0)
        {    
            _x = 2;
            if(--_X < 0)
                _X = 2;
        }
        if(map[_Y][_X][_y][_x] == -1)
            break;
    }
}

///
void print()
{
   // bla bla bla structure
   if( map[_Y][_X][_y][_x] != -1)
       /// print the number;
   // bla bla bla structure
}
Last edited on
just edited the post with the comments, so currently i already have the whole 9 by 9 grid and the set of numbers in the corrent grids, and i have the cursor moving in the whole grid from the 9 x 9 boxes

+---+---+---+---+---+---+---+---+---+
+___|___|___+___|___|___+___|___|___+
+___|___|___+___|___|___+___|___|___+
+   |   |   +   |   |   +   |   |   +
+-----------+-----------+-----------+
+___|___|___+___|___|___+___|___|___+
+___|___|___+___|___|___+___|___|___+
+   |   |   +   |   |   +   |   |   +
+-----------+-----------+-----------+
+___|___|___+___|___|___+___|___|___+
+___|___|___+___|___|___+___|___|___+
+   |   |   +   |   |   +   |   |   +
+---+---+---+---+---+---+---+---+---+


above is the output i have, already with the corresponding default numbers of the sudoku problem displayed and the cursor is already moving freely inside the 9 x 9 boxes. the default numbers displayed by using the array s0, s1, and s2 and the function pops0, pops1 and pops2.
Last edited on
Topic archived. No new replies allowed.