Created
April 20, 2024 23:35
-
-
Save avery-codes/d1999c5e77684c2b52ff4d63e3f1f452 to your computer and use it in GitHub Desktop.
tic tac toe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Grid { | |
| public static String[][] initFrame() { | |
| String[][] grid = new String[6][6]; | |
| for (int i = 0; i <= 5; i++) { // initialize all as " " | |
| for (int j = 0; j <= 5; j++) { | |
| grid[i][j] = " " ; | |
| } | |
| } | |
| // labels for game spaces | |
| grid[0][1] = "1"; | |
| grid[0][3] = "2"; | |
| grid[0][5] = "3"; | |
| grid[1][0] = "A"; | |
| grid[3][0] = "B"; | |
| grid[5][0] = "C"; | |
| // formatting the empty spaces | |
| for (int j = 1; j <= 5; j++){ | |
| if (j % 2 == 0){ | |
| grid[2][j] = "+"; | |
| grid[4][j] = "+"; | |
| } else { | |
| grid[2][j] = "-"; | |
| grid[4][j] = "-"; | |
| } | |
| } | |
| for (int i = 0; i <= 5; i++) { | |
| for (int j = 1; j <= 5; j++) { | |
| if ((i % 2 == 1) && (j % 2 == 0)) { | |
| grid[i][j] = "|"; | |
| } | |
| } | |
| } | |
| return grid; | |
| } | |
| public static String[][] gameplay(String[][] board, int x, int y, String player){ | |
| board[x][y] = player; | |
| return board; | |
| } | |
| public static void print(String[][] board){ | |
| for (int i = 0; i <= 5; i++) { | |
| System.out.print("\n"); | |
| for (int j = 0; j <=5; j++) { | |
| System.out.print(board[i][j]); | |
| System.out.print("\t"); | |
| } | |
| } | |
| } | |
| public static void printAvailable(Main.Space[] availableSpaces) { | |
| System.out.print("\nAvailable spaces: "); | |
| for (int j = 0; j < availableSpaces.length; j++) { | |
| if (availableSpaces[j].playable) { | |
| System.out.printf("%s ", availableSpaces[j].display); | |
| } | |
| } | |
| } | |
| public static boolean winCheck(String[][] board) { | |
| boolean win = false; | |
| // row A, B, C | |
| if (!board[1][1].equals(" ")) { | |
| if ((board[1][1].equals(board[1][3])) && board[1][1].equals(board[1][5])) { // Row A | |
| win = true; | |
| } else if ((board[1][1].equals(board[3][1])) && board[1][1].equals(board[5][1])) { // Column 1 | |
| win = true; | |
| } else if ((board[1][1].equals(board[3][3])) && board[1][1].equals(board[5][5])) { // diagonal -slope | |
| win = true; | |
| } | |
| } else if (!board[3][1].equals(" ")) { | |
| if ((board[3][1].equals(board[3][3])) && board[3][1].equals(board[3][5])) { // Row B | |
| win = true; | |
| } | |
| } else if (!board[5][1].equals(" ")) { | |
| if ((board[5][1].equals(board[5][3])) && board[5][1].equals(board[5][5])) { // Row C | |
| win = true; | |
| } else if ((board[5][1].equals(board[3][3])) && board[5][1].equals(board[1][5])) { // diagonal +slope | |
| win = true; | |
| } | |
| } else if (!board[1][3].equals(" ")) { | |
| if ((board[1][3].equals(board[3][3])) && board[1][3].equals(board[5][3])) { // Column 2 | |
| win = true; | |
| } | |
| } else if (!board[1][5].equals(" ")) { | |
| if ((board[1][5].equals(board[3][5])) && board[1][5].equals(board[5][5])) { // Column 3 | |
| win = true; | |
| } | |
| } | |
| return win; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Scanner; | |
| public class Main { | |
| public static void main(String[] args) { | |
| String[][] board = Grid.initFrame(); | |
| Space[] spaces = initSpaces(); | |
| Scanner input = new Scanner(System.in); | |
| String move; | |
| int x; | |
| int y; | |
| int tally = 0; | |
| String player = " "; | |
| // printing setup | |
| Grid.print(board); | |
| System.out.print("\nAvailable spaces: "); | |
| for (int i = 0; i < spaces.length; i++) { | |
| System.out.printf("%s ", spaces[i].display); | |
| } | |
| System.out.println(); | |
| while (tally <= spaces.length && (!Grid.winCheck(board))) { // play until no spaces or a winner | |
| if (tally % 2 == 0) { | |
| player = "X"; | |
| } else { | |
| player = "O"; | |
| } | |
| // move | |
| System.out.printf("\nPlayer %s: What's your move? \n", player); | |
| move = input.nextLine(); | |
| move = move.toUpperCase(); | |
| for (int i = 0; i < spaces.length; i++) { | |
| if (move.equals(spaces[i].display)) { | |
| x = spaces[i].coordX; | |
| y = spaces[i].coordY; | |
| // check if playable move, else mark as played | |
| if (!spaces[i].playable){ | |
| System.out.printf("Sorry, %s has already been played.\n", move); | |
| break; | |
| } else { | |
| spaces[i].playable = false; | |
| } | |
| board = Grid.gameplay(board, x, y, player); | |
| Grid.print(board); | |
| if (Grid.winCheck(board)){ | |
| System.out.printf("\nPlayer %s wins!\n", player); | |
| break; | |
| } else if (tally == spaces.length) { | |
| System.out.println("This game is a draw!"); | |
| break; | |
| } else { | |
| Grid.printAvailable(spaces); | |
| tally++; | |
| } | |
| } | |
| } | |
| } | |
| input.close(); | |
| } | |
| public static class Space { | |
| int coordX; | |
| int coordY; | |
| String display; | |
| boolean playable; | |
| public Space(int coordX, int coordY, String display, boolean playable) { | |
| this.coordX = coordX; | |
| this.coordY = coordY; | |
| this.display = display; | |
| this.playable = playable; | |
| } | |
| } | |
| public static Space[] initSpaces() { | |
| Space[] spaces = new Space[9]; | |
| spaces[0] = new Space(1, 1, "A1", true); | |
| spaces[1] = new Space(1, 3, "A2", true); | |
| spaces[2] = new Space(1, 5, "A3", true); | |
| spaces[3] = new Space(3, 1, "B1", true); | |
| spaces[4] = new Space(3, 3, "B2", true); | |
| spaces[5] = new Space(3, 5, "B3", true); | |
| spaces[6] = new Space(5, 1, "C1", true); | |
| spaces[7] = new Space(5, 3, "C2", true); | |
| spaces[8] = new Space(5, 5, "C3", true); | |
| return spaces; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment