-
Notifications
You must be signed in to change notification settings - Fork 0
/
Partie.java
155 lines (145 loc) · 5.25 KB
/
Partie.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import javax.swing.JOptionPane;
public class Partie
{
public static void placeship(Ship ship, int row, int column, Orientation orientation, Player player)
{
if (orientation.isHorizontal())
{
player.addShip(ship, row, column, Orientation.HORIZONTAL);
}
else
{
player.addShip(ship, row, column, Orientation.VERTICAL);
}
}
public static boolean verif(Ship ship, int row, int column, Orientation orientation, Player player)
{
Grid grid = player.getGridJoueur();
int size = ship.getSize();
boolean res = true;
if (orientation.isHorizontal())
{
if (column+size > 10)
{
JOptionPane.showMessageDialog(null, "Vous ne pouvez pas placer de bateau \n en dehors du cadre (bien essayé) !", "Erreur 001", JOptionPane.ERROR_MESSAGE);
res = false;
}
else
{
for (int i=0; i<size;i++)
{
if (grid.getCell(row, column+i) instanceof ShipCell)
{
JOptionPane.showMessageDialog(null, "Vous ne pouvez pas placer de bateau \n sur un autre bateau", "Erreur 002", JOptionPane.ERROR_MESSAGE);
res = false;
i=size;
}
else
{
}
}
}
}
else if(!orientation.isHorizontal())
{
if (row+size > 10)
{
JOptionPane.showMessageDialog(null, "Vous ne pouvez pas placer de bateau \n en dehors du cadre (bien essayé) !", "Erreur 001", JOptionPane.ERROR_MESSAGE);
res = false;
}
else
{
for (int i=0; i<size;i++)
{
if (grid.getCell(row+i, column) instanceof ShipCell)
{
JOptionPane.showMessageDialog(null, "Vous ne pouvez pas placer de bateau \n sur un autre bateau", "Erreur 002", JOptionPane.ERROR_MESSAGE);
res = false;
i=size;
}
else
{
}
}
}
}
else
{
}
return res;
}
public static void demande(Ship ship, Player player)
{
boolean first = true;
int row = 1;
int col = 1;
Orientation orientation = Orientation.HORIZONTAL;
String stringship;
if (ship.getSize() == 3)
{
stringship = "sous-marin";
}
else if (ship.getSize() == 4)
{
stringship ="Cuirrassé";
}
else
{
stringship = "Porte-avion";
}
boolean verif = false;
while(!verif || first)
{
boolean first2= true;
Object[] possibleValues = { "Horizontalement", "Verticalement"};
Object selectedValue = JOptionPane.showInputDialog(null,
"Comment-voulez vous placer votre " + stringship + " ?", "Input",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);
if (selectedValue.equals("Horizontalement"))
{
orientation = Orientation.HORIZONTAL;
}
else
{
orientation = Orientation.VERTICAL;
}
while(row<0 || row>9 || col<0 || col>9 || first2)
{
row = -1;
col = -1;
String Row = JOptionPane.showInputDialog("Ligne ?");
String Column = JOptionPane.showInputDialog("Colonne ?");
if (Row.equals("") || Column.equals(""))
{
JOptionPane.showMessageDialog(null, "Tapez quelque chose", "Erreur 008", JOptionPane.ERROR_MESSAGE);
}
else
{
int rowtest = Integer.parseInt(Row);
int coltest = Integer.parseInt(Column);
if(rowtest<0 || rowtest>9)
{
JOptionPane.showMessageDialog(null, "Tapez une valeur de ligne entre 1 et 10", "Erreur 006", JOptionPane.ERROR_MESSAGE);
}
else
{
row = Integer.parseInt(Row)-1;
}
if(coltest<0 || coltest>9)
{
JOptionPane.showMessageDialog(null, "Tapez une valeur de colonne entre 1 et 10", "Erreur 007", JOptionPane.ERROR_MESSAGE);
}
else
{
col = Integer.parseInt(Column)-1;
}
first2=false;
}
}
first = false;
verif = verif(ship, row, col, orientation, player);
}
placeship(ship, row, col, orientation, player);
}
}