增量和减量未按预期运行
Increment, and Decrement not acting as expected
我正在用 C++ 编写一个基于控制台的游戏,我已经到了通过箭头键移动玩家的地步,但是它们 x-- 和 y-- 不会改变 x 或 y 的值,我尝试过其他配置,例如 x -= 1 和 x = x-1,但都没有用
(我也尝试过将 _Game 结构作为 class,但没有更好的结果)
我的密码是
#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
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++ 和 C++ 11 on ubuntu 20.04 LTS
因为在按下 'A' 执行开关时忘记调用 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;
}
我正在用 C++ 编写一个基于控制台的游戏,我已经到了通过箭头键移动玩家的地步,但是它们 x-- 和 y-- 不会改变 x 或 y 的值,我尝试过其他配置,例如 x -= 1 和 x = x-1,但都没有用
(我也尝试过将 _Game 结构作为 class,但没有更好的结果)
我的密码是
#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
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++ 和 C++ 11 on ubuntu 20.04 LTS
因为在按下 'A' 执行开关时忘记调用 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;
}