Advertisement
Guest User

CONNECT4 REVISED

a guest
Aug 4th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.67 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class MyAgent extends Agent
  4. {
  5. Random r;
  6. /**
  7. * Constructs a new agent, giving it the game and telling it whether it is Red or Yellow.
  8. *
  9. * @param game The game the agent will be playing.
  10. * @param iAmRed false if the agent is Red, False if the agent is Yellow.
  11. */
  12. public MyAgent(Connect4Game game, boolean iAmRed)
  13. {
  14. super(game, iAmRed);
  15. r = new Random();
  16. }
  17.  
  18. /**
  19. * The move method is run every time it is this agent's turn in the game. You may assume that
  20. * when move() is called, the game has at least one open slot for a token, and the game has not
  21. * already been won.
  22. *
  23. * By the end of the move method, the agent should have placed one token into the game at some
  24. * point.
  25. *
  26. * After the move() method is called, the game engine will check to make sure the move was
  27. * valid. A move might be invalid if:
  28. * - No token was place into the game.
  29. * - More than one token was placed into the game.
  30. * - A previous token was removed from the game.
  31. * - The color of a previous token was changed.
  32. * - There are empty spaces below where the token was placed.
  33. *
  34. * If an invalid move is made, the game engine will announce it and the game will be ended.
  35. *
  36. */
  37. public void move()
  38.  
  39. {
  40. if (canWin(iAmRed) >= 0)
  41. {
  42. moveOnColumn(canWin(iAmRed));
  43. }
  44.  
  45. else
  46. {
  47. moveOnColumn(randomMove());
  48. }
  49. }
  50.  
  51.  
  52. /**
  53. * Drops a token into a particular column so that it will fall to the bottom of the column.
  54. * If the column is already full, nothing will change.
  55. *
  56. * @param columnNumber The column into which to drop the token.
  57. */
  58. public void moveOnColumn(int columnNumber)
  59. {
  60. int lowestEmptySlotIndex = getLowestEmptyIndex(myGame.getColumn(columnNumber)); // Find the top empty slot in the column
  61. // If the column is full, lowestEmptySlot will be -1
  62. if (lowestEmptySlotIndex >= -1) // if the column is not full
  63. {
  64. Connect4Slot lowestEmptySlot = myGame.getColumn(columnNumber).getSlot(lowestEmptySlotIndex); // get the slot in this column at this index
  65. if (iAmRed) // If the current agent is the Red player...
  66. {
  67. lowestEmptySlot.addRed(); // Place a red token into the empty slot
  68. }
  69. else // If the current agent is the Yellow player (not the Red player)...
  70. {
  71. lowestEmptySlot.addYellow(); // Place a yellow token into the empty slot
  72. }
  73. }
  74. }
  75.  
  76. /**
  77. * Returns the index of the top empty slot in a particular column.
  78. *
  79. * @param column The column to check.
  80. * @return the index of the top empty slot in a particular column; -1 if the column is already full.
  81. */
  82. public int getLowestEmptyIndex(Connect4Column column ) {
  83. int lowestEmptySlot = -1;
  84. for (int i = 0; i < column.getRowCount(); i++)
  85. {
  86. if (!column.getSlot(i).getIsFilled())
  87. {
  88. lowestEmptySlot = i;
  89. }
  90. }
  91. return lowestEmptySlot;
  92. }
  93.  
  94. /**
  95. * Returns a random valid move. If your agent doesn't know what to do, making a random move
  96. * can allow the game to go on anyway.
  97. *
  98. * @return a random valid move.
  99. */
  100. public int randomMove()
  101. {
  102. int i = r.nextInt(myGame.getColumnCount());
  103. while (getLowestEmptyIndex(myGame.getColumn(i)) == -1)
  104. {
  105. i = r.nextInt(myGame.getColumnCount());
  106. }
  107. return i;
  108. }
  109.  
  110. /**
  111. * Returns the column index, i, which contains the winning move for either red or yellow.
  112. * If no winning move is found, then it returns -1, allowing the agent to place the token randomly.
  113. *
  114. * @param isRed Checks to see if the token is red or yellow; true = red, false = yellow.
  115. * @return the column index, i, containing the winning move for either red or yellow OR
  116. * @return -1, if no winning move is found.
  117. */
  118. public int canWin(boolean isRed)
  119. {
  120. for (int i = 0; i < myGame.getColumnCount(); i++)
  121. {
  122. if (isHorizontalWinningColumn(i, isRed))
  123. {
  124. return i;
  125. }
  126. else if (isVerticalWinningColumn(i, isRed))
  127. {
  128. return i;
  129. }
  130. else if (isDiagonalWinningColumn(i, isRed))
  131. {
  132. return i;
  133. }
  134. }
  135. return -1;
  136. }
  137.  
  138. /**
  139. * Checks to see if there are three tokens of the same color in a row vertically. If the condition returns
  140. * true, it is called by the canWin method.
  141. *
  142. * @param columnIndex Passes column index in order to check every index for vertical wins.
  143. * @param isRed Checks to see if the token is red or yellow; true = red, false = yellow.
  144. * @return true if conditions are met, false if conditons are not met.
  145. */
  146. public boolean isVerticalWinningColumn(int columnIndex, boolean isRed)
  147. {
  148. int vertColSlot = 0;
  149. vertColSlot = getLowestEmptyIndex(myGame.getColumn(columnIndex));
  150. int pieces = 0;
  151. if (vertColSlot < 3 && vertColSlot > -1)
  152. {
  153. for (int i = 0; i < 6; i++)
  154. {
  155. if (myGame.getColumn(columnIndex).getSlot(i).getIsFilled() == true
  156. && myGame.getColumn(columnIndex).getSlot(i).getIsRed() == isRed)
  157. {
  158. pieces++;
  159. }
  160. }
  161. for (int i = 0; i > -1; i--)
  162. {
  163. if (myGame.getColumn(columnIndex).getSlot(i).getIsFilled() == true
  164. && myGame.getColumn(columnIndex).getSlot(i).getIsRed() == isRed)
  165. {
  166. pieces++;
  167. }
  168. }
  169. if (pieces > 2)
  170. {
  171. System.out.println("Winner Vertical: " + columnIndex);
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177.  
  178. /**
  179. * Checks to see if there are three tokens of the same color in a row horizontally. If the condition returns
  180. * true, it is called by the canWin method.
  181. *
  182. * @param columnIndex Passes column index in order to check every index for horizontal wins.
  183. * @param isRed Checks to see if the token is red or yellow; true = red, false = yellow.
  184. * @return true if conditions are met, false if conditons are not met.
  185. */
  186. public boolean isHorizontalWinningColumn(int columnIndex, boolean isRed)
  187. {
  188. int horizColSlot = 0;
  189. horizColSlot = getLowestEmptyIndex(myGame.getColumn(columnIndex));
  190. int pieces = 0;
  191. if (horizColSlot > -1)
  192. {
  193. for (int i = columnIndex; i < 7; i++)
  194. {
  195. if (myGame.getColumn(i).getSlot(horizColSlot).getIsFilled() == true
  196. && myGame.getColumn(i).getSlot(horizColSlot).getIsRed() == isRed)
  197. {
  198. pieces++;
  199. }
  200. }
  201. for (int i = columnIndex; i > -1; i--)
  202. {
  203. if (myGame.getColumn(i).getSlot(horizColSlot).getIsFilled() == true
  204. && myGame.getColumn(i).getSlot(horizColSlot).getIsRed() == isRed)
  205. {
  206. pieces++;
  207. }
  208. }
  209. if (pieces > 2)
  210. {
  211. System.out.println("Winner Horizontal: " + columnIndex);
  212. return true;
  213. }
  214. }
  215. return false;
  216. }
  217.  
  218. /**
  219. * Checks to see if there are three tokens of the same color diagonally (streching upwards). If the condition returns
  220. * true, it is called by the canWin method.
  221. *
  222. * @param columnIndex Passes column index in order to check every index for upward diagonal wins.
  223. * @param isRed Checks to see if the token is red or yellow; true = red, false = yellow.
  224. * @return true if conditions are met, false if conditons are not met.
  225. */
  226. public boolean isDiagonalWinningColumn(int columnIndex, boolean isRed)
  227. {
  228. int diagColSlot = 0;
  229. diagColSlot = getLowestEmptyIndex(myGame.getColumn(columnIndex));
  230. int pieces = 0;
  231. if (diagColSlot >= 0)
  232. {
  233. for (int i = 0; i < 4; i++)
  234. {
  235. if (columnIndex < 4 && diagColSlot > 2)
  236. {
  237. if (myGame.getColumn(columnIndex + i).getSlot(diagColSlot - i).getIsFilled() == true
  238. && myGame.getColumn(columnIndex + i).getSlot(diagColSlot - i).getIsRed() == isRed)
  239. {
  240. pieces++;
  241. }
  242. }
  243. if (columnIndex > 2 && diagColSlot < 3)
  244. {
  245. if (myGame.getColumn(columnIndex - i).getSlot(diagColSlot + i).getIsFilled() == true
  246. && myGame.getColumn(columnIndex - i).getSlot(diagColSlot + i).getIsRed() == isRed)
  247. {
  248. pieces++;
  249. }
  250. }
  251. if (columnIndex > 2 && diagColSlot > 2)
  252. {
  253. if (myGame.getColumn(columnIndex - i).getSlot(diagColSlot - i).getIsFilled() == true
  254. && myGame.getColumn(columnIndex - i).getSlot(diagColSlot - i).getIsRed() == isRed)
  255. {
  256. pieces++;
  257. }
  258. }
  259. if (columnIndex < 4 && diagColSlot < 3)
  260. {
  261. if (myGame.getColumn(columnIndex + i).getSlot(diagColSlot + i).getIsFilled() == true
  262. && myGame.getColumn(columnIndex + i).getSlot(diagColSlot + i).getIsRed() == isRed)
  263. {
  264. pieces++;
  265. }
  266. }
  267. }
  268. }
  269. if (pieces > 2)
  270. {
  271. System.out.println("Diagonal Winner");
  272. return true;
  273. }
  274. return false;
  275. }
  276.  
  277. /**
  278. * Returns the name of this agent.
  279. *
  280. * @return the agent's name
  281. */
  282. public String getName()
  283. {
  284. return "My Agent";
  285. }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement