2013-05-21 26 views
0

我試圖尋找我的問題的答案,但找不到任何非常正確的。我想忽略用戶的輸入,如果他們超出界限

我的問題處理有一個數組設置和基本上圍繞一個點域移動小光標。我需要能夠忽略用戶的輸入,如果他們擊中一個箭頭鍵的方向,將他們超出電網的界限。我不確定最好是做什麼。

這是我無法修改的類。我只能從它繼承來做到這一點。

//You can add or delete includes 
#include <iostream> 
#include <stdlib.h> //For system() 
#include <conio.h> //For getche() 
#include <time.h> 
using namespace std; 


const int MAX_HEIGHT = 20; //The height of the grid 
const int MAX_WIDTH = 40; //The width of the grid 

class PickUpGame 
{ 
protected: 
    char Screen[MAX_HEIGHT][MAX_WIDTH]; //The grid to print to the screen 
    int xPos, yPos; //The current x and y position of the users cursor on the grid 

public: 
    //Constructor that will intialize the screen and x and y positions 
    PickUpGame() : xPos(0), yPos(MAX_WIDTH - 1) 
    { 
     SetupScreen(); //Initalize the grid 
    } 

    //Initialize the screen with all '.' characters and set the intial user cursor position on the grid 
    void SetupScreen() 
    { 
     for(int height = 0; height < MAX_HEIGHT; height++) { 
      for(int width = 0; width < MAX_WIDTH; width++) { 
       Screen[height][width] = '.'; //Initialize each grid position 
      } 
     } 
     Screen[xPos][yPos] = '<'; //Set the users initial cursor position 
    } 

    //Print the grid to the screen 
    void Print() 
    { 
     for(int height = 0; height < MAX_HEIGHT; height++) { 
      for(int width = 0; width < MAX_WIDTH; width++) { 
       cout << Screen[height][width]; //Print the character at this location in the grid 
      } 
      cout << endl; //After each row is printed, print a newline character 
     } 
    } 

    //Take in user input to move around the grid 
    void Move(char Direction) 
    { 
     switch(static_cast<int>(Direction)) //Don't know the ASCII characters for the arrow keys so use the ASCII numbers 
     { 
      case 72: //Up arrow 
       Screen[xPos][yPos] = ' '; //Wipe out the users current cursor 
       xPos--; //Move the users x position on the grid 
       Screen[xPos][yPos] = '^'; //Move the users cursor 
       break; 
      case 80: //Down arrow 
       Screen[xPos][yPos] = ' '; 
       xPos++; 
       Screen[xPos][yPos] = 'V'; 
       break; 
      case 75: //Left arrow 
       Screen[xPos][yPos] = ' '; 
       yPos--; 
       Screen[xPos][yPos] = '<'; 
       break; 
      case 77: //Right arrow 
       Screen[xPos][yPos] = ' '; 
       yPos++; 
       Screen[xPos][yPos] = '>'; 
       break; 
     } 
    } 

};

+0

你有試過什麼嗎? – hexacyanide

回答

2

你只需要檢查一個動作是否會超出界限並忽略它。例如,向右移動:

if (right_key_pressed() && x_position < MAX_X_VALUE) { 
    x_position++; 
} 

也就是說,如果他們按鍵和尚未達到最大X位置值,他們將只向右移動。您可以將類似的邏輯應用於其他方向。此外PickUpGame類的後


編輯質疑

由於移動邏輯是PickUpGame,你說你是不允許修改的,這使得它有點討厭。理想情況下,Move內的if語句將檢查邊界。相反,你將不得不從那裏你打電話Move外面做檢查:

if ((Direction == 72 && xPos > 0) || 
    /* do the same for right, down, and left */) { 
    Move(Direction); 
} 

所以,你希望它檢查Direction啓動並有空間可以向上移動,或者如果它是正確的而且有足夠的空間向右移動,或者......等等。然後,只有這樣你才能通過DirectionMove

+0

哦哇我沒有考慮使用循環(真的真的很新手 - 對不起) 謝謝! – bgbp

+0

@BethanyPulcini它不是一個循環 - 只是一個條件語句。循環將是「while」或「for」語句。 –

+0

oo,我覺得我有點困惑。我的項目的代碼已經處理了光標的移動。我不能修改那個類,只能從它繼承。我打算做一個if語句,但是我不知道我會寫什麼來讓程序忽略他們輸入的內容,如果它真的把它們帶出界限的話。 – bgbp

0

聽起來好像你需要做的就是爲用戶在數組中的位置(你可能已經有一個)和兩個最大值和最小值保持一個變量。然後,當用戶按下箭頭鍵時,在移動光標之前,測試動作是否會將它們帶出界限。

+0

我認爲這是我的問題,我有一個if語句來檢查它們是否超出範圍,但如果它出現,我怎麼忽略它們的箭頭?處理光標移動的類是預先寫好的,我不允許修改它只能從它繼承。 – bgbp

+0

嗯......我不會說太多的C++,但是你能否繼承這個類,然後用一個新的Move來覆蓋Move方法,檢查出界?如果沒有,則找到任何代碼調用此類的Move方法,並將條件語句放在該句子的附近。如果一切都失敗了,你可能只需複製粘貼該文本文本,將其稱爲不同的類並重新編譯它,然後使用它來代替這個。 – MegaWidget