Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Phil Crafts / Adam Tiago
- // 10/16/17
- // CSC-220 Data Structures
- //TicTacToe
- package tictactoe;
- public class TicTacToe {
- private char[][] board;
- private int turns;
- public int getTurns() {
- return this.turns;
- }
- public void setTurns(int turns) {
- this.turns = turns;
- }
- public TicTacToe() {
- this.board = new char[3][3];
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++)
- {
- this.board[i][j] = ' ';
- }
- }
- setTurns(0);
- }
- public char getPlayerAt (int r, int c)
- {
- return this.board[r][c];
- }
- public boolean isTied() {
- if (!isWinner('X') && !isWinner('O') && isFull())
- return true;
- else
- return false;
- }
- public boolean isFull() {
- if(getTurns() == 9)
- return true;
- else
- return false;
- }
- public boolean isWinner(char c) {
- for (int i = 0; i < this.board.length; i++) {
- if(this.board[i][0] == c && this.board[i][1] == c && this.board[i][2] == c)
- return true;
- if(this.board[0][i] == c && this.board[1][i] == c && this.board[2][i] == c)
- return true;
- }
- if(this.board[0][0] == c && this.board[1][1] == c && this.board[2][2] == c)
- return true;
- if(this.board[0][2] == c && this.board[1][1] == c && this.board[2][0] == c)
- return true;
- return false;
- }
- public boolean isValid(int r, int c) {
- if(r > 2 || c > 2 || r < 0 || c < 0)
- return false;
- if (this.board[r][c] == ' ')
- return true;
- else
- return false;
- }
- public void playMove(char p, int r, int c) {
- this.board[r][c] = p;
- setTurns(getTurns() + 1);
- }
- @Override
- public String toString() {
- String out = "";
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++)
- {
- out += this.board[i][j] + " ";
- }
- out +=("\n");
- }
- return out;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement