Skip to content

Commit 71abebd

Browse files
committed
Controller Jeu
1 parent 3de70b4 commit 71abebd

File tree

10 files changed

+420
-0
lines changed

10 files changed

+420
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.ecn.medev.controller;
2+
3+
import org.ecn.medev.controller.start.StartMenuController;
4+
5+
/**
6+
* Class used to start a new game session, it is the main class to start the game
7+
*/
8+
public class GameLauncher {
9+
public static void main(String args[]) {
10+
GameSession gameSession = new GameSession();
11+
StartMenuController startMenuController = new StartMenuController(gameSession);
12+
System.out.println("Le jeu est lance!\nTout moment vous pouvez taper 'help' pour avoir les options possibles");
13+
startMenuController.start();
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.ecn.medev.controller;
2+
3+
import lombok.Data;
4+
import org.ecn.medev.Plateau;
5+
6+
/**
7+
* Class that hold temporary user information when joining the game such as login information
8+
*/
9+
@Data
10+
public class GameSession {
11+
private Plateau plateau;
12+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.ecn.medev.controller;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* Class used to designate the simple text interface to user
7+
*/
8+
@Data
9+
public class InputOption {
10+
/**
11+
* The key typed by user to select the option
12+
*/
13+
private OptionKey optionKey;
14+
15+
/**
16+
* Short description of the option. usually one word so all options could be printed on one line
17+
* example: login
18+
*/
19+
private String shortDescription;
20+
/**
21+
* Long description of the option. usually one line so each option is printed on one line
22+
* example: login with username and password
23+
*/
24+
private String longDescription;
25+
26+
27+
public InputOption(OptionKey optionKey) {
28+
this.optionKey = optionKey;
29+
this.shortDescription = "no description provided";
30+
this.longDescription = "no description provided";
31+
}
32+
33+
public InputOption(OptionKey optionKey, String shortDescription, String longDescription) {
34+
this.optionKey = optionKey;
35+
this.shortDescription = shortDescription;
36+
this.longDescription = longDescription;
37+
}
38+
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package org.ecn.medev.controller;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import org.ecn.medev.controller.util.CallbackToDo;
6+
import org.ecn.medev.controller.util.UnsupportedActionException;
7+
8+
import java.util.*;
9+
import java.util.stream.Collectors;
10+
11+
@Getter
12+
@Setter
13+
public abstract class MenuController {
14+
15+
@Getter
16+
private enum MenuOption implements OptionKey {
17+
SHUTDOWN("*"),
18+
GO_PARENT("*");
19+
20+
private final String inputKey;
21+
22+
MenuOption(String s) {
23+
this.inputKey = s;
24+
}
25+
}
26+
27+
private final Map<MenuOption, InputOption> COMMON_OPTIONS = Collections.unmodifiableMap(new HashMap<MenuOption, InputOption>() {{
28+
put(MenuOption.SHUTDOWN, new InputOption(MenuOption.SHUTDOWN, "Terminer", "Terminer le jeu."));
29+
put(MenuOption.GO_PARENT, new InputOption(MenuOption.GO_PARENT, "Menu precedant", "Revenir au menu precedant"));
30+
}});
31+
private final UiMenuType MENU_TYPE;
32+
33+
private MenuController parentMenu;
34+
35+
private Map<String, InputOption> options;
36+
37+
private GameSession gameSession;
38+
39+
public MenuController(UiMenuType MENU_TYPE, List<InputOption> options, GameSession gameSession, MenuController parentMenu) {
40+
this.MENU_TYPE = MENU_TYPE;
41+
if (parentMenu != null) {
42+
this.parentMenu = parentMenu;
43+
options.add(COMMON_OPTIONS.get(MenuOption.GO_PARENT));
44+
} else {
45+
options.add(COMMON_OPTIONS.get(MenuOption.SHUTDOWN));
46+
}
47+
this.options = options.stream().collect(Collectors.toMap(o -> o.getOptionKey().getInputKey(), o -> o));
48+
this.gameSession = gameSession;
49+
}
50+
51+
public void displayLongMenu() {
52+
System.out.println("Choisir une option de la menu:");
53+
options.values().stream().forEach(o -> System.out.println(o.getOptionKey().getInputKey() + " - " + o.getLongDescription()));
54+
}
55+
56+
public void displayShortMenu() {
57+
System.out.print("Options: ");
58+
options.values().stream().forEach(o -> System.out.print(" (" + o.getOptionKey().getInputKey() + ")" + o.getShortDescription()));
59+
System.out.print("\nYour choice: ");
60+
}
61+
62+
public boolean conditionToStart() {
63+
return true;
64+
}
65+
public void start() {
66+
if (!conditionToStart()) {
67+
System.out.println("Condition to start menu are not met");
68+
if (parentMenu != null)
69+
parentMenu.start();
70+
return;
71+
}
72+
displayLongMenu();
73+
try {
74+
doTheChoice(getTheChoice());
75+
} catch (UnsupportedActionException e) {
76+
e.printStackTrace();
77+
}
78+
}
79+
80+
public InputOption getTheChoice() {
81+
String choice = "", st;
82+
System.out.println("---------------------------------");
83+
int i;
84+
boolean isValidInput = false;
85+
Scanner in = new Scanner(System.in);
86+
do {
87+
choice = in.next();
88+
if (choice.equalsIgnoreCase("help")) {
89+
displayLongMenu();
90+
}
91+
if (choice.equalsIgnoreCase("*")) {
92+
if (parentMenu == null) {
93+
return new InputOption(MenuOption.SHUTDOWN);
94+
} else {
95+
return new InputOption(MenuOption.GO_PARENT);
96+
}
97+
}
98+
isValidInput = options.containsKey(choice);
99+
if (!isValidInput) {
100+
displayShortMenu();
101+
}
102+
} while (!isValidInput);
103+
return options.get(choice);
104+
}
105+
106+
public static Integer getIndexChoiceFromList(String header, List<String> options) {
107+
if (options == null || options.size() == 0) {
108+
return null;
109+
}
110+
CallbackToDo callbackToDo = () -> {
111+
System.out.println(header);
112+
int i = 1;
113+
for (String option : options) {
114+
System.out.println(i + " - " + option);
115+
i++;
116+
}
117+
System.out.println("---------------------------------");
118+
};
119+
callbackToDo.call();
120+
121+
return getIndexChoice(options.size(), callbackToDo);
122+
}
123+
124+
public static Integer getIndexChoice(int arraySize, CallbackToDo helpFunction) {
125+
return getIndexChoice(arraySize, "Your choice", helpFunction);
126+
}
127+
128+
public static Integer getIndexChoice(int arraySize, String indexMeaning, CallbackToDo helpFunction) {
129+
if(arraySize <= 0){
130+
return null;
131+
}
132+
Scanner in = new Scanner(System.in);
133+
boolean isValidChoice = false;
134+
String choice = "", st;
135+
Integer indexChoice = null, i = 1;
136+
do {
137+
System.out.print(indexMeaning + " [1-" + arraySize + "]: ");
138+
choice = in.next();
139+
if (choice.equalsIgnoreCase("help") && helpFunction != null) {
140+
helpFunction.call();
141+
}
142+
for (i = 1; i <= arraySize; i++) {
143+
st = "" + i;
144+
if (choice.equals(st)) {
145+
isValidChoice = true;
146+
indexChoice = i - 1;
147+
}
148+
}
149+
} while (!isValidChoice);
150+
return indexChoice;
151+
}
152+
153+
154+
public void doTheChoice(InputOption inputOption) throws UnsupportedActionException {
155+
if (inputOption.getOptionKey() instanceof MenuOption) {
156+
switch ((MenuOption) inputOption.getOptionKey()) {
157+
case SHUTDOWN:
158+
System.out.println("Shutting down...");
159+
return;
160+
case GO_PARENT:
161+
System.out.println("Going back.");
162+
parentMenu.start();
163+
return;
164+
}
165+
}
166+
doInnerChoice(inputOption);
167+
}
168+
169+
/**
170+
* Use {@link #doTheChoice(InputOption)} instead to get functionalities such as going back and exiting application.
171+
* <p>
172+
* This function have recursive behavior that do action based on
173+
* parameter provided. Then it call again itself by {@link #doInnerChoice(InputOption)} {@link #getTheChoice()}
174+
* unless the program end, or it starts another menu controller.
175+
*
176+
* @param inputOption
177+
*/
178+
protected abstract void doInnerChoice(InputOption inputOption) throws UnsupportedActionException;
179+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.ecn.medev.controller;
2+
3+
/**
4+
* Class that return the key typed by user to select the option
5+
*/
6+
public interface OptionKey {
7+
/**
8+
* example for a menu:
9+
* <ol>
10+
* <li>first item</li>
11+
* <li>second item</li>
12+
* <li>third item</li>
13+
* </ol>
14+
* the returned key can be 1 to designate that user had selected first item.
15+
* @return The key typed by user to select the option
16+
*/
17+
String getInputKey();
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.ecn.medev.controller;
2+
3+
public enum UiMenuType {
4+
START_PAGE
5+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.ecn.medev.controller.start;
2+
3+
import org.ecn.medev.Joueur;
4+
import org.ecn.medev.Plateau;
5+
import org.ecn.medev.controller.GameSession;
6+
import org.ecn.medev.controller.InputOption;
7+
import org.ecn.medev.controller.MenuController;
8+
import org.ecn.medev.controller.UiMenuType;
9+
import org.ecn.medev.controller.util.UnsupportedActionException;
10+
11+
import java.sql.SQLException;
12+
import java.sql.Timestamp;
13+
import java.util.*;
14+
import java.util.stream.Collectors;
15+
16+
17+
public class StartMenuController extends MenuController {
18+
public static final UiMenuType MENU_TYPE = UiMenuType.START_PAGE;
19+
public static final List<InputOption> OPTIONS = Collections.unmodifiableList(Arrays.asList(
20+
new InputOption(StartMenuOption.ENTER_GAME, "Lancer Jeu", "Lancer le jeu")
21+
));
22+
23+
public StartMenuController(GameSession gameSession) {
24+
super(MENU_TYPE, new ArrayList<>(OPTIONS), gameSession, null);
25+
}
26+
27+
@Override
28+
protected void doInnerChoice(InputOption inputOption) throws UnsupportedActionException {
29+
StartMenuOption option;
30+
if (inputOption.getOptionKey() instanceof StartMenuOption) {
31+
option = (StartMenuOption) inputOption.getOptionKey();
32+
} else {
33+
throw new UnsupportedActionException("Expecting option key of type " + StartMenuOption.class
34+
+ ", provided : " + inputOption.getOptionKey().getClass());
35+
}
36+
switch (option) {
37+
case ENTER_GAME:
38+
if (getGameSession().getPlateau() == null) {
39+
initializeJeu();
40+
}
41+
commencerLeJeu();
42+
break;
43+
}
44+
displayShortMenu();
45+
doTheChoice(getTheChoice());
46+
}
47+
48+
private void commencerLeJeu() {
49+
Plateau gameBoard = getGameSession().getPlateau();
50+
while(gameBoard.partieEnCours){
51+
//gameBoard.tourDeJeu();
52+
gameBoard.affiche();
53+
gameBoard.tourDeJeu();
54+
}
55+
}
56+
57+
private void initializeJeu() {
58+
System.out.println("Choisit le nombre de personne");
59+
int nombreDePersonne = MenuController.getIndexChoice(6, "Nombre de joueur", null) + 1;
60+
System.out.println("Creation de plateau");
61+
LinkedList<Joueur> joueurs = new LinkedList<>();
62+
for (int i = 0; i < nombreDePersonne; i++) {
63+
joueurs.add(readJoueur(i));
64+
}
65+
Plateau plateau = new Plateau();
66+
plateau.setJoueurs(joueurs);
67+
getGameSession().setPlateau(plateau);
68+
}
69+
70+
71+
private Joueur readJoueur(int index) {
72+
Joueur joueur = new Joueur();
73+
Scanner in = new Scanner(System.in);
74+
String nom = "";
75+
while(nom.isEmpty()) {
76+
System.out.print("Nom du joueur " + (index+1) + " : ");
77+
nom = in.nextLine();
78+
joueur.setNom(nom);
79+
}
80+
return joueur;
81+
}
82+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.ecn.medev.controller.start;
2+
3+
import lombok.Getter;
4+
import org.ecn.medev.controller.OptionKey;
5+
6+
7+
@Getter
8+
public enum StartMenuOption implements OptionKey {
9+
ENTER_GAME("1");
10+
11+
private final String inputKey;
12+
13+
StartMenuOption(String s) {
14+
this.inputKey = s;
15+
}
16+
}

0 commit comments

Comments
 (0)