Advertisement
TheFastFish

tic tac toe

Feb 3rd, 2016
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.90 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /*
  4.  * A console version of tic-tac-toe.
  5.  * KNOWN ISSUES:
  6.  *  -Instead of deciding who is X and who is O,
  7.  *   the logic decides whether X goes first or O goes first.
  8.  *   This issue was noticed too late to fix, so I left it. :P
  9.  *   Player 1 is always X and player 2 is always O, regardless
  10.  *   of who goes first.
  11.  */
  12. public class TicTacToe {
  13.     //class members
  14.     static Scanner input = new Scanner(System.in);
  15.     static String player1, player2;
  16.     static char[][] board = new char[3][3];
  17.     static boolean p1Plays;
  18.     static boolean p1Wins, p2Wins;
  19.    
  20.     public static void main(String[] args) {
  21.         int x, y;
  22.         int guessP1, guessP2;
  23.        
  24.         //player1 setup
  25.         System.out.print("X, enter your name: ");
  26.         player1 = input.next();
  27.        
  28.         do {
  29.             System.out.printf("%s, enter a number between 0 and 9: ", player1);
  30.             guessP1 = input.nextInt();
  31.             if(guessP1 > 9 || guessP1 < 0)
  32.                 System.out.println("Your guess is out of range!");
  33.         } while(guessP1 > 9 || guessP1 < 0);
  34.         System.out.println("");
  35.        
  36.         //player2 setup
  37.         //they get checks and things to be sure they don't pick same as P1
  38.         do {
  39.             System.out.print("O, enter your name: ");
  40.             player2 = input.next();
  41.             if(player1.equals(player2))
  42.                 System.out.println("That name is taken.");
  43.         } while(player1.equals(player2));
  44.        
  45.         do {
  46.             System.out.printf("%s, enter a number between 0 and 9: ", player2);
  47.             guessP2 = input.nextInt();
  48.             if(guessP1 == guessP2)
  49.                 System.out.printf("%s already picked %d!\n", player1, guessP2);
  50.             if(guessP2 > 9 || guessP2 < 0)
  51.                 System.out.println("Your guess is out of range!");
  52.         } while(guessP1 == guessP2 || guessP2 > 9 || guessP2 < 0);
  53.         System.out.println("");
  54.        
  55.         //decision time!
  56.         p1Plays = decideIfP1GoesFirst(guessP1, guessP2);
  57.        
  58.         if(p1Plays)
  59.             System.out.printf("%s goes first.", player1);
  60.         else
  61.             System.out.printf("%s goes first.", player2);
  62.        
  63.         //start game!
  64.         for(y = 0; y < 3; y++) {
  65.             for(x = 0; x < 3; x++)
  66.                 board[y][x] = ' ';
  67.         }
  68.        
  69.         gameLoop();
  70.     }
  71.    
  72.     //choose whether or not P1 goes first
  73.     public static boolean decideIfP1GoesFirst(int guess1, int guess2) {
  74.         int rando = (int)(Math.random() * 10.0);
  75.        
  76.         System.out.printf("Random number: %d\n", rando);
  77.         return Math.abs(guess1 - rando) < Math.abs(guess2 - rando);
  78.     }
  79.    
  80.     //print the game board
  81.     public static void printBoard() {
  82.         int x, y;
  83.         System.out.printf("%s is X.\n%s is O.\n\n", player1, player2);
  84.         System.out.println("  A B C");
  85.         for(y = 0; y < 3; y++) {
  86.             System.out.printf("%d ", y + 1);
  87.             for(x = 0; x < 3; x++) {
  88.                 System.out.printf("%1c", board[y][x]);
  89.                 if(x != 2) System.out.print("|");
  90.             }
  91.             if(y != 2) System.out.print("\n  -+-+-");
  92.             System.out.println("");
  93.         }
  94.     }
  95.    
  96.     //the core game loop
  97.     public static void gameLoop() {
  98.         boolean isValidMove;
  99.         String move;
  100.        
  101.         while(!p1Wins && !p2Wins) {
  102.             //clear screen; this is the only way that seems to work
  103.             System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  104.             //print board
  105.             printBoard();
  106.            
  107.            //is the board full?
  108.             if(boardIsFull()) {
  109.                 System.out.println("\nBoard is full!\nGame over.");
  110.                 break;
  111.             }
  112.            
  113.             //ask for move
  114.             isValidMove = false;
  115.             do {
  116.                 //input prompt
  117.                 System.out.println();
  118.                 if(p1Plays)
  119.                     System.out.printf("%s, where are you going to play? (ex. A1) ", player1);
  120.                 else
  121.                     System.out.printf("%s, where are you going to play? (ex. A1) ", player2);
  122.            
  123.                 move = input.next().toUpperCase();
  124.                
  125.                 //set the board!
  126.                 if(setBoard(move)) isValidMove = true;
  127.                
  128.             } while(!isValidMove);
  129.            
  130.             //loop back, switch players
  131.             p1Plays = !p1Plays;
  132.         }
  133.     }
  134.    
  135.     //test if the board is full
  136.     public static boolean boardIsFull() {
  137.         int y, x;
  138.         for(y= 0; y < 3; y++) {
  139.             for(x = 0; x < 3; x++){
  140.                 if(board[y][x] == ' ') return false;
  141.             }
  142.         }
  143.         return true;
  144.     }
  145.    
  146.     //make the move, but ONLY if it is valid
  147.     public static boolean setBoard(String move) {
  148.         int y, x;
  149.         //this set of switches tests input integrity
  150.         //check row
  151.         switch(move.charAt(0)) {
  152.         case 'A':
  153.             x = 0;
  154.             break;
  155.         case 'B':
  156.             x = 1;
  157.             break;
  158.         case 'C':
  159.             x = 2;
  160.             break;
  161.         default:
  162.             System.out.println("Input is invalid!");
  163.             return false;
  164.         }
  165.         //check column
  166.         switch(move.charAt(1)) {
  167.         case '1':
  168.             y = 0;
  169.             break;
  170.         case '2':
  171.             y = 1;
  172.             break;
  173.         case '3':
  174.             y = 2;
  175.             break;
  176.         default:
  177.             System.out.println("Input is invalid!");
  178.             return false;
  179.         }
  180.        
  181.         //see if the space is taken
  182.         if(board[y][x] != ' ') {
  183.             System.out.println("This space is taken!");
  184.             return false;
  185.         }
  186.        
  187.         //place in board
  188.         if(p1Plays) board[y][x] = 'X';
  189.         else board[y][x] = 'O';
  190.        
  191.         return true;
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement