mirror of
https://github.com/guezoloic/L3-racing-game.git
synced 2026-03-31 04:11:33 +00:00
Ajout de la partie view du projet
This commit is contained in:
144
model/Game.java
Normal file
144
model/Game.java
Normal file
@@ -0,0 +1,144 @@
|
||||
package model;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Game
|
||||
{
|
||||
private boolean paused;
|
||||
private Car[] cars;
|
||||
private Map map;
|
||||
|
||||
private ArrayList<GObserver> obs;
|
||||
|
||||
public static class Builder
|
||||
{
|
||||
private int pnumber = 3;
|
||||
private Map map = null;
|
||||
private State state = new State();
|
||||
|
||||
public Builder setPlayers(int pnumber)
|
||||
{
|
||||
this.pnumber = pnumber;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setMap(Map map)
|
||||
{
|
||||
this.map = map;
|
||||
return this;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public Game build()
|
||||
{
|
||||
if (map == null) defaultMap();
|
||||
return new Game(pnumber, map, state);
|
||||
}
|
||||
}
|
||||
|
||||
public Game(int pnumber, Map map, State state)
|
||||
{
|
||||
int loop = map.getPathSize();
|
||||
this.map = map;
|
||||
|
||||
cars = new Car[pnumber];
|
||||
obs = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < pnumber; i++)
|
||||
{
|
||||
Car car = new Car(loop, state);
|
||||
cars[i] = car;
|
||||
|
||||
// Observer pour avancer
|
||||
obs.add(car);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isFinish()
|
||||
{
|
||||
for (Car car : cars)
|
||||
{
|
||||
if (car.getFuel() == 0)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public synchronized boolean togglePause()
|
||||
{
|
||||
if (paused) notifyAll();
|
||||
paused = !paused;
|
||||
return paused;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void run()
|
||||
{
|
||||
while (!isFinish())
|
||||
{
|
||||
try
|
||||
{
|
||||
step();
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{ e.printStackTrace(); }
|
||||
}
|
||||
}
|
||||
|
||||
/** Retourne la carte du jeu */
|
||||
public Map getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
/** Retourne toutes les voitures du jeu */
|
||||
public Car[] getCars() {
|
||||
return cars;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user