From 221fa7fe095b3ac3aaf05d4afe303ac8983f382f Mon Sep 17 00:00:00 2001 From: Oliver Hensman-Crook Date: Wed, 3 Mar 2021 17:18:28 +0000 Subject: [PATCH 1/6] Tic-Tac-Toe java port --- 89 Tic-Tac-Toe/java/gitignore | 3 + 89 Tic-Tac-Toe/java/src/Board.java | 88 +++++++++++++++ 89 Tic-Tac-Toe/java/src/TicTacToe2.java | 135 ++++++++++++++++++++++++ 89 Tic-Tac-Toe/java/src/make.sh | 2 + 89 Tic-Tac-Toe/java/src/run.sh | 3 + 5 files changed, 231 insertions(+) create mode 100644 89 Tic-Tac-Toe/java/gitignore create mode 100644 89 Tic-Tac-Toe/java/src/Board.java create mode 100644 89 Tic-Tac-Toe/java/src/TicTacToe2.java create mode 100755 89 Tic-Tac-Toe/java/src/make.sh create mode 100755 89 Tic-Tac-Toe/java/src/run.sh diff --git a/89 Tic-Tac-Toe/java/gitignore b/89 Tic-Tac-Toe/java/gitignore new file mode 100644 index 00000000..0909e4b5 --- /dev/null +++ b/89 Tic-Tac-Toe/java/gitignore @@ -0,0 +1,3 @@ +bin +src/make.sh +src/run.sh \ No newline at end of file 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..5fe69cfd --- /dev/null +++ b/89 Tic-Tac-Toe/java/src/TicTacToe2.java @@ -0,0 +1,135 @@ +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(); + } + 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; + } + + } + } + } +} diff --git a/89 Tic-Tac-Toe/java/src/make.sh b/89 Tic-Tac-Toe/java/src/make.sh new file mode 100755 index 00000000..9492cfea --- /dev/null +++ b/89 Tic-Tac-Toe/java/src/make.sh @@ -0,0 +1,2 @@ +javac -d ../bin *.java + diff --git a/89 Tic-Tac-Toe/java/src/run.sh b/89 Tic-Tac-Toe/java/src/run.sh new file mode 100755 index 00000000..84c9d803 --- /dev/null +++ b/89 Tic-Tac-Toe/java/src/run.sh @@ -0,0 +1,3 @@ +cd ../bin +java TicTacToe2 +cd ../src From 35845b0962312a2c1ce2ded7894681dfbe1df2dd Mon Sep 17 00:00:00 2001 From: Oliver Hensman-Crook Date: Wed, 3 Mar 2021 17:20:46 +0000 Subject: [PATCH 2/6] Tic-Tac-Toe java port --- 89 Tic-Tac-Toe/java/.gitignore | 2 ++ 89 Tic-Tac-Toe/java/gitignore | 3 --- 2 files changed, 2 insertions(+), 3 deletions(-) create mode 100644 89 Tic-Tac-Toe/java/.gitignore delete mode 100644 89 Tic-Tac-Toe/java/gitignore diff --git a/89 Tic-Tac-Toe/java/.gitignore b/89 Tic-Tac-Toe/java/.gitignore new file mode 100644 index 00000000..6e532130 --- /dev/null +++ b/89 Tic-Tac-Toe/java/.gitignore @@ -0,0 +1,2 @@ +src/make.sh +src/run.sh diff --git a/89 Tic-Tac-Toe/java/gitignore b/89 Tic-Tac-Toe/java/gitignore deleted file mode 100644 index 0909e4b5..00000000 --- a/89 Tic-Tac-Toe/java/gitignore +++ /dev/null @@ -1,3 +0,0 @@ -bin -src/make.sh -src/run.sh \ No newline at end of file From c6e6192f2b2cea7ee93fc90658eea8de32f6396e Mon Sep 17 00:00:00 2001 From: olliehcrook <53153427+olliehcrook@users.noreply.github.com> Date: Wed, 3 Mar 2021 17:28:23 +0000 Subject: [PATCH 3/6] Delete make.sh --- 89 Tic-Tac-Toe/java/src/make.sh | 2 -- 1 file changed, 2 deletions(-) delete mode 100755 89 Tic-Tac-Toe/java/src/make.sh diff --git a/89 Tic-Tac-Toe/java/src/make.sh b/89 Tic-Tac-Toe/java/src/make.sh deleted file mode 100755 index 9492cfea..00000000 --- a/89 Tic-Tac-Toe/java/src/make.sh +++ /dev/null @@ -1,2 +0,0 @@ -javac -d ../bin *.java - From 3ff7a1c486de7c880e7a79a25e2edc4025834f3e Mon Sep 17 00:00:00 2001 From: olliehcrook <53153427+olliehcrook@users.noreply.github.com> Date: Wed, 3 Mar 2021 17:28:33 +0000 Subject: [PATCH 4/6] Delete run.sh --- 89 Tic-Tac-Toe/java/src/run.sh | 3 --- 1 file changed, 3 deletions(-) delete mode 100755 89 Tic-Tac-Toe/java/src/run.sh diff --git a/89 Tic-Tac-Toe/java/src/run.sh b/89 Tic-Tac-Toe/java/src/run.sh deleted file mode 100755 index 84c9d803..00000000 --- a/89 Tic-Tac-Toe/java/src/run.sh +++ /dev/null @@ -1,3 +0,0 @@ -cd ../bin -java TicTacToe2 -cd ../src From f5d949502b01e6fedd5987a6d72ee69be932ca2c Mon Sep 17 00:00:00 2001 From: olliehcrook <53153427+olliehcrook@users.noreply.github.com> Date: Wed, 3 Mar 2021 17:29:03 +0000 Subject: [PATCH 5/6] Delete .gitignore --- 89 Tic-Tac-Toe/java/.gitignore | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 89 Tic-Tac-Toe/java/.gitignore diff --git a/89 Tic-Tac-Toe/java/.gitignore b/89 Tic-Tac-Toe/java/.gitignore deleted file mode 100644 index 6e532130..00000000 --- a/89 Tic-Tac-Toe/java/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -src/make.sh -src/run.sh From c1bb37472670b707d6aa9ed38aa4add17f4a5916 Mon Sep 17 00:00:00 2001 From: olliehcrook <53153427+olliehcrook@users.noreply.github.com> Date: Wed, 3 Mar 2021 17:37:23 +0000 Subject: [PATCH 6/6] Update TicTacToe2.java --- 89 Tic-Tac-Toe/java/src/TicTacToe2.java | 1 - 1 file changed, 1 deletion(-) diff --git a/89 Tic-Tac-Toe/java/src/TicTacToe2.java b/89 Tic-Tac-Toe/java/src/TicTacToe2.java index 5fe69cfd..c186ea1d 100644 --- a/89 Tic-Tac-Toe/java/src/TicTacToe2.java +++ b/89 Tic-Tac-Toe/java/src/TicTacToe2.java @@ -57,7 +57,6 @@ public class TicTacToe2 { break; } else { System.out.println("INVALID INPUT, TRY AGAIN"); - //in.nextLine(); } in.nextLine(); } catch (Exception e) {