Advertisement
Guest User

Untitled

a guest
Oct 19th, 2014
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.Diagnostics;
  7.  
  8.  
  9. namespace SharedGameClasses {
  10. /// <summary>
  11. /// Models a player who is currently located on a particular square
  12. /// with a certain amount of money.
  13. /// </summary>
  14. public class Player {
  15.  
  16. private const int INITIAL_AMOUNT = 100;
  17.  
  18. // name of the player
  19. private string name;
  20. public string Name {
  21. get {
  22. return name;
  23. }
  24. set {
  25. name = value;
  26. }
  27. }
  28.  
  29. // amount of money owned by player
  30. private int money;
  31. public int Money {
  32. get {
  33. return money;
  34. }
  35. set {
  36. money = value;
  37. }
  38. }
  39.  
  40. // current square that player is on
  41. private Square location;
  42. public Square Location {
  43. get {
  44. return location;
  45. }
  46. set {
  47. location = value;
  48.  
  49. }
  50. }
  51.  
  52. // whether the player is a winner, in the current game.
  53. private bool winner;
  54. public bool Winner {
  55. get {
  56. return winner;
  57. }
  58. set {
  59. winner = value;
  60. }
  61. }
  62.  
  63. // PlayerTokenColour and PlayerTokenImage provide colours for the players' tokens (or "pieces").
  64. private Brush playerTokenColour;
  65. public Brush PlayerTokenColour {
  66. get {
  67. return playerTokenColour;
  68. }
  69. set {
  70. playerTokenColour = value;
  71. playerTokenImage = new Bitmap(1, 1);
  72. using (Graphics g = Graphics.FromImage(PlayerTokenImage)) {
  73. g.FillRectangle(playerTokenColour, 0, 0, 1, 1);
  74. }
  75. }
  76. }
  77.  
  78. private Image playerTokenImage;
  79. public Image PlayerTokenImage {
  80. get {
  81. return playerTokenImage;
  82. }
  83. }
  84.  
  85. /// <summary>
  86. /// Parameterless constructor.
  87. /// Do not want the generic default constructor to be used
  88. /// as there is no way to set the player's name.
  89. /// This replaces the compiler's generic default constructor.
  90. /// Pre: none
  91. /// Post: ALWAYS throws an ArgumentException.
  92. /// </summary>
  93. /// <remarks>NOT TO BE USED!</remarks>
  94. public Player() {
  95. throw new ArgumentException("Parameterless constructor invalid.");
  96. } // end Player constructor
  97.  
  98. /// <summary>
  99. /// Constructor with initialising parameters.
  100. /// Pre: name to be used for this player.
  101. /// Post: this player object has all attributes initialised
  102. /// </summary>
  103. /// <param name="name">Name for this player</param>
  104. public Player(String name, Square initialLocation, Brush playerToken){
  105.  
  106. Name = name;
  107. location = initialLocation;
  108. Money = INITIAL_AMOUNT;
  109. PlayerTokenColour = playerToken;
  110.  
  111.  
  112. //######################### Code needs to be added here ##########################################
  113.  
  114. } // end Player constructor
  115.  
  116. /// <summary>
  117. /// Rolls the two dice to determine
  118. /// the number of squares to move forward; and
  119. /// moves the player's location along the board; and
  120. /// obtains the effect of landing on their final square.
  121. /// Pre: dice are initialised
  122. /// Post: the player is moved along the board and the effect
  123. /// of the location the player landed on is applied.
  124. /// </summary>
  125. /// <param name="d1">first die</param>
  126. /// <param name="d2">second die</param>
  127. public void Play(Die d1, Die d2){
  128.  
  129. var roll1 = d1.Roll();
  130. var roll2 = d2.Roll();
  131.  
  132. int numofSquares = roll1 + roll2;
  133.  
  134. Move(numofSquares);
  135.  
  136.  
  137. //######################### Code needs to be added here ##########################################
  138.  
  139. } // end Play.
  140.  
  141. /// <summary>
  142. /// Moves player the required number of squares forward
  143. /// Pre: the number of squares to move forward
  144. /// Post: the player is moved along the board.
  145. /// NOTE: Refer to Square.cs regarding the NextSquare property.
  146. /// </summary>
  147. /// <param name="numberOfSquares">the number of squares to move</param>
  148. private void Move(int numberOfSquares) {
  149.  
  150.  
  151.  
  152. //######################### Code needs to be added here ##########################################3
  153.  
  154. } //end Move
  155.  
  156. /// <summary>
  157. /// Increments the player's money by amount
  158. /// Pre: amount > 0
  159. /// Post: the player's money amount is increased.
  160. /// </summary>
  161. /// <param name="amount">increment amount</param>
  162. public void Credit(int amount) {
  163.  
  164. Money = Money + amount;
  165.  
  166. } //end Credit
  167.  
  168.  
  169. /// <summary>
  170. /// Decreases the player's money by amount if
  171. /// the player can afford it; otherwise,
  172. /// sets the player's money to 0.
  173. /// Pre: amount > 0
  174. /// Post: player's money is decremented by amount if possible
  175. /// but final amount is not below zero
  176. /// </summary>
  177. /// <param name="amount">decrement amount</param>
  178. public void Debit(int amount){
  179.  
  180. const int loseamount = 25;
  181.  
  182. if (Money >= 25){
  183. Money = Money - loseamount;
  184. } else if (Money < 25){
  185. Money = 0;
  186. }
  187.  
  188. //######################### Code needs to be added here ##########################################3
  189. } //end Debit
  190.  
  191.  
  192. } //end class Player
  193. }
  194.  
  195. using System;
  196. using System.Collections.Generic;
  197. using System.Linq;
  198. using System.Text;
  199. using System.Diagnostics;
  200. using System.Drawing;
  201.  
  202. using System.ComponentModel; // for BindingList.
  203.  
  204. namespace SharedGameClasses {
  205. /// <summary>
  206. /// Plays a game called Hare and the Tortoise
  207. /// </summary>
  208. public static class HareAndTortoiseGame {
  209.  
  210. // Minimum and maximum players per game
  211. private const int MIN_PLAYERS = 2;
  212. public const int MAX_PLAYERS = 6;
  213.  
  214. // The dice
  215. private static Die die1 = new Die(), die2 = new Die();
  216.  
  217. // A BindingList is like an array that can grow and shrink.
  218. //
  219. // Using a BindingList will make it easier to implement the GUI with a DataGridView
  220. private static BindingList<Player> players = new BindingList<Player>();
  221. public static BindingList<Player> Players {
  222. get {
  223. return players;
  224. }
  225. }
  226.  
  227.  
  228. private static int numberOfPlayers = 6; // The value 6 is purely to avoid compiler errors.
  229.  
  230. public static int NumberOfPlayers {
  231. get {
  232. return numberOfPlayers;
  233. }
  234. set {
  235. numberOfPlayers = value;
  236. }
  237. }
  238.  
  239. // Is the current game finished?
  240. private static bool finished = false;
  241. public static bool Finished {
  242. get {
  243. return finished;
  244. }
  245. }
  246.  
  247. /// Some default player names.
  248. ///
  249. /// These are purely for testing purposes and when initialising the players at the start
  250. ///
  251. /// These values are intended to be read-only. I.e. the program code should never update this array.
  252. private static string[] defaultNames = { "One", "Two", "Three", "Four", "Five", "Six" };
  253.  
  254. // Some colours for the players' tokens (or "pieces").
  255. private static Brush[] playerTokenColours = new Brush[MAX_PLAYERS] { Brushes.Black, Brushes.Red,
  256. Brushes.Gold, Brushes.GreenYellow,
  257. Brushes.Fuchsia, Brushes.White };
  258.  
  259.  
  260.  
  261. /// <summary>
  262. /// Initialises each of the players and adds them to the players BindingList.
  263. /// This method is called only once, when the game first startsfrom HareAndTortoiseForm.
  264. ///
  265. /// Pre: none.
  266. /// Post: all the game's players are initialised.
  267. /// </summary>
  268. public static void InitialiseAllThePlayers(){
  269.  
  270.  
  271. //Player Playerone = new Player(defaultNames[1], Board.Squares[0]);
  272. int i = 0;
  273.  
  274. while (i < NumberOfPlayers){
  275. players.Add(new Player(defaultNames[i], Board.Squares[0], playerTokenColours[i]));
  276. i++;
  277. }
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285. //##################### Code needs to be added here. ############################################################
  286.  
  287. } // end InitialiseAllThePlayers
  288.  
  289.  
  290. /// <summary>
  291. /// Puts all the players on the Start square.
  292. /// Pre: none.
  293. /// Post: the game is reset as though it is being played for the first time.
  294. /// </summary>
  295. public static void SetPlayersAtTheStart() {
  296.  
  297.  
  298. //##################### Code needs to be added here. ############################################################
  299.  
  300.  
  301. } // end SetPlayersAtTheStart
  302.  
  303.  
  304. public static void PlayOneRound(){
  305.  
  306. InitialiseAllThePlayers();
  307.  
  308. }
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315. } //end class HareAndTortoiseGame
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement