For my c++ homework assignment, I have to write a Tic-Tac-Toe Win32 console application.
I’ve created a grid on the screen using a coordinate system and I use _getch() and SetConsoleCursorPosition(...) to move the cursor around when the user presses the arrow keys and to draw an X or and O when the users presses one of those keys.
Key part of my question
The grid coordinates are are in the format (x,y) and 1,1 represents the top-left and 5,5 represents the bottom-right. Every arrow key press moves the cursor +/- 2. For example, if the cursor is at 1,1 and the user presses the up arrow, the cursor is moved to 1,5, if the up arrow is pressed again the cursor moves to 1,3, etc.
The movement is handled with this arithmetic expression:
switch ( keyPress ) {
case UP :
position.Y = (12 + position.Y - 2) % 6;
SetConsoleCursorPosition(screen, position);
break;
case DOWN :
position.Y = (12 + position.Y + 2) % 6;
SetConsoleCursorPosition(screen, position);
break;
case LEFT :
position.X = (12 + position.X - 2) % 6;
SetConsoleCursorPosition(screen, position);
break;
case RIGHT :
position.X = (12 + position.X + 2) % 6;
SetConsoleCursorPosition(screen, position);
break;
}
Question
I am not allowed to use magic numbers for the assignment and therefore, in the 12, 2, and 6 need to be replaced with variable names that make sense. What would be appropriate names for those values?
UPDATE
Based on the comments, here is how the game board is displayed on the screen:
The numbers on the x and y axis are the cursor positions where an X or and O is drawn.
0123456
0╔═╤═╤═╗
1║X│ │ ║
2╟─┼─┼─╢
3║ │O│ ║
4╟─┼─┼─╢
5║ │ │X║
6╚═╧═╧═╝
it’s all relative to your initial settings they could look like:
Please make sure to use even values 😉 it gets complicated if the cell is wider and if you want to jump into the middle of the cell.