-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.java
252 lines (237 loc) · 7.3 KB
/
main.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import javafx.application.Application;
//import javafx.beans.value.ChangeListener;
//import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
//import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
//import javafx.scene.control.Label;
//import javafx.scene.layout.BorderPane;
//import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Sphere;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Main extends Application {
// private Matrix a;
public static void serialize(Game obj) throws FileNotFoundException, IOException{
ObjectOutputStream out=null;
try{
out=new ObjectOutputStream(new FileOutputStream("out.txt"));
out.writeObject(obj);
}
finally{
out.close();
}
}
private static Game g=null;
static Stage thestage;
static int noofplayer;
static ChoiceBox<String> ncb;
static ChoiceBox<String> gridbox;
static String grid;
static int m,n;
static GridPane gp;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
menupaine(primaryStage);
}
public static void ButtonClicked(javafx.event.ActionEvent e){
noofplayer=Integer.parseInt((ncb.getValue().toString()));
grid=gridbox.getValue().toString();
if(grid.equals("15x10")){
m=15;
n=10;
}
else{
m=9;
n=6;
}
settings();
}
public static void ngame(javafx.event.ActionEvent e){
noofplayer=Integer.parseInt((ncb.getValue().toString()));
grid=gridbox.getValue().toString();
if(grid.equals("15x10")){
m=15;
n=10;
}
else{
m=9;
n=6;
}
g=new Game(m,n,noofplayer);
game();
}
public static void menupaine(Stage primaryStage){
thestage=primaryStage;
GridPane gridPane=new GridPane();
ncb = new ChoiceBox<String>();
ncb.getItems().addAll ("2","3" ,"4", "5", "6", "7","8");
Text namegameplayer=new Text("No. of players");
Text gridsize=new Text("Choose grid size");
gridbox=new ChoiceBox<String>();
gridbox.getItems().addAll("9x6","15x10");
Button Ngame = new Button("Start Game");
Button Rgame = new Button("Resume Game");
Button Settings = new Button("Settings");
gridPane.setMinSize(500, 500);
gridPane.setPadding(new Insets(10, 10, 10, 10));
gridPane.setAlignment(Pos.CENTER);
gridPane.add(namegameplayer,0,0);
gridPane.add(ncb,1,0);
gridPane.add(gridsize,0,1);
gridPane.add(gridbox,1,1);
gridPane.add(Ngame,0,2);
gridPane.add(Rgame,1,2);
gridPane.add(Settings,2,2);
gridPane.setVgap(5);
gridPane.setHgap(5);
gridPane.setStyle("-fx-background-color: BEIGE;");
Scene SceneMenu = new Scene(gridPane);
primaryStage.setScene(SceneMenu );
primaryStage.show();
Settings.setOnAction(e->ButtonClicked(e));
Ngame.setOnAction(e->ngame(e));
}
public static void settings(){
GridPane gridPane=new GridPane();
int nplayer=noofplayer;
gridPane.setMinSize(500, 500);
gridPane.setPadding(new Insets(10, 10, 10, 10));
gridPane.setAlignment(Pos.CENTER);
for(int i=0;i<nplayer;i++){
Text pc=new Text("Choose colour for " +i);
ChoiceBox<String> ccb = new ChoiceBox<String>();
ccb.getItems().addAll ("red", "blue", "green", "orange", "yellow","black");
gridPane.add(ccb,1,i);
gridPane.add(pc,0,i);
}
gridPane.setVgap(5);
gridPane.setHgap(5);
gridPane.setStyle("-fx-background-color: BEIGE;");
Scene SceneSettings = new Scene(gridPane);
thestage.setScene(SceneSettings);
thestage.show();
}
private static void game(){
Pane root=new Pane();
gp=new GridPane();
Main akla=new Main();
gp.setMinSize(m*50,(n+1)*50);
gp.setAlignment(Pos.CENTER);
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
tile a=akla.new tile();
root.getChildren().add(a);
gp.add(a,j,i);
}
}
ChoiceBox<String> ccb = new ChoiceBox<String>();
ccb.getItems().addAll ("Start game", "Exit");
gp.add(ccb,n+1,0);
Scene scgame = new Scene(gp);
thestage.setScene(scgame);
thestage.show();
}
private int clicks=0;
class tile extends StackPane{
public tile(){
final PhongMaterial redMaterial = new PhongMaterial();
redMaterial.setDiffuseColor(Color.DARKRED);
redMaterial.setSpecularColor(Color.RED);
Rectangle border=new Rectangle(50,50);
border.setFill(null);
border.setStroke(Color.BLACK);
setAlignment(Pos.CENTER);
getChildren().addAll(border);
setOnMouseClicked(event->{
Sphere sphere = new Sphere();
sphere.setRadius(10);
sphere.setMaterial(redMaterial);
getChildren().addAll(sphere);
try {
serialize(g);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int x=gp.getColumnIndex(this);
int y=gp.getRowIndex(this);
//g.getPlayers().get(clicks%g.getPlayers().size()).takeTurn();
g.checkplayers();
g.show();
System.out.println(x+" "+y);
});
}
}
}
//class switchpane extends Application{
// Button btnscene1, btnscene2;
// Label lblscene1, lblscene2;
// FlowPane pane1, pane2;
// Scene scene1, scene2;
// Stage thestage;
//
// public static void main(String[] args) {
// launch(args);
// }
// @Override
// public void start(Stage primaryStage) throws Exception {
// thestage=primaryStage;
// //can now use the stage in other methods
//
// //make things to put on panes
// btnscene1=new Button("Click to go to Other Scene");
// btnscene2=new Button("Click to go back to First Scene");
// btnscene1.setOnAction(e-> ButtonClicked(e));
// btnscene2.setOnAction(e-> ButtonClicked(e));
// lblscene1=new Label();
// lblscene2=new Label();
// //make 2 Panes
// pane1=new FlowPane();
// pane2=new FlowPane();
// pane1.setVgap(10);
// pane2.setVgap(10);
// //set background color of each Pane
// pane1.setStyle("-fx-background-color: tan;-fx-padding: 10px;");
// pane2.setStyle("-fx-background-color: red;-fx-padding: 10px;");
//
// //add everything to panes
// pane1.getChildren().addAll(lblscene1, btnscene1);
// pane2.getChildren().addAll(lblscene2, btnscene2);
// scene1 = new Scene(pane1, 200, 100);
// scene2 = new Scene(pane2, 200, 100);
//
// primaryStage.setTitle("Hello World!");
// primaryStage.setScene(scene1);
// primaryStage.show();
//
// }
// public void ButtonClicked(javafx.event.ActionEvent e)
// {
// if (e.getSource()==btnscene1)
// thestage.setScene(scene2);
// else
// thestage.setScene(scene1);
// }
//
//}