2017-03-21 22 views
0

OK、とても簡単なテキストアドベンチャーゲームに取り組んでいます。今私は機能しているメニューと、できるだけ早くランダムな出会いに変換される非常に単純な「戦闘モード」を持っています。今でも地図は問題ありません。C++テキストアドベンチャーゲーム:マップとしての2D配列

2次元配列システムをマップとして使用し、座標が(それが理にかなっているとすれば)座標として格納されるようにすることです。「北、東、南および西」の4方向を入力して移動できますあなたが行くことができる3種類の場所があります。畑、森、城。地図自体は5×6です。

私がしたいのは、座標2と2の中央で開始するようにしたいのです。たとえば、フィールド(例えば、1の整数が上に表示されます)と同じように移動することができます。 「あなたはフィールドにいる」と言って、城や森などに向かって動くのにも同様のものをやっています。彼らはそこに行くことができない0と記された場所に移動しようとしたら、それらの動きを止めてください。

誰にも助言がありますか?

EDIT:もう一度プログラムを実行しましたが、いくつかのエラーを取り除くことができましたが、まだまだ残っています(正確には26、警告は1です)。 誰でも私にいくつかの提案を提供しますか?

#pragma once 
#include "Map.h" 
#include <iostream> 
using namespace std; 

Map::Map() 
{ 
} 

void main() 
{ 
    // Declare variables & functions 
    int locationy; 
    int locationx; 

    char oper; 
    char location; 

    int pond = 0; 
    int field = 1; 
    int forest = 2; 
    int castle = 3; 

    int mapy; 
    int mapx; 
    int map; 

    //These two values declare the start location on the array map for the player 
    int mapy = 3; 
    int mapx = 3; 

    //These two variables track your current position on the map 
    mapy = 2; 
    mapx = 2; 

    map[locationy][locationx]; 

    //Declares the amount of space within an array 
    int map[6][6] = 
    { 
     { 0, 0, 0, 0, 0, 0 }, 
     { 0, 2, 1, 2, 1, 0 }, 
     { 0, 1, 2, 1, 2, 0 }, 
     { 0, 1, 2, 2, 1, 0 }, 
     { 0, 1, 3, 1, 2, 0 }, 
     { 0, 0, 0, 0, 0, 0 } 
    }; 

    //Asks the player where they want to go around the map 
    cout << "Where to?" << endl; 

    //Request for user to enter an direction (I.e., North, East, South, West.) 
    cout << "Please choose North, East, South or West" << endl; 

    //Displays the inputted values 
    cin >> oper; 

    //Pauses system so we can see what the program does 
    system("pause"); 

    //Checks input from the player 
    if (cin == "North") 
    { 
     //Moves location upwards on the y axis 
     if (map[locationx][locationy + 1] != 0) { locationy += 1; }; 
     else { cout << "That is water, dude. Swimming in platemail is NOT recommended.\n"; 
    } 

    //Checks input from the player 
     if (cin == "East") 
    { 
     //Moves location to the right on the x axis 
     if (map[locationx + 1][locationy] != 0) { locationy += 1; }; 
     else { cout << "That is water, dude. Swimming in platemail is NOT recommended.\n"; 
    } 

    //Checks input from the player 
    if (cin == "South") 
    { 
     //Moves location downwards on the y axis 
     if (map[locationx][locationy - 1] != 0) { locationy += 1; } 
     else { cout << "That is water, dude. Swimming in platemail is NOT recommended.\n"; 
    } 

    //Checks input from the player 
    if (cin == "West") 
    { 
     //Moves location to the left on the x axis 
     if (map[locationx - 1][locationy] != 0) { locationy += 1; } 
     else { cout << "That is water, dude. Swimming in platemail is NOT recommended.\n" 

    }; 
} 

Map::~Map() 
{ 
    ; 
} 
+2

'(もし!マップ[locationx] [locationy + 1] = 0){locationy + = 1 ; } else {cout << "それは水です、おい、platemailでの水泳はお勧めしません。\ n"} ' – user4581301

+0

こんにちは、スタックオーバーフローへようこそ!この種の質問はおそらくこのサイトに適しています:http://codereview.stackexchange.com/。 codereviewでは、あなたの質問に答えることができる人が、長期的にはあなたのためにもっと役立つことがあります。 –

+0

アドバイスをいただき、ありがとうございました。その40分の時間が経過するとすぐに投稿を試みます。その間、あなたは私に関係なく、私に提供できるアドバイスはありますか? –

答えて

1

ようこそC++。今、あなたは基本的な初心者ミスをしました。それは命令的なコードを書いています。 C++はオブジェクト指向であり、これを使用する上での鍵です。コードをはるかに効果的にします。

このような場合には、プロジェクトのどの部分がどのようなものであるか、クラスとして識別できるものを特定する必要があります。あなたはMapでそうしましたが、あなたが逃したのはGameのようなすべてを包むクラスを作ることでした。

ここに私の提案があり、これまでに実装されていない多くの部分があります。

まず、いくつかの地形を表す数字は醜いです。それらを列挙型で置き換えましょう:これ以降は、池が意味するときはいつでも、Terrain :: pondと書くことができます。コードを読みやすくします。 (Game.h中)クラスのレイアウトのための今すぐ

#include <vector> 
using std::vector; 

#include "Terrain.h" 
//alternatively, define Terrain here instead of making it a separate header 

class Game { 

public: 

    Game(); 

    Game(const string& file_name); 
    //Game(file_name) is something you might want to do later - 
    //create an instance of Game based on a file that contains a map, 
    //maybe more data, instead of hard-coding such things 

    void run(); 

private: 

    vector<vector<Terrain> > map; 
    //arrays are something you want to avoid. Use std::vector instead. 

    unsigned int location_x, location_y; 

    bool game_running; 
    //aka not has ended 

    void evaluate_step(); 
    //this method is repeated and repeated until the game ends 
    //and contains the order in which every step is evaluated 

    void handle_input(); 
    //Takes cin and does something with it 

    void evaluate_position(); 
    //takes the position, gets the terrain from the position, 
    //does what happens on that terrain 

    void print(); 
    //prints the whole map to the console 

    void set_default_map(); 
    //sets map to the data you have in your code, 
    //to be used until reading from file is implemented 
} 

今、私たちは(Game.cppに)デフォルトを作成するために、空のコンストラクタを作成します。

Game::Game(){ 

    set_default_map(); 
    location_x = ...; 
    location_y = ...; 
    game_running = true; 
} 

実行されます単に繰り返し評価するステップ:

void Game::run(){ 

    while(game_running){ 
     evaluate_step(); 
    } 
} 

void Game::evaluate_step(){ 

    handle_input(); 
    print(); 
    evaluate_terrain(); 
} 

ここで詳細を説明することができますが、これはあなたにいくつかのアイデアを与えるはずですそのような構造は見えるかもしれません。基本的なアイデアは、分かりやすく分かりやすくするために分けて分けることです。

このクラスについて質問がある場合は、この回答を編集します。あなたのメインで

、そのように呼ばれることになります。

#include "Game.h" 

int main(){ 

    Game game; 
    game.run(); 
    return 0; 
} 

は編集:今、あなたのコードのエラーについて:

  1. あなたが代わりに無効メインのメインint型記述する必要があります。 mainはJava構文のように見えます。
  2. mapyとmapxはint mapy;int mapy=3;の2回宣言されています。それは許されない。最初の宣言を削除するか、代入を宣言しないでください(int mapy;mapy=3;、最初の宣言を削除することをお勧めします)。
  3. map[locationy][locationx];で何がいいのかわからない。これは決して有効なコードではありません。
  4. mapも2つの異なる型(intとint [] [])で2回宣言されています。最初の宣言を削除します。
  5. cinのすべてのケースで、あなたの条件はひどく整形されています。角括弧の後にセミコロン、if(..){..}else{..}if(..){..};else{..}ではありません。また、いずれの場合も、ブラケットがありません。
  6. mapxとlocationxの違いは何ですか? mapxは設定されていますが、locationxが使用されていますか? mapxとmapyを削除し、代わりにlocationxとlocationyを2に設定しました。
  7. 場所は使用されていません。何のために?
  8. 池やフィールドなどを定義しますが、使用しません。
  9. メインはプラグマを一度必要としません(ガードは必要ありません)
  10. operは使用されません。 if(oper == "North")などのif条件を変更したいと思うかもしれません。そのためには、operのタイプを変更する必要があります。 charは1文字しか保持できません。あなたはchar *を使うことができますが、ポインタが悪いので、文字列を使って行くほうがいい(#include <string>が必要です)
  11. 読み込みと評価は1回だけです。一歩を踏み、プログラムが終了する。あなたが望むものではないと思いますが、周りにループを入れたいと思うかもしれません。bool running = true; while(running){...}
  12. どのような方向についても、同じ方法で位置が変更されます。

は、それが実行して作るためにそれらを変更しました(しかし、あなたは本当にデザインを変更する必要があります):

#include <iostream> 
#include <string> 
using std::cout; 
using std::cin; 
using std::endl; 
using std::string; 

void print_water_warning() { 
    cout << "That is water, dude. Swimming in platemail is NOT recommended.\n"; 
} 

int main() { 
    // Declare variables & functions 
    int locationy = 2; 
    int locationx = 2; 

    string oper; 

    //Declares the amount of space within an array 
    int map[6][6] = { {0, 0, 0, 0, 0, 0}, 
         {0, 2, 1, 2, 1, 0}, 
         {0, 1, 2, 1, 2, 0}, 
         {0, 1, 2, 2, 1, 0}, 
         {0, 1, 3, 1, 2, 0}, 
         {0, 0, 0, 0, 0, 0} }; 

    while (true) { 

     //Asks the player where they want to go around the map 
     cout << "Where to?" << endl; 

     //Request for user to enter an direction (I.e., North, East, South, West.) 
     cout << "Please choose North, East, South or West" << endl; 

     //Displays the inputted values 
     cin >> oper; 

     //Checks input from the player 
     if (oper.compare("North") == 0) { 
      //Moves location upwards on the y axis 
      if (map[locationx][locationy + 1] != 0) { 
       locationy += 1; 
      } else { 
       print_water_warning(); 
      } 
     } 

     //Checks input from the player 
     if (oper == "East") { 
      //Moves location to the right on the x axis 
      if (map[locationx + 1][locationy] != 0) { 
       locationx += 1; 
      } else { 
       print_water_warning(); 
      } 
     } 

     //Checks input from the player 
     if (oper == "South") { 
      //Moves location downwards on the y axis 
      if (map[locationx][locationy - 1] != 0) { 
       locationy -= 1; 
      } else { 
       print_water_warning(); 
      } 
     } 

     //Checks input from the player 
     if (oper == "West") { 
      //Moves location to the left on the x axis 
      if (map[locationx - 1][locationy] != 0) { 
       locationx -= 1; 
      } else { 
       print_water_warning(); 
      } 
     } 
    } //end while 
} //end main 
+0

申し訳ありません、私は本当に私のオリジナルのポストでこれを追加しているはずですが、私は非常に新しいC + +とあなたの言葉の多くはまだ私はまだ教えていないものです。また、私の地図が配列として格納されていることが必要であり、別のシステムを使用することで逃げることはできません。関係なく、助けてくれてありがとう、私は本当にそれを感謝し、私は間違いなくそれが動作する方法を見るためにそれを試してみます。 –

+0

@Thane_Mantisはい、要件。私はあなたに少なくともそれについて先生に尋ねることを勧めます。それは、あなたが今までに聞いたことのないいくつかのコンセプトについて詳しく説明したいのであれば、特に質問してください。少なくとも、クラスについて何かを学んだかのように見えますか?またはMap.hは説明なしで与えられますか?列挙型に関しては、それほど難しいことではありません。http://en.cppreference.com/w/cpp/language/enumを見て、それらがどのように機能するかを理解する必要があります。 – Aziuth

+0

@Thane_Mantis私の答えを編集し、最後にあなたのコードのエラーのリストを追加しました。 – Aziuth