I'm pretty new to coding, so I've been making little programs while going through an online course. I'm playing around with the idea of moving the character 'X' around a small grid using the arrow keys on the keyboard.
I've managed to get it working using looping and switch statements, but the default statement keeps executing regardless of which key is pressed.
Any ideas on where I went wrong?
Here's what I got:
#include <iostream>
#include <vector>
#include<conio.h>
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77
using namespace std;
void print_board(int v, int vn, vector <vector<char>> board);
int main()
{
vector <vector<char>> board;
board.resize(11, vector<char>(11));
int v{ 6 };
int vn{ 6 };
int c = 0;
while (1) {
print_board(v, vn, board);
//system("pause");
switch (c=_getch()) {
case KEY_UP: v--;
break;
case KEY_DOWN: v++;
break;
case KEY_LEFT: vn--;
break;
case KEY_RIGHT: vn++;
break;
default: cout << "please use the arrow key" << endl;
//system("pause");
}
}
return 0;
}
void print_board(int v, int vn, vector <vector<char>> board) {
system("CLS");
board.at(v).at(vn) = 'X';
for (int i{ 0 }; i < 11; i++) {
for (int j{ 0 }; j < 11; j++) {
cout << board.at(i).at(j) << " ";
}
cout << endl;
}
}
Thanks in advance.