TIC TAC TOE import java util Scanner import
- Slides: 17
TIC TAC TOE
import java. util. Scanner; import java. util. Random; public class Play. TTT{ public static void main(String[]args){ Scanner reader = new Scanner(System. in); TTTBoard board = new TTTBoard(); System. out. println(board); - - - - -
// Randomly decide who goes first Random gen = new Random(); String letter = "X"; if (gen. next. Int(2) == 1) letter = "O";
while (true){ String winner = board. get. Winner(); 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. next. Int(); System. out. print("Enter the column[1 -3]: "); int column = reader. next. Int(); boolean success = board. place. Xor. O(letter, row, column); 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!"); }
CONSTRUCTOR METHOD FOR THE BOARD public class TTTBoard{ private String[][] board; public TTTBoard(){ board = new String[3][3]; reset(); }
to. String method public String to. String(){ 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; }
get. Winner method public String get. Winner(){ if (winner("X")) return "X"; else if (winner("O")) return "O"; else return null; }
Winner method (private) private boolean winner(String s){ // Create a target string for the search String test = s + s; // Create an array to hold the possible strings int rows = board. length; int columns = board[0]. length; String possibles[] = new String[rows + columns + 2]; // Get the three columns as strings for (int column = 0; column < columns; column ++) possibles[column] = get. Column(column, rows); // Get the three rows as strings for (int row = 0; row < rows; row ++) possibles[columns + row] = get. Row(row, columns); // Get the two diagonals as strings possibles[possibles. length - 2] = get. Diagonal(0, rows, columns); possibles[possibles. length - 1] = get. Diagonal(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; }
get. Column() method (private) private String get. Column(int column, int rows){ String result = ""; for (int row = 0; row < rows; row++) result += board[row][column]; return result; }
get. Row() method (private) private String get. Row(int row, int columns){ String result = ""; for (int column = 0; column < columns; column++) result += board[row][column]; return result; }
get. Diagonal() method (private) private String get. Diagonal(int start. Row, int rows, int columns){ String result = ""; int column = 0; if (start. Row == 0) while (start. Row < rows){ result += board[start. Row][column]; start. Row++; column++; } else while (start. Row >= 0){ result += board[start. Row][column]; start. Row--; column++; } return result; }
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] = "-"; }
place. Xor. O() method (public) public boolean place. Xor. O(String s, int row, int column){ if (board[row - 1][column - 1]. equals("-")){ board[row - 1][column - 1] = s; return true; } else return false; }
test
0 1 2 3 4 5 6 7 X 0 X XX 0 00 X XX 0 XXX , , ,
X 0 x X X 0 0 0 X 0 X 1 0 X 0 2 3 4 5 6 7
X 0 x X X 0 0 0 1 2 3 4 5 6 7 X 0 X 0 X 0 XXX X 0 X 0 XX X 00
- Import java.io.file;
- Import java.util.scanner
- Import java.util.*
- Import java.util.*
- Tic tac toe impossible
- Neural network tic tac toe
- Tic tac toe sequence diagram
- Draw the state space for tic-tac toe problem
- May spelling tic tac toe
- Tic tac toe calculator
- Ai 344
- Tic tac toe
- Tic tac toe design pattern
- Small basic tic tac toe
- Factoring tic tac toe method
- Pigpen letters
- Tic tac toe
- Tic-tac-toe spielen