Download presentation
Presentation is loading. Please wait.
Published byNorma Snow Modified over 9 years ago
1
TIC TAC TOE
2
import java.util.Scanner; import java.util.Random; public class PlayTTT{ public static void main(String[]args){ Scanner reader = new Scanner(System.in); TTTBoard board TTTBoard board = new TTTBoard(); System.out.println(board);board import java.util.Scanner; import java.util.Random; public class PlayTTT{ public static void main(String[]args){ Scanner reader = new Scanner(System.in); TTTBoard board TTTBoard board = new TTTBoard(); System.out.println(board);board
3
// Randomly decide who goes first Random gen = new Random(); String letter = "X"; if (gen.nextInt(2) == 1) letter = "O"; // Randomly decide who goes first Random gen = new Random(); String letter = "X"; if (gen.nextInt(2) == 1) letter = "O";
4
while (true){ String winner = board.getWinner();getWinner(); if (winner != null){ System.out.println(winner + "s win!"); break; } System.out.println(letter + "'s turn"); System.out.print("Enter the row[1-3]: "); int row = reader.nextInt(); System.out.print("Enter the column[1-3]: "); int column = reader.nextInt(); boolean success = board.placeXorO(letter, row, column);board.placeXorO if (success){ System.out.println(board); // Switch the player if (letter == "X") letter = "O"; else letter = "X"; } else System.out.println("Error: cell already occupied!"); } while (true){ String winner = board.getWinner();getWinner(); if (winner != null){ System.out.println(winner + "s win!"); break; } System.out.println(letter + "'s turn"); System.out.print("Enter the row[1-3]: "); int row = reader.nextInt(); System.out.print("Enter the column[1-3]: "); int column = reader.nextInt(); boolean success = board.placeXorO(letter, row, column);board.placeXorO if (success){ System.out.println(board); // Switch the player if (letter == "X") letter = "O"; else letter = "X"; } else System.out.println("Error: cell already occupied!"); }
5
public class TTTBoard{ private String[][] board; public TTTBoard(){ board = new String[3][3]; reset();reset } public class TTTBoard{ private String[][] board; public TTTBoard(){ board = new String[3][3]; reset();reset } CONSTRUCTOR METHOD FOR THE BOARD
6
toString method public String toString(){ String result = ""; for (int row = 0; row < board.length; row++){ for (int column = 0; column < board[0].length; column++) result += board[row][column] + " "; result += "\n"; } return result; } public String toString(){ String result = ""; for (int row = 0; row < board.length; row++){ for (int column = 0; column < board[0].length; column++) result += board[row][column] + " "; result += "\n"; } return result; }
7
getWinner method public String getWinner(){ if (winner("X"))winner return "X"; else if (winner("O")) return "O"; else return null; } public String getWinner(){ if (winner("X"))winner return "X"; else if (winner("O")) return "O"; else return null; }
8
Winner method (private) private boolean winner(String s){ // Create a target string for the search String test = s + s + s;test // Create an array to hold the possible strings int rows = board.length; int columns = board[0].length; String possibles[] = new String[rows + columns + 2];possibles[] // Get the three columns as strings for (int column = 0; column < columns; column ++) possibles[column] = getColumn(column, rows);getColumn // Get the three rows as strings for (int row = 0; row < rows; row ++) possibles[columns + row] = getRow(row, columns);getRow // Get the two diagonals as strings possibles[possibles.length - 2] = getDiagonal(0, rows, columns);getDiagonal( possibles[possibles.length - 1] = getDiagonal(rows - 1, rows, columns); // Search for the target string for (int i = 0; i < possibles.length; i++) if (test.equals(possibles[i])) return true; return false; } private boolean winner(String s){ // Create a target string for the search String test = s + s + s;test // Create an array to hold the possible strings int rows = board.length; int columns = board[0].length; String possibles[] = new String[rows + columns + 2];possibles[] // Get the three columns as strings for (int column = 0; column < columns; column ++) possibles[column] = getColumn(column, rows);getColumn // Get the three rows as strings for (int row = 0; row < rows; row ++) possibles[columns + row] = getRow(row, columns);getRow // Get the two diagonals as strings possibles[possibles.length - 2] = getDiagonal(0, rows, columns);getDiagonal( possibles[possibles.length - 1] = getDiagonal(rows - 1, rows, columns); // Search for the target string for (int i = 0; i < possibles.length; i++) if (test.equals(possibles[i])) return true; return false; }
9
getColumn() method (private) private String getColumn(int column, int rows){getColumn String result = ""; for (int row = 0; row < rows; row++) result += board[row][column]; return result; } private String getColumn(int column, int rows){getColumn String result = ""; for (int row = 0; row < rows; row++) result += board[row][column]; return result; }
10
getRow() method (private) private String getRow(int row, int columns){getRow String result = ""; for (int column = 0; column < columns; column++) result += board[row][column]; return result; } private String getRow(int row, int columns){getRow String result = ""; for (int column = 0; column < columns; column++) result += board[row][column]; return result; }
11
getDiagonal() method (private) private String getDiagonal(int startRow, int rows, int columns){ String result = ""; int column = 0; if (startRow == 0) while (startRow < rows){ result += board[startRow][column]; startRow++; column++; } else while (startRow >= 0){ result += board[startRow][column]; startRow--; column++; } return result; } private String getDiagonal(int startRow, int rows, int columns){ String result = ""; int column = 0; if (startRow == 0) while (startRow < rows){ result += board[startRow][column]; startRow++; column++; } else while (startRow >= 0){ result += board[startRow][column]; startRow--; column++; } return result; }
12
reset() method (public) public void reset(){ for (int row = 0; row < board.length; row++) for (int column = 0; column < board[0].length; column++) board[row][column] = "-"; } public void reset(){ for (int row = 0; row < board.length; row++) for (int column = 0; column < board[0].length; column++) board[row][column] = "-"; }
13
placeXorO() method (public) public boolean placeXorO(String s, int row, int column){ if (board[row - 1][column - 1].equals("-")){ board[row - 1][column - 1] = s; return true; } else return false; } public boolean placeXorO(String s, int row, int column){ if (board[row - 1][column - 1].equals("-")){ board[row - 1][column - 1] = s; return true; } else return false; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.