From 201c70e6d8d5374f4cecbc5712eec671266b3702 Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Sun, 25 Apr 2021 23:40:26 +0800 Subject: 2 Perl scripts ; Java application for Task 2 --- .../cheok-yin-fung/java/NBoxesJavaFX.java | 201 +++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java (limited to 'challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java') diff --git a/challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java b/challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java new file mode 100644 index 0000000000..ad507def6e --- /dev/null +++ b/challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java @@ -0,0 +1,201 @@ +// The Weekly Challenge - 109 +// Task 2 Four Squares(Rectangles) Puzzle +// Run with Box.java and Point.java +// JavaFX Usage may refer to https://github.com/openjfx/samples/tree/master/HelloFX +// Coding References: +// https://www.tutorialspoint.com/how-to-create-a-rectangle-using-javafx +// https://www.javatpoint.com/javafx-text + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.BitSet; + + +// JavaFX /* + +import javafx.application.Application; +import javafx.scene.Group; +import javafx.scene.Scene; +import javafx.scene.paint.Color; +import javafx.stage.Stage; +import javafx.scene.shape.Rectangle; +import javafx.scene.text.*; + +// */ JavaFX + +public class NBoxesJavaFX extends Application +// public class NBoxesJavaFX // for non-JavaFX +{ + + static Box[] box = new Box[] { + new Box( new Point(9,6), new Point(24,15) ), + new Box( new Point(20,10), new Point(35,19) ), + new Box( new Point(31,6), new Point(46,15) ), + new Box( new Point(42,10), new Point(56,19) ) + }; + + static Point[] point = new Point[] { + new Point(16,8), //a + new Point(22,12), //b + new Point(28,16), //c + new Point(33,12), //d + new Point(38,8), //e + new Point(44,12), //f + new Point(49,16) //g + }; + + public static final int N = box.length; + public static final int M = point.length; + + public static int[] oneOfAns = new int[M]; + public static ArrayList finalAnswers = new ArrayList<>(); + +// ========== BEGIN: functions for permutations ================== + public static ArrayList copy(ArrayList val) + { + ArrayList a = new ArrayList<>(); + for (int i=0; i < val.size(); i++) { + a.add(Integer.valueOf(val.get(i))); + } + return a; + } + + public static ArrayList permutations(int[] values) + { + int m = values.length; + ArrayList> arr = new ArrayList<>(); + ArrayList temp = new ArrayList<>(); + temp.add(Integer.valueOf(values[0])); + arr.add(temp); + for (int i = 2; i <= m; i++) { + int cp = arr.size(); + for (int c=0; c < i-1; c++) + for (int d=0; d < cp; d++) { + arr.add( copy( arr.get(d) ) ); + } + int count = 0; + int parameter = arr.size()/i; + for (int k=0; k < i ; k++) { + for (int c = 0; c < parameter; c++) { + ArrayList item = arr.get(count); + item.add(k, Integer.valueOf(values[i-1])); + count++; + } + } + + } + + ArrayList ans = new ArrayList<>(); + Iterator> i_arr = arr.iterator(); + while (i_arr.hasNext()) { + ans.add(toIntArray( i_arr.next() )); + } + return ans; + } + + public static int[] toIntArray (ArrayList sth) { + int[] ans = new int[sth.size()]; + for (int i = 0; i < sth.size(); i++ ) + ans[i] = sth.get(i); + return ans; + } + +// =========== END: functions for permutations ================== + +// =========== BEGIN: functions for JavaFX ======================== +// /* + public void start(Stage stage) + { + Group group = new Group(); + + Rectangle[] shape = new Rectangle[N]; + for (int i=0; i p = permutations(new int[] {1,2,3,4,5,6,7}); + + + + Iterator i_p = p.iterator(); + while (i_p.hasNext()) { + int[] now = i_p.next(); + + boolean consistency = true; + int sum = 0; + for (int j=0; j