This program is supposed to have a map made out of dots and a player which you can move around.
But when I opened this program one day, the text was flickering and moving up and down.
It uses Windows.h to redraw on the console without flickering and to hide the cursor
Does it have to do something with the cmd properties, the code or the Windows.h header file?
#include <iostream>
#include <conio.h>
#include <Windows.h>
char map[1000][1000];
int width = 100;
int height = 10;
char Tmt = '.';
void cls()
{
COORD cursorPosition;
cursorPosition.X = 0;
cursorPosition.Y = 0;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cursorPosition);
}
void hidcur()
{
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(consoleHandle, &info);
}
class Player {
public:
int x;
int y;
char sym;
void Draw()
{
map[x][y] = sym;
}
void Logic()
{
if (x < 1) { x++; }
if (y < 1) { y++; }
if (x > width) { x--; }
if (y > height) { y--; }
}
void Input()
{
if (_kbhit())
{
switch (_getch())
{
case 'w':
map[x][y] = Tmt;
y--;
break;
case 'a':
map[x][y] = Tmt;
x--;
break;
case 's':
map[x][y] = Tmt;
y++;
break;
case 'd':
map[x][y] = Tmt;
x++;
break;
case 'p':
system("cls");
break;
}
}
}
void PrintPos()
{
printf("Pos:\n");
if (x >= 10) { printf(" %d\n", x); }
else { printf(" 0%d\n", x); }
if (y >= 10) { printf(" %d\n", y); }
else { printf(" 0%d\n", y); }
}
};
Player player1;
void Setup()
{
player1.x = 2;
player1.y = 2;
player1.sym = '#';
player1.Draw();
player1.Logic();
for (int mapsui = 1; mapsui <= width; mapsui++)
{
for (int mapsuj = 1; mapsuj <= height; mapsuj++)
{
map[mapsui][mapsuj] = Tmt;
}
}
}
void DrawMap()
{
player1.PrintPos();
printf("\n\n\n");
for (int drawi = 1; drawi <= width; drawi++)
{
for (int drawj = 1; drawj <= height; drawj++)
{
printf("%c", map[drawj][drawi]);
}
printf("\n");
}
}
int main()
{
Setup();
while (1)
{
DrawMap();
player1.Input();
player1.Logic();
player1.Draw();
cls();
hidcur();
}
return 0;
}
In the DrawMap method, when you have the two for loops, you are checking width in the outer loop and height in the inner loop, while it should be the other way round. Note that the outer loop controls the number of lines, and as you are limiting it to the width (100) you are displaying 100 lines most of which are empty, because Setup just initializes 10 lines. As the command window shows less lines (I think it's about 30 by default or something like that) the map goes up and that results in the flicker you see. It should be:
for (int drawi = 1; drawi <= height; drawi++)
{
for (int drawj = 1; drawj <= width; drawj++)
{
printf("%c", map[drawj][drawi]);
}
printf("\n");
}
Note than in Setup you also use the loops in this way (the outer loop checks width and the inner loop checks height) but in that case you are later using the indexes to the map the other way round (first i and then j) so that results in the correct initialization of the map (10 rows of 100 columns).
Related
I am attempting to create a snake game for a class using C++ on Xcode, but this code is not working for me. The errors I am receiving state that all of my curses symbols are undefined.
The 13 errors found in my code are:
Undefined symbol: _cbreak
Undefined symbol: _clear
Undefined symbol: _curs_set
Undefined symbol: _endwin
Undefined symbol: _halfdelay
Undefined symbol: _initscr
Undefined symbol: _keypad
Undefined symbol: _mvprintw
Undefined symbol: _noecho
Undefined symbol: _refresh
Undefined symbol: _stdscr
Undefined symbol: _wgetch
Please let me know! I've spent hours trying to look this up and am not sure what I'm doing wrong or how to fix it!
#include <cstdlib>
#include <ncurses.h>
bool gameOver;
const int width = 20, height = 20;
int x,y, FruitX, FruitY, score;
enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirection dir;
int TailX[100], TailY[100];
int nTail = 0;
void Setup() {
initscr();
clear();
noecho();
cbreak();
curs_set(0);
gameOver = false;
dir = STOP;
x = width/2;
y = height/2;
FruitX = (rand() % width) + 1;
FruitY = (rand() % height) + 1;
score = 0;
}
void Draw() {
clear();
for (int i = 0; i < width + 2; i++)
mvprintw(0, i, "+");
for (int i = 0; i < height + 2; i++)
{
for (int j = 0; j < width + 2; j++)
{
if (i == 0 | i ==21)
mvprintw(i, j, "#");
else if (j == 0 | j == 21)
mvprintw(i, j, "+");
else if (i == y && j == x)
mvprintw(i, j, "0");
else if (i == FruitY && j == FruitX)
mvprintw(i, j, "0");
else
for (int k = 0; k < nTail; k++)
{
if (TailX[k] == j && TailY[k] == i)
mvprintw(i, j, "o");
}
}
}
mvprintw(23, 0, "Score %d", score);
refresh();
}
void Input() {
keypad(stdscr, TRUE);
halfdelay(1);
int c = getch();
switch(c)
{
case KEY_LEFT:
dir = LEFT;
break;
case KEY_RIGHT:
dir = RIGHT;
break;
case KEY_UP:
dir = UP;
break;
case KEY_DOWN:
dir = DOWN;
break;
case 113:
gameOver = true;
break;
}
}
void Logic() {
int prevX = TailX[0];
int prevY = TailY[0];
int prev2X, prev2Y;
TailX[0] = x;
TailY[0] = y;
for (int i = 1; i < nTail; i++)
{
prev2X = TailX[i];
prev2Y = TailY[i];
TailX[i] = prevX;
TailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (dir) {
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}
if (x > width || x < 1 || y > height){
gameOver = true;
}
if (x == FruitX && y == FruitY)
{
score++;
FruitX = (rand() % width)+1;
FruitY = (rand() % height)+1;
nTail++;
}
for (int i = 0; i < nTail; i++)
if (TailX[i] == x && TailY[i] == y)
{
gameOver = true;
}
}
int main() {
Setup();
while(!gameOver)
{
Draw();
Input();
Logic();
}
getch();
endwin();
return 0;
}
UPDATE: I linked libcurses.tbd and libncurses.tbd and the error warnings went away and the build did not fail. However, instead of running how it is supposed to, it now only produces the lines,
"Error opening terminal: unknown.
Program ended with exit code: 1"
help.
I am currently trying to make a cellular automata. I am just getting the basics of direction, movement and rendering.
However, when running the compiler the cells don't move despite calling the Move function.
Here are the files,
Cell.cpp
#include "Cell.h"
#include "CellManager.h"
Cell::Cell()
{
Lifetime = 5 + rand() % 2 - 1;
}
Cell::~Cell()
{
}
void Cell::Move(int dir)
{
switch (dir)
{
default: y -= 2;
break;
case 0: y -= 2;
break;
case 1: x += 2;
break;
case 2: y += 2;
break;
case 3: x -= 2;
break;
}
if (x > 800)
{
x = 0;
} else if (x < 0)
{
x = 800;
}
if (y > 800)
{
y = 0;
}
else if (y < 0)
{
y = 800;
}
}
int Cell::ChangeDir(int dir)
{
dir = rand() % 3;
return dir;
}
void Cell::Draw(sf::RenderTarget& target)
{
sf::RectangleShape cell;
cell.setSize(sf::Vector2f(2.f,2.f));
cell.setOutlineColor(colour);
cell.setPosition(x, y);
target.draw(cell);
}
void Cell::SetUp(int X, int Y, sf::Color Colour, int dir)
{
x = X;
y = Y;
colour = Colour;
Dir = dir;
}
CellManager.cpp
#include "CellManager.h"
#include "Cell.h"
void CellManager::UpdateCells(vector<Cell> cells, sf::RenderTarget& target)
{
for (int i = 0; i < cells.size(); i++)
{
cells[i].ChangeDir(cells[i].Dir);
cells[i].Move(cells[i].Dir);
cells[i].Draw(target);
}
}
void CellManager::CreateInstance()//TODO
{
}
I do not understand where I am going wrong as the switch statement works but the cells just refuse to move. Any help would be appreciated :)
Your function, CellManager::UpdateCell's parameter vector<Cell> cells COPIES the vector of cells, then changes the copies. You probably want to pass by reference instead. This would look like: std::vector<Cell>& cells.
Other note:
ChangeDir does not change the member variable Dir. You could pass in by reference for that as well, or just not pass in anything at all and use Dir = rand() % 3;.
Edit #2: Ok, I did another *.cpp to check if the codes for Arrow Keys were right. Doing that, I noticed keyPressed variable in detectKeyPressing() had the wrong type of variable, so I changed it from char to int and changed the codes.
Once I did that, it worked. Now I have put the limits, so the Player cannot go outside the square. But I have another problem, the movement is too tough and if you press the keys too fast, the instructions run with a annoying delay. I know I should use either Sleep(ms) or Delay(ms), but I don't know when I should use it.
This is the new code:
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <Windows.h>
using namespace std;
int detectKeyPressing() {
// 0: Escape
// 1: Enter
//2: Up
// 3: Left
// 4: Down
// 5: Right
int keyPressed = 0;
while (keyPressed != 27) {
if (keyPressed == 0 || keyPressed == 224) {
keyPressed = _getch(); //First value of _getch() when any of the arrow keys are pressed is "224", the next one is the code depending of which arrow you pressed
}
else if (keyPressed == 13) {
return 1;
}
else {
switch (keyPressed) {
//Up
case 72:
return 2;
break;
//Left
case 75:
return 3;
break;
//Down
case 80:
return 4;
break;
//Right
case 77:
return 5;
break;
//Default
default:
return -1;
break;
}
}
};
return 0;
};
int mainMenu() {
int enterPressed = 0;
cout << "Press Enter to Begin, or ESC to exit" << endl;
enterPressed = detectKeyPressing();
system("cls");
return enterPressed;
};
void draw(int playerX, int playerY) {
//Player coordinates, made for testing
cout << "Player.x = " << playerX << endl << "Player.y = " << playerY << endl;
//The next 8 spaces go blank
for (int i = 1; i < 8; i++) {
cout << endl;
}
//Square Limit Making
//Top Limit
for (int iw = 1; iw < 80; iw++) {
cout << "-";
}
cout << endl;
//Border limits and inside the Square
for (int ih = 1; ih < 30; ih++) {
//Left border
cout << "|";
//Inside the Square
for (int iw = 1; iw < 78; iw++) {
if (iw == playerX && ih == playerY) {
cout << "a"; //This is supposed to be ♥ but I don't know how to put it in the screen with a cout
}
else {
cout << " ";
}
}
//Right border
cout << "|" << endl;
}
//Bottom limit
for (int iw = 1; iw < 80; iw++) {
cout << "-";
}
}
int main() {
//Hide cursor
HANDLE hCon;
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
cci.dwSize = 1;
cci.bVisible = FALSE;
SetConsoleCursorInfo(hCon, &cci);
//Variable Making
int gameStarted = -1; // 1 if game is running, 0 if not
//int t = 0; //Turn Counter, not useful for now
Sleep(200); //Wait to get a new seed
srand(time(NULL)); //Seed for rand()
//Menu Loop, remember, 1 if game starts running, 0 if you exit
while (gameStarted > 1 || gameStarted < 0) {
gameStarted = mainMenu();
}
//Like Void Start() in Unity
if (gameStarted == 1) {
int pressedKey = -1; //Creating pressedKey at Start
class Player {
public:
int life = 20;
int accuracy = 80 + (rand() % 100) / 20;
int damage = 5 + (accuracy / 10) + (rand() % 100) / 50;
bool isAlive = true;
int x = 39;
int y = 24;
int speed = 1;
};
class Enemy {
public:
int life = 100;
int satisfaction = 0;
bool isAlive = true;
bool isSatisfied = false;
int damage = 2 + (rand() % 100) / 20;
};
Player Player;
Enemy Enemy;
draw(Player.x, Player.y);
//Like Void Update() in Unity
while (gameStarted != 0) {
pressedKey = detectKeyPressing(); // Save detectKeyPressing()'s return in pressedKey
//Draw if proyectile is moving - not yet
//Draw if player is moving (pay attention specially to this part)
if (pressedKey == 0) {
gameStarted = 0; //if ESC is pressed, exit the loop and exits
}
//If any of the Arrow Keys are pressed
else if (pressedKey > 1 && pressedKey < 6) {
switch (pressedKey) {
//Up
case 2:
Sleep(200);
if (Player.y == Player.speed) {
Player.y = Player.speed; //Top Limit
}
else {
Player.y -= Player.speed;
}
break;
//Left
case 3:
Sleep(200);
if (Player.x == Player.speed) {
Player.x = Player.speed; //Left Limit
}
else {
Player.x -= Player.speed;
}
break;
//Down
case 4:
Sleep(200);
if (Player.y == 30 - Player.speed) {
Player.y = 30 - Player.speed; //Bottom Limit
}
else {
Player.y += Player.speed;
}
break;
//Right
case 5:
Sleep(200);
if (Player.x == 78 - Player.speed) {
Player.x = 78 - Player.speed; //Right Limit
}
else {
Player.x += Player.speed;
}
break;
};
system("cls"); //Erase all
draw(Player.x, Player.y); //Redraw everything, with Player.x or Player.y modified
};
};
};
return 0;
};
Edit #1: I fixed the mistakes you told me, here's the main function modified. It isn't working though.
int main(){
//Hide cursor
HANDLE hCon;
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
cci.dwSize = 50;
cci.bVisible = FALSE; //Changed "TRUE" to "FALSE"
SetConsoleCursorInfo(hCon, &cci);
//Variable Making
int gameStarted = -1; // 1 if game is running, 0 if not
//int t = 0; //Turn Counter, not useful for now
Sleep(200); //Wait to get a new seed
srand(time(NULL)); //Seed for rand()
//Menu Loop, remember, 1 if game starts running, 0 if you exit
while (gameStarted > 1 || gameStarted < 0) {
gameStarted = mainMenu();
}
//Like Void Start() in Unity
if (gameStarted == 1) {
int pressedKey = -1; //Creating pressedKey at Start
class Player {
public:
int life = 20;
int accuracy = 80 + (rand() % 100) / 20;
int damage = 5 + (accuracy / 10) + (rand() % 100) / 50;
bool isAlive = true;
int x = 39;
int y = 24;
int speed = 2;
};
class Enemy {
public:
int life = 100;
int satisfaction = 0;
bool isAlive = true;
bool isSatisfied = false;
int damage = 2 + (rand() % 100) / 20;
};
Player Player;
Enemy Enemy;
draw(Player.x, Player.y);
//Like Void Update() in Unity
while (gameStarted != 0) {
pressedKey = detectKeyPressing(); //Save detectKeyPressing()'s return in pressedKey
//Draw if proyectile is moving - not yet
//Draw if player is moving (pay attention specially to this part)
if (pressedKey == 0) {
gameStarted = 0; //if ESC is pressed, exit the loop and exits
}
//If any of the Arrow Keys are pressed
else if (pressedKey > 1 && pressedKey < 6) {
cout << "There's no problem in Else If statement"; //Couts made for testing
switch (pressedKey) {
cout << "There's no problem in Switch statement";
//Up
case 2:
Player.y -= Player.speed;
cout << "You moved Up";
break;
//Left
case 3:
Player.x -= Player.speed; //Fixed Left movement
cout << "You moved Left";
break;
//Down
case 4:
Player.y += Player.speed;
cout << "You moved Down";
break;
//Right
case 5:
Player.x += Player.speed;
cout << "You moved Right";
break;
};
//system("cls"); //Erase all
//draw(Player.x, Player.y); //Redraw everything, with Player.x and Player.y supposedly modified
};
};
};
return 0;
};
Initial Post: I'm trying to do something like an Undertale normal fight and now I'm doing the "dodging attacks" part, but I'm stuck at making the player move (Yep, that "a") because it didn't update when I press an arrow key (for movement). It is supposed to draw, and put the Player in Player.x and Player.y, so I did something in main() to edit these variables depending on the arrow key you pressed, and then erase and re-draw with the Player.x or Player.y modified.
Here's the code:
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <Windows.h>
using namespace std;
int detectKeyPressing(){
// 0: Escape
// 1: Enter
// 2: Up
// 3: Left
// 4: Down
// 5: Right
char keyPressed = 0;
while (keyPressed != 27){
if(keyPressed == 0){
keyPressed = _getch();
}
else if(keyPressed == 13){
return 1;
}
else{
switch (keyPressed) {
//Up
case 65:
return 2;
break;
//Left
case 68:
return 3;
break;
//Down
case 66:
return 4;
break;
//Right
case 67:
return 5;
break;
//Default
default:
return -1;
break;
}
}
};
return 0;
};
int mainMenu(){
int enterPressed = 0;
cout << "Press Enter to Begin, or ESC to exit" << endl;
enterPressed = detectKeyPressing();
system("cls");
return enterPressed;
};
void draw(int playerX, int playerY) {
//Player coordinates, made for testing
cout << "Player.x = " << playerX << endl << "Player.y = " << playerY << endl;
//The next 8 spaces go blank
for (int i = 1; i < 8; i++) {
cout << endl;
}
//Square Limit Making
//Top Limit
for (int iw = 1; iw < 80; iw++) {
cout << "-";
}
cout << endl;
//Border limits and inside the Square
for (int ih = 1; ih < 30; ih++) {
//Left border
cout << "|";
//Inside the Square
for (int iw = 1; iw < 78; iw++) {
if (iw == playerX && ih == playerY){
cout << "a"; //This is supposed to be ♥ but I don't know how to put it in the screen with a cout
}
else {
cout << " ";
}
}
//Right border
cout << "|" << endl;
}
//Bottom limit
for (int iw = 1; iw < 80; iw++) {
cout << "-";
}
}
int main(){
//Hide cursor
HANDLE hCon;
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
cci.dwSize = 50;
cci.bVisible = TRUE;
SetConsoleCursorInfo(hCon, &cci);
//Variable Making
int gameStarted = -1; // 1 if game is running, 0 if not
//int t = 0; //Turn Counter, not useful for now
Sleep(200); //Wait to get a new seed
srand(time(NULL)); //Seed for rand()
//Menu Loop, remember, 1 if game starts running, 0 if you exit
while (gameStarted > 1 || gameStarted < 0) {
gameStarted = mainMenu();
}
//Like Void Start() in Unity
if (gameStarted == 1) {
class Player {
public:
int life = 20;
int accuracy = 80 + (rand() % 100) / 20;
int damage = 5 + (accuracy / 10) + (rand() % 100) / 50;
bool isAlive = true;
int x = 39;
int y = 24;
int speed = 2;
};
class Enemy {
public:
int life = 100;
int satisfaction = 0;
bool isAlive = true;
bool isSatisfied = false;
int damage = 2 + (rand() % 100) / 20;
};
Player Player;
Enemy Enemy;
draw(Player.x, Player.y);
//Like Void Update() in Unity
while (gameStarted != 0) {
//Draw if proyectile is moving - not yet
//Draw if player is moving (pay attention specially to this part)
if (detectKeyPressing() == 0) {
gameStarted = 0; //if ESC is pressed, exit the loop and exits
}
//If any of the Arrow Keys are pressed
else if (detectKeyPressing() > 1 && detectKeyPressing() < 6) {
switch (detectKeyPressing()) {
//Up
case 2:
Player.y -= Player.speed;
break;
//Left
case 3:
Player.x += Player.speed;
break;
//Down
case 4:
Player.y += Player.speed;
break;
//Right
case 5:
Player.x += Player.speed;
break;
};
system("cls"); //Erase all
draw(Player.x, Player.y); //Redraw everything, with Player.x and Player.y supposedly modified
};
};
};
return 0;
};
I did a few tests and it seems that the else if in "//If any of the Arrow Keys is Pressed" part isn't running but I don't know why.
I would really appreciate any help you can provide. Sorry if anything isn't well written, I'm not a native english speaker.
Where you have this comment
//Draw if proyectile is moving - not yet add a variable to save your pressed key, something like
int pressedKey = detectKeyPressing();
Then, use that variable to check which condition is met within your if-else.
What’s happening is that you’re calling your function, thus asking/waiting for an input each time a condition is being checked.
beginner to C++ and attempting a dungeon crawler beginner task, upon testing one of the classes I found that printing the 2D array works, however if it is edited and printed, it resets and prints original values it was initialised with.
InitGrid is used to initialize the array.
class MAP
{
public:
/*
Difficulty used in number of enemies and traps created.
1-5 Difficulty
| 1 - Too easy | 2 - Easy | 3 - Normal | 4 - Hard | 5 - Insane |
*/
int Difficulty = Hard; //### Temporary, user selection needs implemented ###
int SpawnPos;
int TPosX; //Treasure Postion, used to checkwinstate.
int TPosY; //Treasure Postion, used to checkwinstate.
char Grid[MAPHeight][MAPWidth];
void InitGrid()
{
for (int y = 0; y < 9; y++) //Row loop
{
for (int x = 0; x < 14; x++) //Column loop
{
Grid[y][x] = { '.' };
}
}
}
void PrintMap()
{
for (int y = 0; y < 9; y++) //This loops on the rows.
{
for (int PrintX = 0; PrintX < 14; PrintX++) //This loops on the columns
{
cout << Grid[y][PrintX] << " ";
}
cout << endl;
}
}
void GenerateEnemies()
{
for (int i = 0; i < Difficulty; i++)
{
int TotEnemies = (Difficulty * 1.5);
for (TotEnemies; TotEnemies == 0; TotEnemies--)
{
int x = rand() % (MAPWidth - 1);
int y = rand() % (MAPHeight - 1);
if (Grid[y][x] == '.')
{
Grid[y][x] = '#';
}
else
{
GenerateEnemies();
}
}
}
}
// Generate Enemies
void GenerateTraps()
{
for (int i = 0; i < Difficulty; i++)
{
int TotTraps = (Difficulty * 1.5);
for (TotTraps; TotTraps == 0; TotTraps--)
{
int x = rand() % (MAPWidth - 1);
int y = rand() % (MAPHeight - 1);
if (Grid[y][x] == '.')
{
Grid[y][x] = 'T';
}
else
{
GenerateTraps();
}
}
}
}
void GenerateTreasure()
{
int x = rand() % MAPWidth;
int y = rand() % MAPHeight;
/*
Randomly selects spawn location
uses tmp variables to overwrite
that grid location.
*/
if (Grid[y][x] == '.')
{
Grid[y][x] = 'X';
}
else
{
GenerateTreasure();
}
TPosX = x;
TPosY = y;
}
};
//PLAYER CLASS
class PLAYER : public MAP
{
public:
int Health = (Difficulty * 1.5);
int PPosX;
int PPosY;
char Direction; //Use cin to get user input after map is updated, used in switch case to move player.
void GenerateSpawn()
{
int x = rand() % (MAPWidth - 1);
int y = rand() % (MAPHeight - 1);
/*
Randomly selects spawn location
uses tmp variables to overwrite
that grid location.
*/
Grid[y][x] = 'P';
PPosX = x;
PPosY = y;
}
void AllowMove(int y, int x)
{
if (Grid[y][x] == '.')
{
Grid[y][x] = '#';
}
else if (Grid[y][x] == 'X')
{
//GameWin();
}
else if (Grid[y][x] == '#')
{
//DamagePlayer();
}
else {}
}
void MovePlayer()
{
switch (Direction)
{
case 'w':
{
int x = PPosX;
int y = (PPosY + 1);
void AllowMove(int y, int x);
}
break;
case 'a':
{
int x = (PPosX - 1);
int y = PPosY;
void AllowMove(int y, int x);
}
break;
case 's':
{
int x = (PPosX);
int y = PPosY;
void AllowMove(int y, int x);
}
break;
case 'd':
{
int x = (PPosX + 1);
int y = PPosY;
void AllowMove(int y, int x);
}
break;
default:
cout << "invalid character, try again." << endl;
Sleep(5000);
//Call function to retry
}
}
};
//######### End #########
//Main Function
int main() {
srand(time(NULL)); //Used to seed rand() values.
SetConsoleTitle(TEXT("Dungeon Crawler"));
//Objects
MAP Map;
PLAYER Player;
Map.InitGrid();
Player.GenerateSpawn();
//Map.GenerateTreasure();
//Map.GenerateEnemies();
//Map.GenerateTraps();
Map.PrintMap();
cout << endl;
}
However, when I run these, I get the following image:
I've attempted debugging in visual studio using breakpoints and at some point it does set the grid value to 'P' but I couldn't find where I gets 'reset' for lack of a better term.
char Grid[MAPHeight][MAPWidth]; needs to be moved outside of the class as a global variable. Currently as the PLAYER class inherits from the MAPS class, when GenerateSpawn() edits the Grid that is created specific to the object linked to PLAYER.
When it is a global variable, it is separate, then when edited by GenerateSpawn() and called by void PrintMap() they both use the same Grid.
That way when it is printed to console it correctly prints out the map.
Answered my own question in case someone else stumbles upon this.
I have a char buffer[200]; Its content is (Serial.println(buffer)'s output):
AT+CGPSINF=2
2,195941,4613.161699,N,608.476854,E,1,9,1.398636,435.450073,M,48.200287,M,,0000
OK
I would like to have in char fix[] only this
2,195941,4613.161699,N,608.476854,E,1,9,1.398636,435.450073,M,48.200287,M,,0000
I spent a lot of time working around this code but without success
static int gps_read () {
read_AT_string("AT+CGPSINF=2",3000);
Serial.println(F("------- Show buffer------"));
Serial.println(buffer);
Serial.println(F("-------------------------\n"));
char fix[BUFFERSIZE];
int z = 0;
int y = 0;
int w = 0;
for(y=0; y < BUFFERSIZE; y++)
{
if(buffer[y]==',') z++;
if(z > 0){
if(buffer[y] == '\n') break;
fix[w] = buffer[y];
w++;
}
//if(y==num)//if the command is right, run return
// y=strlen(buffer)+1;
}
Serial.println(F("------- Show Fix------"));
Serial.println(fix);
Serial.println(F("----------------------"));
How can I fill and resize the char fix[] with the line which look like:
2,195941,4613.161699,N,608.476854,E,1,9,1.398636,435.450073,M,48.200287,M,,0000
In my above code, it prints me that:
------- Show Fix------
,202653,4613.164908,N,608.479232,E,1,9,1.183249,427.253052,M,48.200226,M,,0000
b
----------------------
I lost first '2'.
The newline and the 'b' and space should be removed.
I solve like this, but I am not satisfied.
char* gps_read (void) {
if(strstr(read_AT_string("AT+CGPSINF=2",3000),"OK") != NULL)
{
Serial.println(F("------- Show buffer------"));
Serial.println(buffer);
Serial.println(F("-------------------------\n"));
char fix[BUFFERSIZE];
int z = 0;
int y = 0;
int w = 0;
for(y=0; y < BUFFERSIZE; y++)
{
if(buffer[y]==',') z++;
if(z > 0){
if(buffer[y] == '\n'){
fix[w-1]='\0';
break;
}
fix[w] = buffer[y+1];
w++;
}
}
Serial.println(F("------- Show Fix------"));
Serial.println(fix);
Serial.println(F("----------------------"));
return fix;
}
else
{
return "Error : no fix";
}
}
It's not important of '2,' is not display.
How would you do better?