mirror of
https://github.com/guezoloic/L3-racing-game.git
synced 2026-03-28 19:13:41 +00:00
rework: ajout plusieurs classes et modification logique
This commit is contained in:
265
src/Game.java
265
src/Game.java
@@ -1,132 +1,165 @@
|
||||
import java.util.ArrayList;
|
||||
// import java.awt.Color;
|
||||
// import java.util.ArrayList;
|
||||
|
||||
public class Game
|
||||
{
|
||||
private boolean paused;
|
||||
private Car[] cars;
|
||||
private Map map;
|
||||
|
||||
private ArrayList<GObserver> obs;
|
||||
private final int time = 1000;
|
||||
// public class Game
|
||||
// {
|
||||
// private boolean paused;
|
||||
// private Car[] cars;
|
||||
// private Map map;
|
||||
// private ArrayList<Color> colors;
|
||||
|
||||
public static class Builder
|
||||
{
|
||||
private int pnumber = 3;
|
||||
private Map map = null;
|
||||
private State state = new State();
|
||||
// private final int time = 1000;
|
||||
|
||||
public Builder setPlayers(int pnumber)
|
||||
{
|
||||
this.pnumber = pnumber;
|
||||
return this;
|
||||
}
|
||||
// public static class Builder
|
||||
// {
|
||||
// private int pnumber = 3;
|
||||
// private Map map = null;
|
||||
// private State state = new State();
|
||||
// private ArrayList<Color> colors = new ArrayList<>(3);
|
||||
|
||||
// public Builder setPlayers(int pnumber)
|
||||
// {
|
||||
// this.pnumber = pnumber;
|
||||
// return this;
|
||||
// }
|
||||
|
||||
// public Builder addColor(Color color)
|
||||
// {
|
||||
// colors.add(color);
|
||||
// return this;
|
||||
// }
|
||||
|
||||
public Builder setMap(Map map)
|
||||
{
|
||||
this.map = map;
|
||||
return this;
|
||||
}
|
||||
// public Builder addColor(Color[] c)
|
||||
// {
|
||||
// for (Color color : c)
|
||||
// colors.add(color);
|
||||
// return this;
|
||||
// }
|
||||
|
||||
public Builder setState(State s)
|
||||
{
|
||||
this.state = s;
|
||||
return this;
|
||||
}
|
||||
// public Builder remColor(Color color)
|
||||
// {
|
||||
// colors.remove(color);
|
||||
// return this;
|
||||
// }
|
||||
|
||||
// public Builder setMap(Map map)
|
||||
// {
|
||||
// this.map = map;
|
||||
// return this;
|
||||
// }
|
||||
|
||||
public Builder defaultMap()
|
||||
{
|
||||
Integer[][] map = new Integer[][]
|
||||
{
|
||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, -1, -1, 5, 0 },
|
||||
{ 0, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 0, 0, -1, 0, 0, -1, 0 },
|
||||
{ 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0 },
|
||||
{ 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0 },
|
||||
{ 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, -1, -1, 2, 0, 0, -1, 0 },
|
||||
{ 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0 },
|
||||
{ 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0 },
|
||||
{ 0, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -3, 0 },
|
||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||
};
|
||||
// public Builder setState(State s)
|
||||
// {
|
||||
// this.state = s;
|
||||
// return this;
|
||||
// }
|
||||
|
||||
// public Builder defaultMap()
|
||||
// {
|
||||
// Integer[][] map = new Integer[][]
|
||||
// {
|
||||
// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||
// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, -1, -1, 5, 0 },
|
||||
// { 0, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 0, 0, -1, 0, 0, -1, 0 },
|
||||
// { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0 },
|
||||
// { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0 },
|
||||
// { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, -1, -1, 2, 0, 0, -1, 0 },
|
||||
// { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0 },
|
||||
// { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0 },
|
||||
// { 0, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -3, 0 },
|
||||
// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||
// };
|
||||
|
||||
this.map = Map.fromInts(map);
|
||||
return this;
|
||||
}
|
||||
// this.map = Map.fromInts(map);
|
||||
// return this;
|
||||
// }
|
||||
|
||||
public Game build()
|
||||
{
|
||||
if (map == null) defaultMap();
|
||||
return new Game(pnumber, map, state);
|
||||
}
|
||||
}
|
||||
// public Game build()
|
||||
// {
|
||||
// if (map == null) defaultMap();
|
||||
// return new Game(pnumber, map, state, colors);
|
||||
// }
|
||||
// }
|
||||
|
||||
public Game(int pnumber, Map map, State state)
|
||||
{
|
||||
int loop = map.getPathSize();
|
||||
this.map = map;
|
||||
// public Game(int pnumber, Map map, State state, ArrayList<Color> colors)
|
||||
// {
|
||||
// this.map = map;
|
||||
// this.colors = colors;
|
||||
|
||||
cars = new Car[pnumber];
|
||||
obs = new ArrayList<>();
|
||||
// cars = new Car[pnumber];
|
||||
|
||||
for (int i = 0; i < pnumber; i++)
|
||||
{
|
||||
Car car = new Car(loop, state);
|
||||
cars[i] = car;
|
||||
// init(pnumber, state);
|
||||
// }
|
||||
|
||||
// Observer pour avancer
|
||||
obs.add(car);
|
||||
}
|
||||
}
|
||||
// private Game init(int pnumber, State state)
|
||||
// {
|
||||
// // ajout de voiture
|
||||
// int loop = map.getPathSize();
|
||||
|
||||
private boolean isFinish()
|
||||
{
|
||||
for (Car car : cars)
|
||||
{
|
||||
if (car.getFuel() == 0)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// for (int i = 0; i < pnumber; i++)
|
||||
// {
|
||||
// cars[i] = new Car(loop, state);
|
||||
|
||||
public synchronized boolean togglePause()
|
||||
{
|
||||
if (paused) notifyAll();
|
||||
paused = !paused;
|
||||
return paused;
|
||||
}
|
||||
// Color color = (i < colors.size()) ? colors.get(i) : Color.BLUE;
|
||||
// // new Dashboard(cars[i], "Voiture " + (i+1), this::togglePause, color, 300, 300, 300, 300*i);
|
||||
// }
|
||||
|
||||
// // new Track();
|
||||
// // new Rankboard();
|
||||
// return this;
|
||||
// }
|
||||
|
||||
private void step() throws InterruptedException
|
||||
{
|
||||
for (GObserver o : obs)
|
||||
{
|
||||
synchronized (this)
|
||||
{
|
||||
// pause du jeu, while si on notifyall sans faire exprès un autre bout de code
|
||||
while (paused)
|
||||
{
|
||||
wait();
|
||||
}
|
||||
}
|
||||
boolean isSuccess = o.apply();
|
||||
if (!isSuccess)
|
||||
{
|
||||
System.err.println("Une erreur s'est produite pendant le jeu.");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
// private boolean isFinish()
|
||||
// {
|
||||
// for (Car car : cars)
|
||||
// {
|
||||
// if (car.getFuel() == 0)
|
||||
// return true;
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
|
||||
public void run()
|
||||
{
|
||||
while (!isFinish())
|
||||
{
|
||||
try
|
||||
{
|
||||
step();
|
||||
Thread.sleep(time);
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{ e.printStackTrace(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
// public synchronized boolean togglePause()
|
||||
// {
|
||||
// if (paused) notifyAll();
|
||||
// paused = !paused;
|
||||
// GameView.update();
|
||||
// return paused;
|
||||
// }
|
||||
|
||||
// private void step() throws InterruptedException
|
||||
// {
|
||||
// for (Car c : cars)
|
||||
// {
|
||||
// synchronized (this)
|
||||
// {
|
||||
// // pause du jeu, while si on notifyall sans faire exprès un autre bout de code
|
||||
// while (paused)
|
||||
// {
|
||||
// wait();
|
||||
// }
|
||||
// }
|
||||
// c.run();
|
||||
|
||||
// if (!isSuccess)
|
||||
// {
|
||||
// System.err.println("Une erreur s'est produite pendant le jeu.");
|
||||
// System.exit(1);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// public void run()
|
||||
// {
|
||||
// while (!isFinish())
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// step();
|
||||
// Thread.sleep(time);
|
||||
// }
|
||||
// catch (InterruptedException e)
|
||||
// { e.printStackTrace(); }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user