我有这个正在制作的棋盘游戏。当我掷一次骰子时,所有标记都会移动。如果他们降落在某些方格上,他们就会赢钱并有另一个回合。我可以加钱,但还不能让那个方格上的玩家再转一圈。我没有完全正确的代码,任何人都可以提供建议吗?谢谢。
/// <summary>
/// Rolls the two dice to determine
/// the number of squares to move forward; and
/// moves the player's location along the board; and
/// obtains the effect of landing on their final square.
/// Pre: dice are initialised
/// Post: the player is moved along the board and the effect
/// of the location the player landed on is applied.
/// </summary>
/// <param name="d1">first die</param>
/// <param name="d2">second die</param>
public void Play(Die d1, Die d2) {
int squares = 0;
//roll the two given dice
d1.Roll();
d2.Roll();
// get the values on the dice
// add the values togeather into an int
squares = d1.FaceValue + d2.FaceValue;
// move the player that many squares forward
Move(squares);
//after move, we need to check the final square to win/loose points
if (location.Number % 10 == 0)
location.LandOn(this);
else if (location.Number % 5 == 0)
location.LandOn(this);
任何帮助表示感谢。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace SharedGameClasses {
/// <summary>
/// A Ordinary square as well as being the superclass
/// for a Bad Investment Square and a Lottery Win Square
/// </summary>
public class Square {
// This square's number.
private int number;
public int Number {
get {
return number;
}
}
// The name of this square.
// This is the ‘type’ of square it is: Ordinary, Start, Finish, Lottery Win or
Bad Investment.
private string name;
public string Name {
get {
return name;
}
}
private Board board; // A reference to the board that contains this square.
public Square NextSquare {
get {
Debug.Assert (Number < Board.FINISH_SQUARE_NUMBER, "Number
<Board.FINISH_SQUARE_NUMBER",
"The Finish square is the last square.");
return board.Squares[Number + 1];
}
}
/// <summary>
/// Parameterless constructor.
/// Do not want the generic default constructor to be used
/// as there is no way to set the square's data.
/// This replaces the compiler's generic default constructor.
/// Pre: none
/// Post: ALWAYS throws an ArgumentException.
/// </summary>
/// <remarks>NOT TO BE USED!</remarks>
public Square() {
throw new ArgumentException("Parameterless constructor invalid.");
} // end Square constructor
/// <summary>
/// Constructor with initialisation parameters.
/// </summary>
/// <param name="name">name for this square</param>
/// <param name="number">number for this square</param>
public Square(Board board, int number, string name) {
this.board = board;
this.number = number;
this.name = name;
} // end Square constructor
/// <summary>
/// Performs the necessary action when a player lands on this type of square.
///
/// Landing on an ordinary square has
/// no consequential action to be performed at this time.
///
/// Pre: the player who lands on this square
/// Post: none.
/// </summary>
/// <param name="player">who landed on this square</param>
/// <remarks>Virtual method</remarks>
public virtual void LandOn(Player player) {
// This method is implemented within subclasses of Square
// perhaps something will be done in a future version of this game
// for an Ordinary square
} //end LandOn
/// <summary>
/// Check if a square is the Start square.
/// Pre: an initialised square location to check
/// Post: whether the supplied location is the Start square.
/// </summary>
/// <returns>
/// true if the square is the Start square,
/// false otherwise
/// </returns>
public bool IsStart() {
// check whether the location is the Start square.
return (number == Board.START_SQUARE_NUMBER);
} //end IsStart
/// <summary>
/// Checks if a square is the Finish square.
/// Pre: an initialised square location to check
/// Post: whether the supplied location is the Finish square.
/// </summary>
/// <returns>
/// true if the square is the Finish square,
/// false otherwise
/// </returns>
public bool IsFinish() {
return (number == Board.FINISH_SQUARE_NUMBER);
// check if the square is a Finish square.
} // end IsFinish
}
这是播放代码: using System; 使用 System.Collections.Generic;使用 System.Linq;使用 System.Text;使用 System.Drawing;使用 System.Diagnostics;
namespace SharedGameClasses {
/// <summary>
/// Models a player who is currently located on a particular square
/// with a certain amount of money.
/// </summary>
public class Player {
private const int INITIAL_AMOUNT = 100;
private Color randomColor = new Color();
// name of the player
private string name;
public string Name {
get {
return name;
}
set {
name = value;
}
}
// amount of money owned by player
private int money;
public int Money {
get {
return money;
}
set {
money = value;
}
}
// current square that player is on
private Square location;
public Square Location {
get {
return location;
}
set {
location = value;
}
}
// whether the player is a winner, in the current game.
private bool winner;
public bool Winner {
get {
return winner;
}
set {
winner = value;
}
}
private bool finished;
public bool Finished
{
get
{
return finished;
}
set
{
finished = value;
}
}
// PlayerTokenColour and PlayerTokenImage provide colours for the players'
tokens (or "pieces").
// These are not used in the Console version of the game, but will be important
in the GUI version.
private Brush playerTokenColour;
public Brush PlayerTokenColour
{
get { return playerTokenColour; }
set
{
playerTokenColour = value;
playerTokenImage = new Bitmap(1, 1);
using (Graphics g = Graphics.FromImage(PlayerTokenImage))
{
g.FillRectangle(playerTokenColour, 0, 0, 1, 1);
}
}
}
private Image playerTokenImage;
public Image PlayerTokenImage {
get {
return playerTokenImage;
}
}
/// <summary>
/// Generates a randomly-chosen colour.
/// </summary>
/// <returns>A random colour</returns>
public Color GenerateARandomColour(Random randomNumber)
{
const int HIGHEST_COLOUR_VALUE = 255;
int red = randomNumber.Next(0, HIGHEST_COLOUR_VALUE);
int green = randomNumber.Next(0, HIGHEST_COLOUR_VALUE);
int blue = randomNumber.Next(0, HIGHEST_COLOUR_VALUE);
Color randomColor = Color.FromArgb(red, green, blue);
return randomColor;
}
/// <summary>
/// Parameterless constructor.
/// Do not want the generic default constructor to be used
/// as there is no way to set the player's name.
/// This replaces the compiler's generic default constructor.
/// Pre: none
/// Post: ALWAYS throws an ArgumentException.
/// </summary>
/// <remarks>NOT TO BE USED!</remarks>
public Player() {
throw new ArgumentException("Parameterless constructor invalid.");
} // end Player constructor
/// <summary>
/// Constructor with initialising parameters.
/// Pre: name to be used for this player.
/// Post: initialised object with random color
/// </summary>
/// <param name="name">Name for this player</param>
public Player(String name, Square initialLocation) {
Random randomNumber = new Random();
this.name = name;
this.location = initialLocation;
this.randomColor = GenerateARandomColour(randomNumber);
this.PlayerTokenColour = new SolidBrush(this.randomColor);
} // end Player constructor
/// <summary>
/// Rolls the two dice to determine
/// the number of squares to move forward; and
/// moves the player's location along the board; and
/// obtains the effect of landing on their final square.
/// Pre: dice are initialised
/// Post: the player is moved along the board and the effect
/// of the location the player landed on is applied.
/// </summary>
/// <param name="d1">first die</param>
/// <param name="d2">second die</param>
public void Play(Die d1, Die d2) {
int squares = 0;
//roll the two given dice
d1.Roll();
d2.Roll();
// get the values on the dice
// add the values togeather into an int
squares = d1.FaceValue + d2.FaceValue;
// move the player that many squares forward
Move(squares);
//after move, we need to check the final square to win/loose points
if (location.Number % 10 == 0)
{
location.LandOn(this);
d1.Roll();
d2.Roll();
Move(squares);
}
else if (location.Number % 5 == 0)
location.LandOn(this);
} // end Play.
/// <summary>
/// Moves player the required number of squares forward
/// Pre: the number of squares to move forward
/// Post: the player is moved along the board.
/// NOTE: Refer to Square.cs regarding the NextSquare property.
/// </summary>
/// <param name="numberOfSquares">the number of squares to move</param>
private void Move(int numberOfSquares) {
//create a loop to call nextsquare numberOfSquares times
while (numberOfSquares > 0)
{
if (location.Number == Board.FINISH_SQUARE_NUMBER -1)
{
Finished = true;
return;
}
location = location.NextSquare;
numberOfSquares = numberOfSquares - 1;
}
} //end Move
/// <summary>
/// Increments the player's money by amount
/// Pre: amount > 0
/// Post: the player's money amount is increased.
/// </summary>
/// <param name="amount">increment amount</param>
public void Credit(int amount) {
Money = Money + amount;
} //end Credit
/// <summary>
/// Decreases the player's money by amount if
/// the player can afford it; otherwise,
/// sets the player's money to 0.
/// Pre: amount > 0
/// Post: player's money is decremented by amount if possible
/// but final amount is not below zero
/// </summary>
/// <param name="amount">decrement amount</param>
///
public void Debit(int amount)
{
if (money <= 25)
{
money = 0;
}
else
{
Money = Money - amount;
}
}
//end Debit
/// Sets the location of the player (mutator).
/// Pre: a square to be used as the player's current location.
/// Post: sets the player to a location on the board,
/// if the location was 'start', the player's amount was also
/// reset to the start amount.
public void ResetToLocation(Square square) {
if ( square.Number == Board.START_SQUARE_NUMBER){
Money = 100;
location = square;
}
else
{
location = square;
}
} //end ResetToStart
} //end class Player
}
board 设置为 6 行 7 列
if (location.Number % 10 == 0){
location.LandOn(this);
this.Play(d1, d2);}
else if (location.Number % 5 == 0)
location.LandOn(this);