From a98662a4f0521d3fd7537b79849f26140d171e9f Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Wed, 5 Jan 2022 11:35:09 -0500 Subject: [PATCH 01/21] Create HighIQ.java --- 48_High_IQ/java/HighIQ.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 48_High_IQ/java/HighIQ.java diff --git a/48_High_IQ/java/HighIQ.java b/48_High_IQ/java/HighIQ.java new file mode 100644 index 00000000..58f3203a --- /dev/null +++ b/48_High_IQ/java/HighIQ.java @@ -0,0 +1,4 @@ +public class HighIQ { + + +} From c9d26373b597ea8e2983f02dbca7d6426dc3ce10 Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Wed, 5 Jan 2022 11:36:38 -0500 Subject: [PATCH 02/21] Move java to src folder --- 48_High_IQ/java/{ => src}/HighIQ.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 48_High_IQ/java/{ => src}/HighIQ.java (100%) diff --git a/48_High_IQ/java/HighIQ.java b/48_High_IQ/java/src/HighIQ.java similarity index 100% rename from 48_High_IQ/java/HighIQ.java rename to 48_High_IQ/java/src/HighIQ.java From 9b47c69be55bb6b24ff8bdf4a71c735a7d14aae0 Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Wed, 5 Jan 2022 11:37:24 -0500 Subject: [PATCH 03/21] Create HighIQGame.java --- 48_High_IQ/java/src/HighIQGame.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 48_High_IQ/java/src/HighIQGame.java diff --git a/48_High_IQ/java/src/HighIQGame.java b/48_High_IQ/java/src/HighIQGame.java new file mode 100644 index 00000000..c7d4df9b --- /dev/null +++ b/48_High_IQ/java/src/HighIQGame.java @@ -0,0 +1,5 @@ +public class HighIQGame { + public static void main(String[] args) { + + } +} From 917915048e22d329c521ff8a214c132beae651a2 Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Wed, 5 Jan 2022 11:41:21 -0500 Subject: [PATCH 04/21] Create Board.java --- 48_High_IQ/java/src/Board.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 48_High_IQ/java/src/Board.java diff --git a/48_High_IQ/java/src/Board.java b/48_High_IQ/java/src/Board.java new file mode 100644 index 00000000..18b17cf5 --- /dev/null +++ b/48_High_IQ/java/src/Board.java @@ -0,0 +1,10 @@ +public class Board { + + public Board() { + + } + + public String toString() { + return ""; + } +} From 4ea34f845644d89cdc009c7c33a6937201b3fb1e Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Wed, 5 Jan 2022 11:49:07 -0500 Subject: [PATCH 05/21] Add method signatures to HighIQ.java --- 48_High_IQ/java/src/HighIQ.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/48_High_IQ/java/src/HighIQ.java b/48_High_IQ/java/src/HighIQ.java index 58f3203a..2516bb1b 100644 --- a/48_High_IQ/java/src/HighIQ.java +++ b/48_High_IQ/java/src/HighIQ.java @@ -1,4 +1,10 @@ public class HighIQ { + public HighIQ() { + + } + public void play() { + + } } From fb8a87bade402b7ddd225f8554bfe5a3af4d2682 Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Wed, 5 Jan 2022 11:59:12 -0500 Subject: [PATCH 06/21] Added the board matrix --- 48_High_IQ/java/src/Board.java | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/48_High_IQ/java/src/Board.java b/48_High_IQ/java/src/Board.java index 18b17cf5..6a320bd1 100644 --- a/48_High_IQ/java/src/Board.java +++ b/48_High_IQ/java/src/Board.java @@ -1,10 +1,28 @@ public class Board { - public Board() { - - } + private int[][] board; - public String toString() { - return ""; - } + public Board() { + board = new int[7][7]; + + //Set all of the corners to -1, and place pegs in proper spaces + for(int i = 0; i < 7; i++) { + for(int j = 0; j < 7; j++) { + if((i < 3 || i > 5) && (j < 3 || j > 5)) { + //If both i and j are either less than 3 or greater than 5, then the index is a corner + board[i][j] = -1; + } else if(i == 4 && j == 4) { + //Do not place a peg in the center + board[i][j] = 0; + } else { + //Place a peg everywhere else + board[i][j] = 1; + } + } + } + } + + public String toString() { + return ""; + } } From b250689f378aff08202fa72e735f233c2f45f109 Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Wed, 5 Jan 2022 12:20:07 -0500 Subject: [PATCH 07/21] Use Map instead of 2d array --- 48_High_IQ/java/src/Board.java | 36 ++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/48_High_IQ/java/src/Board.java b/48_High_IQ/java/src/Board.java index 6a320bd1..8d183675 100644 --- a/48_High_IQ/java/src/Board.java +++ b/48_High_IQ/java/src/Board.java @@ -1,25 +1,27 @@ +import java.util.Map; +import java.util.HashMap; + public class Board { - private int[][] board; + private final Map board; public Board() { - board = new int[7][7]; - - //Set all of the corners to -1, and place pegs in proper spaces - for(int i = 0; i < 7; i++) { - for(int j = 0; j < 7; j++) { - if((i < 3 || i > 5) && (j < 3 || j > 5)) { - //If both i and j are either less than 3 or greater than 5, then the index is a corner - board[i][j] = -1; - } else if(i == 4 && j == 4) { - //Do not place a peg in the center - board[i][j] = 0; - } else { - //Place a peg everywhere else - board[i][j] = 1; - } - } + board = new HashMap<>(); + + int[] locations = new int[] {13,14,15, + 22,23,24, + 29,30,31,32,33,34,35, + 38,39,40,42,43,44, + 47,48,49,50,51,52,53, + 58,59,60, + 67,68,69}; + + for(int i : locations) { + //put board(i) in } + + //set the center position as 0 + } public String toString() { From fd489bf3da56b950c1ca12f68af9eb5f94c357f2 Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Wed, 5 Jan 2022 14:30:02 -0500 Subject: [PATCH 08/21] Using single class --- 48_High_IQ/java/src/Board.java | 30 ------------ 48_High_IQ/java/src/HighIQ.java | 82 +++++++++++++++++++++++++++++---- 2 files changed, 74 insertions(+), 38 deletions(-) delete mode 100644 48_High_IQ/java/src/Board.java diff --git a/48_High_IQ/java/src/Board.java b/48_High_IQ/java/src/Board.java deleted file mode 100644 index 8d183675..00000000 --- a/48_High_IQ/java/src/Board.java +++ /dev/null @@ -1,30 +0,0 @@ -import java.util.Map; -import java.util.HashMap; - -public class Board { - - private final Map board; - - public Board() { - board = new HashMap<>(); - - int[] locations = new int[] {13,14,15, - 22,23,24, - 29,30,31,32,33,34,35, - 38,39,40,42,43,44, - 47,48,49,50,51,52,53, - 58,59,60, - 67,68,69}; - - for(int i : locations) { - //put board(i) in - } - - //set the center position as 0 - - } - - public String toString() { - return ""; - } -} diff --git a/48_High_IQ/java/src/HighIQ.java b/48_High_IQ/java/src/HighIQ.java index 2516bb1b..cf0c05ab 100644 --- a/48_High_IQ/java/src/HighIQ.java +++ b/48_High_IQ/java/src/HighIQ.java @@ -1,10 +1,76 @@ +import java.io.PrintStream; +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; + public class HighIQ { - - public HighIQ() { - - } - - public void play() { - - } + + private Map board; + private PrintStream out; + private Scanner scanner; + + public HighIQ() { + out = System.out; + scanner = new Scanner(System.in); + board = new HashMap<>(); + + //Set of all locations to put initial pegs on + int[] locations = new int[]{ + 13, 14, 15, 22, 23, 24, 29, 30, 31, 32, 33, 34, 35, 38, 39, 40, 42, 43, 44, 47, 48, 49, 50, 51, 52, 53, 58, 59, 60, 67, 68, 69 + }; + + for (int i : locations) { + board.put(i, true); + } + + board.put(41, false); + } + + public boolean move() { + System.out.println("MOVE WHICH PIECE"); + int from = scanner.nextInt(); + + //using the getOrDefault, which will make the statement false if it is an invalid position + if(!board.getOrDefault(from,false)) { + return false; + } + + System.out.println("TO WHERE"); + int to = scanner.nextInt(); + + if(board.getOrDefault(to,true)) { + return false; + } + + //Do nothing if they are the same + if(from == to) { + return true; + } + + //using the difference to check if the relative locations are valid + int difference = Math.abs(to - from); + if(difference != 2 && difference != 18) { + return false; + } + + //check if there is a peg between from and to + if(!board.getOrDefault((to + from) / 2,false)) { + return false; + } + + return true; + } + + public void play() { + while(true) { + while(!move()) { + System.out.println("ILLEGAL MOVE, TRY AGAIN..."); + + } + } + } + + public void printBoard() { + + } } From f8cc93aaeab6f7a73ebc15e1a91957b75e794e5a Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Thu, 6 Jan 2022 08:50:17 -0500 Subject: [PATCH 09/21] added getChar method --- 48_High_IQ/java/src/HighIQ.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/48_High_IQ/java/src/HighIQ.java b/48_High_IQ/java/src/HighIQ.java index cf0c05ab..75426197 100644 --- a/48_High_IQ/java/src/HighIQ.java +++ b/48_High_IQ/java/src/HighIQ.java @@ -71,6 +71,17 @@ public class HighIQ { } public void printBoard() { - + + } + + private char getChar(int position) { + Boolean value = board.get(position); + if(value == null) { + return ' '; + } else if(value) { + return '!'; + } else { + return 'O'; + } } } From 918b42149e0f4abe2512eaf69343fb1cbe7baef5 Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Thu, 6 Jan 2022 08:52:12 -0500 Subject: [PATCH 10/21] Implemented Possible printBoard Doing this through web browser, so unsure if this will work or compile.. Will check later --- 48_High_IQ/java/src/HighIQ.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/48_High_IQ/java/src/HighIQ.java b/48_High_IQ/java/src/HighIQ.java index 75426197..93340556 100644 --- a/48_High_IQ/java/src/HighIQ.java +++ b/48_High_IQ/java/src/HighIQ.java @@ -71,7 +71,12 @@ public class HighIQ { } public void printBoard() { - + for(int i = 0; i < 7; i++) { + for(int j = 11; j < 18; j++) { + out.print(getChar(j + 9 * i)); + } + out.println(); + } } private char getChar(int position) { From 056c8cd4d971174bd68aed81708a2de1b39e59a2 Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Thu, 6 Jan 2022 09:06:15 -0500 Subject: [PATCH 11/21] Update HighIQ.java --- 48_High_IQ/java/src/HighIQ.java | 36 ++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/48_High_IQ/java/src/HighIQ.java b/48_High_IQ/java/src/HighIQ.java index 93340556..77e0e5ae 100644 --- a/48_High_IQ/java/src/HighIQ.java +++ b/48_High_IQ/java/src/HighIQ.java @@ -62,12 +62,38 @@ public class HighIQ { } public void play() { - while(true) { - while(!move()) { - System.out.println("ILLEGAL MOVE, TRY AGAIN..."); - + do { + do { + while(!move()) { + System.out.println("ILLEGAL MOVE, TRY AGAIN..."); + } + } while(!isGameFinished()); + + int pegCount = 0; + for(Integer key : board.getKeySet()) { + if(board.getOrDefault(key,false)) { + pegCount++; + } } - } + + out.println("YOU HAD " + pegCount + " PEGS REMAINING"); + + if(pegCount == 1) { + out.println("BRAVO! YOU MADE A PERFECT SCORE!"); + out.println("SAVE THIS PAPER AS A RECORD OF YOUR ACCOMPLISHMENT!"); + } + + } while(playAgain()); + } + + private boolean playAgain() { + out.println("PLAY AGAIN (YES OR NO)"); + return scanner.nextLine().toLowerCase().equals("yes"); + } + + + public boolean isGameFinished() { + return false; } public void printBoard() { From aaa8d6186061983dc6af1f8b7b9ec09c0b926353 Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Thu, 6 Jan 2022 09:34:20 -0500 Subject: [PATCH 12/21] Implemented GameFinished does it work? Unsure... --- 48_High_IQ/java/src/HighIQ.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/48_High_IQ/java/src/HighIQ.java b/48_High_IQ/java/src/HighIQ.java index 77e0e5ae..99d046fe 100644 --- a/48_High_IQ/java/src/HighIQ.java +++ b/48_High_IQ/java/src/HighIQ.java @@ -93,7 +93,26 @@ public class HighIQ { public boolean isGameFinished() { - return false; + for(Integer key : board.getKeySet()) { + if(board.get(key)) { + //Spacing is either 1 or 9 + for(int space = 1; space <= 9; space += 8) { + //Next val is the next spot, prev and next after are the two spots where the peg would go if a move was possible + Boolean nextVal = board.get(key + space); + Boolean prevAfter = board.get(key - space); + Boolean nextAfter = board.get(key + space * 2); + + if(nextVal != null && nextVal) { + if((prevAfter != null && !prevAfter) || (nextAfter != null && !nextAfter)) { + return false; + } + } + + } + } + + } + return true; } public void printBoard() { From 31dec710639d360536207692634a24b3e2bf108e Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Thu, 6 Jan 2022 09:45:04 -0500 Subject: [PATCH 13/21] Simplified isGameFinished Unsure if I should simplify further --- 48_High_IQ/java/src/HighIQ.java | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/48_High_IQ/java/src/HighIQ.java b/48_High_IQ/java/src/HighIQ.java index 99d046fe..83cff254 100644 --- a/48_High_IQ/java/src/HighIQ.java +++ b/48_High_IQ/java/src/HighIQ.java @@ -96,18 +96,13 @@ public class HighIQ { for(Integer key : board.getKeySet()) { if(board.get(key)) { //Spacing is either 1 or 9 - for(int space = 1; space <= 9; space += 8) { - //Next val is the next spot, prev and next after are the two spots where the peg would go if a move was possible - Boolean nextVal = board.get(key + space); - Boolean prevAfter = board.get(key - space); - Boolean nextAfter = board.get(key + space * 2); - - if(nextVal != null && nextVal) { - if((prevAfter != null && !prevAfter) || (nextAfter != null && !nextAfter)) { - return false; - } + //Looking to the right and down from every point, checking for both directions of movement + for(int space : new int[] {1,9}) { + Boolean nextToPeg = board.getOrDefault(key + space,false); + Boolean hasMovableSpace = !board.getOrDefault(key - space,true) || !board.getOrDefault(key + space * 2, true); + if(nextToPeg && hasMovableSpace) { + return false; } - } } From e19a4aef19457cdb6256899c062b88e2580679fd Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Thu, 6 Jan 2022 09:46:58 -0500 Subject: [PATCH 14/21] Added leaving comment --- 48_High_IQ/java/src/HighIQ.java | 1 + 1 file changed, 1 insertion(+) diff --git a/48_High_IQ/java/src/HighIQ.java b/48_High_IQ/java/src/HighIQ.java index 83cff254..b1a40831 100644 --- a/48_High_IQ/java/src/HighIQ.java +++ b/48_High_IQ/java/src/HighIQ.java @@ -84,6 +84,7 @@ public class HighIQ { } } while(playAgain()); + out.println("SO LONG FOR NOW."); } private boolean playAgain() { From 821ae4befe7bb7845e7fcea6f1f28a0308bd778a Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Thu, 6 Jan 2022 10:02:44 -0500 Subject: [PATCH 15/21] Moving re-playability to other script --- 48_High_IQ/java/src/HighIQ.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/48_High_IQ/java/src/HighIQ.java b/48_High_IQ/java/src/HighIQ.java index b1a40831..37fd6aa2 100644 --- a/48_High_IQ/java/src/HighIQ.java +++ b/48_High_IQ/java/src/HighIQ.java @@ -9,9 +9,9 @@ public class HighIQ { private PrintStream out; private Scanner scanner; - public HighIQ() { + public HighIQ(Scanner sccanner) { out = System.out; - scanner = new Scanner(System.in); + this.scanner = scanner; board = new HashMap<>(); //Set of all locations to put initial pegs on @@ -25,6 +25,7 @@ public class HighIQ { board.put(41, false); } + public boolean move() { System.out.println("MOVE WHICH PIECE"); @@ -63,7 +64,6 @@ public class HighIQ { public void play() { do { - do { while(!move()) { System.out.println("ILLEGAL MOVE, TRY AGAIN..."); } @@ -83,14 +83,12 @@ public class HighIQ { out.println("SAVE THIS PAPER AS A RECORD OF YOUR ACCOMPLISHMENT!"); } - } while(playAgain()); - out.println("SO LONG FOR NOW."); } - private boolean playAgain() { - out.println("PLAY AGAIN (YES OR NO)"); - return scanner.nextLine().toLowerCase().equals("yes"); - } +// private boolean playAgain() { +// out.println("PLAY AGAIN (YES OR NO)"); +// return scanner.nextLine().toLowerCase().equals("yes"); +// } public boolean isGameFinished() { From 048b9c31a283b6b40bafd3480cd7cc5667f1eab6 Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Thu, 6 Jan 2022 11:38:17 -0500 Subject: [PATCH 16/21] Added re-playability --- 48_High_IQ/java/src/HighIQGame.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/48_High_IQ/java/src/HighIQGame.java b/48_High_IQ/java/src/HighIQGame.java index c7d4df9b..efbd09ec 100644 --- a/48_High_IQ/java/src/HighIQGame.java +++ b/48_High_IQ/java/src/HighIQGame.java @@ -1,5 +1,11 @@ +import java.util.Scanner; + public class HighIQGame { public static void main(String[] args) { - + Scanner scanner = new Scanner(System.in); + do { + new HighIQ(scanner).play(); + System.out.println("PLAY AGAIN (YES OR NO)"); + } while(scanner.nextLine().toLowerCase().equals("yes")); } } From 18016b59ed7f647cef92213243cb20ba8b689983 Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Thu, 6 Jan 2022 13:15:48 -0500 Subject: [PATCH 17/21] Typo Fixes --- 48_High_IQ/java/src/HighIQ.java | 6 +++--- 48_High_IQ/java/src/HighIQGame.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/48_High_IQ/java/src/HighIQ.java b/48_High_IQ/java/src/HighIQ.java index 37fd6aa2..37528748 100644 --- a/48_High_IQ/java/src/HighIQ.java +++ b/48_High_IQ/java/src/HighIQ.java @@ -9,7 +9,7 @@ public class HighIQ { private PrintStream out; private Scanner scanner; - public HighIQ(Scanner sccanner) { + public HighIQ(Scanner scanner) { out = System.out; this.scanner = scanner; board = new HashMap<>(); @@ -70,7 +70,7 @@ public class HighIQ { } while(!isGameFinished()); int pegCount = 0; - for(Integer key : board.getKeySet()) { + for(Integer key : board.keySet()) { if(board.getOrDefault(key,false)) { pegCount++; } @@ -92,7 +92,7 @@ public class HighIQ { public boolean isGameFinished() { - for(Integer key : board.getKeySet()) { + for(Integer key : board.keySet()) { if(board.get(key)) { //Spacing is either 1 or 9 //Looking to the right and down from every point, checking for both directions of movement diff --git a/48_High_IQ/java/src/HighIQGame.java b/48_High_IQ/java/src/HighIQGame.java index efbd09ec..4df22853 100644 --- a/48_High_IQ/java/src/HighIQGame.java +++ b/48_High_IQ/java/src/HighIQGame.java @@ -6,6 +6,6 @@ public class HighIQGame { do { new HighIQ(scanner).play(); System.out.println("PLAY AGAIN (YES OR NO)"); - } while(scanner.nextLine().toLowerCase().equals("yes")); + } while(scanner.nextLine().equalsIgnoreCase("yes")); } } From 45345d593d136668e36d63668eed61e124435f63 Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Thu, 6 Jan 2022 13:16:55 -0500 Subject: [PATCH 18/21] Reformatted Code --- 48_High_IQ/java/src/HighIQ.java | 84 ++++++++++++++++----------------- 1 file changed, 40 insertions(+), 44 deletions(-) diff --git a/48_High_IQ/java/src/HighIQ.java b/48_High_IQ/java/src/HighIQ.java index 37528748..5c3f31aa 100644 --- a/48_High_IQ/java/src/HighIQ.java +++ b/48_High_IQ/java/src/HighIQ.java @@ -5,7 +5,7 @@ import java.util.Scanner; public class HighIQ { - private Map board; + private Map board; private PrintStream out; private Scanner scanner; @@ -25,104 +25,100 @@ public class HighIQ { board.put(41, false); } - + + public void play() { + do { + while (!move()) { + System.out.println("ILLEGAL MOVE, TRY AGAIN..."); + } + } while (!isGameFinished()); + + int pegCount = 0; + for (Integer key : board.keySet()) { + if (board.getOrDefault(key, false)) { + pegCount++; + } + } + + out.println("YOU HAD " + pegCount + " PEGS REMAINING"); + + if (pegCount == 1) { + out.println("BRAVO! YOU MADE A PERFECT SCORE!"); + out.println("SAVE THIS PAPER AS A RECORD OF YOUR ACCOMPLISHMENT!"); + } + } public boolean move() { System.out.println("MOVE WHICH PIECE"); int from = scanner.nextInt(); //using the getOrDefault, which will make the statement false if it is an invalid position - if(!board.getOrDefault(from,false)) { + if (!board.getOrDefault(from, false)) { return false; } System.out.println("TO WHERE"); int to = scanner.nextInt(); - if(board.getOrDefault(to,true)) { + if (board.getOrDefault(to, true)) { return false; } //Do nothing if they are the same - if(from == to) { + if (from == to) { return true; } //using the difference to check if the relative locations are valid int difference = Math.abs(to - from); - if(difference != 2 && difference != 18) { + if (difference != 2 && difference != 18) { return false; } //check if there is a peg between from and to - if(!board.getOrDefault((to + from) / 2,false)) { + if (!board.getOrDefault((to + from) / 2, false)) { return false; } return true; } - public void play() { - do { - while(!move()) { - System.out.println("ILLEGAL MOVE, TRY AGAIN..."); - } - } while(!isGameFinished()); - - int pegCount = 0; - for(Integer key : board.keySet()) { - if(board.getOrDefault(key,false)) { - pegCount++; - } - } - - out.println("YOU HAD " + pegCount + " PEGS REMAINING"); - - if(pegCount == 1) { - out.println("BRAVO! YOU MADE A PERFECT SCORE!"); - out.println("SAVE THIS PAPER AS A RECORD OF YOUR ACCOMPLISHMENT!"); - } - - } - // private boolean playAgain() { // out.println("PLAY AGAIN (YES OR NO)"); // return scanner.nextLine().toLowerCase().equals("yes"); // } - - + public boolean isGameFinished() { - for(Integer key : board.keySet()) { - if(board.get(key)) { + for (Integer key : board.keySet()) { + if (board.get(key)) { //Spacing is either 1 or 9 //Looking to the right and down from every point, checking for both directions of movement - for(int space : new int[] {1,9}) { - Boolean nextToPeg = board.getOrDefault(key + space,false); - Boolean hasMovableSpace = !board.getOrDefault(key - space,true) || !board.getOrDefault(key + space * 2, true); - if(nextToPeg && hasMovableSpace) { + for (int space : new int[]{1, 9}) { + Boolean nextToPeg = board.getOrDefault(key + space, false); + Boolean hasMovableSpace = !board.getOrDefault(key - space, true) || !board.getOrDefault(key + space * 2, true); + if (nextToPeg && hasMovableSpace) { return false; } } } - } return true; } public void printBoard() { - for(int i = 0; i < 7; i++) { - for(int j = 11; j < 18; j++) { + for (int i = 0; i < 7; i++) { + for (int j = 11; j < 18; j++) { out.print(getChar(j + 9 * i)); } out.println(); } } - + private char getChar(int position) { Boolean value = board.get(position); - if(value == null) { + if (value == null) { return ' '; - } else if(value) { + } else if (value) { return '!'; } else { return 'O'; From 3d4f5c685adbb4865e707490474cf039efacd9c7 Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Thu, 6 Jan 2022 13:22:56 -0500 Subject: [PATCH 19/21] Added Instructions --- 48_High_IQ/java/src/HighIQ.java | 3 +++ 48_High_IQ/java/src/HighIQGame.java | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/48_High_IQ/java/src/HighIQ.java b/48_High_IQ/java/src/HighIQ.java index 5c3f31aa..a0278771 100644 --- a/48_High_IQ/java/src/HighIQ.java +++ b/48_High_IQ/java/src/HighIQ.java @@ -27,7 +27,10 @@ public class HighIQ { } public void play() { + + do { + printBoard(); while (!move()) { System.out.println("ILLEGAL MOVE, TRY AGAIN..."); } diff --git a/48_High_IQ/java/src/HighIQGame.java b/48_High_IQ/java/src/HighIQGame.java index 4df22853..e74e5783 100644 --- a/48_High_IQ/java/src/HighIQGame.java +++ b/48_High_IQ/java/src/HighIQGame.java @@ -2,10 +2,34 @@ import java.util.Scanner; public class HighIQGame { public static void main(String[] args) { + + printInstructions(); + Scanner scanner = new Scanner(System.in); do { new HighIQ(scanner).play(); System.out.println("PLAY AGAIN (YES OR NO)"); } while(scanner.nextLine().equalsIgnoreCase("yes")); } + + public static void printInstructions() { + System.out.println("HERE IS THE BOARD:"); + System.out.println(" ! ! !"); + System.out.println(" 13 14 15\n"); + System.out.println(" ! ! !"); + System.out.println(" 22 23 24\n"); + System.out.println("! ! ! ! ! ! !"); + System.out.println("29 30 31 32 33 34 35\n"); + System.out.println("! ! ! ! ! ! !"); + System.out.println("38 39 40 41 42 43 44\n"); + System.out.println("! ! ! ! ! ! !"); + System.out.println("47 48 49 50 51 52 53\n"); + System.out.println(" ! ! !"); + System.out.println(" 58 59 60\n"); + System.out.println(" ! ! !"); + System.out.println(" 67 68 69"); + System.out.println("TO SAVE TYPING TIME, A COMPRESSED VERSION OF THE GAME BOARD"); + System.out.println("WILL BE USED DURING PLAY. REFER TO THE ABOVE ONE FOR PEG"); + System.out.println("NUMBERS. OK, LET'S BEGIN."); + } } From 110b77fccd5d8cbc956abae1c1538964ab9a8e27 Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Thu, 6 Jan 2022 13:35:37 -0500 Subject: [PATCH 20/21] Cleanup --- 48_High_IQ/java/src/HighIQ.java | 49 ++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/48_High_IQ/java/src/HighIQ.java b/48_High_IQ/java/src/HighIQ.java index a0278771..d77a2ec2 100644 --- a/48_High_IQ/java/src/HighIQ.java +++ b/48_High_IQ/java/src/HighIQ.java @@ -3,11 +3,25 @@ import java.util.HashMap; import java.util.Map; import java.util.Scanner; +/** + * Game of HighIQ + *

+ * Based on the Basic Game of HighIQ Here: + * https://github.com/coding-horror/basic-computer-games/blob/main/48_High_IQ/highiq.bas + * + * No additional functionality has been added + */ public class HighIQ { - private Map board; - private PrintStream out; - private Scanner scanner; + //Game board, as a map of position numbers to their values + private final Map board; + + //Output stream + private final PrintStream out; + + //Input scanner to use + private final Scanner scanner; + public HighIQ(Scanner scanner) { out = System.out; @@ -26,13 +40,14 @@ public class HighIQ { board.put(41, false); } + /** + * Plays the actual game, from start to finish. + */ public void play() { - - do { printBoard(); while (!move()) { - System.out.println("ILLEGAL MOVE, TRY AGAIN..."); + out.println("ILLEGAL MOVE, TRY AGAIN..."); } } while (!isGameFinished()); @@ -51,8 +66,12 @@ public class HighIQ { } } + /** + * Makes an individual move + * @return True if the move was valid, false if the user made an error and the move is invalid + */ public boolean move() { - System.out.println("MOVE WHICH PIECE"); + out.println("MOVE WHICH PIECE"); int from = scanner.nextInt(); //using the getOrDefault, which will make the statement false if it is an invalid position @@ -60,7 +79,7 @@ public class HighIQ { return false; } - System.out.println("TO WHERE"); + out.println("TO WHERE"); int to = scanner.nextInt(); if (board.getOrDefault(to, true)) { @@ -83,14 +102,18 @@ public class HighIQ { return false; } + //Actually move + board.put(from,false); + board.put(to,true); + board.put((from + to) / 2, false); + return true; } -// private boolean playAgain() { -// out.println("PLAY AGAIN (YES OR NO)"); -// return scanner.nextLine().toLowerCase().equals("yes"); -// } - + /** + * Checks if the game is finished + * @return True if there are no more moves, False otherwise + */ public boolean isGameFinished() { for (Integer key : board.keySet()) { if (board.get(key)) { From 60b84070c99624f8fa5d7db18c9d26d854b90686 Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Thu, 6 Jan 2022 13:40:28 -0500 Subject: [PATCH 21/21] Added Header --- 48_High_IQ/java/src/HighIQGame.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/48_High_IQ/java/src/HighIQGame.java b/48_High_IQ/java/src/HighIQGame.java index e74e5783..43400fb3 100644 --- a/48_High_IQ/java/src/HighIQGame.java +++ b/48_High_IQ/java/src/HighIQGame.java @@ -13,6 +13,8 @@ public class HighIQGame { } public static void printInstructions() { + System.out.println("\t\t\t H-I-Q"); + System.out.println("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); System.out.println("HERE IS THE BOARD:"); System.out.println(" ! ! !"); System.out.println(" 13 14 15\n");