From fd489bf3da56b950c1ca12f68af9eb5f94c357f2 Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Wed, 5 Jan 2022 14:30:02 -0500 Subject: [PATCH] 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() { + + } }