micher43

P7.6

Nov 15th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.34 KB | None | 0 0
  1. import java.util.*;
  2. public class ticTacToe {
  3.  
  4.     public static void main(String[] args) {
  5.  
  6.        
  7.         public class TTTConsoleNonOO2P {
  8.            int EMPTY = 0;
  9.            int CROSS = 1;
  10.            int OTHER = 2;
  11.          
  12.            int PLAYING = 0;
  13.            int DRAW = 1;
  14.            int CROSS_WON = 2;
  15.            int OTHER_WON = 3;
  16.          
  17.            int ROWS = 3;
  18.            int COLS = 3;
  19.            int[][] board = new int[ROWS][COLS];
  20.                                                              
  21.            int currentState;
  22.                                            
  23.            int currentPlayer;
  24.            int currntRow, currentCol;
  25.          
  26.            public static Scanner in = new Scanner(System.in);
  27.          
  28.            public void main(String[] args) {
  29.               initGame();
  30.               do {
  31.                  playerMove(currentPlayer);
  32.                  updateGame(currentPlayer, currntRow, currentCol);
  33.                  printBoard();
  34.                  if (currentState == CROSS_WON) {
  35.                     System.out.println("'X' wins.");
  36.                  } else if (currentState == OTHER_WON) {
  37.                     System.out.println("'O' wins.");
  38.                  } else if (currentState == DRAW) {
  39.                     System.out.println("draw");
  40.                  }
  41.                  currentPlayer = (currentPlayer == CROSS) ? OTHER : CROSS;
  42.               } while (currentState == PLAYING);
  43.            }
  44.          
  45.            public void initGame() {
  46.               for (int row = 0; row < ROWS; ++row) {
  47.                  for (int col = 0; col < COLS; ++col) {
  48.                     board[row][col] = EMPTY;  
  49.                  }
  50.               }
  51.               currentState = PLAYING;
  52.               currentPlayer = CROSS;  
  53.            }
  54.          
  55.        
  56.            public void playerMove(int theSeed) {
  57.               boolean validInput = false;  
  58.               do {
  59.                  if (theSeed == CROSS) {
  60.                     System.out.print("Player 'X', enter your move (row[1-3] column[1-3]): ");
  61.                  } else {
  62.                     System.out.print("Player 'O', enter your move (row[1-3] column[1-3]): ");
  63.                  }
  64.                  int row = in.nextInt() - 1;  // array index starts at 0 instead of 1
  65.                  int col = in.nextInt() - 1;
  66.                  if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY) {
  67.                     currntRow = row;
  68.                     currentCol = col;
  69.                     board[currntRow][currentCol] = theSeed;  
  70.                     validInput = true;  
  71.                  } else {
  72.                     System.out.println("This move at (" + (row + 1) + "," + (col + 1)
  73.                           + ") is not valid. Try again...");
  74.                  }
  75.               } while (!validInput);  
  76.            }
  77.    
  78.            public void updateGame(int theSeed, int currentRow, int currentCol) {
  79.               if (hasWon(theSeed, currentRow, currentCol)) {  
  80.                  currentState = (theSeed == CROSS) ? CROSS_WON : OTHER_WON;
  81.               } else if (isDraw()) {  
  82.                  currentState = DRAW;
  83.               }
  84.              
  85.            }
  86.          
  87.            public boolean isDraw() {
  88.               for (int row = 0; row < ROWS; ++row) {
  89.                  for (int col = 0; col < COLS; ++col) {
  90.                     if (board[row][col] == EMPTY) {
  91.                        return false;  
  92.                     }
  93.                  }
  94.               }
  95.               return true;  
  96.            }
  97.          
  98.          
  99.            public boolean hasWon(int theSeed, int currentRow, int currentCol) {
  100.               return (board[currentRow][0] == theSeed        
  101.                            && board[currentRow][1] == theSeed
  102.                            && board[currentRow][2] == theSeed
  103.                       || board[0][currentCol] == theSeed      
  104.                            && board[1][currentCol] == theSeed
  105.                            && board[2][currentCol] == theSeed
  106.                       || currentRow == currentCol            
  107.                            && board[0][0] == theSeed
  108.                            && board[1][1] == theSeed
  109.                            && board[2][2] == theSeed
  110.                       || currentRow + currentCol == 2  
  111.                            && board[0][2] == theSeed
  112.                            && board[1][1] == theSeed
  113.                            && board[2][0] == theSeed);
  114.            }
  115.          
  116.            public void printBoard() {
  117.               for (int row = 0; row < ROWS; ++row) {
  118.                  for (int col = 0; col < COLS; ++col) {
  119.                     printCell(board[row][col]);
  120.                     if (col != COLS - 1) {
  121.                        System.out.print("|");  
  122.                     }
  123.                  }
  124.                  System.out.println();
  125.                  if (row != ROWS - 1) {
  126.                     System.out.println("-----------");
  127.                  }
  128.               }
  129.               System.out.println();
  130.            }
  131.          
  132.            public void printCell(int content) {
  133.               switch (content) {
  134.                  case EMPTY:  System.out.print("   "); break;
  135.                  case OTHER: System.out.print(" O "); break;
  136.                  case CROSS:  System.out.print(" X "); break;
  137.               }
  138.            }
  139.         }
  140.     }
  141.     }
Advertisement
Add Comment
Please, Sign In to add comment