this is a code for my project ,the project is
In the beginning of the program, the users will choose either to create the road by them self by entering the speed of the car, the lane in which the car is moving and the position of the car in the lane or to let the program run randomly.
When the users choose the first choice they will edit the car number of position per lane , the car lane , the number of car and the speed of each car.
But when the users choose the second choice the program will be running randomly and they will get the road fill with a number of cars and each car has it position and speed .
Finally, the program will finishes the execution either it was the first choice or the second when all the cars reach the np position, all the cars must np position as fast as possible , in case one of the cars face other car in her way then this car can change her lane if the adjacent position if the lane is empty.
import java.util.Scanner;
public class Road
{
// declare array and veriables
private int road[][];
private int carsSpeed[];
private int carsLane[];
private int carPos[];
private int endPos;
private int nCars;
private int nLanes;
private int nPos;
public Road(int road1[][], int carsSpeed1[], int carsLane1[], int carPos1[])
{
road= road1;
carsSpeed= carsSpeed1;
carsLane= carsLane1;
carPos= carPos1;
}
// read input
public void readInput()
{
Scanner input = new Scanner(System.in);
System.out.println(" Please Enter your Cars Speed;");
int carsSpeed =input.nextInt();
System.out.println("Please Enter how many lines you want:");
int carsLane = input.nextInt();
System.out.println(" Please Enter your Cars Position:");
int carPos = input.nextInt();
}
// fill road with cars
public void fillCars()
{
for(int cari=0; cari<=nCars; ++cari)
{
int cariLane = carsLane[cari];
int cariPos = carPos[cari];
road[cariLane][cariPos]=cari;
}
}
//simulate car movement
public void simulate()
{
boolean done = false;
while (!done) //while loop
{
done= moveCars();
}
}
public boolean moveCars() //movecars boolean method
{
boolean finishAll = true ;
for(int cari=1; cari<=nCars; cari++)
finishAll= finishAll && moveCar(nCars);
return finishAll;
}
public boolean moveCar(int cari)
{
if(carPos[cari]>= endPos)
{
carPos[cari]++;
return true;
}
else
{
moveForward(cari);
return false;
}
}
public boolean moveForward ( int cari)
{
if(carPos[cari]<carsSpeed[cari])
{
carPos[cari]--;
return true;
}
else
{
return false;
}
}
// display car and road
public void displayCars()
{
}
}
import java.util.Scanner;
public class SimulateRoad
{
public static void main(String args[])
{
Scanner input = new Scanner (System.in); //create scanner to obtain input from command window
final int NCARS = 50;
final int NLANES = 10;
final int NPOS = 10000;
// create array road and cars
int road[][] = new int [NLANES][NCARS];
int carsSpeed[] = new int [NCARS];
int carsLane[] = new int [NCARS];
int carPos[] = new int [NCARS];
int choice=0;
Road road1= new Road ( road, carsSpeed, carsLane, carPos);
MoveRandomly Ram1= new MoveRandomly ( road, carsSpeed, carsLane, carPos);
//prombt user to enter
System.out.println("Choose Your Option From The Menu : ");
System.out.println(" 1- Enter Your Own Movement. ");
System.out.println(" 2- Randomly Movement. ");
System.out.println(" 3- Exit The Program. ");
System.out.println("Enter Your Choice:");
choice=input.nextInt(); // read the input from the user
//pick the ption based on the users choice
switch(choice)
{
case 1:
road1.readInput();
road1.fillCars();
road1.simulate();
road1.displayCars();
break;
case 2:
Ram1.RanMove();
break;
case 3:
break;
default:
System.out.print(" Invalid Choice ");
break;
}
}
}
this is what i got so far i,m not sure about the movefoward method also i dont know how to display the cars if someone could help i will really presuate it i'm a beginner BTW...