Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. import java.awt.List;
  2. import java.util.ArrayList;
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. class Main {
  7. public static ArrayList<Integer> bPotentialColumn = new ArrayList<Integer>();
  8. public static ArrayList<Integer> iPotentialColumn = new ArrayList<Integer>();
  9. public static ArrayList<Integer> nPotentialColumn = new ArrayList<Integer>();
  10. public static ArrayList<Integer> gPotentialColumn = new ArrayList<Integer>();
  11. public static ArrayList<Integer> oPotentialColumn = new ArrayList<Integer>();
  12.  
  13. public static ArrayList<Integer> bColumn = new ArrayList<Integer>();
  14. public static ArrayList<Integer> iColumn = new ArrayList<Integer>();
  15. public static ArrayList<Integer> nColumn = new ArrayList<Integer>();
  16. public static ArrayList<Integer> gColumn = new ArrayList<Integer>();
  17. public static ArrayList<Integer> oColumn = new ArrayList<Integer>();
  18.  
  19. public static void main(String[] args) {
  20. Scanner reader = new Scanner(System.in); // Reading from System.in
  21.  
  22. PopulatePotentialBingoNumbers();
  23.  
  24. boolean inGame = true;
  25. boolean hasStartedNewGame = false;
  26. while (inGame) {
  27.  
  28. if (hasStartedNewGame == false) {
  29. if (StartNewGame(reader)) {
  30. StartNewGame();
  31. System.out.println("Here is your card!");
  32. System.out.println(CardDescriptionWithHeader());
  33. hasStartedNewGame = true;
  34. } else {
  35. inGame = false;
  36. }
  37. }
  38.  
  39. if (AskThenCallNewNumber(reader)) {
  40. System.out.println("WINNER");
  41. hasStartedNewGame = false;
  42. }
  43. }
  44. }
  45.  
  46. private static boolean AskThenCallNewNumber(Scanner reader)
  47. {
  48. System.out.println("Enter the called number: ");
  49. int response = reader.nextInt(); // Scans the next token of the input as an int.
  50. CallNumber(response);
  51.  
  52. System.out.println(CardDescriptionWithHeader());
  53.  
  54. return CheckForWin();
  55. }
  56.  
  57. private static boolean StartNewGame(Scanner reader)
  58. {
  59. boolean hasNoResponse = false;
  60. while (hasNoResponse == false) {
  61. System.out.println("Start New Game? (Y: YES, N: NO) ");
  62. String response = reader.next(); // Scans the next token of the input as an string.
  63. if (response.equals("Y")) {
  64. return true;
  65. }
  66.  
  67. if (response.equals("N")) {
  68. return false;
  69. }
  70.  
  71. System.out.println("Please enter either Y for YES or N for NO");
  72. }
  73.  
  74. return false;
  75. }
  76.  
  77. private static void StartNewGame()
  78. {
  79. PopulateColumnWithRandomNumbers();
  80. }
  81.  
  82. private static void CallNumber(Integer calledNumber)
  83. {
  84. if (bColumn.contains(calledNumber)) {
  85. int index = bColumn.indexOf(calledNumber);
  86. bColumn.add(index, 0);
  87. }
  88.  
  89. if (iColumn.contains(calledNumber)) {
  90. int index = iColumn.indexOf(calledNumber);
  91. iColumn.add(index, 0);
  92. }
  93.  
  94. if (nColumn.contains(calledNumber)) {
  95. int index = nColumn.indexOf(calledNumber);
  96. nColumn.add(index, 0);
  97. }
  98.  
  99. if (gColumn.contains(calledNumber)) {
  100. int index = gColumn.indexOf(calledNumber);
  101. gColumn.add(index, 0);
  102. }
  103.  
  104. if (oColumn.contains(calledNumber)) {
  105. int index = oColumn.indexOf(calledNumber);
  106. oColumn.add(index, 0);
  107. }
  108. }
  109.  
  110. private static boolean CheckForWin()
  111. {
  112. if (CheckForWinInColumn(bColumn)) {
  113. return true;
  114. }
  115.  
  116. if (CheckForWinInColumn(iColumn)) {
  117. return true;
  118. }
  119.  
  120. if (CheckForWinInColumn(nColumn)) {
  121. return true;
  122. }
  123.  
  124. if (CheckForWinInColumn(gColumn)) {
  125. return true;
  126. }
  127.  
  128. if (CheckForWinInColumn(oColumn)) {
  129. return true;
  130. }
  131.  
  132. for (int i = 0; i < 5; i += 1) {
  133. if (CheckForWinInRow(i)) {
  134. return true;
  135. }
  136. }
  137.  
  138. if (bColumn.get(0) == 0 &&
  139. iColumn.get(1) == 0 &&
  140. nColumn.get(2) == 0 &&
  141. gColumn.get(3) == 0 &&
  142. oColumn.get(4) == 0) {
  143.  
  144. return true;
  145. }
  146.  
  147. if (bColumn.get(4) == 0 &&
  148. iColumn.get(3) == 0 &&
  149. nColumn.get(2) == 0 &&
  150. gColumn.get(1) == 0 &&
  151. oColumn.get(0) == 0) {
  152.  
  153. return true;
  154. }
  155.  
  156. return false;
  157. }
  158.  
  159. private static boolean CheckForWinInRow(int column)
  160. {
  161. if (bColumn.indexOf(column) != 0) {
  162. return false;
  163. }
  164.  
  165. if (iColumn.indexOf(column) != 0) {
  166. return false;
  167. }
  168.  
  169. if (nColumn.indexOf(column) != 0) {
  170. return false;
  171. }
  172.  
  173. if (gColumn.indexOf(column) != 0) {
  174. return false;
  175. }
  176.  
  177. if (oColumn.indexOf(column) != 0) {
  178. return false;
  179. }
  180. return true;
  181. }
  182.  
  183. private static boolean CheckForWinInColumn(ArrayList<Integer> column)
  184. {
  185. for (int i = 0; i < column.size(); i += 1) {
  186. if (column.indexOf(i) != 0) {
  187. return false;
  188. }
  189. }
  190. return true;
  191. }
  192.  
  193. private static void PopulatePotentialBingoNumbers()
  194. {
  195. bPotentialColumn.clear();
  196. bPotentialColumn.addAll(PotentialBingoNumbersForColumn(1));
  197.  
  198. iPotentialColumn.clear();
  199. iPotentialColumn.addAll(PotentialBingoNumbersForColumn(16));
  200.  
  201. nPotentialColumn.clear();
  202. nPotentialColumn.addAll(PotentialBingoNumbersForColumn(31));
  203.  
  204. gPotentialColumn.clear();
  205. gPotentialColumn.addAll(PotentialBingoNumbersForColumn(46));
  206.  
  207. oPotentialColumn.clear();
  208. oPotentialColumn.addAll(PotentialBingoNumbersForColumn(61));
  209. }
  210.  
  211. private static ArrayList<Integer> PotentialBingoNumbersForColumn(int starting)
  212. {
  213. ArrayList<Integer> column = new ArrayList<>();
  214. for (int i = 0; i < 15; i += 1) {
  215. column.add(starting + i);
  216. }
  217. return column;
  218. }
  219.  
  220. public static void PopulateColumnWithRandomNumbers()
  221. {
  222. bColumn.clear();
  223. bColumn.addAll(RandomNumbersFrom(bPotentialColumn, 5));
  224.  
  225. iColumn.clear();
  226. iColumn.addAll(RandomNumbersFrom(iPotentialColumn, 5));
  227.  
  228. nColumn.clear();
  229. nColumn.addAll(RandomNumbersFrom(nPotentialColumn, 5));
  230.  
  231. gColumn.clear();
  232. gColumn.addAll(RandomNumbersFrom(gPotentialColumn, 5));
  233.  
  234. oColumn.clear();
  235. oColumn.addAll(RandomNumbersFrom(oPotentialColumn, 5));
  236. }
  237.  
  238. private static ArrayList<Integer> RandomNumbersFrom(ArrayList<Integer> potential, Integer count)
  239. {
  240. ArrayList<Integer> column = new ArrayList<>();
  241. column.addAll(potential);
  242. while(column.size() > count) {
  243. column.remove(new Random().nextInt(column.size()));
  244. }
  245. return column;
  246. }
  247.  
  248. private static String CardDescriptionWithHeader()
  249. {
  250. String card = "B I N G O" + "\n";
  251.  
  252. for (int i = 0; i < 5; i += 1) {
  253. card += RowForIndex(i) + "\n";
  254. }
  255. return card;
  256. }
  257.  
  258. private static String RowForIndex(int index)
  259. {
  260. return bColumn.toArray()[index] + " " +
  261. iColumn.toArray()[index] + " " +
  262. nColumn.toArray()[index] + " " +
  263. gColumn.toArray()[index] + " " +
  264. oColumn.toArray()[index];
  265. }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement