2013-12-15 62 views
-4

我已經創建了這個使用鏈表的專賣板。專賣實施規則

已將值賦予方塊,但需要能夠將這些值的形式添加到玩家類中分配的整數。

任何幫助或建議,將不勝感激(我現在道歉他們還要修訂的意見)的代碼如下:

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

namespace MonoPolyFin 
{ 

public class Square//Creating Squares for the board 
{ 

    public Square next;//Declaring the Square as the next node 
    public string g;//String to store the Square data 

    public Square(string g)//assigning the string to g 
    { 
     this.g = g; 
    } 



    public class Board/*The Board class creating the linked list and printing it out to      the console window*/ 
    { 

     public Square head;//Declaring as the head 
     Square current;//and current 

     public void Add(Square s)/*The Add method for adding nodes in order to the linked list checking each time to see if the head of 
     the list is empty */ 
     { 
      if (head == null)//if the head is empty 
      { 
       head = s;//the head equals the string 
       current = head;//the current node is the head 
      } 
      else 
      { 
       current.next = s;//if not empty the current node is the next node 
       current = current.next;//then the next 
       current.next = head;//keep going round the board through the linked list 
      } 
     } 

     public void Writeline(ref Square gamesquare)//The writeline method to print out in the console 
     { 
      Random r = new Random();//Creates a new object using the random method 
      int n = r.Next(1, 6);//Creates a random number between 1-6 
      Console.WriteLine(n);//Writes the stored number to the console 
      Console.WriteLine("Player moves to " + gamesquare.g);//write the gamesquare to the console that is saved in the string g 
      Console.ReadKey();//read whats in the console and throw it out 


      if (n <= 1)//If the number is less than 1 
      { 

       head = current 
        ;//Move one positions down the list 

      } 
      else if (n <= 2)//If the number is less than 2 
      { 

       head = head.next.next;//Move two positions down the list 

      } 

      else if (n <= 3)//If the number is less than 3 
      { 

       head = head.next.next.next;//Move three positions down the list 

      } 
      else if (n <= 4)//If the number is less than 4 
      { 

       head = head.next.next.next.next;//Move four positions down the list 

      } 
      else if (n <= 5)//If the number is less than 5 
      { 

       head = head.next.next.next.next.next;//Move five positions down the list 

      } 
      else if (n <= 6)//If the number is less than 6 
      { 

       head = head.next.next.next.next.next.next;//Move six positions down the list 

      } 
     } 
    } 


    public class Player1 
     { 

      public string Dog; 
      public int Money = 2000; 
      public Square position = null; 

      } 


    public class Player2 
    { 

     public string Car; 
     public int Money = 2000; 
     public Square position = null; 

    } 


    static void Main(string[] args)//Main method 
     { 
      int go = 200;//Value of the square in the linked list 
      int strand = 220;//Value of the square in the linked list 
      int trafalger_square = 220;//Value of the square in the linked list 
      int leicester_square = 280;//Value of the square in the linked list 
      int piccadilly = 280;//Value of the square in the linked list 
      int regent_street = 300;//Value of the square in the linked list 
      int oxford_street = 300;//Value of the square in the linked list 
      int mayfair = 400;//Value of the square in the linked list 
      int park_lane = 400;//Value of the square in the linked list 
      int whitechapel = 60;//Value of the square in the linked list 
      int old_kent_road = 60;//Value of the square in the linked list 
      int euston = 100;//Value of the square in the linked list 
      int islington = 100;//Value of the square in the linked list 
      int pall_mall = 140;//Value of the square in the linked list 
      int whitehall = 140;//Value of the square in the linked list 
      int vine_street = 180;//Value of the square in the linked list 
      int bow_strett = 180;//Value of the square in the linked list 

      Board list = new Board();//constructor to create the list 
      list.Add(new Square("Go Collect £" + go));//Add to the linked list using the Add method also assigning the int value 
      list.Add(new Square("Strand £" + strand + " Black"));//Add to the linked list using the Add method also assigning the int value 
      list.Add(new Square("Trafalger Square £" + trafalger_square + " Black"));//Add to the linked list using the Add method also assigning the int value 
      list.Add(new Square("Gas"));//Add to the linked list 
      list.Add(new Square("Leicester Square £" + leicester_square + " Aqua"));//Add to the linked list using the Add method also assigning the int value 
      list.Add(new Square("Piccadilly £" + piccadilly + " Aqua"));//Add to the linked list using the Add method also assigning the int value 
      list.Add(new Square("Card"));//Add to the linked list 
      list.Add(new Square("Regent Street £" + regent_street + " Yellow"));//Add to the linked list using the Add method also assigning the int value 
      list.Add(new Square("Oxford £" + oxford_street + " Yellow"));//Add to the linked list using the Add method also assigning the int value 
      list.Add(new Square("Electric"));//Add to the linked list 
      list.Add(new Square("Mayfair £" + mayfair + " Green"));//Add to the linked list using the Add method also assigning the int value 
      list.Add(new Square("Park Lane £" + park_lane + " Green"));//Add to the linked list using the Add method also assigning the int value 
      list.Add(new Square("Jail"));//Add to the linked list 
      list.Add(new Square("Old Kent Road £" + old_kent_road + " Blue"));//Add to the linked list using the Add method also assigning the int value 
      list.Add(new Square("Whitechapel £" + whitechapel + " Blue"));//Add to the linked list using the Add method also assigning the int value 
      list.Add(new Square("Water"));//Add to the linked list 
      list.Add(new Square("Islington £" + islington + " Red"));//Add to the linked list using the Add method also assigning the int value 
      list.Add(new Square("Euston £" + euston + " Red"));//Add to the linked list using the Add method also assigning the int value 
      list.Add(new Square("Card"));//Add to the linked list 
      list.Add(new Square("Pall Mall £" + pall_mall + " Pink"));//Add to the linked list using the Add method also assigning the int value 
      list.Add(new Square("Whitehall £" + whitehall + " Pink"));//Add to the linked list using the Add method also assigning the int value 
      list.Add(new Square("Phone"));//Add to the linked list 
      list.Add(new Square("Bow Street £" + bow_strett + " Brown"));//Add to the linked list using the Add method also assigning the int value 
      list.Add(new Square("Vine Street £" + vine_street + " Brown"));//Add to the linked list using the Add method also assigning the int value 

      for (int i = 0; i <= 10; i++) 
      { 

       Player2 car = new Player2(); 
       car.position = list.head; 
       Console.WriteLine("Car roles the dice and roles"); 
       list.Writeline(ref car.position); 

       Player1 dog = new Player1(); 
       dog.position = list.head; 
       Console.WriteLine("Dog roles the dice and roles"); 
       list.Writeline(ref dog.position); 
      } 

      } 

     } 
    } 
+3

忽略這個看起來像作業的事實,你已經發布了一個非常大的代碼牆。請縮小您的代碼,以便僅查看您有問題的代碼。 – gunr2171

+0

無可否認,這是我迄今爲止所做的功課,作業,儘管我尋找的解決方案我自己找不到。我發佈了代碼牆,以便查看代碼的人知道我已經掌握了哪些內容,並且希望能夠向我提供有關在上述問題中添加了代碼的位置的建議。 – itchebantye

+0

恩,我本來期望在它上面觸發的「廣場」上發生一個事件,它執行了一個動作。 PS遊戲的一個有趣的部分是讓業主分心,所以他們沒有注意到你剛剛在mayfair的酒店登陸...... –

回答

1

好像你的正方形應該由一個類型的層次結構。

廣場

  • 卡,抽獎
    • 機會
    • 公益金
  • 稅/ GO
  • 監獄
  • 可擁有
    • 實用
    • 鐵路
    • 彩色

所有這些都將有一個多態的方法

virtual void VisitFrom(int diceRoll, Player activePlayer); 

例如,實用的實現可能。

virtual void VisitFrom(int diceRoll, Player activePlayer) 
{ 
    if (owner == Bank) 
     activePlayer.offerProperty(this, 150); 
    else if (activePlayer != owner) 
     activePlayer.Pay(owner, ((otherUtility.owner == owner)? 10: 4) * diceRoll); 
} 

哦,另一方面,這兩個球員應該是同一類的實例。

對於具有單獨行爲的對象,您需要單獨的類。對象相似時使用實例。

當然,您還需要將Color實例關聯到ColorGroups中,以便在同一個玩家擁有它們時給予獎勵。

+0

你的權利,我已經在同一個班級執行球員(建議),因爲他們有相同的行爲!我不知道如何構造層次結構,因爲正方形是鏈接列表的一部分,所有這些都是我的問題。 – itchebantye

+0

@itchebantye:我所展示的層次結構旨在成爲繼承層次結構,而不是數據結構。您仍然可以將它們作爲鏈接列表鏈接在一起。由於鏈接在基類中。 –