Files
it-convos/Bwinf_Pakplatz_vscode/programm.java
2022-10-12 19:59:56 +02:00

279 lines
6.2 KiB
Java

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class programm
{
Scanner scan;
Scanner scanOut;
Scanner scanOut2;
String last;
String filename;
int vCars;
int hCars;
char[] arr;
char[] tmpArr;
String tmpOutput;
int steps;
char tmpChar2;
public static void main(String[] args)
{
if(args.length == 0)
{
new programm();
}
else{
for (int i=0;i<args.length;i++)
{
new programm(args[i]);
}
}
}
public programm()
{
filename="parkplatz0.txt";
readFile();
read();
createArray();
printArr();
solve();
}
public programm(String pFilename)
{
filename=pFilename;
readFile();
read();
createArray();
printArr();
solve();
}
public void readFile()
{
try {
scan= new Scanner(new File(filename));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void read()
{
scan.next();
last = scan.next();
hCars = scan.nextInt();
vCars = vCars();//anzahl vertikaler Autos
}
public void createArray()
{
arr = new char[vCars];
readFile();
scan.nextLine();
for(int i=0;i<vCars;i++)
{
arr[i]='0';
}
int x = scan.nextInt();
for(int i=0;i<x;i++)
{
String tmpString = scan.next();//System.out.println("S:"+tmpString);
char tmpChar = tmpString.charAt(0);
String stringPos = scan.next();//System.out.println("P:"+stringPos);
int pos = Integer.parseInt(stringPos);
arr[pos]=tmpChar;
arr[pos+1]=tmpChar;
}
}
public int vCars()
{
char cLast=last.charAt(0);
int vc = (int)(cLast-64);
return vc;
}
public void printArr()
{
System.out.println(arr);
}
public void solve()
{
steps = 0;
setArr2();
for (int i=0;i<tmpArr.length;i++)
{
setArr2();
tmpOutput = "";
char tmpChar = (char)(i+65);
tmpOutput += tmpChar;
tmpOutput += ": ";
if (tmpArr[i]=='0')
{
System.out.println(tmpOutput +"Frei");
}
else{
while (tmpArr[i] != '0')
{
if(goRight(i) == true && canRight(i) == true && canCanRight(i) == true)
{
moveRight(tmpArr[i]);
}
else if(canLeft(i) == true && canCanLeft(i))
{
moveLeft(tmpArr[i]);
}
else if (canRight(i) == true && canCanRight(i) == true)
{
moveRight(tmpArr[i]);
}
else
{
System.out.println("Error");
return;
}
}
System.out.println(tmpOutput);
steps = 0;
}
}
}
public boolean goRight(int input)
{
boolean goRight;
if(input < tmpArr.length-1 && tmpArr[input] == tmpArr[input+1])
{
goRight = true;
}
else
{
goRight = false;
}
return goRight;
}
public void setArr2()
{
createArray();
tmpArr = arr;
}
public boolean canLeft(int ppos)
{
boolean cl = false;
if ( ppos>=2 )
{
cl = true;
}
return cl;
}
public boolean canRight(int ppos)
{
boolean cr = false;
if ( ppos<tmpArr.length-2)
{
cr = true;
}
return cr;
}
public boolean canCanRight(int input)
{
boolean ccr = false;
int n = 0;
for(int i=0;i<tmpArr.length;i++)
{
if(i>input && tmpArr[i] == '0')
{
n++;
}
}
if (n>0 && goRight(input) == true)
{
ccr = true;
}
if (n>=2 && goRight(input) == false)
{
ccr = true;
}
return ccr;
}
public boolean canCanLeft(int input)
{
boolean ccl = false;
int n = 0;
for(int i=0;i<tmpArr.length;i++)
{
if(i<input && tmpArr[i] == '0')
{
n++;
}
}
if (n>0 && goRight(input) == false)
{
ccl = true;
}
if (n>=2 && goRight(input) == true)
{
ccl = true;
}
return ccl;
}
public void moveRight(char car)
{
boolean found = false ;
int t = 0;
while (found == false)
{
if(tmpArr[t] == car)
{
if(tmpArr[t+2] != '0' && canRight(t+2) == true )
{
moveRight(tmpArr[t+2]);
}
if (canRight(t) == false)
{
moveLeft(car);
}
tmpOutput += tmpArr[t];
tmpOutput += " Rechts ";
tmpArr[t+2]=tmpArr[t];
tmpArr[t]='0';
found = true;
}
t++;
}
}
public void moveLeft(char car)
{
boolean found = false ;
int t = 0;
while (found == false)
{
if(tmpArr[t] == car)
{
if(tmpArr[t-1] != '0' && canLeft(t+1))
{
moveLeft(tmpArr[t-1]);
}
tmpOutput += tmpArr[t];
tmpOutput += " Links ";
tmpArr[t-1]=tmpArr[t];
tmpArr[t+1]='0';
found = true;
}
t++;
}
}
}