Imported existing sources

This commit is contained in:
Daniel Brunner
2016-12-06 12:11:37 +01:00
parent b51ff1ca82
commit 20acf8ef83
15 changed files with 280 additions and 0 deletions

12
PGTL-Programs.iml Normal file
View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@@ -0,0 +1,10 @@
package ninja.brunner.pgtl;
import ninja.brunner.pgtl.program3.Program;
public class Main {
public static void main(String[] args) throws InterruptedException {
Program.main(args);
}
}

View File

@@ -0,0 +1,23 @@
package ninja.brunner.pgtl.program1;
public class Program {
public static void main(String[] args) {
String[] zahlen = { "0", "1", "2", "3.3", "4", "falsch"};
double summe = 0.0;
for(String zahl : zahlen) {
System.out.println(zahl);
double parsedAsInteger;
try
{
parsedAsInteger = Double.parseDouble(zahl);
}
catch (Exception ex)
{
System.out.println("Zahl kann nicht geparst werden.");
}
}
System.out.println("Summe: " + summe);
}
}

View File

@@ -0,0 +1,86 @@
package ninja.brunner.pgtl.program2;
public class Program {
static char[] instructions = {
//Program 1: abc
'i', 1, 'a',
'#', '+', '#', '+', '#',
//Program 2: Hello World
'i', 10, 'a', //Initialize RAM with 10 bytes in size filled with character a
'S', 'H', '#', '>', //Set value to H and inc ptr
'S', 'e', '#', '>', //Set value to e and inc ptr
'+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '#', '>', //Inc until char l is reached and inc ptr
'+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '#', '>', //Inc until char l is reached and inc ptr
'+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '#', //Inc until char o is reached and inc ptr
'<', '<', '<', '<', '<', '<', '<', '<', '<', '<', '>',
'-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '#', '>',
'+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '#', '>',
'+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '#', '>',
'+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '#', '>',
'+', '+', '+', '#', '>',
'-', '-', '-', '-', '-', '-', '-', '-', '-', '-',
'-', '-', '-', '-', '-', '-', '-', '-', '-', '-',
'-', '-', '-', '-', '-', '-', '-', '-', '-', '-',
'-', '-', '-', '-', '-', '-', '-', '-', '-', '#',
's', //Sleep 1 second
'n',
'j', 11 //Jump to instr offset 8 (Program 2)
};
public static void main(String[] args) throws InterruptedException {
char[] memory = null;
int currMemIndex = 0;
int currInstrIndex = 0;
hui:
while (true) {
switch (instructions[currInstrIndex++]) {
case 'i':
memory = new char[instructions[currInstrIndex]];
for (int i = 0; i < memory.length; i++)
memory[i] = instructions[currInstrIndex + 1];
currInstrIndex += 2;
break;
case '#':
System.out.print(memory[currMemIndex]);
break;
case 'S': //set memory cell to following byte
memory[currMemIndex] = instructions[currInstrIndex];
currInstrIndex++;
break;
case 's':
Thread.sleep(1000);
break;
case 'r':
currInstrIndex = 0;
currMemIndex = 0;
memory = null;
break;
case 'j':
currInstrIndex = instructions[currInstrIndex];
break;
case 'X':
break hui;
case '-':
memory[currMemIndex]--;
break;
case '+':
memory[currMemIndex]++;
break;
case '<':
if (currMemIndex == 0)
currMemIndex = memory.length - 1;
else
currMemIndex--;
break;
case '>':
if (currMemIndex == memory.length - 1)
currMemIndex = 0;
else
currMemIndex++;
break;
}
}
}
}

View File

@@ -0,0 +1,10 @@
package ninja.brunner.pgtl.program3;
public class Program {
public static void main(String[] args) throws InterruptedException {
Rennen rennen = new Rennen();
rennen.run();
System.out.println("Rennen beendet!");
}
}

View File

@@ -0,0 +1,34 @@
package ninja.brunner.pgtl.program3;
import java.util.Random;
public class Rennen {
public Random random;
public Rennstrecke rennStrecke;
public Rennen()
{
random = new Random();
rennStrecke = new Rennstrecke(this, random.nextInt(50) + 25, random.nextInt(10) + 15);
}
public void run() throws InterruptedException {
while(true) {
rennStrecke.kriechen();
System.out.println(rennStrecke.toString());
boolean anySchneckeUnterwegs = false;
for(Schnecke schnecke : rennStrecke.schnecken)
if(schnecke.position < rennStrecke.length) {
anySchneckeUnterwegs = true;
break;
}
if(!anySchneckeUnterwegs)
break;
Thread.sleep(1000);
}
}
}

View File

@@ -0,0 +1,57 @@
package ninja.brunner.pgtl.program3;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.ConsoleHandler;
public class Rennstrecke {
public Rennen rennen;
public int length;
public ArrayList<Schnecke> schnecken;
public int maxNameLength;
private String[] names = {
"Namgung",
"Hwangbo",
"Jegal",
"Sagong",
"Seonu",
"Seomun",
"Dokgo",
"Dongbang"
};
public Rennstrecke(Rennen rennen, int length, int schneckenCount)
{
this.rennen = rennen;
this.length = length;
schnecken = new ArrayList<Schnecke>();
for(int i = 0; i < schneckenCount; i++)
schnecken.add(new Schnecke(this, names[rennen.random.nextInt(names.length)]));
maxNameLength = 0;
for(Schnecke schnecke : schnecken) {
if(schnecke.name.length() > maxNameLength)
maxNameLength = schnecke.name.length();
}
}
public void kriechen() {
for(Schnecke schnecke : schnecken)
schnecke.kriechen();
}
public String toString()
{
StringBuilder sb = new StringBuilder();
for(Schnecke schnecke : schnecken) {
sb.append(schnecke.toString());
sb.append('\n');
}
return sb.toString();
}
}

View File

@@ -0,0 +1,48 @@
package ninja.brunner.pgtl.program3;
import java.util.Random;
public class Schnecke {
public static final int minSpeed = 0;
public static final int maxSpeed = 5;
public Rennstrecke rennstrecke;
public String name;
public int position;
public Schnecke(Rennstrecke rennstrecke, String name)
{
this.rennstrecke = rennstrecke;
this.name = name;
position = 0;
}
public void kriechen()
{
if(position < rennstrecke.length) {
position += rennstrecke.rennen.random.nextInt(maxSpeed - minSpeed) + minSpeed;
if(position > rennstrecke.length)
position = rennstrecke.length;
}
}
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append(name);
for(int i = name.length(); i < rennstrecke.maxNameLength; i++)
sb.append(' ');
sb.append(": ");
for(int i = 1; i < position; i++)
sb.append('.');
sb.append('M');
for(int i = position + 1; i < rennstrecke.length; i++)
sb.append(' ');
sb.append('#');
return sb.toString();
}
}