Advertisement
Guest User

Untitled

a guest
Sep 19th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.20 KB | None | 0 0
  1. package eu.kristinaeapost.tripstrapstrull;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  * Trips-Traps-Trull is a strategy computer-user game.
  7.  * @author Eapost
  8.  *
  9.  */
  10.  
  11. public class TicTacToe {
  12.    
  13.     /**
  14.      * USER_PEG - user's turn.
  15.      */
  16.     public static final int USER_PEG = 1;
  17.    
  18.     /**
  19.      * COMPUTER_PEG - computer's turn.
  20.      */
  21.     public static final int COMPUTER_PEG = -1;
  22.    
  23.     /**
  24.      * WINNING_ROWS - all possible winning combinations.
  25.      */
  26.     public static final int[][] WINNING_ROWS = {
  27.         {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}
  28.     };
  29.  
  30.     /**
  31.      * Standard main method.
  32.      * @param args
  33.      */
  34.     public static void main(String[] args) {
  35.         int[] grid = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0};
  36.         Scanner scanner = new Scanner(System.in);
  37.        
  38.         int winner = 0;
  39.         while (winner == 0) {
  40.             int userInput;
  41.             do {
  42.                 userInput = readInput(scanner);
  43.             } while(!makeUserMove(userInput, grid));
  44.             drawBoard(grid);
  45.            
  46.             if (checkWinner(grid, USER_PEG)) {
  47.                 System.out.println("You are the winner!");
  48.                 System.exit(1);
  49.             }
  50.            
  51.             if (checkDraw(grid)) {
  52.                 System.out.println("Draw!");
  53.                 System.exit(1);
  54.             }
  55.            
  56.             int computerMove = makeMove(grid);
  57.             grid[computerMove] = COMPUTER_PEG;
  58.             drawBoard(grid);
  59.            
  60.             if (checkWinner(grid, COMPUTER_PEG)) {
  61.                 System.out.println("Artificial intelligence!");
  62.                 System.exit(1);
  63.             }
  64.         }  
  65.     }  
  66.    
  67.     /**
  68.      * makeUserMove - function to make user's move and to control the input.
  69.      * @param userInput - the input of the user.
  70.      * @param grid - current state of the board.
  71.      * @return - return true if the input is right,
  72.      * return false if the cell is taken or the input value is wrong.
  73.      */
  74.     public static boolean makeUserMove(int userInput, int[] grid) {
  75.         if (userInput > 0 && userInput < 10) {
  76.             if (grid[userInput - 1] == 0) {
  77.                 grid[userInput - 1] = USER_PEG;
  78.                 return true;
  79.             }
  80.             System.out.println("Already taken, choose another!");
  81.             return false;
  82.         }
  83.         System.out.println("Wrong value entered!");
  84.         return false;
  85.     }
  86.    
  87.     /**
  88.      * readInput - function to read a number from the input and return it.
  89.      * @param scanner - object to read the input.
  90.      * @return number read from the input.
  91.      */
  92.     public static int readInput(Scanner scanner) {
  93.         int userInput = 0;
  94.         if (scanner.hasNextInt()) {
  95.             userInput = scanner.nextInt();
  96.         }
  97.         scanner.nextLine();
  98.         return userInput;
  99.     }
  100.    
  101.     /**
  102.      * drawBoard - function to draw/print the board.
  103.      * @param grid - current state of the board.
  104.      */
  105.     public static void drawBoard(int[] grid) {
  106.         final int cellsInRow = 3;
  107.         // looping through all the cells
  108.         for (int i = 0; i < grid.length; i++) {
  109.             if (i % cellsInRow == 0) {
  110.                 // moving to a new line
  111.                 System.out.println(" ");
  112.                 System.out.println("+---+---+---+");
  113.                 System.out.print("| ");
  114.             }
  115.            
  116.             if (grid[i] == 0) {
  117.                 System.out.print(" ");
  118.             } else if (grid[i] == -1) {
  119.                 System.out.print("O");
  120.             } else {
  121.                 System.out.print("X");
  122.             }
  123.             System.out.print(" | ");
  124.         }
  125.         // closing lower boundary
  126.         System.out.println(" ");
  127.         System.out.println("+---+---+---+");   
  128.     }
  129.    
  130.     /**
  131.      * checkWin - wrapper function to check who's the winner.
  132.      * @param grid - current state of the board.
  133.      * @return 1 if the winner is user, -1 if computer wins,
  134.      * 0 if there is no winner.
  135.      */
  136.     public static int checkWin(int[] grid) {
  137.         if (checkWinner(grid, USER_PEG)) {
  138.             return 1;
  139.         }
  140.         if (checkWinner(grid, COMPUTER_PEG)) {
  141.             return -1;
  142.         }
  143.         return 0;
  144.     }
  145.    
  146.     /**
  147.      * checkWinner - check if specified player has three cells in a row.
  148.      * @param grid - current state of the board.
  149.      * @param player - specifies which player (computer or user).
  150.      * @return true if specified player has won (has 3 cells in a row).
  151.      */
  152.     public static boolean checkWinner(int[] grid, int player) {
  153.         int winningCombination = player * 3;
  154.        
  155.         return ((grid[0] + grid[1] + grid[2] == winningCombination)
  156.                 || (grid[3] + grid[4] + grid[5] == winningCombination)
  157.                 || (grid[6] + grid[7] + grid[8] == winningCombination)
  158.                 || (grid[0] + grid[4] + grid[8] == winningCombination)
  159.                 || (grid[2] + grid[4] + grid[6] == winningCombination)
  160.                 || (grid[0] + grid[3] + grid[6] == winningCombination)
  161.                 || (grid[1] + grid[4] + grid[7] == winningCombination)
  162.                 || (grid[2] + grid[5] + grid[8] == winningCombination));
  163.        
  164.     }
  165.    
  166.     /**
  167.      * sumRow - function to check the sum of the cells in a row.
  168.      * @param grid - current state of the board.
  169.      * @param row - a row of the board.
  170.      * @return the sum of the cells.
  171.      */
  172.     public static int sumRow(int[] grid, int[] row) {
  173.         int sum = 0;
  174.         for (int cell: row) {
  175.             sum += grid[cell];
  176.         }
  177.         return sum;
  178.     }
  179.    
  180.     /**
  181.      * makeMove - function to make moves.
  182.      * @param grid - current state of the board.
  183.      * @return computer's move.
  184.      */
  185.     public static int makeMove(int[] grid) {
  186.         int computerMove = -1;
  187.        
  188.         computerMove = findWinningCell(grid, COMPUTER_PEG);
  189.         if (computerMove == -1) {
  190.             computerMove = findWinningCell(grid, USER_PEG);
  191.         }
  192.         if (computerMove == -1) {
  193.             computerMove = findRandomCell(grid);
  194.         }
  195.        
  196.         return computerMove;
  197.     }
  198.    
  199.     /**
  200.      * Function to check if specified player has a potential to put 3 cells in a row.
  201.      * @param grid - current state of the board.
  202.      * @param player - specifies which player (computer or user).
  203.      * @return the index of the missing cell.
  204.      */
  205.     public static int findWinningCell(int[] grid, int player) {
  206.         for (int[] cells : WINNING_ROWS) {
  207.             int cellSum = sumRow(grid, cells);
  208.             if (cellSum == player * 2) {
  209.                 for (int cell: cells) {
  210.                     if (grid[cell] == 0) {
  211.                         return cell;
  212.                     }
  213.                 }
  214.             }
  215.         }
  216.         return -1; 
  217.     }
  218.    
  219.     /**
  220.      * findRandomCell - function for choosing computer moves.
  221.      * Preferably centre first, then corners and other remaining cells.
  222.      * @param grid - current state of the board.
  223.      * @return computer move.
  224.      */
  225.     public static int findRandomCell(int[] grid) {
  226.         if (grid[4] == 0) {
  227.             return 4;
  228.         }
  229.        
  230.         int computerMove = -1;
  231.         computerMove = findAvailableSpot(grid, new int[] {0, 2, 6, 8});
  232.  
  233.        
  234.         if (computerMove == -1) {
  235.             computerMove = findAvailableSpot(grid, new int[] {1, 4, 5, 7});        
  236.  
  237.         }
  238.         return computerMove;
  239.     }  
  240.    
  241.     /**
  242.      * Function to find a random available cell from given cell indexes.
  243.      * @param grid - current state of the board.
  244.      * @param cells - all the cells of the board.
  245.      * @return the index of the available cell.
  246.      */
  247.     public static int findAvailableSpot(int[] grid, int[] cells) {
  248.         int index = (int) Math.round(Math.random() * (cells.length - 1));
  249.        
  250.         for (int i = 0; i < cells.length; i++) {
  251.             if (grid[cells[index]] == 0) {
  252.                 return cells[index];
  253.             }
  254.             if (++index >= cells.length) {
  255.                 index = 0;
  256.             }
  257.         }
  258.         return -1;
  259.     }
  260.    
  261.     /**
  262.      * checkDraw - function to check the draw.
  263.      * @param grid - current state of the board.
  264.      * @return false if there are empty cells, so it's not a draw.
  265.      * Return true if all cells are taken and there is no winner,
  266.      * so it's a draw.
  267.      */
  268.     public static boolean checkDraw(int[] grid) {
  269.         for (int cell: grid) {
  270.             if (cell == 0) {
  271.                 return false;
  272.             }
  273.         }
  274.         return true;
  275.     }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement