Created
December 19, 2018 10:46
-
-
Save serhatyuna/1970f38d5af9a93474657d9eb9d37fb2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import enigma.console.TextWindow; | |
import enigma.core.Enigma; | |
import enigma.console.Console; | |
import enigma.event.TextMouseEvent; | |
import enigma.event.TextMouseListener; | |
import enigma.console.TextAttributes; | |
import java.awt.Color; | |
import java.awt.event.KeyEvent; | |
import java.awt.event.KeyListener; | |
import java.util.Random; | |
public class Game { | |
/* Enigma Console & Enigma Mouse and Key Listener */ | |
private Console cn = Enigma.getConsole("BigCity", 100, 21, 25, 1); | |
private TextWindow consoleText = cn.getTextWindow(); | |
private TextAttributes attrWalls = new TextAttributes(Color.WHITE, Color.BLUE); | |
private TextAttributes attrWallsGreen = new TextAttributes(Color.WHITE, Color.GREEN); | |
private TextAttributes attrPath = new TextAttributes(Color.BLACK, Color.WHITE); | |
private int mousepr; // mouse pressed? | |
private int mousex, mousey, mouseXTemp, mouseYTemp; // mouse text coords. | |
private int keypr; // key pressed? | |
private int rkey; // key (for press/release) | |
private int selectedX, selectedY; | |
private Object[] info = new Object[4]; | |
private int prevDown; | |
private boolean oneSecondMove = false; | |
private boolean willBePaused = false; | |
// Timer | |
private int timeCounter; | |
private int timerCounter; | |
// Bus Stops & Bus Lines | |
private BusStop[] busStops; | |
private BusLine[] busLines; | |
private Bus[] buses; | |
private CircularQueue newPassengers; | |
private int passengerIDCount; | |
// Random | |
private Random random = new Random(); | |
Game() { | |
// Initializing the variables | |
timeCounter = 0; | |
timerCounter = 0; | |
busStops = new BusStop[]{ | |
new BusStop('A', 1, 1), | |
new BusStop('B', 13, 1), | |
new BusStop('C', 31, 1), | |
new BusStop('D', 49, 1), | |
new BusStop('E', 1, 7), | |
new BusStop('F', 13, 7), | |
new BusStop('G', 31, 7), | |
new BusStop('H', 49, 7), | |
new BusStop('I', 1, 13), | |
new BusStop('J', 13, 13), | |
new BusStop('K', 31, 13), | |
new BusStop('L', 43, 13), | |
new BusStop('M', 7, 19), | |
new BusStop('N', 13, 19), | |
new BusStop('O', 31, 19), | |
new BusStop('P', 43, 19) | |
}; | |
busLines = new BusLine[]{ | |
new BusLine('A', "AEIJKL", busStops), | |
new BusLine('B', "BFEIJNM", busStops), | |
new BusLine('C', "CGKJFEA", busStops), | |
new BusLine('D', "DCGKJNM", busStops), | |
new BusLine('L', "LPOKGCDH", busStops), | |
new BusLine('M', "MNJFGKOP", busStops) | |
}; | |
buses = new Bus[]{ | |
busLines[0].getBuses()[0], | |
busLines[0].getBuses()[1], | |
busLines[1].getBuses()[0], | |
busLines[1].getBuses()[1], | |
busLines[2].getBuses()[0], | |
busLines[2].getBuses()[1], | |
busLines[3].getBuses()[0], | |
busLines[3].getBuses()[1], | |
busLines[4].getBuses()[0], | |
busLines[4].getBuses()[1], | |
busLines[5].getBuses()[0], | |
busLines[5].getBuses()[1] | |
}; | |
passengerIDCount = 0; | |
createNewPassengerQueue(); | |
} | |
public void start() throws InterruptedException { | |
createMouseKeyListeners(); | |
printScreen(); | |
loop(); | |
} | |
// Main Loop | |
private void loop() throws InterruptedException { | |
while (true) { | |
keyOperations(); | |
if (timerCounter == 0) { | |
placePassenger(); | |
printScreen(); | |
if (timeCounter > 1) { | |
moveBusses(); | |
} | |
checkPassengers(); | |
if (timeCounter > 1) | |
printBus(); | |
} | |
consoleText.setCursorPosition(61, 0); | |
System.out.println("Time: " + timeCounter + " "); | |
consoleText.setCursorPosition(61, 15); | |
System.out.println(); | |
Thread.sleep(100); | |
timerCounter++; | |
if (timerCounter == 10) { | |
timeCounter++; | |
timerCounter = 0; | |
} | |
if (oneSecondMove) { | |
oneSecondMove = false; | |
} | |
} | |
} | |
// Prints all things on the screen | |
private void printScreen() { | |
for (int i = 0; i < 21; i++) { | |
for (int j = 0; j < 57; j++) { | |
if ((i == 1 && ((j >= 1 && j <= 7) || (j <= 25 && j >= 13) || (j <= 49 && j >= 31))) || | |
(i == 7 && ((j >= 1 && j <= 31) || (j >= 37 && j <= 55))) || | |
(i == 13 && ((j >= 1 && j <= 43) || (j >= 49 && j <= 55))) || | |
(i == 19 && ((j >= 1 && j <= 19) || (j >= 25 && j <= 55))) || | |
((j == 1) && (i >= 1 && i <= 13)) || ((j == 7) && (i >= 13 && i <= 19)) || | |
((j == 13) && (i >= 1 && i <= 19)) || ((j == 19) && (i >= 7 && i <= 19)) || | |
((j == 31) && (i >= 1 && i <= 19)) || ((j == 37) && (i >= 1 && i <= 19)) || | |
((j == 55) && (i >= 7 && i <= 13)) || ((j == 43) && (i >= 7 && i <= 19)) || | |
((j == 48) && (i >= 13 && i <= 19)) || ((j == 49) && (i >= 1 && i <= 7))) { | |
consoleText.output(j, i, ' ', attrPath); | |
} else { | |
consoleText.output(j, i, '#', attrWalls); | |
} | |
if (j == 1 && i == 1) { | |
consoleText.output(j, i, 'A', attrPath); | |
} else if (j == 2 && i == 2) { | |
consoleText.output(j, i, String.valueOf(busStops[0].getWaiting()).charAt(0), attrWalls); | |
} else if (j == 13 && i == 1) { | |
consoleText.output(j, i, 'B', attrPath); | |
} else if (j == 14 && i == 2) { | |
consoleText.output(j, i, String.valueOf(busStops[1].getWaiting()).charAt(0), attrWalls); | |
} else if (j == 31 && i == 1) { | |
consoleText.output(j, i, 'C', attrPath); | |
} else if (j == 32 && i == 2) { | |
consoleText.output(j, i, String.valueOf(busStops[2].getWaiting()).charAt(0), attrWalls); | |
} else if (j == 49 && i == 1) { | |
consoleText.output(j, i, 'D', attrPath); | |
} else if (j == 50 && i == 2) { | |
consoleText.output(j, i, String.valueOf(busStops[3].getWaiting()).charAt(0), attrWalls); | |
} else if (j == 1 && i == 7) { | |
consoleText.output(j, i, 'E', attrPath); | |
} else if (j == 2 && i == 8) { | |
consoleText.output(j, i, String.valueOf(busStops[4].getWaiting()).charAt(0), attrWalls); | |
} else if (j == 13 && i == 7) { | |
consoleText.output(j, i, 'F', attrPath); | |
} else if (j == 14 && i == 8) { | |
consoleText.output(j, i, String.valueOf(busStops[5].getWaiting()).charAt(0), attrWalls); | |
} else if (j == 31 && i == 7) { | |
consoleText.output(j, i, 'G', attrPath); | |
} else if (j == 32 && i == 8) { | |
consoleText.output(j, i, String.valueOf(busStops[6].getWaiting()).charAt(0), attrWalls); | |
} else if (j == 49 && i == 7) { | |
consoleText.output(j, i, 'H', attrPath); | |
} else if (j == 50 && i == 8) { | |
consoleText.output(j, i, String.valueOf(busStops[7].getWaiting()).charAt(0), attrWalls); | |
} else if (j == 1 && i == 13) { | |
consoleText.output(j, i, 'I', attrPath); | |
} else if (j == 2 && i == 14) { | |
consoleText.output(j, i, String.valueOf(busStops[8].getWaiting()).charAt(0), attrWalls); | |
} else if (j == 13 && i == 13) { | |
consoleText.output(j, i, 'J', attrPath); | |
} else if (j == 14 && i == 14) { | |
consoleText.output(j, i, String.valueOf(busStops[9].getWaiting()).charAt(0), attrWalls); | |
} else if (j == 31 && i == 13) { | |
consoleText.output(j, i, 'K', attrPath); | |
} else if (j == 32 && i == 14) { | |
consoleText.output(j, i, String.valueOf(busStops[10].getWaiting()).charAt(0), attrWalls); | |
} else if (j == 43 && i == 13) { | |
consoleText.output(j, i, 'L', attrPath); | |
} else if (j == 44 && i == 14) { | |
consoleText.output(j, i, String.valueOf(busStops[11].getWaiting()).charAt(0), attrWalls); | |
} else if (j == 7 && i == 19) { | |
consoleText.output(j, i, 'M', attrPath); | |
} else if (j == 8 && i == 20) { | |
consoleText.output(j, i, String.valueOf(busStops[12].getWaiting()).charAt(0), attrWalls); | |
} else if (j == 13 && i == 19) { | |
consoleText.output(j, i, 'N', attrPath); | |
} else if (j == 14 && i == 20) { | |
consoleText.output(j, i, String.valueOf(busStops[13].getWaiting()).charAt(0), attrWalls); | |
} else if (j == 31 && i == 19) { | |
consoleText.output(j, i, 'O', attrPath); | |
} else if (j == 32 && i == 20) { | |
consoleText.output(j, i, String.valueOf(busStops[14].getWaiting()).charAt(0), attrWalls); | |
} else if (j == 43 && i == 19) { | |
consoleText.output(j, i, 'P', attrPath); | |
} else if (j == 44 && i == 20) { | |
consoleText.output(j, i, String.valueOf(busStops[15].getWaiting()).charAt(0), attrWalls); | |
} | |
} | |
} | |
if (busStops[0].getWaiting() >= 10) | |
consoleText.output(3, 2, String.valueOf(busStops[0].getWaiting()).charAt(1), attrWalls); | |
if (busStops[1].getWaiting() >= 10) | |
consoleText.output(14, 2, String.valueOf(busStops[1].getWaiting()).charAt(1), attrWalls); | |
if (busStops[2].getWaiting() >= 10) | |
consoleText.output(32, 2, String.valueOf(busStops[2].getWaiting()).charAt(1), attrWalls); | |
if (busStops[3].getWaiting() >= 10) | |
consoleText.output(51, 2, String.valueOf(busStops[3].getWaiting()).charAt(1), attrWalls); | |
if (busStops[4].getWaiting() >= 10) | |
consoleText.output(3, 8, String.valueOf(busStops[4].getWaiting()).charAt(1), attrWalls); | |
if (busStops[5].getWaiting() >= 10) | |
consoleText.output(15, 8, String.valueOf(busStops[5].getWaiting()).charAt(1), attrWalls); | |
if (busStops[6].getWaiting() >= 10) | |
consoleText.output(33, 8, String.valueOf(busStops[6].getWaiting()).charAt(1), attrWalls); | |
if (busStops[7].getWaiting() >= 10) | |
consoleText.output(51, 8, String.valueOf(busStops[7].getWaiting()).charAt(1), attrWalls); | |
if (busStops[8].getWaiting() >= 10) | |
consoleText.output(3, 14, String.valueOf(busStops[8].getWaiting()).charAt(1), attrWalls); | |
if (busStops[9].getWaiting() >= 10) | |
consoleText.output(15, 14, String.valueOf(busStops[9].getWaiting()).charAt(1), attrWalls); | |
if (busStops[10].getWaiting() >= 10) | |
consoleText.output(33, 14, String.valueOf(busStops[10].getWaiting()).charAt(1), attrWalls); | |
if (busStops[11].getWaiting() >= 10) | |
consoleText.output(45, 14, String.valueOf(busStops[11].getWaiting()).charAt(1), attrWalls); | |
if (busStops[12].getWaiting() >= 10) | |
consoleText.output(9, 20, String.valueOf(busStops[12].getWaiting()).charAt(1), attrWalls); | |
if (busStops[13].getWaiting() >= 10) | |
consoleText.output(15, 20, String.valueOf(busStops[13].getWaiting()).charAt(1), attrWalls); | |
if (busStops[14].getWaiting() >= 10) | |
consoleText.output(33, 20, String.valueOf(busStops[14].getWaiting()).charAt(1), attrWalls); | |
if (busStops[15].getWaiting() >= 10) | |
consoleText.output(45, 20, String.valueOf(busStops[15].getWaiting()).charAt(1), attrWalls); | |
printPassengerQueue(); | |
printStatistics(); | |
} | |
// Move Buses | |
private void moveBusses() { | |
for (BusLine busLine : busLines) { | |
for (int i = 0; i < 2; i++) { | |
if (busLine.getBus(i).isWaiting()) { | |
busLine.getBus(i).setWaitingTime(busLine.getBus(i).getWaitingTime() - 1); | |
if (busLine.getBus(i).getWaitingTime() == 0) { | |
busLine.getBus(i).setWaiting(false); | |
} | |
} | |
if (timeCounter % 2 == 0 && !busLine.getBus(i).isWaiting()) { | |
if (!busLine.getDirections(i).equals("")) { | |
printBus(busLine, i); | |
} else { | |
busLine.setDirections(new StringBuilder(busLine.getDirectionsTemp(i)).reverse().toString(), i); | |
busLine.setDirectionsTemp("", i); | |
printBus(busLine, i); | |
} | |
char directionReverse = busLine.getDirections(i).charAt(0); | |
busLine.setDirections(busLine.getDirections(i).substring(1), i); | |
switch (directionReverse) { | |
case 'L': | |
directionReverse = 'R'; | |
break; | |
case 'R': | |
directionReverse = 'L'; | |
break; | |
case 'U': | |
directionReverse = 'D'; | |
break; | |
case 'D': | |
directionReverse = 'U'; | |
break; | |
} | |
busLine.setDirectionsTemp(busLine.getDirectionsTemp(i) + directionReverse, i); | |
} | |
} | |
} | |
} | |
// Prints buses | |
private void printBus(BusLine busLine, int i) { | |
char direction = busLine.getDirections(i).charAt(0); | |
if (direction == 'L') { | |
if (!busLine.getBus(i).isWaiting()) | |
busLine.getBus(i).setX(busLine.getBus(i).getX() - 1); | |
char countOfPassengers = (char) (busLine.getBus(i).getCountOfPassenger() + 48); | |
consoleText.output(busLine.getBus(i).getX(), busLine.getBus(i).getY(), countOfPassengers, attrPath); | |
} else if (direction == 'R') { | |
if (!busLine.getBus(i).isWaiting()) | |
busLine.getBus(i).setX(busLine.getBus(i).getX() + 1); | |
char countOfPassengers = (char) (busLine.getBus(i).getCountOfPassenger() + 48); | |
consoleText.output(busLine.getBus(i).getX(), busLine.getBus(i).getY(), countOfPassengers, attrPath); | |
} else if (direction == 'D') { | |
if (!busLine.getBus(i).isWaiting()) | |
busLine.getBus(i).setY(busLine.getBus(i).getY() + 1); | |
char countOfPassengers = (char) (busLine.getBus(i).getCountOfPassenger() + 48); | |
consoleText.output(busLine.getBus(i).getX(), busLine.getBus(i).getY(), countOfPassengers, attrPath); | |
} else if (direction == 'U') { | |
if (!busLine.getBus(i).isWaiting()) | |
busLine.getBus(i).setY(busLine.getBus(i).getY() - 1); | |
char countOfPassengers = (char) (busLine.getBus(i).getCountOfPassenger() + 48); | |
consoleText.output(busLine.getBus(i).getX(), busLine.getBus(i).getY(), countOfPassengers, attrPath); | |
} | |
} | |
// Prints statistics | |
private void printStatistics() { | |
consoleText.setCursorPosition(61, 8); | |
consoleText.output("Waiting : "); | |
int numberOfPassengersWaiting = getNumberOfPassengersWaiting(); | |
consoleText.output(String.valueOf(numberOfPassengersWaiting)); | |
consoleText.setCursorPosition(61, 9); | |
consoleText.output("Travelling : "); | |
int numberOfPassengersTravelling = getNumberOfPassengersTravelling(); | |
consoleText.output(String.valueOf(numberOfPassengersTravelling)); | |
consoleText.setCursorPosition(61, 10); | |
consoleText.output("Bus fullness: "); | |
double busFullness = getBusFullness(); | |
String fullness = String.valueOf(busFullness); | |
if (busFullness == 0) { | |
consoleText.output(fullness + "%"); | |
} else { | |
consoleText.output(fullness.substring(0, 4) + "%"); | |
} | |
} | |
private int getNumberOfPassengersWaiting() { | |
int sum = 0; | |
for (BusStop busStop : busStops) { | |
if (busStop != null) sum += busStop.getWaiting(); | |
} | |
return sum; | |
} | |
private int getNumberOfPassengersTravelling() { | |
int sum = 0; | |
for (BusLine busLine : busLines) { | |
if (busLine != null) { | |
for (Bus bus : busLine.getBuses()) { | |
if (bus != null) sum += bus.getCountOfPassenger(); | |
} | |
} | |
} | |
return sum; | |
} | |
private double getBusFullness() { | |
int numberOfPassengersTravelling = getNumberOfPassengersTravelling(); | |
final double maxPassenger = 96.0; | |
return (100 * numberOfPassengersTravelling) / maxPassenger; | |
} | |
// Prints New Passengers Queue | |
private void printPassengerQueue() { | |
Stack tempPassengerStack = new Stack(15); | |
for (int i = 0; i < 15; i++) { | |
tempPassengerStack.push(newPassengers.peek()); | |
newPassengers.enqueue(newPassengers.dequeue()); | |
} | |
consoleText.setCursorPosition(63, 3); | |
consoleText.output("New Passengers"); | |
consoleText.setCursorPosition(61, 4); | |
consoleText.output("-------------------"); | |
consoleText.setCursorPosition(61, 5); | |
consoleText.output("> "); | |
while (!tempPassengerStack.isEmpty()) { | |
Passenger passenger = (Passenger) tempPassengerStack.pop(); | |
char startingStop = passenger.getStartPoint(); | |
consoleText.output(startingStop); | |
} | |
consoleText.output(" >"); | |
consoleText.setCursorPosition(61, 6); | |
consoleText.output("-------------------"); | |
} | |
// Places the passenger | |
private void placePassenger() { | |
Passenger passenger = (Passenger) newPassengers.dequeue(); // Passenger which will be placed | |
for (int i = 0; i < 16; i++) { | |
if (busStops[i].getName() == passenger.getStartPoint()) { | |
busStops[i].addPassenger(passenger); | |
break; | |
} | |
} | |
int randomLineIndex = random.nextInt(6); | |
Passenger newPassenger = new Passenger(passengerIDCount, busLines[randomLineIndex]); | |
newPassengers.enqueue(newPassenger); | |
passengerIDCount++; | |
} | |
// Creates new passengers queue | |
private void createNewPassengerQueue() { | |
newPassengers = new CircularQueue(15); | |
int randomLineIndex; | |
for (int i = 0; i < 15; i++) { | |
randomLineIndex = random.nextInt(6); | |
Passenger newPassenger = new Passenger(passengerIDCount, busLines[randomLineIndex]); | |
newPassengers.enqueue(newPassenger); | |
passengerIDCount++; | |
} | |
} | |
// Creates mouse & key listeners | |
private void createMouseKeyListeners() { | |
TextMouseListener tmlis; | |
KeyListener klis; | |
// ------ Standard code for mouse and keyboard ------ Do not change | |
tmlis = new TextMouseListener() { | |
public void mouseClicked(TextMouseEvent arg0) { | |
} | |
public void mousePressed(TextMouseEvent arg0) { | |
if (mousepr == 0) { | |
mousepr = 1; | |
mousex = arg0.getX(); | |
mousey = arg0.getY(); | |
} | |
} | |
public void mouseReleased(TextMouseEvent arg0) { | |
} | |
}; | |
klis = new KeyListener() { | |
public void keyTyped(KeyEvent e) { | |
} | |
public void keyPressed(KeyEvent e) { | |
if (keypr == 0) { | |
keypr = 1; | |
rkey = e.getKeyCode(); | |
} | |
} | |
public void keyReleased(KeyEvent e) { | |
} | |
}; | |
consoleText.addTextMouseListener(tmlis); | |
consoleText.addKeyListener(klis); | |
} | |
// Key Operations | |
private void keyOperations() throws InterruptedException { | |
if (keypr == 1 || (willBePaused && timerCounter == 0)) { | |
if (rkey == KeyEvent.VK_SPACE || (willBePaused && timerCounter == 0)) { | |
keypr = 0;// last action | |
rkey = 0; | |
willBePaused = false; | |
boolean flag = false; | |
consoleText.setCursorPosition(71, 0); | |
consoleText.output("(paused)"); | |
while (!flag) { | |
mouseOperations(); | |
Thread.sleep(10); | |
if (rkey == KeyEvent.VK_R) { | |
flag = true; | |
for (int k = 0; k < 20; k++) { | |
consoleText.setCursorPosition(61, 12 + k); | |
consoleText.output(" "); | |
} | |
} else if (rkey == KeyEvent.VK_SPACE) { | |
// 1 sn hareket edecek | |
oneSecondMove = true; | |
willBePaused = true; | |
flag = true; | |
} else { | |
rkey = 0; | |
keypr = 0; | |
} | |
} | |
} | |
keypr = 0; | |
rkey = 0; | |
} | |
} | |
// Mouse Operations | |
private void mouseOperations() { | |
if (mousepr == 1) { // if mouse button pressed | |
for (int i = 0; i < busStops.length; i++) { | |
if ((mousex == busStops[i].getX() && mousey == busStops[i].getY()) || (mouseXTemp == busStops[i].getX() && mouseYTemp == busStops[i].getY())) { | |
consoleText.output(busStops[i].getX() + 1, busStops[i].getY() + 1, (char) (busStops[i].getWaiting() + '0'), attrWallsGreen); | |
mouseXTemp = mousex; | |
mouseYTemp = mousey; | |
info[0] = busStops[i].getName(); | |
info[1] = i; | |
} else if (mousex != busStops[i].getX() || mousey != busStops[i].getY()) { | |
consoleText.output(busStops[i].getX() + 1, busStops[i].getY() + 1, (char) (busStops[i].getWaiting() + '0'), attrWalls); | |
} | |
if (mouseXTemp != busStops[i].getX() || mouseYTemp != busStops[i].getY()) { | |
consoleText.output(busStops[i].getX() + 1, busStops[i].getY() + 1, (char) (busStops[i].getWaiting() + '0'), attrWalls); | |
} | |
consoleText.setCursorPosition(61, 12); | |
int down = 0; | |
if (info[0] != null) { | |
for (int k = 0; k < 20; k++) { | |
consoleText.setCursorPosition(61, 12 + k); | |
consoleText.output(" "); | |
} | |
consoleText.setCursorPosition(61, 12); | |
consoleText.output("Bus Stop [" + info[0] + "] Passengers:\t\t"); | |
for (int j = 0; j < busStops[(int) info[1]].getWaitingPassengers().length; j++) { | |
consoleText.setCursorPosition(61, 13 + down); | |
if (busStops[(int) info[1]].getWaitingPassengers()[j] != null) { | |
consoleText.output(busStops[(int) info[1]].getWaitingPassengers()[j].getId() + ": Line" + busStops[(int) info[1]].getWaitingPassengers()[j].getLine().getName() + " " + busStops[(int) info[1]].getWaitingPassengers()[j].getStartPoint() + " - " + busStops[(int) info[1]].getWaitingPassengers()[j].getEndPoint()); | |
if (busStops[(int) info[1]].getWaitingPassengers()[j].getLuggageNumber() == 0) { | |
consoleText.output("(L:-)"); | |
} else { | |
consoleText.output("(L:"); | |
for (int k = 0; k < busStops[(int) info[1]].getWaitingPassengers()[j].getLuggages().length; k++) { | |
Luggage luggage = busStops[(int) info[1]].getWaitingPassengers()[j].getLuggages()[k]; | |
if (luggage != null) { | |
if (k + 1 != busStops[(int) info[1]].getWaitingPassengers()[j].getLuggages().length && busStops[(int) info[1]].getWaitingPassengers()[j].getLuggages()[k + 1] != null) { | |
consoleText.output(String.valueOf(luggage.getId()) + ","); | |
} else { | |
consoleText.output(String.valueOf(luggage.getId())); | |
} | |
} | |
} | |
consoleText.output(')'); | |
} | |
down++; | |
} | |
prevDown = down; | |
} | |
} | |
info[2] = null; | |
} | |
for (int i = 0; i < buses.length; i++) { | |
if ((mousex == buses[i].getX() && mousey == buses[i].getY()) || (mouseXTemp == buses[i].getX() && mouseYTemp == buses[i].getY())) { | |
consoleText.output(buses[i].getX(), buses[i].getY(), (char) (buses[i].getCountOfPassenger() + '0'), attrWallsGreen); | |
mouseXTemp = mousex; | |
mouseYTemp = mousey; | |
info[2] = buses[i].getName(); | |
info[3] = i; | |
} else if (mousex != buses[i].getX() || mousey != buses[i].getY()) { | |
consoleText.output(buses[i].getX(), buses[i].getY(), (char) (buses[i].getCountOfPassenger() + '0'), attrPath); | |
} | |
if (mouseXTemp != buses[i].getX() || mouseYTemp != buses[i].getY()) { | |
consoleText.output(buses[i].getX(), buses[i].getY(), (char) (buses[i].getCountOfPassenger() + '0'), attrPath); | |
} | |
consoleText.setCursorPosition(61, 12); | |
int down = 0; | |
if (info[2] != null) { | |
for (int k = 0; k < 20; k++) { | |
consoleText.setCursorPosition(61, 12 + k); | |
consoleText.output(" "); | |
} | |
consoleText.setCursorPosition(61, 12); | |
consoleText.output("Bus [" + buses[(int) info[3]].getName() + "] Passengers:"); | |
for (int j = 0; j < buses[(int) info[3]].getPassengers().length; j++) { | |
consoleText.setCursorPosition(61, 13 + down); | |
if (buses[(int) info[3]].getPassengers()[j] != null) { | |
consoleText.output(buses[(int) info[3]].getPassengers()[j].getId() + ": " + buses[(int) info[3]].getPassengers()[j].getStartPoint() + "-" + buses[(int) info[3]].getPassengers()[j].getEndPoint()); | |
if (buses[(int) info[3]].getPassengers()[j].getLuggageNumber() == 0) { | |
consoleText.output(" (L:-)"); | |
} else { | |
consoleText.output(" (L:"); | |
for (int k = 0; k < buses[(int) info[3]].getPassengers()[j].getLuggages().length; k++) { | |
Luggage luggage = buses[(int) info[3]].getPassengers()[j].getLuggages()[k]; | |
if (luggage != null) { | |
if (k + 1 != buses[(int) info[3]].getPassengers()[j].getLuggages().length && buses[(int) info[3]].getPassengers()[j].getLuggages()[k + 1] != null) { | |
consoleText.output(String.valueOf(luggage.getId()) + ","); | |
} else { | |
consoleText.output(String.valueOf(luggage.getId())); | |
} | |
} | |
} | |
consoleText.output(')'); | |
} | |
down++; | |
} | |
for (int j1 = 0; j1 < 8; j1++) { | |
consoleText.setCursorPosition(86, 12 + j1); | |
consoleText.output("|"); | |
consoleText.setCursorPosition(90, 12 + j1); | |
consoleText.output("|"); | |
} | |
consoleText.setCursorPosition(61, 19); | |
consoleText.output("Bus [" + buses[(int) info[3]].getName() + "] Luggage:"); | |
consoleText.setCursorPosition(86, 20); | |
consoleText.output("-----"); | |
for (int k = 0; k < 8; k++) { | |
consoleText.setCursorPosition(87, 12 + k); | |
consoleText.output(" "); | |
} | |
if (buses[(int) info[3]].getLuggages().size() != 0) { | |
Stack temp = new Stack(8); | |
int space = 8 - buses[(int) info[3]].getLuggages().size(); | |
while (!buses[(int) info[3]].getLuggages().isEmpty()) { | |
consoleText.setCursorPosition(87, 12 + space); | |
Luggage l = (Luggage) buses[(int) info[3]].getLuggages().peek(); | |
consoleText.output(String.valueOf(l.getId())); | |
space++; | |
temp.push(buses[(int) info[3]].getLuggages().pop()); | |
} | |
while (!temp.isEmpty()) { | |
buses[(int) info[3]].getLuggages().push(temp.pop()); | |
} | |
} | |
prevDown = down; | |
} | |
info[0] = null; | |
} | |
} | |
mousepr = 0; | |
} | |
} | |
// Prints busses but without moving | |
private void printBus() { | |
for (BusLine busLine : busLines) { | |
char countOfPassengers1 = (char) (busLine.getBus(0).getCountOfPassenger() + 48); | |
consoleText.output(busLine.getBus(0).getX(), busLine.getBus(0).getY(), countOfPassengers1, attrPath); | |
char countOfPassengers2 = (char) (busLine.getBus(1).getCountOfPassenger() + 48); | |
consoleText.output(busLine.getBus(1).getX(), busLine.getBus(1).getY(), countOfPassengers2, attrPath); | |
} | |
} | |
// Does getting in and out things for all busses | |
private void checkPassengers() { | |
for (BusLine busLine : busLines) { | |
for (int i = 0; i < 2; i++) { | |
for (BusStop busStop : busStops) { | |
if (busLine.getBus(i).getX() == busStop.getX() && busLine.getBus(i).getY() == busStop.getY()) { | |
// Get passengers out | |
if (busLine.getBus(i).getPassengers().length > 0) { | |
for (int j = 0; j < busLine.getBus(i).getPassengers().length; j++) { | |
if (busLine.getBus(i).getPassengers()[j] != null && busLine.equals(busLine.getBus(i).getPassengers()[j].getLine())) { | |
if (busLine.getBus(i).getPassengers()[j].getEndPoint() == busStop.getName()) { | |
int waitingTime = (busLine.getBus(i).getPassengers()[j].getLuggages() != null) ? busLine.getBus(i).getPassengers()[j].getLuggages().length : 0; | |
if (waitingTime > 0) { | |
busLine.getBus(0).setWaiting(true); | |
busLine.getBus(0).setWaitingTime(busLine.getBus(0).getWaitingTime() + waitingTime); | |
} | |
busLine.getBus(i).deletePassenger(busLine.getBus(i).getPassengers()[j]); | |
} | |
} | |
} | |
} | |
// Get passengers in | |
if (busStop.getWaiting() > 0 && busLine.getBus(i).hasEmptySeat()) { | |
for (Passenger p : busStop.getWaitingPassengers()) { | |
if (p != null && busLine.equals(p.getLine())) { | |
String lineStr = busLine.getLineStr(); | |
int indexOfStop = lineStr.indexOf(busStop.getName()); | |
int indexOfEndPoint = lineStr.indexOf(p.getEndPoint()); | |
if (indexOfEndPoint > indexOfStop && i == 0) { | |
// Getting passengers in for first bus | |
if (p.getLuggages() != null) { | |
if (8 - busLine.getBus(0).getLuggages().size() > p.getLuggages().length) { | |
int waitingTime = (p.getLuggages() != null) ? p.getLuggages().length : 0; | |
if (waitingTime > 0) { | |
busLine.getBus(0).setWaiting(true); | |
busLine.getBus(0).setWaitingTime(busLine.getBus(0).getWaitingTime() + waitingTime); | |
} | |
busStop.deletePassenger(p); | |
busLine.getBus(0).addPassenger(p); | |
} | |
} | |
} else if (indexOfEndPoint < indexOfStop && i == 1) { | |
// Getting passengers in for second bus | |
if (p.getLuggages() != null) { | |
if (8 - busLine.getBus(1).getLuggages().size() > p.getLuggages().length) { | |
int waitingTime = (p.getLuggages() != null) ? p.getLuggages().length : 0; | |
if (waitingTime > 0) { | |
busLine.getBus(1).setWaiting(true); | |
busLine.getBus(1).setWaitingTime(busLine.getBus(1).getWaitingTime() + waitingTime); | |
} | |
busStop.deletePassenger(p); | |
busLine.getBus(1).addPassenger(p); | |
} | |
} | |
} | |
} | |
} | |
} else { | |
busLine.getBus(i).setWaiting(false); | |
} | |
break; | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment