jeudi 25 mars 2021

Increment, and Decrement not acting as expected

I am writing a console based game in C++ and I have gotten to the point of moving the player by arrow keys, but they x--, and y-- do not change the values of x or y, I have tried other configurations like x -= 1, and x = x-1, but neither have worked

(i have also tried the _Game struct as a class, with no better results)

my code is

#include <iostream>
#include <unistd.h>
#include <termios.h>
// #include "colorlib.hpp"
// #include "dict.hpp"

using namespace std;
// using namespace UnixColor;
// using namespace Dict;



// dict colors = initColors();
// string red = getC(colors, "red");
// string gray = getC(colors, "gray");

int x, y;

struct _Game {
    int board[5][5] {
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0}
    };

    void checkBoard() {
        for(int i = 0; i < 5; i++) {
            for(int o = 0; o < 5; o++) {
                if(board[i][o] == 1) {
                    board[i][o] = 0;
                }
            }
        }
        board[y][x] = 1;
    }

    void bprint() {
        checkBoard();
        for(auto var : board) {
            for(int i = 0; i < 5; i++) {
                // cout << var[i];
                if(var[i] == 0) {
                    // cout << gray << "{}" << RESET;
                    cout << "{}";
                }else if(var[i] == 1) {
                    // cout << red << "[]" << RESET;
                    cout << "[]";
                }
            }
            cout << endl;
        }
    }

};

// taken from https://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed
char getch() {
    char buf = 0;
    struct termios old = {0};
    if (tcgetattr(0, &old) < 0)
        perror("tcsetattr()");
    old.c_lflag &= ~ICANON;
    old.c_lflag &= ~ECHO;
    old.c_cc[VMIN] = 1;
    old.c_cc[VTIME] = 0;
    if (tcsetattr(0, TCSANOW, &old) < 0)
        perror("tcsetattr ICANON");
    if (read(0, &buf, 1) < 0)
        perror ("read()");
    old.c_lflag |= ICANON;
    old.c_lflag |= ECHO;
    if (tcsetattr(0, TCSADRAIN, &old) < 0)
        perror ("tcsetattr ~ICANON");
    return (buf);
}

int main() {
    _Game game;
    x = 0;
    y = 0;
    char card;
    // for(int i = 0; i < 4; i++) {
    //  system("clear");
    //  game.bprint();
    //  x--;
    //  sleep(1);
    // }
    // for(int i = 0; i < 4; i++) {
    //  system("clear");
    //  game.bprint();
    //  y--;
    //  sleep(1);
    // }
    while(true) {
        system("clear");
        game.bprint();
        card = getch();
        if(card == '[') {
            card = getch();
            switch(card) {
                case 'A':
                    y--;
                case 'B':
                    y++;
                case 'D':
                    x = x-1;
                case 'C':
                    x++;
                default:
                    cout << card;
            }
            cout << x << ", " << y << endl;
        }
        else if(card == 'e') {
            break;
        }
    }
    system("clear");
    game.bprint();
}

g++ and C++ 11 on ubuntu 20.04 LTS

Aucun commentaire:

Enregistrer un commentaire