Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.70 KB | None | 0 0
  1.  
  2. /**
  3. * TicTacToe is a 1D tic tac toe game.
  4. *
  5. * @author Jussi Pohjolainen
  6. * @version 2014.0930
  7. * @since 1.0
  8. */
  9. class TicTacToe {
  10.  
  11. /**
  12. * The 1D board.
  13. */
  14. private static char[] board;
  15.  
  16. /**
  17. * The amount needed for won.
  18. */
  19. private final static int AMOUNT_OF_CHARS_IN_A_ROW = 3;
  20.  
  21. /**
  22. * Max size for the board.
  23. */
  24. private static final int MAX_SIZE = 2000;
  25.  
  26. /**
  27. * Game iteration.
  28. */
  29. private static boolean gameIsOn = true;
  30.  
  31. /**
  32. * The character for player x.
  33. */
  34. private static final char PLAYER_X = 'X';
  35.  
  36. /**
  37. * The character for player 0.
  38. */
  39. private static final char PLAYER_0 = '0';
  40.  
  41. /**
  42. * String to be used when player x wins.
  43. */
  44. private static final String PLAYER_X_WON = "Player " + PLAYER_X + " won!";
  45.  
  46. /**
  47. * String to be used when player 0 wins.
  48. */
  49. private static final String PLAYER_0_WON = "Player " + PLAYER_0 + " won!";
  50.  
  51. /**
  52. * String to be used when asking board size.
  53. */
  54. private static final String GIVE_SIZE = "Give board size";
  55.  
  56. /**
  57. * String to be used when asking position of the player.
  58. */
  59. private static final String POSITION_FOR_PLAYER = "\nGive pos for player";
  60.  
  61. /**
  62. * String to be used when informing about tie.
  63. */
  64. private static final String TIE = "It's a tie!";
  65.  
  66. /**
  67. * Asks in a console a board size from the user.
  68. *
  69. * Size must be between 3 - MAX_SIZE, otherwise
  70. * it asks again. BUG: crashes if user gives non-numeric value
  71. *
  72. * @param input where user input is read.
  73. * @return board size between 3 - MAX_SIZE
  74. */
  75. private static int giveSize(Scanner input) {
  76.  
  77. int size = -1;
  78.  
  79. while (!(size >= 3 && size <= MAX_SIZE)) {
  80.  
  81. System.out.println(GIVE_SIZE);
  82. size = input.nextInt();
  83. }
  84.  
  85. return size;
  86. }
  87.  
  88. /**
  89. * Initializes the board with ' ' chars with given size.
  90. *
  91. * @param size the max size of the board.
  92. */
  93. private static void initializeBoard(int size) {
  94.  
  95. board = new char[size];
  96.  
  97. for (int i = 0; i < board.length; i++) {
  98. board[i] = ' ';
  99. }
  100. }
  101.  
  102. /**
  103. * Prints the board to console.
  104. *
  105. * @param board the board to be used.
  106. */
  107. private static void printBoard(char[] board) {
  108.  
  109. for (int i = 0; i < board.length; i++) {
  110. System.out.print("[" + board[i] + "]");
  111. }
  112. }
  113.  
  114. /**
  115. * Asks from user where to put the player on the board.
  116. *
  117. * Method returns if player won the game by returning either
  118. * true (won) or false (not won)
  119. *
  120. * @param player character of the player.
  121. * @param input where user input is read.
  122. * @return if player won the game (true), otherwise false.
  123. */
  124. private static boolean ask(char player, Scanner input) {
  125.  
  126. // if there is still room in the board
  127. if (availablePlaceInBoard()) {
  128.  
  129. // Ask the place from the user
  130. int place = askPlace(player, input);
  131.  
  132. // Put the player on the board
  133. board[place] = player;
  134.  
  135. // Check if the player won.
  136. return checkVictory(player);
  137. } else {
  138. // if not, return false
  139. return false;
  140. }
  141. }
  142.  
  143. /**
  144. * Checks if there is still room in the board.
  145. *
  146. * @return true if available places are left, otherwise false.
  147. */
  148. private static boolean availablePlaceInBoard() {
  149.  
  150. for (char element : board) {
  151.  
  152. if (element == ' ') {
  153. return true;
  154. }
  155. }
  156.  
  157. return false;
  158. }
  159.  
  160. /**
  161. * Asks in a console board place for the player.
  162. *
  163. * Asking is done again if user gives invalid input; place is
  164. * taken, place is off-board. BUG: crash if user gives non-numeric
  165. * value.
  166. *
  167. * @param player character of the player.
  168. * @param input where user input is read.
  169. * @return the index of the board place.
  170. */
  171. private static int askPlace(char player, Scanner input) {
  172.  
  173. int place = -1;
  174.  
  175. // if place is off-board or taken, ask again.
  176. while (!(place >= 0 && place < board.length && board[place] == ' ')) {
  177.  
  178. System.out.println(POSITION_FOR_PLAYER + ": " + player);
  179. place = input.nextInt();
  180. }
  181.  
  182. return place;
  183. }
  184.  
  185. /**
  186. * Checks if user won.
  187. *
  188. * @param player the character of the player
  189. * @return if player won (true) or lost (false)
  190. */
  191. private static boolean checkVictory(char player) {
  192. int numberOfPlayers = 0;
  193.  
  194. for (int i = 0; i < board.length; i++) {
  195.  
  196. if (board[i] == player) {
  197. // found a player in the board
  198.  
  199. // how many players in a row?
  200. numberOfPlayers = checkHowMany(player, i);
  201.  
  202. // Add the found number to i
  203. i = i + numberOfPlayers;
  204. }
  205.  
  206. // If we found enough players in a row, return true.
  207. if (numberOfPlayers == AMOUNT_OF_CHARS_IN_A_ROW) {
  208. return true;
  209. }
  210. }
  211.  
  212. return false;
  213. }
  214.  
  215. /**
  216. * Checks how many players are in a row.
  217. *
  218. * @param player character of the player.
  219. * @param index where to start to look for the players.
  220. * @return the number of players in a row.
  221. */
  222. private static int checkHowMany(char player, int index) {
  223.  
  224. int numberOfPlayers = 0;
  225.  
  226. while (index < board.length && board[index] == player) {
  227. numberOfPlayers++;
  228. index++;
  229. }
  230.  
  231. return numberOfPlayers;
  232. }
  233.  
  234. /**
  235. * Starts the app.
  236. *
  237. * @param args command line arguments, not used.
  238. */
  239. public static void main(String[] args) {
  240. Scanner input = new Scanner(System.in);
  241.  
  242. // Ask board size
  243. int size = giveSize(input);
  244.  
  245. // Initialize the board with ' ' chars
  246. initializeBoard(size);
  247.  
  248. // print the board to console
  249. printBoard(board);
  250.  
  251. // Is board full?
  252. boolean boardFull = false;
  253.  
  254. // Did X win?
  255. boolean winX = false;
  256.  
  257. // Did 0 win?
  258. boolean winZero = false;
  259.  
  260. // Loop while gameIsOn
  261. while (gameIsOn) {
  262.  
  263. // Ask the position for the player X
  264. winX = ask(PLAYER_X, input);
  265.  
  266. // Print the board
  267. printBoard(board);
  268.  
  269. // Was the board full?
  270. boardFull = !availablePlaceInBoard();
  271.  
  272. // If not, then ask 0
  273. if (!boardFull) {
  274.  
  275. // Ask the posiiton for the player 0
  276. winZero = ask(PLAYER_0, input);
  277.  
  278. // print the board.
  279. printBoard(board);
  280. }
  281.  
  282. // If X won and 0 not.
  283. if (winX && !winZero) {
  284. System.out.println("\n" + PLAYER_X_WON);
  285. }
  286.  
  287. // If 0 won and X not
  288. if (winZero && !winX) {
  289. System.out.println("\n" + PLAYER_0_WON);
  290. }
  291.  
  292. // if somebody won or board was full, end the game
  293. gameIsOn = !(winX || winZero || boardFull);
  294. }
  295.  
  296. // If nobody won or both won.
  297. if ((!winX && !winZero) || (winX && winZero)) {
  298. System.out.println("\n" + TIE);
  299. }
  300. }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement