morry2341

Untitled

Apr 13th, 2023
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package ThirtyMinutes;
  2.  
  3. import java.lang.reflect.Method;
  4. import java.util.Random;
  5.  
  6. public class Tic_Tac_Toe {
  7.     /*in the gameboard method overwrite the board I pass in as a parameter with a brand new empty board,
  8.     * then pring that brand new board*/
  9.  
  10.     private static char[][] initBoard(){
  11.         //initialise the board
  12.             char[][] board = new char[][]{{' ', '|', ' ', '|', ' '},
  13.                     {'-', '+', '-', '+', '-'},
  14.                     {' ', '|', ' ', '|', ' '},
  15.                     {'-', '+', '-', '+', '-'},
  16.                     {' ', '|', ' ', '|', ' '}};
  17.             System.out.println("initialised");
  18.             return board;
  19.     }
  20.  
  21.     private static void printBoard(char[][] board){
  22.         //take the game board and print it
  23.         initBoard();
  24.             for(char[] zeile : board){
  25.                 for(char c : zeile){
  26.                     System.out.print(c);
  27.                 }
  28.                 System.out.println();
  29.             }
  30.        System.out.println("printed");
  31.     }
  32.  
  33.     private static void caseDecision(char board[][],int pos, String user){
  34.  
  35.         char ch = ' ';
  36.         if(user.equals("human")){
  37.             System.out.println("I am a human");
  38.             ch = 'O';
  39.         } else if (user.equals("com")){
  40.             System.out.println("I am a computer");
  41.             ch = 'X';
  42.  
  43.         }
  44.  
  45.         switch(pos){
  46.             case 1:
  47.                 board[0][0] = ch;
  48.                 break;
  49.             case 2:
  50.                 board[0][1] = ch;
  51.                 break;
  52.             case 3:
  53.                 board[0][2] = ch;
  54.                 break;
  55.             case 4:
  56.                 board[1][0] = ch;
  57.                 break;
  58.             case 5:
  59.                 board[1][1] = ch;
  60.                 break;
  61.             case 6:
  62.                 board[1][2] = ch;
  63.                 break;
  64.             case 7:
  65.                 board[2][0] = ch;
  66.                 break;
  67.             case 8:
  68.                 board[2][1] = ch;
  69.                 break;
  70.             case 9:
  71.                 board[2][2] = ch;
  72.                 break;
  73.             default:
  74.                 System.out.println("QA");
  75.                 break;
  76.         }
  77.         printBoard(board); //this will display the X
  78.  
  79.     }
  80.     public static void main(String[] args) {
  81.         char[][] board = new char[3][3];
  82.         System.out.println("Welcome to Tic Tac Toe");
  83.  
  84.         System.out.println("Select your pos 1-9");
  85.         int pos = new java.util.Scanner(System.in).nextInt();
  86.         Random rdm = new Random();
  87.         int comPos = rdm.nextInt(9) + 1; //the AI can replace this line to inject pos
  88.         caseDecision(board, pos, "human");
  89.         caseDecision(board, comPos, "com");
  90.  
  91.     }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment