Skip to content

Instantly share code, notes, and snippets.

@jananpatel2002
Last active October 14, 2021 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jananpatel2002/34a2d1d0743d6ceb3f5ada709ae18f8b to your computer and use it in GitHub Desktop.
Save jananpatel2002/34a2d1d0743d6ceb3f5ada709ae18f8b to your computer and use it in GitHub Desktop.
//*
// * Name: Antonio Silvestri
// * Date: 10/1/2018
// * Course Number: CSC-220
// * Course Name: Data Structures
// * Problem Number: HW4
// * Email: silvestri@stcc.edu
// * Main Program to Drive TicTacToe Class V3.2
// */
package tictactoe;
import java.util.Scanner;
public class PlayTicTacToe {
private static void playGame(Scanner keyboard) {
char p = 'X';
TicTacToe ttt = new TicTacToe();
int r, c;
do {
System.out.println(ttt);
do {
System.out.print("'" + p + "', choose your location (row, column): ");
try {
r = keyboard.nextInt();
c = keyboard.nextInt();
if (!ttt.isValid(r, c))
System.out.println("That is not a valid location. Try again.");
else if (ttt.playerAt(r, c) != ' ')
System.out.println("That location is already full. Try again.");
else
break;
}
catch (Exception e) {
System.out.println("Bad Integer Entered. Try Again.");
keyboard.nextLine(); //IMPORTANT! Clear keyboard!!!
}
} while (true);
ttt.playMove(p, r, c);
if (p == 'X')
p = 'O';
else
p = 'X';
} while (!ttt.isWinner('X') && !ttt.isWinner('O') && !ttt.isFull());
System.out.println(ttt);
String status;
if (ttt.isWinner('X'))
status = "X is the winner!";
else if (ttt.isWinner('O'))
status = "O is the winner!";
else
status = "The game is a tie.";
status += " After " + ttt.getTurns() + " plays.";
System.out.println(status);
}
// **********************************************
private static void process(Scanner sc, String args[]) {
playGame(sc);
sc.nextLine(); // IMPORTANT!! Reset Scanner
}
// **********************************************
private static boolean doThisAgain(Scanner sc, String prompt) {
System.out.print(prompt);
String doOver = sc.nextLine();
return doOver.equalsIgnoreCase("Y");
}
// **********************************************
public static void main(String args[]) {
final String TITLE = "Play Tic Tac Toe V1.0";
final String CONTINUE_PROMPT = "Play again? [y/N] ";
System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(System.in);
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
System.out.println("Thank you for using " + TITLE);
}
}
//*
// * Name: Janan Patel
// * Date: 10/13/2021
// * Course Number: CSC-220
// * Course Name: Data Structures
// * Problem Number: HW4
// * Email: jkpatel2001@student.stcc.edu
// * Tictactoe methods
// */
package tictactoe;
public class TicTacToe {
private int turns;
private char[][] board = new char[3][3];
public TicTacToe() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = ' ';
}
}
this.turns = 0;
}
public int getTurns() {
return this.turns;
}
public char playerAt(int r, int c) {
return this.board[r][c];
}
public String toString() {
System.out.println();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sb.append(this.board[i][j] + " ");
}
sb.append("\n");
}
return sb.toString();
}
public boolean isTied() {
return (this.isFull() == true && (this.isWinner('X') == false && this.isWinner('O') == false));
}
public boolean isWinner(char p) {
;
if ((this.board[0][0] == p && this.board[0][1] == p && this.board[0][2] == p)
|| (this.board[1][0] == p && this.board[1][1] == p && this.board[1][2] == p)
|| (this.board[2][0] == p && this.board[2][1] == p && this.board[2][2] == p)
|| (this.board[0][0] == p && this.board[1][0] == p && this.board[2][0] == p)
|| (this.board[0][1] == p && this.board[1][1] == p && this.board[2][1] == p)
|| (this.board[0][2] == p && this.board[1][2] == p && this.board[2][2] == p)
|| (this.board[0][0] == p && this.board[1][1] == p && this.board[2][2] == p)
|| (this.board[0][2] == p && this.board[1][1] == p && this.board[2][0] == p)
)
return true;
else
return false;
}
public boolean isFull() {
return (this.turns == 9);
}
public boolean isValid(int r, int c) {
return ((r<3 && r>-1) && (c<3 && c>-1));
}
public void playMove(char p, int r, int c) {
this.board[r][c] = p;
this.turns++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment