aboutsummaryrefslogtreecommitdiff
path: root/Java/Beginer training/src
diff options
context:
space:
mode:
authorSyndamia <kami02882@gmail.com>2019-07-29 11:46:36 +0300
committerSyndamia <kami02882@gmail.com>2019-07-29 11:46:36 +0300
commitbc09da5a7b65b08b5d5dcd1e90173ad3b6081c23 (patch)
treec66cebc02aac30ff859c06ca462f3dd58b6809b0 /Java/Beginer training/src
parent65edf7296baf48aad1b4e0c09b57f1a7f48791a8 (diff)
downloadSelf-learning-bc09da5a7b65b08b5d5dcd1e90173ad3b6081c23.tar
Self-learning-bc09da5a7b65b08b5d5dcd1e90173ad3b6081c23.tar.gz
Self-learning-bc09da5a7b65b08b5d5dcd1e90173ad3b6081c23.zip
Did some more work in Python and started officially learning Java
Diffstat (limited to 'Java/Beginer training/src')
-rw-r--r--Java/Beginer training/src/Game_test.java72
-rw-r--r--Java/Beginer training/src/Training.java18
-rw-r--r--Java/Beginer training/src/VampireNumber explanation.txt39
-rw-r--r--Java/Beginer training/src/VampireNumber.java45
4 files changed, 174 insertions, 0 deletions
diff --git a/Java/Beginer training/src/Game_test.java b/Java/Beginer training/src/Game_test.java
new file mode 100644
index 0000000..e0400dd
--- /dev/null
+++ b/Java/Beginer training/src/Game_test.java
@@ -0,0 +1,72 @@
+import java.util.Arrays;
+import java.io.*;
+import java.text.*;
+
+public class Game_test {
+ public static final char[][] ORIGINAL_MAP = {
+ {' ', '-', '-', '-', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '[', ' ', '|', ' '},
+ {' ', '-', '<', '-', ' '}
+ };
+
+ public static void main(String args[]) {
+ Console cons = System.console();
+
+ while (true){
+ ReWriteMap();
+ System.out.println("Type direction and press enter:");
+ String direction = cons.readLine();
+
+ switch (direction.charAt(0)) {
+ case 'w': Player.yPos++; break;
+ case 's': Player.yPos--; break;
+ case 'a': Player.xPos--; break;
+ case 'd': Player.xPos++; break;
+ }
+
+ try { Thread.sleep(1000); }
+ catch (Exception ex) { }
+ }
+ }
+
+ public static void ReWriteMap(){
+ var map = Arrays.copyOf(ORIGINAL_MAP, ORIGINAL_MAP.length);
+
+ for (int i = 0; i < map.length; i++){
+ for (int j = 0; j < map[0].length; j++){
+ if (i == Player.yPos && j == Player.xPos) map[i][j] = Player.body;
+
+ System.out.print(map[i][j]);
+ }
+ System.out.println();
+ }
+ }
+}
+
+class Player {
+ public static char body = '*';
+
+ public static int yPos = 0, xPos = 1; //y is up/down ; x is left/right
+}
diff --git a/Java/Beginer training/src/Training.java b/Java/Beginer training/src/Training.java
new file mode 100644
index 0000000..95886d0
--- /dev/null
+++ b/Java/Beginer training/src/Training.java
@@ -0,0 +1,18 @@
+import java.io.*;
+
+public class Training {
+ public static void main(String args[]){
+ PrintFibonacci(7);
+ }
+
+ public static void PrintFibonacci (int length) {
+ int a = 1, b = 0;
+ for (int i = 0; i < length; i++){
+ System.out.printf("%d ", a);
+
+ int temp = a;
+ a += b;
+ b = temp;
+ }
+ }
+}
diff --git a/Java/Beginer training/src/VampireNumber explanation.txt b/Java/Beginer training/src/VampireNumber explanation.txt
new file mode 100644
index 0000000..10ac553
--- /dev/null
+++ b/Java/Beginer training/src/VampireNumber explanation.txt
@@ -0,0 +1,39 @@
+VampireNumber Java task, code explanation with the number 1827:
+
+1827 (Number that we are checking)
+0123 (Indexes of digits, in code they are named: first, second, third & fourth)
+
+The first and second number that are multiplied are illustrated combined as one (the first half of the calculated numbers is the first multiple, and the second half - the second)
+For example, the number 1278 is actually 12 and 78 and they are multiplied to check for the result
+This is done like that to better illustrate how the numbers change
+
+Cycle: Swap 1 with 3 (indexes):
+1827 1728
+1782 1287
+1278 1872
+
+2178 2871
+2817 2718
+2781 2187 NOTE: 2187 is the answer, so the code would stop here, but this example is continued to the end
+
+7281 7182
+7128 7821
+7812 7218
+
+8712 8217
+8271 8172
+8127 8721
+
+1827 (Here the cycles end)
+
+Cycle: it rotates the last 3 digits, for example if we have 123,the 3 gets in the front and the others move back, i.e. 312
+
+ 1 2 3 => 3 1 2
+ ^-----'
+
+Swap 1 with 3: as the name suggests, it makes a second version of the number by swapping the 1st (index) and 2nd (index) digit
+
+ 0 1 2 3 => 0 3 2 1
+ ^---^
+
+ -the swap is there because with the cycle we miss out on half of the possible numbers \ No newline at end of file
diff --git a/Java/Beginer training/src/VampireNumber.java b/Java/Beginer training/src/VampireNumber.java
new file mode 100644
index 0000000..63d7085
--- /dev/null
+++ b/Java/Beginer training/src/VampireNumber.java
@@ -0,0 +1,45 @@
+public class VampireNumber {
+ public static void main(String args[]){
+ //NOTE: Works ONLY with 4 digit numbers, for bigger use BigVampireNumber
+ System.out.println(FindVampireNumber(6880));
+ }
+
+ public static String FindVampireNumber(int result){
+ String num = result + "";
+ int first = num.charAt(0) - 48, second = num.charAt(1) - 48, third = num.charAt(2) - 48, fourth = num.charAt(3) - 48;
+
+ for (int i = 0; i < 4; i++){
+ for (int j = 0; j < 3; j++){
+ int first_half = Combine(first, second), second_half = Combine(third, fourth);
+ int swapped_first_half = Combine(first, fourth), swapped_second_half = Combine(third, second);
+
+ if (first_half * second_half == result) {
+ return result + " = " + first_half + " * " + second_half;
+ }
+ else if (swapped_first_half *swapped_second_half == result){
+ return result + " = " + swapped_first_half + " * " + swapped_second_half;
+ }
+
+ switch (j) {
+ case 0:
+ case 1:
+ int temp = fourth;
+ fourth = second; second = temp;
+ temp = third;
+ third = fourth; fourth = temp;
+
+ break;
+ case 2:
+ temp = second;
+ second = first; first = temp;
+ break;
+ }
+ }
+ }
+ return result + " isn't a vampire number";
+ }
+
+ private static int Combine(int first, int second) {
+ return (first * 10) + second;
+ }
+}