Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Connect_4
  8. {
  9. class State
  10. {
  11. /// <summary>
  12. /// Enum containing the possible types of fields present on the board
  13. /// </summary>
  14. public enum FIELD
  15. {
  16. EMPTY = 0,
  17. MIN = 1, //human
  18. MAX = 2 //computer
  19. }
  20.  
  21. public static readonly int WIDTH = 7;
  22. public static readonly int HEIGHT = 6;
  23.  
  24. private FIELD[,] board { get; set; }
  25. private List<State> children;
  26.  
  27. /// <summary>
  28. /// Field for storing the alpha-beta value corresponding to this State
  29. /// </summary>
  30. public int AlphaBetaValue { get; set; }
  31.  
  32. /// <summary>
  33. /// Creates a new State with an empty board and without children
  34. /// </summary>
  35. public State()
  36. {
  37. this.Board = new FIELD[HEIGHT, WIDTH];
  38. InitializeBoard();
  39. this.Children = new List<State>();
  40. }
  41.  
  42. /// <summary>
  43. /// Creates a new State with a given board and without children
  44. /// </summary>
  45. /// <param name="board"> The Field[,] board that correspunds to the state</param>
  46. public State(FIELD[,] board)
  47. {
  48. this.Board = board;
  49. this.Children = new List<State>();
  50. }
  51.  
  52. /// <summary>
  53. /// Copy constructor
  54. /// </summary>
  55. /// <param name="input"> The State object that has to be copied </param>
  56. public State(State input)
  57. {
  58. this.Board = State.CopyBoard(input.Board);
  59. this.Children = new List<State>();
  60. foreach (var item in input.Children)
  61. {
  62. this.Children.Add(item);
  63. }
  64. }
  65.  
  66. /// <summary>
  67. /// Property for getting the list of children belonging to a state
  68. /// </summary>
  69. public List<State> Children
  70. {
  71. get { return this.children; }
  72. private set { this.children = value; }
  73. }
  74.  
  75. /// <summary>
  76. /// Property for getting and setting the board belonging to a state
  77. /// </summary>
  78. public FIELD[,] Board
  79. {
  80. get { return this.board; }
  81. set { this.board = value; }
  82. }
  83.  
  84. /// <summary>
  85. /// Static method that returns a copy of the board given as parameter
  86. /// </summary>
  87. /// <param name="input"> The board that has to be copied </param>
  88. /// <returns> The copy of the input </returns>
  89. public static FIELD[,] CopyBoard(FIELD[,] input)
  90. {
  91. FIELD[,] copy = new FIELD[input.GetLength(0), input.GetLength(1)];
  92.  
  93. for (int i = 0; i < input.GetLength(0); i++)
  94. {
  95. for (int j = 0; j < input.GetLength(1); j++)
  96. {
  97. copy[i, j] = input[i, j];
  98. }
  99. }
  100.  
  101. return copy;
  102. }
  103.  
  104. /// <summary>
  105. /// Method to check if a disc can be added to a column of the board
  106. /// </summary>
  107. /// <param name="col"> The column in which we want to add </param>
  108. /// <returns> True if a disc can be added </returns>
  109. public bool CanAddToBoard(int col)
  110. {
  111. if (col < Board.GetLength(1))
  112. {
  113. for (int i = Board.GetLength(0) - 1; i >= 0; --i)
  114. {
  115. if (Board[i, col] == FIELD.EMPTY)
  116. {
  117. return true;
  118. }
  119. }
  120. }
  121. return false;
  122. }
  123.  
  124. /// <summary>
  125. /// Adds a disc to the specified column
  126. /// </summary>
  127. /// <param name="col"> The column where the disc will be added </param>
  128. /// <param name="value"> The disc (FIELD) type that will be added </param>
  129. public void AddToBoard(int col, FIELD value)
  130. {
  131. //Console.WriteLine($"Adding to column {col+1}");
  132. if (col < Board.GetLength(1))
  133. {
  134. for (int i = Board.GetLength(0) - 1; i >= 0; --i)
  135. {
  136. if (Board[i, col] == FIELD.EMPTY)
  137. {
  138. Board[i, col] = value;
  139. return;
  140. }
  141. }
  142. }
  143. else
  144. {
  145. throw new Exception("Column out of range");
  146. }
  147. }
  148.  
  149. /// <summary>
  150. /// Prints the current board
  151. /// </summary>
  152. public void PrintBoard()
  153. {
  154. for (int i = 0; i < Board.GetLength(0); ++i)
  155. {
  156. for (int j = 0; j < Board.GetLength(1); ++j)
  157. {
  158. switch (Board[i, j])
  159. {
  160. case FIELD.EMPTY:
  161. Console.Write("- ");
  162. break;
  163. case FIELD.MIN:
  164. Console.Write("O ");
  165. break;
  166. case FIELD.MAX:
  167. Console.Write("X ");
  168. break;
  169. default:
  170. break;
  171. }
  172. Console.Write(" ");
  173. }
  174. Console.WriteLine();
  175. }
  176. Console.WriteLine();
  177. }
  178.  
  179. private void InitializeBoard()
  180. {
  181. for (int i = 0; i < Board.GetLength(0); ++i)
  182. {
  183. for (int j = 0; j < Board.GetLength(1); j++)
  184. {
  185. Board[i, j] = FIELD.EMPTY;
  186. }
  187. }
  188. }
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement