地下城爬行游戏,运动力学问题

问题描述 投票:-2回答:1

我是c ++的初学者,我正在编写一个名为dungeoncrawl.cpp的程序。

所以对于程序的第一部分(地图生成和随机位置)到目前为止这么好,但是对于运动机制我发现了一个问题。是的我到目前为止只添加左右,但程序似乎只能识别switch语句中的第二种情况,例如当我按'd'时,播放器向右移动,但是当我按'a'时没有任何反应,如果我改变案件的顺序,则会发生相反的情况。我很确定我可以使用if语句使其工作,但我很好奇。谢谢您的帮助。

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

// dungeoncrawl.cpp
// Make a program that outputs a simple grid based gameboard to the screen
// using either numbers or characters.
// Allow the user (marked by G in the example) to move either up, down, left,
// or right each turn.
// If the player steps on a trap then they lose. If the make it to the
// treasure 'X' then they win.

int i;
char table[76];

class game {
 public:
  void board() {  // GAME BOARD
    srand(time(0));
    for (i = 0; i <= 76; i++) {
      if (i % 11 == 0) {
        table[i] = '\n';
      } else {
        table[i] = '.';
      }
    }
    // ENEMIES
    for (int x = 0; x < 3; x++) {
      i = (rand() % 75);
      if (table[i] != '\n' && table[i] != 'G' && table[i] != 'X' &&
          table[i] != 'T') {
        table[i] = 'T';
      } else {
        i = (rand() % 75 + 1);
        if (table[i] != '\n' && table[i] != 'G' && table[i] != 'X' &&
            table[i] != 'T') {
          table[i] = 'T';
        }
      }
    }
    // PLAYER

    i = (rand() % 25 + 1);  // player initiantes in the beggining of the board
    if (table[i] != '\n' && table[i] != 'G' && table[i] != 'X' &&
        table[i] != 'T') {
      table[i] = 'G';
    } else {
      i = (rand() % 25 + 1);
      if (table[i] != '\n' && table[i] != 'G' && table[i] != 'X' &&
          table[i] != 'T') {
        table[i] = 'G';
      }
    }
    // TREASURE
    table[75] = 'X';
    // PRINT BOARD
    for (i = 0; i <= 76; i++) cout << table[i];
  }
};

// board:
// game obj;
// obj.board();

int main() {
  char move;
  game obj;
  obj.board();
  // player move
  while (table[75] != 'G') {
    cout << "\n"
         << "Make your move: ";
    cin >> move;
    switch (move) {
      case 'a':
        for (i = 0; i <= 76; i++) {
          if (table[i] == 'G') {
            table[i] = '.';
            table[i - 2] = 'G';
            break;
          }
        }
      case 'd':
        for (i = 0; i <= 76; i++) {
          if (table[i] == 'G') {
            table[i] = '.';
            table[i + 2] = 'G';
            break;
          }
        }
    }
    for (i = 0; i <= 76; i++) cout << table[i];
  }
  return 0;
}
c++ switch-statement
1个回答
0
投票

你没有足够的break语句,你所拥有的休息是在for循环中并且突破了

试试这个:

case 'a':

for (i=0;i<=76;i++){
if (table [i]=='G') {
table [i]='.';
table [i-2]='G';break;}
  }
break;

case 'd':

for (i=0;i<=76;i++){
if (table [i]=='G') {
table [i]='.';
table [i+2]='G';break;}
} 
break;
© www.soinside.com 2019 - 2024. All rights reserved.