Guest User

Untitled

a guest
Jan 21st, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.23 KB | None | 0 0
  1. // The "GroupAConnectFour" class.
  2. import java.awt.*;
  3. import hsa.Console;
  4.  
  5. public class GroupAConnectFour
  6. {
  7. static Console c; // The output console
  8. static Console cDebug;
  9. public static void main (String[] args)
  10. {
  11. c = new Console (10, 25);
  12. cDebug = new Console ();
  13. int[] [] board = new int [6] [7];
  14. int lRow = 0;
  15. int lColumn = 0;
  16. int win = 0;
  17. char nex = 'q';
  18. int curPos = 0;
  19. byte red = 1;
  20. boolean bro = false;
  21. int toPos = 0;
  22. boolean ai = false; //CHANGE DIS
  23. c.println ("Group A's: Connect Four");
  24. c.println ("A moves selector left, \n\tD right, \n\tand S drops.");
  25. c.println ("Press any key to continue");
  26.  
  27. c.getChar ();
  28.  
  29. clearBoard (board);
  30. c.setTextColor (Color.BLUE);
  31.  
  32. while (win == 0 && bro == false)
  33. {
  34.  
  35. refresh (c, board, curPos);
  36.  
  37. if ((ai == true && red == 1) || (ai == false))
  38. {
  39. nex = c.getChar ();
  40. if (nex == 'a')
  41. {
  42. toPos = curPos - 1;
  43. if (toPos < 0)
  44. {
  45. toPos = board.length;
  46. }
  47. }
  48. else if (nex == 'd')
  49. {
  50. toPos = curPos + 1;
  51. if (toPos >= board [0].length)
  52. {
  53. toPos = 0;
  54. }
  55. }
  56. }
  57. else if (ai == true)
  58. {
  59. toPos = aiMove (board, cDebug);
  60. nex = 's';
  61. }
  62.  
  63. while (toPos != curPos)
  64. {
  65. if (toPos < curPos)
  66. {
  67. curPos--;
  68.  
  69. }
  70. else if (toPos > curPos)
  71. {
  72. curPos++;
  73. }
  74. }
  75. if (nex == 's')
  76. {
  77. if (findRow (board, curPos) != -1)
  78. {
  79. lColumn = curPos;
  80. lRow = findRow (board, curPos);
  81. board [lRow] [lColumn] = (int) red;
  82. red = (byte) (-(red));
  83. }
  84. }
  85. //cDebug.println(lRow + "\t" + checkForWinner (board, lRow, lColumn));
  86. win = checkForWinner (board, lRow, lColumn);
  87. //c.println(win);
  88. bro = true;
  89.  
  90. for (int xx = 0 ; xx < board.length ; xx++)
  91. {
  92. for (int yy = 0 ; yy < board [0].length ; yy++)
  93. {
  94. if (board [xx] [yy] == 0)
  95. {
  96. bro = false;
  97. break;
  98. }
  99.  
  100. }
  101. if (bro == false)
  102. {
  103. break;
  104. }
  105. }
  106. }
  107. refresh (c, board, curPos);
  108. if (win == 1)
  109. {
  110. c.setTextColor (Color.RED);
  111. c.println ("Red Win!");
  112. }
  113. else if (win == -1)
  114. {
  115. c.setTextColor (Color.BLACK);
  116. c.println ("Black Win!");
  117. }
  118. else
  119. {
  120. c.setTextColor (Color.BLUE);
  121. c.println ("Noone Win!");
  122. }
  123. } // main method
  124.  
  125.  
  126. public static int checkForWinner (int[] [] board, int lastRow, int lastColumn)
  127. {
  128. int totalInColumn = 0;
  129. int totalInRow = 0;
  130. int winCheck = 0;
  131. //Check for columns
  132. for (int i = 0 ; i < 3 ; i++)
  133. {
  134. int columnCheck = board [i] [lastColumn];
  135. for (int p = 1 ; p < 4 ; p++)
  136. columnCheck = columnCheck + board [i + p] [lastColumn];
  137. if (Math.abs (columnCheck) == 4)
  138. {
  139. winCheck = (board [lastRow] [lastColumn]);
  140. return winCheck;
  141. }
  142. }
  143.  
  144. //Check for rows
  145. for (int i = 0 ; i < 4 ; i++)
  146. {
  147. int rowCheck = board [lastRow] [i];
  148. for (int p = 1 ; p < 4 ; p++)
  149. rowCheck = rowCheck + board [lastRow] [i + p];
  150. if (Math.abs (rowCheck) == 4)
  151. {
  152. winCheck = (board [lastRow] [lastColumn]);
  153. return winCheck;
  154. }
  155. }
  156. //Check for Diagonals
  157. for (int i = 1 ; i < 4 ; i++)
  158. {
  159. int diagonalCheck = board [lastRow] [lastColumn];
  160.  
  161. // Row + 1 Column + 1
  162. if (lastRow + i < 6 && lastColumn + i < 7)
  163. {
  164. for (int x = 1 ; x < 4 ; x++)
  165. {
  166. if (lastRow + x < 6 && lastColumn + x < 7)
  167. diagonalCheck = diagonalCheck + board [lastRow + x] [lastColumn + x];
  168. cDebug.clear ();
  169. cDebug.println ((lastRow + i) + " " + " " + (lastColumn + i) + " " + diagonalCheck);
  170. }
  171.  
  172. }
  173.  
  174. //Row -1 Column - 1
  175.  
  176. if (lastRow >= 3 && lastColumn >= 3)
  177. {
  178.  
  179.  
  180. for (int x = 1 ; x < 4 ; x++)
  181. {
  182. diagonalCheck = diagonalCheck + board [lastRow - x] [lastColumn - x];
  183. cDebug.clear ();
  184. cDebug.println ((lastRow + x) + " lastColumn + i=" + (lastColumn + x) + " diagonaCheck=" + diagonalCheck + " i=" + x);
  185. }
  186.  
  187.  
  188. }
  189.  
  190. //Row -1 Column + 1
  191.  
  192. if (lastRow - i > 0 && lastRow >= 3 && lastColumn + i < 7)
  193. {
  194. for (int x = 1 ; x < 4 ; x++)
  195. {
  196.  
  197. diagonalCheck = diagonalCheck + board [lastRow - x] [lastColumn + x];
  198. cDebug.clear ();
  199. cDebug.println ((lastRow) + " " + " " + (lastColumn) + " " + diagonalCheck + " minus 1 plus 1");
  200. }
  201. }
  202.  
  203. //Row + 1 Column - 1
  204.  
  205. if (lastColumn - i > 0 && lastColumn >= 3 && lastRow + i < 7)
  206. {
  207. for (int x = 1 ; x < 4 ; x++)
  208. {
  209.  
  210. diagonalCheck = diagonalCheck + board [lastRow + x] [lastColumn - x];
  211. cDebug.clear ();
  212. cDebug.println ((lastRow) + " " + " " + (lastColumn) + " " + diagonalCheck + " minus 1 plus 1");
  213. }
  214. }
  215.  
  216.  
  217.  
  218. if (Math.abs (diagonalCheck) == 4)
  219. {
  220. winCheck = (board [lastRow] [lastColumn]);
  221. return winCheck;
  222. }
  223. }
  224.  
  225. return 0;
  226. }
  227.  
  228.  
  229. private static int findRow (int[] [] board, int col)
  230. {
  231. int row = 0;
  232. for (int i = 0 ; i < board.length ; i++)
  233. {
  234. if (board [i] [col] == 0)
  235. {
  236. row = i;
  237. return row;
  238. }
  239. else
  240. {
  241. row = -1;
  242. }
  243. }
  244. return row;
  245. }
  246.  
  247.  
  248. public static void clearBoard (int[] [] board)
  249. {
  250. for (int xx = 0 ; xx < board.length ; xx++)
  251. {
  252. for (int yy = 0 ; yy < board [0].length ; yy++)
  253. {
  254. board [xx] [yy] = 0;
  255. }
  256. }
  257.  
  258. }
  259.  
  260.  
  261. public static void refresh (Console c, int[] [] board, int curPos)
  262. {
  263. c.clear ();
  264. for (int xx = 0 ; xx < board [0].length ; xx++)
  265. {
  266. c.setTextColor (Color.GREEN);
  267. if (xx == curPos)
  268. {
  269. c.setTextColor (Color.MAGENTA);
  270. c.print ("V");
  271. c.setTextColor (Color.GREEN);
  272. }
  273. else
  274. {
  275. c.print ("~");
  276. }
  277. c.print (" ");
  278. }
  279. c.println ();
  280. for (int xx = board.length - 1 ; xx >= 0 ; xx--)
  281. {
  282. for (int yy = 0 ; yy < board [0].length ; yy++)
  283. {
  284. if (board [xx] [yy] == 1)
  285. {
  286. c.setTextColor (Color.RED);
  287. c.print ("O" + " ");
  288. }
  289. else if (board [xx] [yy] == -1)
  290. {
  291. c.setTextColor (Color.BLACK);
  292. c.print ("X" + " ");
  293. }
  294. else
  295. {
  296. c.setTextColor (Color.BLUE);
  297. c.print ("." + " ");
  298. }
  299. }
  300. c.println ();
  301. }
  302. c.setTextColor (Color.BLUE);
  303. }
  304.  
  305.  
  306. public static int aiMove (int[] [] board, Console c)
  307. {
  308. int toMove = 0;
  309. int preMove = 0;
  310.  
  311. c.clear ();
  312. toMove = (int) (Math.random () * board.length);
  313. for (int xx = 0 ; xx < board [0].length ; xx++)
  314. {
  315. c.println (xx + "\t" + checkForWinner (board, findRow (board, xx), xx));
  316. if (checkForWinner (board, findRow (board, xx), xx) != 0)
  317. {
  318. toMove = xx;
  319.  
  320. break;
  321. }
  322. }
  323.  
  324. //toMove = (int)(Math.random()*board.length);
  325. return toMove;
  326. }
  327. } // GroupAConnectFour class
Add Comment
Please, Sign In to add comment