-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI.cpp
1220 lines (1000 loc) · 38.3 KB
/
GUI.cpp
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<fstream>
#include<sstream>
#include<iomanip>
#include<iostream>
#include<string>
//#include<windows.h>
//#include<netdb.h>
using namespace std;
#include"GUI.h"
using namespace GUI;
#include<stdlib.h>
#include"environment.h"
#include "player.h"
#include "conexion.h"
const int NEWGAME_BTN_ID=102,SELECTMAP_BTN_ID=103,DISPLAY_BTN_ID=104,NEWMAP_BTN_ID=105;
const int NEWMAP_OK_BTN_ID=202,NEWMAP_CANCEL_BTN_ID=203;
const int SELECTMAP_SELECT_ID=302,SELECTMAP_CANCEL_BTN_ID=303;
const int DO_ONE_STEP_BTN_ID=402,DO_ONE_RUN_BTN_ID=403,NEXT_RUN_BTN_ID=404,DO_ALL_RUN_BTN_ID=405;
const int REINICIAR_BTN_ID=500, CURSOR_PUT1_BTN_ID=501, CURSOR_PUT2_BTN_ID=502, CURSOR_PUT3_BTN_ID=503,
CURSOR_PUT4_BTN_ID=504, CURSOR_PUT5_BTN_ID=505, CURSOR_PUT6_BTN_ID=506, CURSOR_PUT7_BTN_ID=507,CURSOR_BOOM_BTN_ID=508;
const int ERROR_OK_BTN_ID=602;
int main_window;
char field_ip_server[1024]="150.214.190.149",field_nickname[1024]="Tu_NIF",field_ip_ninja[1024]="150.214.190.149";
char display_step[20]="10",display_time[20]="10";
char ip_server[1024],mi_nick[1024],file_name_temp[1024], nick_adversario[1024]="Adversario";
int tx,ty,tw,th;
float magnification_x,magnification_y;
long long life_time=1000,current_time,total_runs=10,current_run,dirty_degree,consumed_energy;
long long total_dirty_degree,total_consumed_energy;
char time_step_msg[100],action_msg[100],dirty_degree_msg[100],consumed_energy_msg[100];
void GUI::cerrar(){
cout << "Cerrando ventana\n";
}
//int JugadorActivo = 1;
int modo=0, jugador=0;
Environment *env=0;
Player *player1=0, *player2=0;
//RandomNumberGenerator *rng=NULL;
//Evaluator *evaluator=NULL;
Environment::ActionType action;
ifstream fin;
conexion_client Cliente;
conexion_server Servidor;
bool partida_remota=false, esperando_server=false, esperando_client=false;
int conectado;
GLUI *main_glui,*score_glui,*new_map_glui,*select_map_glui,*error_glui, *cursor_glui;
GLUI_Button *new_map_btn,*select_map_btn,*new_map_ok_btn,*new_map_cancel_btn;
GLUI_Button *do_one_step_btn,*display_btn,*do_one_run_btn,*next_run_btn,*do_all_run_btn;
GLUI_Button *cursor_put1, *cursor_put2, *cursor_put3, *cursor_put4, *cursor_put5, *cursor_put6, *cursor_put7, *cursor_boom;
GLUI_Button *reiniciar_btn;
GLUI_Button *quit_btn;
GLUI_StaticText *time_step_text,*action_text,*dirty_degree_text,*consumed_energy_text;
GLUI_StaticText *complete_runs_text,*total_score_text,*average_score_text, *juega_txt;
GLUI_FileBrowser *map_list;
GLUI_RadioGroup *radio1, *radio2;
GLUI_Panel *obj_panel1, *obj_panel2;
GLUI_EditText *obj_ip, *obj_mi_nick;
int GUI::startDraw(int argc,char **argv){
glutInit(&argc,argv);
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowPosition(50,50);
glutInitWindowSize(668,693);
//glutCloseFunc(cerrar);
//glutWMCloseFunc(cerrar);
//glutSetOption ( GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION );
main_window=glutCreateWindow("PRACTICA 3: DESCONECTA 4 BOOM");
GLUI_Master.set_glutReshapeFunc(myGlutReshape);
glutDisplayFunc(myGlutDisplay);
//glutIdleFunc(myGlutIdle);
//glutTimerFunc(100,myGlutTimer2,10);
glEnable(GL_DEPTH_TEST);
score_glui=GLUI_Master.create_glui_subwindow(main_window,GLUI_SUBWINDOW_BOTTOM);
new GLUI_StaticText(score_glui,"");//�H�ťզ氵���j
time_step_text = new GLUI_StaticText(score_glui,"");
new GLUI_StaticText(score_glui,"");//�H�ťզ氵���j
action_text = new GLUI_StaticText(score_glui,"");
new GLUI_StaticText(score_glui,"");//�H�ťզ氵���j
dirty_degree_text = new GLUI_StaticText(score_glui,"");
new GLUI_StaticText(score_glui,"");//�H�ťզ氵���j
consumed_energy_text = new GLUI_StaticText(score_glui,"");
new GLUI_StaticText(score_glui,"");//�H�ťզ氵���j
new GLUI_Column(score_glui,false);
new GLUI_Column(score_glui,false);
new GLUI_Column(score_glui,false);
new GLUI_Column(score_glui,false);
new GLUI_Column(score_glui,false);
new GLUI_Column(score_glui,false);
new GLUI_Column(score_glui,false);
new GLUI_Column(score_glui,false);
new GLUI_Column(score_glui,false);
new GLUI_Column(score_glui,false);
new GLUI_StaticText(score_glui,"");//�H�ťզ氵���j
// new GLUI_Column(score_glui,false);
// new GLUI_Column(score_glui,false);
// new GLUI_Column(score_glui,false);
complete_runs_text = new GLUI_StaticText(score_glui,"");
new GLUI_StaticText(score_glui,"");//�H�ťզ氵���j
total_score_text = new GLUI_StaticText(score_glui,"");
new GLUI_StaticText(score_glui,"");//�H�ťզ氵���j
average_score_text = new GLUI_StaticText(score_glui,"");
new GLUI_StaticText(score_glui,"");//�H�ťզ氵���j
score_glui->set_main_gfx_window( main_window );
main_glui=GLUI_Master.create_glui_subwindow(main_window,GLUI_SUBWINDOW_RIGHT);
new GLUI_StaticText(main_glui,"");//�H�ťզ氵���j
//new_map_btn=new GLUI_Button(main_glui,"NewMap",NEWMAP_BTN_ID,control_cb);
select_map_btn=new GLUI_Button(main_glui,"Opciones",SELECTMAP_BTN_ID,control_cb);
new GLUI_StaticText(main_glui,"");//�H�ťզ氵���j
do_one_step_btn=new GLUI_Button(main_glui,"Jug. Heuristica",DO_ONE_STEP_BTN_ID,control_cb);
do_one_step_btn->disable();
do_one_run_btn=new GLUI_Button(main_glui,"Partida Completa",DO_ONE_RUN_BTN_ID,control_cb);
do_one_run_btn->disable();
//next_run_btn=new GLUI_Button(main_glui,"NextRun",NEXT_RUN_BTN_ID,control_cb);
//next_run_btn->disable();
//do_all_run_btn=new GLUI_Button(main_glui,"DoAllRun",DO_ALL_RUN_BTN_ID,control_cb);
//do_all_run_btn->disable();
new GLUI_StaticText(main_glui,"");//�H�ťզ氵���j
reiniciar_btn=new GLUI_Button(main_glui,"Reiniciar",REINICIAR_BTN_ID,control_cb);
new GLUI_StaticText(main_glui,"");//�H�ťզ氵���j
new GLUI_StaticText(main_glui,"");//�H�ťզ氵���j
new GLUI_StaticText(main_glui,"Opciones de Display");//�H�ťզ氵���j
new GLUI_EditText(main_glui,"Pasos:",display_step);
//new GLUI_EditText(main_glui,"Time:",display_time);
new GLUI_StaticText(main_glui,"");//�H�ťզ氵���j
display_btn=new GLUI_Button(main_glui,"Display",DISPLAY_BTN_ID,control_cb);
display_btn->disable();
new GLUI_StaticText(main_glui,"");//�H�ťզ氵���j
new GLUI_StaticText(main_glui,"");//�H�ťզ氵���j
juega_txt =new GLUI_StaticText(main_glui,"");
cursor_put1=new GLUI_Button(main_glui,"Col 1",CURSOR_PUT1_BTN_ID,control_cb);
cursor_put1->disable();
cursor_put2=new GLUI_Button(main_glui,"Col 2",CURSOR_PUT2_BTN_ID,control_cb);
cursor_put2->disable();
cursor_put3=new GLUI_Button(main_glui,"Col 3",CURSOR_PUT3_BTN_ID,control_cb);
cursor_put3->disable();
cursor_put4=new GLUI_Button(main_glui,"Col 4",CURSOR_PUT4_BTN_ID,control_cb);
cursor_put4->disable();
cursor_put5=new GLUI_Button(main_glui,"Col 5",CURSOR_PUT5_BTN_ID,control_cb);
cursor_put5->disable();
cursor_put6=new GLUI_Button(main_glui,"Col 6",CURSOR_PUT6_BTN_ID,control_cb);
cursor_put6->disable();
cursor_put7=new GLUI_Button(main_glui,"Col 7",CURSOR_PUT7_BTN_ID,control_cb);
cursor_put7->disable();
cursor_boom=new GLUI_Button(main_glui,"BOOM",CURSOR_BOOM_BTN_ID,control_cb);
cursor_boom->disable();
new GLUI_StaticText(main_glui,"");//�H�ťզ氵���j
new GLUI_StaticText(main_glui,"");//�H�ťզ氵���j
quit_btn = new GLUI_Button(main_glui,"Salir",0,(GLUI_Update_CB)exit );//QUIT
main_glui->set_main_gfx_window( main_window );
newGame();
glutMainLoop();
return 0;
}
void GUI::showScore(){
ostringstream sout;
sout.str("");
if (env->JugadorActivo()==1)
sout<<"Juega VERDE";
else
sout << "Juega AZUL";
juega_txt->set_text(sout.str().c_str());
sout.str("");
sout<<"JUGADOR VERDE: " << env->ActionStr( (Environment::ActionType) env->Last_Action(1)) << endl;
time_step_text->set_text(sout.str().c_str());
sout.str("");
sout<<"JUGADOR AZUL: " << env->ActionStr( (Environment::ActionType) env->Last_Action(2)) << endl;
action_text->set_text(sout.str().c_str());
sout.str("");
switch(modo){
case 2: // soy cliente
if (partida_remota and esperando_server)
sout<<"Esperando la jugada de " << nick_adversario << endl;
else
sout<<"Te toca jugar...";
break;
case 3: // soy servidor
if (partida_remota and esperando_client)
sout<<"Esperando la jugada de " << nick_adversario << endl;
else
sout<<mi_nick << " te toca jugar...";
break;
default:
sout << "";
}
dirty_degree_text->set_text(sout.str().c_str());
/*sout.str("");
sout<<"INSTANTE : " << env->Get_Instante() << endl;
consumed_energy_text->set_text(sout.str().c_str());*/
sout.str("");
if (env->JuegoTerminado()){
int ganador=env->RevisarTablero2();
if (ganador==2)
sout<<"GANA EL JUGADOR AZUL"<<endl;
else if (ganador==1)
sout<<"GANA EL JUGADOR VERDE" << endl;
else
sout<<"SE HA PRODUCIDO UN EMPATE" <<endl;
cursor_boom->disable();
}
else {
sout << "" <<endl;
}
consumed_energy_text->set_text(sout.str().c_str());
//complete_runs_text->set_text(sout.str().c_str());
//long long complete_runs=current_run-(current_time!=life_time);
//The Round has been completed
sout.str("");
if (env->Put_FichaBOOM_now())
sout<<"Jugada: " << env->N_Jugada() << " (Ficha bomba)";
else
sout<<"Jugada: " << env->N_Jugada();
complete_runs_text->set_text(sout.str().c_str());
/*sout.str("");
if (Juego_Terminado()){
bool act[6];
env->possible_actions(act);
int n_act=0;
for (int i=0; i<5 and n_act==0; i++)
if (act[i])
n_act++;
if (n_act==0){
if (env->JugadorActivo()==1)
sout<<"GANA EL JUGADOR AZUL";
else
sout<<"GANA EL JUGADOR VERDE";
}
else if (env->Marcador(1)>env->Marcador(2))
sout<<"GANA EL JUGADOR VERDE";
else if (env->Marcador(2)>env->Marcador(1))
sout<<"GANA EL JUGADOR AZUL";
else
sout<<"SE HA PRODUCIDO UN EMPATE";
}
total_score_text->set_text(sout.str().c_str());
sout.str("");
sout<<"SUCIEDAD OBJETIVO: " << env->Suciedad_Objetivo() << endl;
average_score_text->set_text(sout.str().c_str());
*/
}
void GUI::myGlutReshape(int w,int h){
GLUI_Master.get_viewport_area(&tx,&ty,&tw,&th);
glViewport(tx,ty,tw,th);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(tw<=th){
magnification_x=1;
magnification_y=(GLfloat)th/(GLfloat)tw;
}
else{
magnification_x=(GLfloat)tw/(GLfloat)th;
magnification_y=1;
}
glOrtho(-10.0*magnification_x,10.0*magnification_x,-10.0*magnification_y,10.0*magnification_y,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
}
void GUI::myGlutDisplay(){
glClearColor(200.0/255,200.0/255,200.0/255,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluOrtho2D(-50.0,50.0,-50.0,50.0);
if(env!=0){
env->Show(tw,th);
showScore();
}
glFlush();
glutPostRedisplay();
glutSwapBuffers();
}
void GUI::myGlutTimer(int x){
if (partida_remota){
if (esperando_server){
if (!env->JuegoTerminado()){
//Recibir Accion del servidor
char aux;
cout << "Espero accion del servidor" << endl;
aux = Cliente.Recibir_Accion_del_Servidor()-48;
cout << "Accion Recibida: " << (int) (aux) << endl;
action = static_cast<Environment::ActionType> (aux);
env->AcceptAction(action);
env->ChangePlayer();
UpdateButton_Sin_Reiniciar();
esperando_server=false;
myGlutDisplay();
//glutTimerFunc(1000, myGlutTimer2,0);
if (env->JuegoTerminado()){
Cliente.Cerrar_Conexion();
esperando_server=false;
esperando_client=false;
select_map_btn->enable();
modo=0;
}
}
else {
Cliente.Cerrar_Conexion();
esperando_server=false;
esperando_client=false;
select_map_btn->enable();
modo=0;
}
}
else if (esperando_client){
if (!env->JuegoTerminado()){
//Recibir Accion
cout << "Esperando la accion del otro jugador" << endl;
char ac=Servidor.Recibir_Accion()-48;
cout << "Accion Recibida: " << (int) (ac) << endl;
//Actualizar el estado
action = static_cast<Environment::ActionType> (ac);
env->AcceptAction(action);
env->ChangePlayer();
UpdateButton_Sin_Reiniciar();
esperando_client=false;
myGlutDisplay();
//glutTimerFunc(1000, myGlutTimer2,0);
if (env->JuegoTerminado()){
Servidor.Cerrar_Conexion();
esperando_server=false;
esperando_client=false;
select_map_btn->enable();
modo=0;
}
}
else {
Servidor.Cerrar_Conexion();
esperando_server=false;
esperando_client=false;
select_map_btn->enable();
modo=0;
}
}
}
}
bool Juego_Terminado(){
return env->JuegoTerminado();
}
bool Existe(const char *fichname){
fstream f;
f.open(fichname,fstream::in);
if (f.is_open()){
f.close();
return true;
}
else {
cout << "No existe el fichero " << fichname << endl;
return false;
}
}
void GUI::Load_Conf(){
fstream f("game.conf",fstream::in);
string aux;
getline(f,aux);
strcpy(ip_server,aux.c_str());
getline(f,aux);
strcpy(mi_nick,aux.c_str());
f.close();
}
void GUI::Save_Conf(){
strcpy(ip_server,field_ip_server);
strcpy(mi_nick,field_nickname);
fstream f("game.conf",fstream::out);
f << ip_server << endl;
f << mi_nick << endl;
f.close();
}
void GUI::control_cb(int id){
switch(id){
case SELECTMAP_BTN_ID:
//new_map_btn->disable();
select_map_btn->disable();
if (Existe("game.conf")){
Load_Conf();
}
else {
Save_Conf();
}
strcpy(file_name_temp,field_ip_ninja);
select_map_glui=GLUI_Master.create_glui("New Game Setting",0,100,100);
obj_panel1 = new GLUI_Panel( select_map_glui, "Modo de Juego" );
radio1 = new GLUI_RadioGroup( obj_panel1, &modo,5,control_cb );
new GLUI_RadioButton( radio1, "Normal" );
new GLUI_RadioButton( radio1, "Vs mi heuristica" );
new GLUI_RadioButton( radio1, "Vs un jugador remoto (yo Cliente)" );
new GLUI_RadioButton( radio1, "Vs un jugador remoto (yo Server)" );
new GLUI_RadioButton( radio1, "Vs Ninja 1" );
new GLUI_RadioButton( radio1, "Vs Ninja 2" );
new GLUI_RadioButton( radio1, "Vs Ninja 3" );
obj_panel2 = new GLUI_Panel( select_map_glui, "Jugador" );
radio2 = new GLUI_RadioGroup( obj_panel2,&jugador,2,control_cb );
new GLUI_RadioButton( radio2, "Jugador 1 (Verde)" );
new GLUI_RadioButton( radio2, "Jugador 2 (Azul)" );
obj_mi_nick = new GLUI_EditText(select_map_glui,"Mi Apodo:",mi_nick);
//map_list=new GLUI_FileBrowser(select_map_glui,"",false,SELECTMAP_SELECT_ID,control_cb);
obj_ip = new GLUI_EditText(select_map_glui,"IP Servidor:",ip_server);
obj_ip->set_w(200);
//conect_btn=new GLUI_Button(select_map_glui,"Conect",CONECT_BTN_ID,control_cb);
new_map_cancel_btn=new GLUI_Button(select_map_glui,"Ok",SELECTMAP_CANCEL_BTN_ID,control_cb);
break;
case DISPLAY_BTN_ID:
for(long long t=atol(display_step);t>0 && !Juego_Terminado();--t){
doOneStep();
myGlutDisplay();
}
if (Juego_Terminado()){
do_one_run_btn->disable();
do_one_step_btn->disable();
cursor_put1->disable();
cursor_put2->disable();
cursor_put3->disable();
cursor_put4->disable();
cursor_put5->disable();
cursor_put6->disable();
cursor_put7->disable();
cursor_boom->disable();
display_btn->disable();
}
break;
case NEWMAP_OK_BTN_ID:
strcpy(field_ip_server,ip_server);
strcpy(field_nickname,mi_nick);
newGame();
//new_map_btn->enable();
select_map_btn->enable();
new_map_glui->close();
break;
case NEWMAP_CANCEL_BTN_ID:
//new_map_btn->enable();
select_map_btn->enable();
new_map_glui->close();
break;
case SELECTMAP_SELECT_ID:
strcpy(field_ip_server,ip_server);
strcpy(field_nickname,mi_nick);
newGame();
//new_map_btn->enable();
select_map_btn->enable();
select_map_glui->close();
break;
case SELECTMAP_CANCEL_BTN_ID:
//new_map_btn->enable();
strcpy(field_ip_server,ip_server);
strcpy(field_nickname,mi_nick);
Save_Conf();
myGlutDisplay();
glutPostRedisplay();
glutSetWindow(main_window);
glutPostRedisplay();
select_map_btn->enable();
select_map_glui->close();
partida_remota=false;
//cout << "Modo: " << modo << " Jugador Elegido: " << jugador << endl;
if (modo==2){ // En red yo en modo cliente
char mensaje[1024];
Cliente.Establecer_Conexion(ip_server, "5127");
cout << "Empezamos las partida..." << endl;
cout << "Enviando mi nick al servidor..." << endl;
Cliente.Enviar_Msg(mi_nick);
cout << "Le mando que voy a jugar contra otra maquina..." << endl;
Cliente.Enviar_Accion('0');
cout << "Recibiendo el nick del servidor..." << endl;
Cliente.Recibir_Msg(nick_adversario);
cout << "Nick Adversario: " << nick_adversario <<endl;
mensaje[0]=Cliente.Recibir_Accion_del_Servidor();
//Cliente.Recibir_Msg(mensaje);
jugador = mensaje[0]-48;
cout << "Yo juego con el jugador " << jugador+1 << endl;
select_map_btn->enable();
select_map_glui->close();
partida_remota=true;
}
else if (modo==3){ // En red yo en modo servidor
char mensaje[1024];
Servidor.Crear_Servidor();
conectado = Servidor.Aceptar_Cliente();
cout << "Recibir el nick del adversario: " << endl;
Servidor.Recibir_Mensaje(nick_adversario);
cout << "El adversario es " << nick_adversario << endl;
cout << "Esperando al cliente\n";
Servidor.Recibir_Accion();
cout << "Enviar mi nick del adversario: " << endl;
Servidor.Enviar_Cliente_Msg(mi_nick);
cout << "Empezamos las partida..." << endl;
// Le indica al cliente que jugador es
if (jugador==0)
Servidor.Enviar_Accion_al_Cliente('0');
else
Servidor.Enviar_Accion_al_Cliente('1');
cout << "Yo soy el jugador " << jugador+1 << endl;
select_map_btn->enable();
select_map_glui->close();
partida_remota=true;
}
else if (modo==4){ // Vs Ninja 1
char mensaje[1024];
Cliente.Establecer_Conexion(field_ip_ninja, "5127");
cout << "Empezamos las partida..." << endl;
cout << "Enviando mi nick al servidor..." << mi_nick << endl;
Cliente.Enviar_Msg(mi_nick);
cout << "Le mando que voy a jugar contra el ninja 1 ..." << endl;
Cliente.Enviar_Accion('1');
cout << "Recibiendo el nick del servidor..." << endl;
Cliente.Recibir_Msg(nick_adversario);
cout << "Nick Adversario: " << nick_adversario <<endl;
cout << "Le mando que soy el jugador " << jugador+1 << endl;
if (jugador==0)
Cliente.Enviar_Accion('0');
else
Cliente.Enviar_Accion('1');
cout << "Yo juego con el jugador " << jugador+1 << endl;
select_map_btn->enable();
select_map_glui->close();
partida_remota=true;
}
else if (modo==5){ // Vs Ninja 2
char mensaje[1024];
Cliente.Establecer_Conexion(field_ip_ninja, "5128");
cout << "Empezamos las partida..." << endl;
cout << "Enviando mi nick al servidor..." << mi_nick << endl;
Cliente.Enviar_Msg(mi_nick);
cout << "Le mando que voy a jugar contra el ninja 2 ..." << endl;
Cliente.Enviar_Accion('1');
cout << "Recibiendo el nick del servidor..." << endl;
Cliente.Recibir_Msg(nick_adversario);
cout << "Nick Adversario: " << nick_adversario <<endl;
cout << "Le mando que soy el jugador " << jugador+1 << endl;
if (jugador==0)
Cliente.Enviar_Accion('0');
else
Cliente.Enviar_Accion('1');
cout << "Yo juego con el jugador " << jugador+1 << endl;
select_map_btn->enable();
select_map_glui->close();
partida_remota=true;
}
else if (modo==6){ // Vs Ninja 3
char mensaje[1024];
Cliente.Establecer_Conexion(field_ip_ninja, "5129");
cout << "Empezamos las partida..." << endl;
cout << "Enviando mi nick al servidor..." << mi_nick << endl;
Cliente.Enviar_Msg(mi_nick);
cout << "Le mando que voy a jugar contra el ninja 3 ..." << endl;
Cliente.Enviar_Accion('1');
cout << "Recibiendo el nick del servidor..." << endl;
Cliente.Recibir_Msg(nick_adversario);
cout << "Nick Adversario: " << nick_adversario <<endl;
cout << "Le mando que soy el jugador " << jugador+1 << endl;
if (jugador==0)
Cliente.Enviar_Accion('0');
else
Cliente.Enviar_Accion('1');
cout << "Yo juego con el jugador " << jugador+1 << endl;
select_map_btn->enable();
select_map_glui->close();
partida_remota=true;
}
newGame();
break;
case DO_ONE_STEP_BTN_ID:
doNextMove();
break;
case DO_ONE_RUN_BTN_ID:
doOneRun();
break;
case CURSOR_PUT1_BTN_ID:
case CURSOR_PUT2_BTN_ID:
case CURSOR_PUT3_BTN_ID:
case CURSOR_PUT4_BTN_ID:
case CURSOR_PUT5_BTN_ID:
case CURSOR_PUT6_BTN_ID:
case CURSOR_PUT7_BTN_ID:
case CURSOR_BOOM_BTN_ID:
//cout << "\tModo: "<< modo << endl;
switch (modo){
case 0: // Modo Normal
do_move(id-501);
break;
case 1: // Vs mi heuristica
if ((env->JugadorActivo()==1 and jugador==0) or (env->JugadorActivo()==2 and jugador==1)){
do_move(id-501);
doOneStep();
}
break;
case 2: // Vs jugador remoto (yo cliente)
if ((env->JugadorActivo()==1 and jugador==0) or (env->JugadorActivo()==2 and jugador==1)){
cout << "Jugador Activo: " << env->JugadorActivo() << " " << mi_nick << endl;
do_move(id-501);
char aux=(id-501)+48;
// Enviar Accion
cout << "Envio la accion al servidor" << endl;
Cliente.Enviar_Accion(aux);
UpdateButton2();
esperando_client=false;
esperando_server=true;
myGlutDisplay();
glutTimerFunc(1000,myGlutTimer,10);
}
else{
cout << "Jugador Activo: " << env->JugadorActivo() << " " << nick_adversario << endl;
}
break;
case 3: // Vs jugador remoto (yo servidor)
if ((env->JugadorActivo()==1 and jugador==0) or (env->JugadorActivo()==2 and jugador==1)){
do_move(id-501);
cout << "Envio accion al cliente" << endl;
char aux = (id-501)+48;
Servidor.Enviar_Accion_al_Cliente(aux);
cout << "Accion Enviada: " << (int) (aux) << endl;
UpdateButton2();
esperando_client=true;
esperando_server=false;
myGlutDisplay();
glutTimerFunc(1000,myGlutTimer,10);
}
break;
case 4: // Vs ninja
case 5: // Vs ninja
case 6: // Vs ninja
if ((env->JugadorActivo()==1 and jugador==0) or (env->JugadorActivo()==2 and jugador==1)){
cout << "Jugador Activo: " << env->JugadorActivo() << " " << mi_nick << endl;
do_move(id-501);
char aux=(id-501)+48;
// Enviar Accion
cout << "Envio la accion al servidor" << endl;
Cliente.Enviar_Accion(aux);
UpdateButton2();
esperando_client=false;
esperando_server=true;
myGlutDisplay();
glutTimerFunc(1000,myGlutTimer,10);
}
else{
cout << "Jugador Activo: " << env->JugadorActivo() << " " << "NINJA" << endl;
}
break;
}
break;
case ERROR_OK_BTN_ID:
error_glui->close();
break;
case REINICIAR_BTN_ID:
newGame();
break;
default:
break;
}
glutPostRedisplay();
}
void GUI::doNextMove(){
switch (modo){
case 0: //Normal
case 1: //Vs mi heuristica
doOneStep();
break;
case 2: //Vs jugador remoto (yo cliente)
if ((env->JugadorActivo()==1 and jugador==0) or (env->JugadorActivo()==2 and jugador==1)){
//Peticion automatica de jugada del jugador local
player1->Perceive(*env);
action = player1->Think();
env->AcceptAction(action);
env->ChangePlayer();
UpdateButton2();
myGlutDisplay();
char aux = static_cast<char>(action) + 48;
// Enviar Accion
cout << "Envio la accion al servidor" << endl;
Cliente.Enviar_Accion(aux);
esperando_client=false;
esperando_server=true;
glutTimerFunc(1000,myGlutTimer,10);
}
break;
case 3: //Vs jugador remoto (yo servidor)
if ((env->JugadorActivo()==1 and jugador==0) or (env->JugadorActivo()==2 and jugador==1)){
//Peticion automatica de jugada del jugador local
player1->Perceive(*env);
action = player1->Think();
env->AcceptAction(action);
env->ChangePlayer();
UpdateButton2();
myGlutDisplay();
cout << "Envio accion al cliente" << endl;
char aux = static_cast<char>(action) + 48;
Servidor.Enviar_Accion_al_Cliente(aux);
cout << "Accion Enviada: " << (int) (aux) << endl;
esperando_client=true;
esperando_server=false;
glutTimerFunc(1000,myGlutTimer,10);
}
break;
case 4: //Vs ninja
case 5: //Vs ninja
case 6: //Vs ninja
if ((env->JugadorActivo()==1 and jugador==0) or (env->JugadorActivo()==2 and jugador==1)){
//Peticion automatica de jugada del jugador local
player1->Perceive(*env);
action = player1->Think();
env->AcceptAction(action);
env->ChangePlayer();
UpdateButton2();
myGlutDisplay();
char aux = static_cast<char>(action) + 48;
// Enviar Accion
cout << "Envio la accion al servidor" << endl;
Cliente.Enviar_Accion(aux);
esperando_client=false;
esperando_server=true;
glutTimerFunc(1000,myGlutTimer,10);
}
break;
}
}
void GUI::doOneStep(){
if (!Juego_Terminado()){
if (env->JugadorActivo()==1){
player1->Perceive(*env);
action = player1->Think();
env->AcceptAction(action);
env->ChangePlayer();
}
else {
player2->Perceive(*env);
action = player2->Think();
env->AcceptAction(action);
env->ChangePlayer();
}
}
UpdateButton();
}
void GUI::do_move(int accion){
env->AcceptAction(static_cast<Environment::ActionType> (accion));
myGlutDisplay();
//Sleep(100);
env->ChangePlayer();
UpdateButton();
}
void GUI::doOneRun(){
do_one_run_btn->disable();
while(!Juego_Terminado()){
doOneStep();
myGlutDisplay();
//Sleep(100);
//glutMainLoop();
}
UpdateButton();
}
void GUI::UpdateButton(){
if (env==0 or Juego_Terminado()){
do_one_step_btn->disable();
select_map_btn->enable();
cursor_put1->disable();
cursor_put2->disable();
cursor_put3->disable();
cursor_put4->disable();
cursor_put5->disable();
cursor_put6->disable();
cursor_put7->disable();
display_btn->disable();
quit_btn->enable();
}
else {
do_one_step_btn->enable();
quit_btn->enable();
select_map_btn->enable();
reiniciar_btn->enable();
bool act[8];
env->possible_actions(act);
if (act[0])
cursor_put1->enable();
else
cursor_put1->disable();
if (act[1])
cursor_put2->enable();
else
cursor_put2->disable();
if (act[2])
cursor_put3->enable();
else
cursor_put3->disable();
if (act[3])
cursor_put4->enable();
else
cursor_put4->disable();
if (act[4])
cursor_put5->enable();
else
cursor_put5->disable();
if (act[5])
cursor_put6->enable();
else
cursor_put6->disable();
if (act[6])
cursor_put7->enable();
else
cursor_put7->disable();
if (act[7])
cursor_boom->enable();
else
cursor_boom->disable();
}
}
void GUI::UpdateButton_Sin_Reiniciar(){
if (env==0 or Juego_Terminado()){
do_one_step_btn->disable();
select_map_btn->enable();
cursor_put1->disable();
cursor_put2->disable();
cursor_put3->disable();
cursor_put4->disable();
cursor_put5->disable();
cursor_put6->disable();
cursor_put7->disable();
display_btn->disable();
quit_btn->enable();
}
else {
do_one_step_btn->enable();
quit_btn->enable();
select_map_btn->enable();
reiniciar_btn->disable();
bool act[8];
env->possible_actions(act);
if (act[0])
cursor_put1->enable();
else
cursor_put1->disable();
if (act[1])
cursor_put2->enable();
else
cursor_put2->disable();
if (act[2])
cursor_put3->enable();
else
cursor_put3->disable();
if (act[3])