Advertisement
Omar_Natour

Natour, O. 10/17/16 Csc-220 TicTacToe

Oct 17th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. /*
  2.  * Omar Natour
  3.  * 10/17/2016
  4.  * Csc-220 Data Structures
  5.  * Hw4 Tic Tac Toe
  6.  * Create a tic tac toe object
  7.  * Ojnatour0001@student.stcc.edu
  8.  */
  9.  
  10. package tictactoe;
  11.  
  12. public class TicTacToe {
  13.  
  14.     private char[][] caBoard = new char[3][3];
  15.     private int turns;
  16.  
  17.     public TicTacToe() {
  18.  
  19.         for (int i = 0; i < caBoard.length; i++) {
  20.             for (int j = 0; j < caBoard[i].length; j++) {
  21.                 caBoard[i][j] = ' ';
  22.             }
  23.         }
  24.         turns = 0;
  25.     }
  26.  
  27.     public int getTurns() {
  28.         return this.turns;
  29.     }
  30.  
  31.     public char getPlayerAt(int r, int c) {
  32.         return this.caBoard[r][c];
  33.     }
  34.  
  35.     public String toString() {
  36.         String sBoard = "+---+---+---+ \n";
  37.  
  38.         for (int i = 0; i < caBoard.length; i++) {
  39.             for (int j = 0; j < caBoard[i].length; j++) {
  40.                 sBoard += ("| " + caBoard[i][j] + " ");
  41.             }
  42.             sBoard += "|";
  43.             sBoard += "\n";
  44.  
  45.             for (int k = 0; k < caBoard.length; k++) {
  46.                 sBoard += ("+---");
  47.             }
  48.  
  49.             sBoard += "+";
  50.             sBoard += ("\n");
  51.         }
  52.         return sBoard;
  53.     }
  54.  
  55.     public void playMove(char p, int r, int c) {
  56.         if (isValid(r, c)) {
  57.             this.caBoard[r][c] = p;
  58.             this.turns++;
  59.         }
  60.     }
  61.  
  62.     public boolean isValid(int r, int c) {
  63.         if ((r >= 0 && r <= this.caBoard.length) && (c >= 0 && c <= this.caBoard[r].length))
  64.             return true;
  65.         return false;
  66.     }
  67.  
  68.     public boolean isWinner(char p) {
  69.  
  70.         if ((caBoard[0][0] == p && caBoard[1][1] == p && caBoard[2][2] == p)
  71.                 || (caBoard[0][2] == p && caBoard[1][1] == p && caBoard[2][0] == p))
  72.             return true;
  73.  
  74.         for (int i = 0; i < this.caBoard.length; i++) {
  75.             if (checkColumn(p, i))
  76.                 return true;
  77.         }
  78.  
  79.         for (int i = 0; i < this.caBoard.length; i++) {
  80.             if (checkRow(p, i))
  81.                 return true;
  82.         }
  83.  
  84.         return false;
  85.     }
  86.  
  87.     private boolean checkRow(char p, int r) {
  88.         for (int i = 0; i < this.caBoard.length; i++) {
  89.             if (caBoard[r][i] != p)
  90.                 return false;
  91.         }
  92.         return true;
  93.     }
  94.  
  95.     private boolean checkColumn(char p, int c) {
  96.         for (int i = 0; i < this.caBoard.length; i++) {
  97.             if (caBoard[i][c] != p)
  98.                 return false;
  99.         }
  100.         return true;
  101.     }
  102.  
  103.     public boolean isFull() {
  104.         if (this.turns >= 9)
  105.             return true;
  106.         return false;
  107.     }
  108.  
  109.     private boolean isTied() {
  110.         if (isFull() && !(isWinner('X') || isWinner('O')))
  111.             return true;
  112.         return false;
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement