2016-12-10 6 views
0

私はC#の新機能で、幽霊のような家のコンテキストで迷路ゲームのプロジェクトに取り組んでいます。オブジェクトとリンクされたリストを使用します。しかし、何週間もの努力をしているにもかかわらず、私はコードに苦しんでおり、記事を読んで、オンラインチュートリアルを見ていて、今までにはもっと混乱しているのです。私はこれがより効率的でより多くのOOPであるように感じるので、配列ではなくこのアプローチを使用したいと思っています。 私はシンプルなif/else構造を使うつもりだと思っていましたが、このレベルのコーディングでは面倒すぎると感じています。 私はそれに費やした多くの時間の後にあきらめたくないので、任意の助け、建設的な批評やアイデアは高く評価されるだろうが、私はその点に到達して感じる。これはこれまでの私のコードです。事前に 感謝:)構文の問題C#、ojects/call get、setを使用できません。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace maze_3 
{//open namespace 
    class Room 
    {//open class room 

     private string RoomName; 
     private Room N = null; // so that when i set the links they are automatically null unless i specify in main 
     private Room E = null; 
     private Room S = null; 
     private Room W = null; 

     public Room X { get; private set; } // the software implemented this 

     public void setName(string N) // so that i am able to set the name of the room 
     { 
      RoomName = N; 
     } 

     public void setN(Room X) // sets the north direction between the objects of the rooms 
     { 
      N = X; 
     } 

     public void setE(Room X) 
     { 
      E = X; 
     } 

     public void setW(Room X) 
     { 
      W = X; 
     } 

     public void setS(Room x) 
     { 
      S = X; 
     } 

     public Room getN() // want to get a direction from the user, in this case they would input a north direction 
     { 
      return N; 
     } 

     public Room getE() 
     { 
      return E; 
     } 

     public Room getS() 
     { 
      return S; 
     } 

     public Room getW() 
     { 
      return W; 
     }  

    static void Main(string[] args)// it is asking for a ; here but also says that I should declare it as extern, partial etc 

    class chamber// its telling me that a ; is expected ??? 
    {//open class chamber 

     chamber gh = new chamber();// my objects that are the rooms in the haunted house. 
     chamber kit = new chamber(); 
     chamber Pan = new chamber(); 
     chamber Dun = new chamber(); 
     chamber dr = new chamber(); 
     chamber lib = new chamber(); 
     chamber din = new chamber(); 
     chamber sr = new chamber(); 
     chamber weap = new chamber(); 
     chamber tow = new chamber(); 
     chamber gal = new chamber(); 
     chamber tr = new chamber(); 

     gh.RoomName("Great Hall"); //to set the object name as the Great Hall, all get and set links were available in class 
      gh.setW(dr); ///I want to set a west direction between my object gh to dr 
      gh.SetS(kit);// to set a south link between my two objects 

      dr.setName("Drawing Room");//states that all my setlinks are not valid in the current context- this is for all 
      dr.setS(lib); //it states the ; is not a valid token in class, struct or interface 

      kit.setName("Kitchen");// it states that my objects e.g kit is not valid in this current context-this is for all 
      kit.setS(pan); 

      pan.setName("Pantry"); 
      pan.SetE(dun); /// this is a dead end in the game 

      lib.setName("Library "); 
      lib.setE(din); 

      din.setName("Dining Room"); 
      din.setN(sr); 
      din.setE(gal); 
      din.setS(weap); //dead end in the game 

      sr.setName("Smoking Room"); 
      sr.setE(tow);//dead end 

      gal.setName("Treasure Room"); 
      gal.setS(tr)  

      /// </summary> so i wanted to have the actual game play to follow a linked list with a if else structure. 
      int finish = 0; 
     string choice;  
     Room current; 

     current=gh; 
      while (finish !=1) (finish ==0) //im confused as to which condition is best to use. 
      {Console.WriteLine("You are in room " + current.getRoomname() + "and you can move "); 
     if(current.getN() != null) 
        Console.WriteLine("North (N) "); 
     if(current.getE() != null) 
        Console.WriteLine("East (E) "); 

     Console.WriteLine("Please enter the direction you wish to go in "); 
     string choice = Console.ReadLine(); 
     if (choice[0] == 'E') // or alternative means of getting in a choice from a string 
        current = current.getE();// to be able to move to next 'current'room 

      // i was going to do this for all the options. 

     if(current == tr // ie last room 
      exit = 1; 
     Console.Writeline ("Well done you have found the treasure room");    
     }//close chamber 

     }//close class rooom 

    } //close namespace 
+0

あなたの問題をもっと説明できますか?私はあなたの問題を理解していませんでした。 – pooyan

+0

クラスの定義が乱れていると思います。 '} // close class'に'} // close class rooom'と '} // close namespace'を付けてください。 – bradbury9

+0

申し訳ありません。まず、オブジェクトを正常に作成した後、メインの設定メソッドと組み合わせて使用​​することができず、同様に設定メソッドを使用することができません。これは次の問題と関連していると思います。なぜなら、ゲームプレイ、つまりconsole.writelineを設定しているときにシンタックスが機能していないからです。 –

答えて

0

あなたは()の主を持たないクラスのオブジェクトを作成しました。プロジェクトにはメインが1つしかありません。したがって、アプリケーション内でメインルームをメインにするか、ルームクラスからメインメソッドを削除する必要があります。変更については私のコードを参照してください。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace maze_3 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Room home = new Room("White House"); 
      home.CreateRoom("W", "West Wing"); 
     } 
    } 
    class Room 
    {//open class room 

     private string RoomName; 
     private Room N = null; // so that when i set the links they are automatically null unless i specify in main 
     private Room E = null; 
     private Room S = null; 
     private Room W = null; 
     private List<Chamber> chambers = new List<Chamber>(); 

     public Room() { } 
     public Room(string name) 
     { 
      this.RoomName = name; 
     } 
     public void CreateRoom(string direction, string name) 
     { 
      Room newRoom = new Room(); 
      newRoom.RoomName = name; 
      switch (direction) 
      { 
       case "N": 
        N = newRoom; 
        break; 
       case "E": 
        E = newRoom; 
        break; 
       case "S": 
        S = newRoom; 
        break; 
       case "W": 
        W = newRoom; 
        break; 

      } 
     } 


    }//close class rooom 


    public class Chamber// its telling me that a ; is expected ??? 
    {//open class chamber 
     string name = ""; 
    }//close chamber 
} 
+0

構文は次のとおりです。Room newRoom = new Room();私のオブジェクトを作成したときと同じです。もしそうなら、私は名前を設定します - 'string name = ""'?また、これは私が持っているif else構造を置き換えるか、現在のオブジェクトがある場所でこのスロットを置き換えます。ありがとう –

+0

新しい部屋が親に接続されるように、メインと変更されたコードを含むようにコードを更新しました。 switch文があなたの 'IF'を置き換えます。コンストラクタはRoomName – jdweng

関連する問題