letsdoitjava

Tic Tac Toe

Jul 24th, 2019
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.17 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.  
  7. import java.util.*;
  8. class TicTacToe
  9. {
  10.     // setting size of board with char array named ttt
  11.     char ttt[][] = new char[3][3];
  12.     static final char player1 = 'O';
  13.     static final char player2 = 'X';
  14.     Scanner scan =new Scanner(System.in);
  15.  
  16.     String playerID(char player)
  17.     {
  18.         // Prints the identity of the player
  19.         // For example:
  20.         // player2: X
  21.         if (player == player1)
  22.             return "player1: " + player;
  23.         else
  24.             return "player2: " + player;
  25.     }
  26.  
  27.     void getPlayerInput(char player)
  28.     {
  29.         // ******* Details to fill in ************
  30.  
  31.         // Prompt the user for a cell input. Make sure its a legal
  32.         // cell designator. Also make sure the cell doesn't already
  33.         // have a player in it. Prompt the user again in the case
  34.         // of any problem. Once a valid spot has been found,
  35.         // you will issue a statement like:
  36.         char choice;
  37.         System.out.println("Entering Input For:" + playerID(player));
  38.        
  39.         // setting all possible options for a draw
  40.         while(true) {
  41.             System.out.print("enter in cell [a, b, ... i]");
  42.             choice = scan.next().charAt(0);
  43.  
  44.             int choiceTaken = 0;
  45.  
  46.             switch(choice)
  47.             {
  48.             case 'a':
  49.                 if(ttt[0][0] != player1 || ttt[0][0] != player2) {
  50.                     ttt[0][0] = player;
  51.                     choiceTaken = 1;
  52.                 }else {
  53.                     System.out.println("Slot is already chosen by "+ playerID(ttt[0][0]));
  54.                 }
  55.                 break;
  56.             case 'b':
  57.                 if(ttt[0][1] != player1 || ttt[0][1] != player2) {
  58.                     ttt[0][1] = player;
  59.                     choiceTaken = 1;
  60.                 }else {
  61.                     System.out.println("Slot is already chosen by "+ playerID(ttt[0][1]));
  62.                 }
  63.                 break;
  64.             case 'c':
  65.                 if(ttt[0][2] != player1 || ttt[0][2] != player2) {
  66.                     ttt[0][2] = player;
  67.                     choiceTaken = 1;
  68.                 }else {
  69.                     System.out.println("Slot is already chosen by "+ playerID(ttt[0][2]));
  70.                 }
  71.                 break;
  72.             case 'd':
  73.                 if(ttt[1][0] != player1 || ttt[1][0] != player2) {
  74.                     ttt[1][0] = player;
  75.                     choiceTaken = 1;
  76.                 }else {
  77.                     System.out.println("Slot is already chosen by "+ playerID(ttt[1][0]));
  78.                 }
  79.                 break;
  80.             case 'e':
  81.                 if(ttt[1][1] != player1 || ttt[1][1] != player2) {
  82.                     ttt[1][1] = player;
  83.                     choiceTaken = 1;
  84.                 }else {
  85.                     System.out.println("Slot is already chosen by "+ playerID(ttt[1][1]));
  86.                 }
  87.                 break;
  88.             case 'f':
  89.                 if(ttt[1][2] != player1 || ttt[1][2] != player2) {
  90.                     ttt[1][2] = player;
  91.                     choiceTaken = 1;
  92.                 }else {
  93.                     System.out.println("Slot is already chosen by "+ playerID(ttt[1][2]));
  94.                 }
  95.                 break;
  96.             case 'g':
  97.                 if(ttt[2][0] != player1 || ttt[2][0] != player2) {
  98.                     ttt[2][0] = player;
  99.                     choiceTaken = 1;
  100.                 }else {
  101.                     System.out.println("Slot is already chosen by "+ playerID(ttt[2][0]));
  102.                 }
  103.                 break;
  104.             case 'h':
  105.                 if(ttt[2][1] != player1 || ttt[2][1] != player2) {
  106.                     ttt[2][1] = player;
  107.                     choiceTaken = 1;
  108.                 }else {
  109.                     System.out.println("Slot is already chosen by "+ playerID(ttt[2][1]));
  110.                 }
  111.                 break;
  112.             case 'i':
  113.                 if(ttt[2][2] != player1 || ttt[2][2] != player2) {
  114.                     ttt[2][2] = player;
  115.                     choiceTaken = 1;
  116.                 }else {
  117.                     System.out.println("Slot is already chosen by "+ playerID(ttt[2][2]));
  118.                 }
  119.                 break;
  120.             default:
  121.                 System.out.println("Please enter a valid slot");
  122.             }
  123.  
  124.             if(choiceTaken == 1) {
  125.                 break;
  126.             }
  127.         }
  128.  
  129.     }
  130.  
  131.     boolean gameIsDraw()
  132.     {
  133.         // ******* Details to fill in ************
  134.         // If all ttt entries have been taken return true
  135.         // otherwise return false
  136.         for(int i = 0; i < ttt.length; i++)
  137.             for(int j = 0; j < ttt [i].length; j++)
  138.                 if( ttt [i][j] == ' ')
  139.                     return false;
  140.         return true;
  141.     }
  142.  
  143.     boolean winner(char player)
  144.     {
  145.         // ******* Details to fill in ************
  146.         // Check to see if the parameter player has won
  147.         // Winning means that player shows up in a line of
  148.         // three. The line can be a column, row or a diagonal
  149.         // Return true if player is a winner, otherwise return false.
  150.         for(int i = 0; i < 3; i++)
  151.  
  152.  
  153.             if(ttt[i][0] == player && ttt[i][1] == player && ttt[i][2] == player)
  154.                 return true;
  155.  
  156.         for(int i = 0; i < 3; i++)
  157.  
  158.             if(ttt[0][i] == player && ttt[1][i] == player && ttt[2][i] == player)
  159.                 return true;
  160.  
  161.         if(ttt[0][0] == player && ttt[1][1] == player && ttt[2][2] == player)
  162.             return true;
  163.  
  164.         if(ttt[2][0] == player && ttt[1][1] == player && ttt[0][2] == player)
  165.             return true;
  166.  
  167.         return false;
  168.     }
  169.  
  170.     void displayBoard()
  171.     {
  172.         // ******* Details to fill in ************
  173.         // game board spacing
  174.         System.out.println("===============================e");
  175.         System.out.println("      ---a------b------c---");
  176.         System.out.println("      |  "+ ttt[0][0] +"   |  "+ ttt[0][1]+"   |  "+ ttt[0][2]+"  |\n");
  177.         System.out.println("      ---d------e------f---");
  178.         System.out.println("      |  "+ ttt[1][0] +"   |  "+ ttt[1][1]+"   |  "+ ttt[1][2]+"  |\n");
  179.         System.out.println("      ---g------h------i---");
  180.         System.out.println("      |  "+ ttt[2][0] +"   |  "+ ttt[2][1]+"   |  "+ ttt[2][2]+"  |\n");
  181.         System.out.println("      --------------------");
  182.     }
  183.  
  184.     void newgame()
  185.     {
  186.         char currPlayer = player1;
  187.         for(int i = 0; i < 3; i++)
  188.             for(int j = 0; j < 3; j++)
  189.                 ttt [i][j] =' ';
  190.         boolean continueOn = true;
  191.         while (continueOn)
  192.         {
  193.             displayBoard();
  194.             if (gameIsDraw())
  195.             {
  196.                 System.out.println("******** Game Ends in Draw");
  197.                 continueOn = false;
  198.             }
  199.             else
  200.             {
  201.                 getPlayerInput(currPlayer);
  202.                 if (winner(currPlayer))
  203.                 {
  204.                     System.out.println("******** We have a winner: " + playerID(currPlayer));
  205.                     // calling displayBoard method
  206.                     displayBoard();  
  207.                     continueOn = false;
  208.                 }
  209.                 else
  210.                 {
  211.                     // otherwise continue this pattern for play turns
  212.                     if (currPlayer == player1) currPlayer = player2;
  213.                     else currPlayer = player1;
  214.                 }
  215.             }
  216.         }
  217.     }
  218.     public static void main(String[] args)
  219.     {
  220.         TicTacToe game = new TicTacToe();
  221.         String str;
  222.         do
  223.         {
  224.             game.newgame();
  225.             System.out.println("Do you want to play Tic-Tac-Toe (y/n)?");
  226.             str = game.scan.next();
  227.         } while ("y".equals(str));
  228.         // otherwise prints Bye
  229.         System.out.println("Bye");
  230.     }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment