Guest User

Untitled

a guest
Jan 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 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.  
  9. public static void main (String[] args)
  10. {
  11. c = new Console (10, 25);
  12.  
  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.  
  21.  
  22. c.println("Group A's: Connect Four");
  23. c.println("A moves selector left, \n\tD right, \n\tand S drops.");
  24. c.println("Press any key to continue");
  25.  
  26. c.getChar();
  27.  
  28. clearBoard (board);
  29. c.setTextColor(Color.BLUE);
  30.  
  31. while (win == 0)
  32. {
  33.  
  34. refresh(c,board, curPos);
  35. nex = c.getChar ();
  36. if (nex == 'a')
  37. {
  38. curPos--;
  39. if (curPos < 0)
  40. {
  41. curPos = board.length;
  42. }
  43. }
  44. else if (nex == 'd')
  45. {
  46. curPos++;
  47. if (curPos >= board[0].length)
  48. {
  49. curPos = 0;
  50. }
  51. }
  52. else if (nex == 's')
  53. {
  54. if (findRow (board, curPos) != -1)
  55. {
  56. lColumn = curPos;
  57. lRow = findRow (board, curPos);
  58. board [lRow] [lColumn] = (int) red;
  59. red = (byte) (-(red));
  60. }
  61. }
  62. win = checkForWinner (board, lRow, lColumn);
  63. //c.println(win);
  64. }
  65. refresh(c,board, curPos);
  66. if (win == 1)
  67. {
  68. c.setTextColor(Color.RED);
  69. c.println("Red Win!");
  70. } else
  71. {
  72. c.setTextColor(Color.BLACK);
  73. c.println("Black Win!");
  74. }
  75. } // main method
  76.  
  77.  
  78. public static int checkForWinner (int[] [] board, int lastRow, int lastColumn)
  79. {
  80. int totalInColumn = 0;
  81. int totalInRow = 0;
  82. int winCheck = 0;
  83. //Check for columns
  84. for (int i = 0 ; i < 3 ; i++)
  85. {
  86. int columnCheck = board [i] [lastColumn];
  87. for (int p = 1 ; p < 4 ; p++)
  88. columnCheck = columnCheck + board [i + p] [lastColumn];
  89. if (Math.abs (columnCheck) == 4)
  90. {
  91. winCheck = (board [lastRow] [lastColumn]);
  92. return winCheck;
  93. }
  94. }
  95.  
  96. //Check for rows
  97. for (int i = 0 ; i < 3 ; i++)
  98. {
  99. int rowCheck = board [lastRow] [i];
  100. for (int p = 1 ; p < 4 ; p++)
  101. rowCheck = rowCheck + board [lastRow] [i + p];
  102. if (Math.abs (rowCheck) == 4)
  103. {
  104. winCheck = (board [lastRow] [lastColumn]);
  105. return winCheck;
  106. }
  107. }
  108.  
  109. return 0;
  110. }
  111.  
  112.  
  113. private static int findRow (int[][] board, int col)
  114. {
  115. int row = 0;
  116. for (int i = 0 ; i < board.length ; i++)
  117. {
  118. if (board [i] [col] == 0)
  119. {
  120. row = i;
  121. return row;
  122. }
  123. else
  124. {
  125. row = -1;
  126. }
  127. }
  128. return row;
  129. }
  130.  
  131. public static void clearBoard(int[][]board)
  132. {
  133. for (int xx = 0; xx < board.length; xx++)
  134. {
  135. for (int yy = 0; yy < board[0].length; yy++)
  136. {
  137. board[xx][yy] = 0;
  138. }
  139. }
  140.  
  141. }
  142.  
  143. public static void refresh(Console c, int[][] board, int curPos)
  144. {
  145. c.clear ();
  146. for (int xx = 0 ; xx < board[0].length ; xx++)
  147. {
  148. if (xx == curPos)
  149. {
  150. c.setTextColor(Color.MAGENTA);
  151. c.print ("V");
  152. c.setTextColor(Color.BLUE);
  153. }
  154. else
  155. {
  156. c.print ("~");
  157. }
  158. c.print (" ");
  159. }
  160. c.println ();
  161. for (int xx = board.length - 1 ; xx >= 0 ; xx--)
  162. {
  163. for (int yy = 0 ; yy < board [0].length ; yy++)
  164. {
  165. if (board [xx] [yy] == 1)
  166. {
  167. c.setTextColor(Color.RED);
  168. c.print("O"+ " ");
  169. } else if (board [xx] [yy] == -1)
  170. {
  171. c.setTextColor(Color.BLACK);
  172. c.print("X"+ " ");
  173. } else
  174. {
  175. c.setTextColor(Color.BLUE);
  176. c.print("."+ " ");
  177. }
  178. }
  179. c.println ();
  180. }
  181. c.setTextColor(Color.BLUE);
  182. }
  183. } // GroupAConnectFour class
Add Comment
Please, Sign In to add comment