diff --git a/89 Tic-Tac-Toe/java/src/Board.java b/89 Tic-Tac-Toe/java/src/Board.java new file mode 100644 index 00000000..091f7bb2 --- /dev/null +++ b/89 Tic-Tac-Toe/java/src/Board.java @@ -0,0 +1,88 @@ +/** + * @author Ollie Hensman-Crook + */ +public class Board { + private char arr[]; + + public Board() { + this.arr = new char[9]; + for (int x = 1; x <= 9; x++) { + this.arr[x - 1] = ' '; + } + } + + + /** + * Place 'X' or 'O' on the board position passed + * @param position + * @param player + */ + public void setArr(int position, char player) { + if (player == 'X') { + this.arr[position-1] = 'X'; + } else { + this.arr[position -1] = 'O'; + } + } + + public void printBoard() { + System.out.format("%-3c ! %-3c ! %-3c\n----+----+----\n%-3c ! %-3c ! %-3c\n----+----+----\n%-3c ! %-3c ! %-3c\n", + this.arr[0], this.arr[1], this.arr[2], this.arr[3], this.arr[4], this.arr[5], this.arr[6], this.arr[7], this.arr[8] + ); + } + + + /** + * @param x + * @return the value of the char at a given position + */ + public char getBoardValue(int x) { + return arr[x-1]; + } + + + /** + * Go through the board and check for win (horizontal, diagonal, vertical) + * @param player + * @return whether a win has occured + */ + public boolean checkWin(char player) { + if(this.arr[0] == player && this.arr[1] == player && this.arr[2] == player) + return true; + + + if(this.arr[3] == player && this.arr[4] == player && this.arr[5] == player) + return true; + + + if(this.arr[6] == player && this.arr[7] == player && this.arr[8] == player) + return true; + + if(this.arr[0] == player && this.arr[4] == player && this.arr[8] == player) + return true; + + if(this.arr[2] == player && this.arr[4] == player && this.arr[6] == player) + return true; + + if(this.arr[0] == player && this.arr[3] == player && this.arr[6] == player) + return true; + + if(this.arr[1] == player && this.arr[4] == player && this.arr[7] == player) + return true; + + if(this.arr[2] == player && this.arr[5] == player && this.arr[8] == player) + return true; + + return false; + } + + /** + * Reset the board + */ + public void clear() { + for (int x = 1; x <= 9; x++) { + this.arr[x - 1] = ' '; + } + } + +} \ No newline at end of file diff --git a/89 Tic-Tac-Toe/java/src/TicTacToe2.java b/89 Tic-Tac-Toe/java/src/TicTacToe2.java new file mode 100644 index 00000000..c186ea1d --- /dev/null +++ b/89 Tic-Tac-Toe/java/src/TicTacToe2.java @@ -0,0 +1,134 @@ +import java.util.Scanner; +import java.util.Random; + +/** + * @author Ollie Hensman-Crook + */ +public class TicTacToe2 { + public static void main(String[] args) { + Board gameBoard = new Board(); + Random compChoice = new Random(); + char yourChar; + char compChar; + Scanner in = new Scanner(System.in); + + System.out.println(" TIC-TAC-TOE"); + System.out.println("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); + System.out.println("\nTHE BOARD IS NUMBERED: "); + System.out.println(" 1 2 3\n 4 5 6\n 7 8 9\n"); + + while (true) { + // ask if the player wants to be X or O and if their input is valid set their + // play piece as such + System.out.println("DO YOU WANT 'X' OR 'O'"); + while (true) { + try { + char input; + input = in.next().charAt(0); + + if (input == 'X' || input == 'x') { + yourChar = 'X'; + compChar = 'O'; + break; + } else if (input == 'O' || input == 'o') { + yourChar = 'O'; + compChar = 'X'; + } else { + System.out.println("THATS NOT 'X' OR 'O', TRY AGAIN"); + in.nextLine(); + } + + } catch (Exception e) { + System.out.println("THATS NOT 'X' OR 'O', TRY AGAIN"); + in.nextLine(); + } + } + + while (true) { + System.out.println("WHERE DO YOU MOVE"); + + // check the user can move where they want to and if so move them there + while (true) { + int input; + try { + input = in.nextInt(); + if (gameBoard.getBoardValue(input) == ' ') { + gameBoard.setArr(input, yourChar); + break; + } else { + System.out.println("INVALID INPUT, TRY AGAIN"); + } + in.nextLine(); + } catch (Exception e) { + System.out.println("INVALID INPUT, TRY AGAIN"); + in.nextLine(); + } + } + + gameBoard.printBoard(); + System.out.println("THE COMPUTER MOVES TO"); + + while (true) { + int position = 1 + compChoice.nextInt(9); + if (gameBoard.getBoardValue(position) == ' ') { + gameBoard.setArr(position, compChar); + break; + } + } + + gameBoard.printBoard(); + + // if there is a win print if player won or the computer won and ask if they + // want to play again + if (gameBoard.checkWin(yourChar)) { + System.out.println("YOU WIN, PLAY AGAIN? (Y/N)"); + gameBoard.clear(); + while (true) { + try { + char input; + input = in.next().charAt(0); + + if (input == 'Y' || input == 'y') { + break; + } else if (input == 'N' || input == 'n') { + System.exit(0); + } else { + System.out.println("THATS NOT 'Y' OR 'N', TRY AGAIN"); + in.nextLine(); + } + + } catch (Exception e) { + System.out.println("THATS NOT 'Y' OR 'N', TRY AGAIN"); + in.nextLine(); + } + } + break; + } else if (gameBoard.checkWin(compChar)) { + System.out.println("YOU LOSE, PLAY AGAIN? (Y/N)"); + gameBoard.clear(); + while (true) { + try { + char input; + input = in.next().charAt(0); + + if (input == 'Y' || input == 'y') { + break; + } else if (input == 'N' || input == 'n') { + System.exit(0); + } else { + System.out.println("THATS NOT 'Y' OR 'N', TRY AGAIN"); + in.nextLine(); + } + + } catch (Exception e) { + System.out.println("THATS NOT 'Y' OR 'N', TRY AGAIN"); + in.nextLine(); + } + } + break; + } + + } + } + } +}