我使用链接列表创建了这个Monopoly板。
已为方块指定了值,但需要能够在播放器类中指定的整数中添加或删除这些值。
任何帮助或建议都将不胜感激(我现在为他们尚未修改的评论道歉)代码如下:
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);
}
}
}
}
答案 0 :(得分:1)
好像你的方块应该是一个类型层次结构。
方
所有这些都将具有多态方法
virtual void VisitFrom(int diceRoll, Player activePlayer);
例如,Utility的实现可能是。
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);
}
哦,另一方面,两个玩家应该是同一个类的实例。
对于具有不同行为的对象,您需要单独的类。当对象行为相似时使用实例。
当然,您还需要将有色实例关联到ColorGroups,以便在同一玩家拥有奖励时给予奖励。