微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

增量和减量未按预期运行

如何解决增量和减量未按预期运行

我正在用 C++ 编写一个基于控制台的游戏,我已经到了通过箭头键移动玩家的地步,但是它们 x-- 和 y-- 不会改变 x 或 y 的值,我试过了其他配置,如 x -= 1 和 x = x-1,但都没有工作

(我也尝试了 _Game 结构作为一个类,但没有更好的结果)

我的代码

#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}
    };

    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();
}

ubuntu 20.04 LTS 上的 g++ 和 C++ 11

解决方法

由于您在按下“A”时忘记在 switch 的情况下调用 break,因此执行是:y--; y++,x--; x++;。 'D' 的效果几乎相同。如果您想打破案例的顺序,您需要像这样使用 break

switch(card) {
   case 'A':
      y--;
      break; // jumps to the end of the switch
   case 'B':
      y++;
      break;
   case 'D':
      x = x-1;
      break;
   case 'C':
      x++;
      break;
   default:
      cout << card;
      break;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。