#include <iostream>
#include <conio.h>
#include <Windows.h>
// whole cpp file scope vars here setup the size of the matrix
const int x_matrix = 4;
const int y_matrix = 4;
// create the 2 dim array
char matrix[x_matrix][y_matrix];
// O position inside the matrix
int x_pos = 1;
int y_pos = 1;
// use windows.h to reset cursosPos and avoid cls command that causing "flickering"
void ClearScreen()
{
COORD cursorPosition;
cursorPosition.X = 0;
cursorPosition.Y = 0;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cursorPosition);
}
static void initMatrix()
{
ClearScreen();
for (int x = 0; x < x_matrix; x++)
{
for (int y = 0; y < y_matrix; y++)
{
matrix[x][y] = 'x';
}
}
}
static void showMatrix()
{
for (int x = 0; x < x_matrix; x++)
{
for (int y = 0; y < y_matrix; y++)
{
std::cout << matrix[x][y];
}
std::cout << "\n";
}
}
static void updateMatrix(int _x = 0, int _y = 0)
{
initMatrix();
matrix[_x-1][_y-1] = 'O';
}
int main()
{
initMatrix();
updateMatrix(x_pos, y_pos);
showMatrix();
do
{
if (_getch() != '\033') //if ESC key pressed terminate program
{
switch (_getch())
{
case 72: //arrow key up
x_pos = (x_pos <= 1) ? x_pos : --x_pos;
updateMatrix(x_pos, y_pos);
showMatrix();
break;
case 80: //arrow key down
x_pos = (x_pos >= x_matrix) ? x_pos : ++x_pos;
updateMatrix(x_pos, y_pos);
showMatrix();
break;
case 77: //arrow key right
y_pos = (y_pos >= y_matrix) ? y_pos : ++y_pos;
updateMatrix(x_pos, y_pos);
showMatrix();
break;
case 75: //arrow key left
y_pos = (y_pos <= 1) ? y_pos : --y_pos;
updateMatrix(x_pos, y_pos);
showMatrix();
break;
default:
break;
}
}
else
{
return false;
}
} while (true);
}
#Move point on 2d array (practicing for fun)
4 messages · Page 1 of 1 (latest)
Curious about why update is consistently written as upate here
oh shut, I might missed this part lol
