Advertisement
letsdoitjava

Tic Tac Toe

Jul 16th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. // Tic Tac Toe program that plays the game with user
  2. // Renee Waggoner
  3. // July 26, 2019
  4. // Special Requirements: None
  5. package tic_tac_toe;
  6. import java.util.*;
  7. class TicTacToe
  8. {
  9.     char ttt[][] = new char[3][3];
  10.     static final char player1 = 'O';
  11.     static final char player2 = 'X';
  12.     Scanner scan =new Scanner(System.in);
  13.  
  14.     String playerID(char player)
  15.     {
  16.         // Prints the identity of the player
  17.         // For example:
  18.         // player2: X
  19.         if (player == player1)
  20.             return "player1: " + player;
  21.         else
  22.             return "player2: " + player;
  23.     }
  24.    
  25.     void getPlayerInput(char player)
  26.     {
  27.         // ******* Details to fill in ************
  28.  
  29.         // Prompt the user for a cell input. Make sure its a legal
  30.         // cell designator. Also make sure the cell doesn't already
  31.         // have a player in it. Prompt the user again in the case
  32.         // of any problem. Once a valid spot has been found,
  33.         // you will issue a statement like:
  34.         char choice;
  35.         System.out.println("Entering Input For:" + playerID(player));
  36.  
  37.         System.out.println("enter in cell [a, b, ... i]");
  38.         choice = scan.next().charAt(0);
  39.  
  40.         switch(choice)
  41.         {
  42.         case 'a':ttt[0][0] = player;break;
  43.         case 'b':ttt[0][1] = player;break;
  44.         case 'c':ttt[0][2] = player;break;
  45.         case 'd':ttt[1][0] = player;break;
  46.         case 'e':ttt[1][1] = player;break;
  47.         case 'f':ttt[1][2] = player;break;
  48.         case 'g':ttt[2][0] = player;break;
  49.         case 'h':ttt[2][1] = player;break;
  50.         case 'i':ttt[2][2] = player;break;
  51.         }
  52.  
  53.     }
  54.    
  55.     boolean gameIsDraw()
  56.     {
  57.         // ******* Details to fill in ************
  58.         // If all ttt entries have been taken return true
  59.         // otherwise return false
  60.         for(int i = 0; i < ttt.length; i++)
  61.             for(int j = 0; j < ttt [i].length; j++)
  62.                 if( ttt [i][j] == ' ')
  63.                     return false;
  64.         return true;
  65.     }
  66.    
  67.     boolean winner(char player)
  68.     {
  69.         // ******* Details to fill in ************
  70.         // Check to see if the parameter player has won
  71.         // Winning means that player shows up in a line of
  72.         // three. The line can be a column, row or a diagonal
  73.         // Return true if player is a winner, otherwise return false.
  74.         for(int i = 0; i < 3; i++)
  75.  
  76.             if(ttt[i][0] == player && ttt[i][1] == player && ttt[i][2] == player)
  77.                 return true;
  78.  
  79.         for(int i = 0; i < 3; i++)
  80.  
  81.             if(ttt[0][i] == player && ttt[1][i] == player && ttt[2][i] == player)
  82.                 return true;
  83.  
  84.         if(ttt[0][0] == player && ttt[1][1] == player && ttt[2][2] == player)
  85.             return true;
  86.  
  87.         if(ttt[2][0] == player && ttt[1][1] == player && ttt[0][2] == player)
  88.             return true;
  89.  
  90.         return false;
  91.  
  92.     }
  93.    
  94.     void displayBoard()
  95.     {
  96.          // ******* Details to fill in ************
  97.         // game board spacing
  98.             System.out.println("===============================e");
  99.             System.out.println("--a-----b-----c--");
  100.             System.out.println("| "+ttt[0][0]+"   | "+ttt[0][1]+"   | "+ttt[0][2]+" |\n");
  101.             System.out.println("--d-----e-----f--");
  102.             System.out.println("| "+ttt[1][0]+"   | "+ttt[1][1]+"   | "+ttt[1][2]+" |\n");
  103.             System.out.println("--g-----h-----i--");
  104.             System.out.println("| "+ttt[2][0]+"   | "+ttt[2][1]+"   | "+ttt[2][2]+" |\n");
  105.             System.out.println("-----------------");
  106.            
  107.         }
  108.    
  109.     void newgame()
  110.     {
  111.         char currPlayer = player1;
  112.         for(int i = 0; i < 3; i++)
  113.             for(int j = 0; j < 3; j++)
  114.                 ttt [i][j] =' ';
  115.         boolean continueOn = true;
  116.         while (continueOn)
  117.         {
  118.             displayBoard();
  119.             if (gameIsDraw())
  120.             {
  121.                 System.out.println("******** Game Ends in Draw");
  122.                 continueOn = false;
  123.             }
  124.             else
  125.             {
  126.                 getPlayerInput(currPlayer);
  127.                 if (winner(currPlayer))
  128.                 {
  129.                     System.out.println("******** We have a winner: " + playerID(currPlayer));
  130.                     // calling displayBoard method
  131.                     displayBoard();  
  132.                     continueOn = false;
  133.                 }
  134.                 else
  135.                 {
  136.                     if (currPlayer == player1) currPlayer = player2;
  137.                     else currPlayer = player1;
  138.                 }
  139.             }
  140.         }
  141.     }
  142.     public static void main(String[] args)
  143.     {
  144.         TicTacToe game = new TicTacToe();
  145.         String str;
  146.         do
  147.         {
  148.             game.newgame();
  149.             System.out.println("Do you want to play Tic-Tac-Toe (y/n)?");
  150.             str = game.scan.next();
  151.         } while ("y".equals(str));
  152.         // otherwise prints Bye
  153.         System.out.println("Bye");
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement