Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * TicTacToe is a 1D tic tac toe game.
- *
- * @author Jussi Pohjolainen
- * @version 2014.0930
- * @since 1.0
- */
- class TicTacToe {
- /**
- * The 1D board.
- */
- private static char[] board;
- /**
- * The amount needed for won.
- */
- private final static int AMOUNT_OF_CHARS_IN_A_ROW = 3;
- /**
- * Max size for the board.
- */
- private static final int MAX_SIZE = 2000;
- /**
- * Game iteration.
- */
- private static boolean gameIsOn = true;
- /**
- * The character for player x.
- */
- private static final char PLAYER_X = 'X';
- /**
- * The character for player 0.
- */
- private static final char PLAYER_0 = '0';
- /**
- * String to be used when player x wins.
- */
- private static final String PLAYER_X_WON = "Player " + PLAYER_X + " won!";
- /**
- * String to be used when player 0 wins.
- */
- private static final String PLAYER_0_WON = "Player " + PLAYER_0 + " won!";
- /**
- * String to be used when asking board size.
- */
- private static final String GIVE_SIZE = "Give board size";
- /**
- * String to be used when asking position of the player.
- */
- private static final String POSITION_FOR_PLAYER = "\nGive pos for player";
- /**
- * String to be used when informing about tie.
- */
- private static final String TIE = "It's a tie!";
- /**
- * Asks in a console a board size from the user.
- *
- * Size must be between 3 - MAX_SIZE, otherwise
- * it asks again. BUG: crashes if user gives non-numeric value
- *
- * @param input where user input is read.
- * @return board size between 3 - MAX_SIZE
- */
- private static int giveSize(Scanner input) {
- int size = -1;
- while (!(size >= 3 && size <= MAX_SIZE)) {
- System.out.println(GIVE_SIZE);
- size = input.nextInt();
- }
- return size;
- }
- /**
- * Initializes the board with ' ' chars with given size.
- *
- * @param size the max size of the board.
- */
- private static void initializeBoard(int size) {
- board = new char[size];
- for (int i = 0; i < board.length; i++) {
- board[i] = ' ';
- }
- }
- /**
- * Prints the board to console.
- *
- * @param board the board to be used.
- */
- private static void printBoard(char[] board) {
- for (int i = 0; i < board.length; i++) {
- System.out.print("[" + board[i] + "]");
- }
- }
- /**
- * Asks from user where to put the player on the board.
- *
- * Method returns if player won the game by returning either
- * true (won) or false (not won)
- *
- * @param player character of the player.
- * @param input where user input is read.
- * @return if player won the game (true), otherwise false.
- */
- private static boolean ask(char player, Scanner input) {
- // if there is still room in the board
- if (availablePlaceInBoard()) {
- // Ask the place from the user
- int place = askPlace(player, input);
- // Put the player on the board
- board[place] = player;
- // Check if the player won.
- return checkVictory(player);
- } else {
- // if not, return false
- return false;
- }
- }
- /**
- * Checks if there is still room in the board.
- *
- * @return true if available places are left, otherwise false.
- */
- private static boolean availablePlaceInBoard() {
- for (char element : board) {
- if (element == ' ') {
- return true;
- }
- }
- return false;
- }
- /**
- * Asks in a console board place for the player.
- *
- * Asking is done again if user gives invalid input; place is
- * taken, place is off-board. BUG: crash if user gives non-numeric
- * value.
- *
- * @param player character of the player.
- * @param input where user input is read.
- * @return the index of the board place.
- */
- private static int askPlace(char player, Scanner input) {
- int place = -1;
- // if place is off-board or taken, ask again.
- while (!(place >= 0 && place < board.length && board[place] == ' ')) {
- System.out.println(POSITION_FOR_PLAYER + ": " + player);
- place = input.nextInt();
- }
- return place;
- }
- /**
- * Checks if user won.
- *
- * @param player the character of the player
- * @return if player won (true) or lost (false)
- */
- private static boolean checkVictory(char player) {
- int numberOfPlayers = 0;
- for (int i = 0; i < board.length; i++) {
- if (board[i] == player) {
- // found a player in the board
- // how many players in a row?
- numberOfPlayers = checkHowMany(player, i);
- // Add the found number to i
- i = i + numberOfPlayers;
- }
- // If we found enough players in a row, return true.
- if (numberOfPlayers == AMOUNT_OF_CHARS_IN_A_ROW) {
- return true;
- }
- }
- return false;
- }
- /**
- * Checks how many players are in a row.
- *
- * @param player character of the player.
- * @param index where to start to look for the players.
- * @return the number of players in a row.
- */
- private static int checkHowMany(char player, int index) {
- int numberOfPlayers = 0;
- while (index < board.length && board[index] == player) {
- numberOfPlayers++;
- index++;
- }
- return numberOfPlayers;
- }
- /**
- * Starts the app.
- *
- * @param args command line arguments, not used.
- */
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- // Ask board size
- int size = giveSize(input);
- // Initialize the board with ' ' chars
- initializeBoard(size);
- // print the board to console
- printBoard(board);
- // Is board full?
- boolean boardFull = false;
- // Did X win?
- boolean winX = false;
- // Did 0 win?
- boolean winZero = false;
- // Loop while gameIsOn
- while (gameIsOn) {
- // Ask the position for the player X
- winX = ask(PLAYER_X, input);
- // Print the board
- printBoard(board);
- // Was the board full?
- boardFull = !availablePlaceInBoard();
- // If not, then ask 0
- if (!boardFull) {
- // Ask the posiiton for the player 0
- winZero = ask(PLAYER_0, input);
- // print the board.
- printBoard(board);
- }
- // If X won and 0 not.
- if (winX && !winZero) {
- System.out.println("\n" + PLAYER_X_WON);
- }
- // If 0 won and X not
- if (winZero && !winX) {
- System.out.println("\n" + PLAYER_0_WON);
- }
- // if somebody won or board was full, end the game
- gameIsOn = !(winX || winZero || boardFull);
- }
- // If nobody won or both won.
- if ((!winX && !winZero) || (winX && winZero)) {
- System.out.println("\n" + TIE);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement