MrThoe

TicTacToe Done

Mar 7th, 2023
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. public class TicTacToe {
  2.  
  3.     // String[row][col]
  4.     private String[][] board;
  5.     private boolean xturn;
  6.     String player;
  7.    
  8.     public TicTacToe(){
  9.         board = new String[3][3];
  10.         for(int i=0; i<3; i++){
  11.             for(int j=0; j < 3; j++){
  12.                 board[i][j] = "-";
  13.             }
  14.         }
  15.         xturn = false;
  16.         player = "O";
  17.     }
  18.    
  19.     public void printBoard(){
  20.         System.out.println("  0 1 2");
  21.         for(int i = 0; i < 3; i++){
  22.             System.out.println(i + " " +board[i][0]
  23.               + " " + board[i][1] + " " + board[i][2]);
  24.         }
  25.         if(!checkWin()){
  26.             System.out.println("It is " + player + "'s turn.");
  27.         }
  28.     }
  29.    
  30.     public boolean isValid(int r, int c){
  31.         if(board[r][c].equals("-")){
  32.             return true;
  33.         }
  34.         return false;
  35.     }
  36.    
  37.     public void placePlayer(int r, int c){
  38.         if(xturn){
  39.             player = "X";
  40.             board[r][c] = player;
  41.             player = "O";
  42.            
  43.         } else {
  44.             player = "O";
  45.             board[r][c] = player;
  46.             player = "X";
  47.         }
  48.         xturn = !xturn;  //CHANGES PLAYERS
  49.     }
  50.    
  51.     public boolean checkWin(){
  52.         if(checkRow() || checkCol() || checkDiag()){
  53.             return true;
  54.         }
  55.         return false;
  56.     }
  57.    
  58.     public boolean checkRow(){
  59.         for(int i = 0; i < 3; i++){
  60.             if(board[i][0] == board[i][1] && board[i][1]== board[i][2]
  61.               && board[i][0] != "-"){
  62.                   return true;
  63.               }
  64.         }
  65.         return false;
  66.     }
  67.    
  68.     public boolean checkCol(){
  69.         for(int i = 0; i < 3; i++){
  70.             if(board[0][i] == board[1][i] && board[1][i]== board[2][i]
  71.               && board[0][i] != "-"){
  72.                   return true;
  73.               }
  74.         }
  75.         return false;
  76.     }
  77.    
  78.     public boolean checkDiag(){
  79.         if(board[1][1] != "-"){
  80.             if(board[0][0] == board[1][1] && board[1][1] == board[2][2]){
  81.                 return true;
  82.             }
  83.             if(board[2][0] == board[1][1] && board[1][1] == board[0][2]){
  84.                 return true;
  85.             }
  86.         }
  87.         return false;
  88.     }
  89.    
  90.     public String getPlayer(){            
  91.         if(player == "X"){
  92.             player = "O";
  93.         } else {
  94.             player = "X";
  95.         }
  96.         return player;
  97.     }
  98.    
  99. }
  100.  
  101.  
  102.  
  103. import java.util.Scanner;
  104.  
  105. public class MyProgram
  106. {
  107.     public static void main(String[] args)
  108.     {
  109.         TicTacToe thoe = new TicTacToe();
  110.         int x;
  111.         int y;
  112.         Scanner sc = new Scanner(System.in);
  113.         thoe.printBoard();
  114.         while(!thoe.checkWin()){
  115.             System.out.println("Which Row? ");
  116.             x = Integer.valueOf(sc.nextLine());
  117.             System.out.println("Which Column? ");
  118.             y = Integer.valueOf(sc.nextLine());
  119.             if(thoe.isValid(x,y)){
  120.                 thoe.placePlayer(x,y);
  121.             } else {
  122.                 System.out.println("Invalid choice!");
  123.             }
  124.  
  125.             thoe.printBoard();
  126.         }
  127.         System.out.println(thoe.getPlayer() + " wins!");
  128.        
  129.  
  130.     }
  131. }
  132.  
  133.  
Advertisement
Add Comment
Please, Sign In to add comment