Advertisement
nate23nate23

hw 4 -works

Oct 17th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. /*
  2.  * Name: Nate Wheeler
  3.  * Date: Oct 16, 2016
  4.  * Course Number: csc220
  5.  * Course Name: Data Structures
  6.  * Problem Number: hw 4
  7.  * Email: nate23nate23@gmail.com
  8.  * Short Description of the Problem:
  9.  * Write a class called TicTacToe to handle the
  10.  * basics of a two-player game of Tic-Tac-Toe.
  11.  */
  12. package compsci220;
  13.  
  14. public class TicTacToe {
  15.     char[][] board;
  16.     int turns;
  17.  
  18.     public TicTacToe() {
  19.         board = new char[3][3];
  20.         turns = 0;
  21.         for (int i = 0; i < board.length; i++) {
  22.             for (int j = 0; j < board[i].length; j++) {
  23.                 board[i][j] = ' ';
  24.             }
  25.         }
  26.     }
  27.  
  28.     public int getTurns() {
  29.         return this.turns;
  30.     }
  31.  
  32.     public char getPlayerAt(int r, int c) {
  33.         return this.board[r][c];
  34.     }
  35.  
  36.     public String toString() {
  37.         String array = "";
  38.         for (int i = 0; i < board.length; i++) {
  39.             for (int j = 0; j < board[0].length; j++) {
  40.                 array += " " + this.board[i][j];
  41.             }
  42.             array += "\n";
  43.         }
  44.         return array;
  45.     }
  46.  
  47.     public boolean isFull() {
  48.         if (this.turns == 9)
  49.             return true;
  50.         else
  51.             return false;
  52.     }
  53.  
  54.     public boolean isValid(int r, int c) {
  55.         if (0 <= r && r <= 2 && c >= 0 && c <= 2)
  56.             return true;
  57.         else
  58.             return false;
  59.     }
  60.  
  61.     public boolean isTied() {
  62.         if (this.isFull() == true && this.isWinner('X') == false && this.isWinner('O') == false)
  63.             return true;
  64.         else
  65.             return false;
  66.     }
  67.  
  68.     public boolean isWinner(char p) {
  69.         Character.toUpperCase(p);
  70.         if (p == this.board[0][0] && p == this.board[0][1] && p == this.board[0][2])
  71.             return true;
  72.         if (p == this.board[1][0] && p == this.board[1][1] && p == this.board[1][2])
  73.             return true;
  74.         if (p == this.board[2][0] && p == this.board[2][1] && p == this.board[2][2])
  75.             return true;
  76.         if (p == this.board[0][0] && p == this.board[1][0] && p == this.board[2][0])
  77.             return true;
  78.         if (p == this.board[0][1] && p == this.board[1][1] && p == this.board[2][1])
  79.             return true;
  80.         if (p == this.board[0][2] && p == this.board[1][2] && p == this.board[2][2])
  81.             return true;
  82.         if (p == this.board[0][0] && p == this.board[1][1] && p == this.board[2][2])
  83.             return true;
  84.         if (p == this.board[0][2] && p == this.board[1][1] && p == this.board[2][0])
  85.             return true;
  86.         return false;
  87.  
  88.     }
  89.  
  90.     public void playMove(char p, int r, int c) {
  91.         Character.toUpperCase(p);
  92.         this.turns++;
  93.         if (p == 'X' || p == 'O')
  94.             this.board[r][c] = p;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement