Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eu.kristinaeapost.tripstrapstrull;
- import java.util.Scanner;
- /**
- * Trips-Traps-Trull is a strategy computer-user game.
- * @author Eapost
- *
- */
- public class TicTacToe {
- /**
- * USER_PEG - user's turn.
- */
- public static final int USER_PEG = 1;
- /**
- * COMPUTER_PEG - computer's turn.
- */
- public static final int COMPUTER_PEG = -1;
- /**
- * WINNING_ROWS - all possible winning combinations.
- */
- public static final int[][] WINNING_ROWS = {
- {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}
- };
- /**
- * Standard main method.
- * @param args
- */
- public static void main(String[] args) {
- int[] grid = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0};
- Scanner scanner = new Scanner(System.in);
- int winner = 0;
- while (winner == 0) {
- int userInput;
- do {
- userInput = readInput(scanner);
- } while(!makeUserMove(userInput, grid));
- drawBoard(grid);
- if (checkWinner(grid, USER_PEG)) {
- System.out.println("You are the winner!");
- System.exit(1);
- }
- if (checkDraw(grid)) {
- System.out.println("Draw!");
- System.exit(1);
- }
- int computerMove = makeMove(grid);
- grid[computerMove] = COMPUTER_PEG;
- drawBoard(grid);
- if (checkWinner(grid, COMPUTER_PEG)) {
- System.out.println("Artificial intelligence!");
- System.exit(1);
- }
- }
- }
- /**
- * makeUserMove - function to make user's move and to control the input.
- * @param userInput - the input of the user.
- * @param grid - current state of the board.
- * @return - return true if the input is right,
- * return false if the cell is taken or the input value is wrong.
- */
- public static boolean makeUserMove(int userInput, int[] grid) {
- if (userInput > 0 && userInput < 10) {
- if (grid[userInput - 1] == 0) {
- grid[userInput - 1] = USER_PEG;
- return true;
- }
- System.out.println("Already taken, choose another!");
- return false;
- }
- System.out.println("Wrong value entered!");
- return false;
- }
- /**
- * readInput - function to read a number from the input and return it.
- * @param scanner - object to read the input.
- * @return number read from the input.
- */
- public static int readInput(Scanner scanner) {
- int userInput = 0;
- if (scanner.hasNextInt()) {
- userInput = scanner.nextInt();
- }
- scanner.nextLine();
- return userInput;
- }
- /**
- * drawBoard - function to draw/print the board.
- * @param grid - current state of the board.
- */
- public static void drawBoard(int[] grid) {
- final int cellsInRow = 3;
- // looping through all the cells
- for (int i = 0; i < grid.length; i++) {
- if (i % cellsInRow == 0) {
- // moving to a new line
- System.out.println(" ");
- System.out.println("+---+---+---+");
- System.out.print("| ");
- }
- if (grid[i] == 0) {
- System.out.print(" ");
- } else if (grid[i] == -1) {
- System.out.print("O");
- } else {
- System.out.print("X");
- }
- System.out.print(" | ");
- }
- // closing lower boundary
- System.out.println(" ");
- System.out.println("+---+---+---+");
- }
- /**
- * checkWin - wrapper function to check who's the winner.
- * @param grid - current state of the board.
- * @return 1 if the winner is user, -1 if computer wins,
- * 0 if there is no winner.
- */
- public static int checkWin(int[] grid) {
- if (checkWinner(grid, USER_PEG)) {
- return 1;
- }
- if (checkWinner(grid, COMPUTER_PEG)) {
- return -1;
- }
- return 0;
- }
- /**
- * checkWinner - check if specified player has three cells in a row.
- * @param grid - current state of the board.
- * @param player - specifies which player (computer or user).
- * @return true if specified player has won (has 3 cells in a row).
- */
- public static boolean checkWinner(int[] grid, int player) {
- int winningCombination = player * 3;
- return ((grid[0] + grid[1] + grid[2] == winningCombination)
- || (grid[3] + grid[4] + grid[5] == winningCombination)
- || (grid[6] + grid[7] + grid[8] == winningCombination)
- || (grid[0] + grid[4] + grid[8] == winningCombination)
- || (grid[2] + grid[4] + grid[6] == winningCombination)
- || (grid[0] + grid[3] + grid[6] == winningCombination)
- || (grid[1] + grid[4] + grid[7] == winningCombination)
- || (grid[2] + grid[5] + grid[8] == winningCombination));
- }
- /**
- * sumRow - function to check the sum of the cells in a row.
- * @param grid - current state of the board.
- * @param row - a row of the board.
- * @return the sum of the cells.
- */
- public static int sumRow(int[] grid, int[] row) {
- int sum = 0;
- for (int cell: row) {
- sum += grid[cell];
- }
- return sum;
- }
- /**
- * makeMove - function to make moves.
- * @param grid - current state of the board.
- * @return computer's move.
- */
- public static int makeMove(int[] grid) {
- int computerMove = -1;
- computerMove = findWinningCell(grid, COMPUTER_PEG);
- if (computerMove == -1) {
- computerMove = findWinningCell(grid, USER_PEG);
- }
- if (computerMove == -1) {
- computerMove = findRandomCell(grid);
- }
- return computerMove;
- }
- /**
- * Function to check if specified player has a potential to put 3 cells in a row.
- * @param grid - current state of the board.
- * @param player - specifies which player (computer or user).
- * @return the index of the missing cell.
- */
- public static int findWinningCell(int[] grid, int player) {
- for (int[] cells : WINNING_ROWS) {
- int cellSum = sumRow(grid, cells);
- if (cellSum == player * 2) {
- for (int cell: cells) {
- if (grid[cell] == 0) {
- return cell;
- }
- }
- }
- }
- return -1;
- }
- /**
- * findRandomCell - function for choosing computer moves.
- * Preferably centre first, then corners and other remaining cells.
- * @param grid - current state of the board.
- * @return computer move.
- */
- public static int findRandomCell(int[] grid) {
- if (grid[4] == 0) {
- return 4;
- }
- int computerMove = -1;
- computerMove = findAvailableSpot(grid, new int[] {0, 2, 6, 8});
- if (computerMove == -1) {
- computerMove = findAvailableSpot(grid, new int[] {1, 4, 5, 7});
- }
- return computerMove;
- }
- /**
- * Function to find a random available cell from given cell indexes.
- * @param grid - current state of the board.
- * @param cells - all the cells of the board.
- * @return the index of the available cell.
- */
- public static int findAvailableSpot(int[] grid, int[] cells) {
- int index = (int) Math.round(Math.random() * (cells.length - 1));
- for (int i = 0; i < cells.length; i++) {
- if (grid[cells[index]] == 0) {
- return cells[index];
- }
- if (++index >= cells.length) {
- index = 0;
- }
- }
- return -1;
- }
- /**
- * checkDraw - function to check the draw.
- * @param grid - current state of the board.
- * @return false if there are empty cells, so it's not a draw.
- * Return true if all cells are taken and there is no winner,
- * so it's a draw.
- */
- public static boolean checkDraw(int[] grid) {
- for (int cell: grid) {
- if (cell == 0) {
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement