-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1352 lines (1201 loc) · 35.4 KB
/
main.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
//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h"
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <ctime>
#include <cstdlib>
//Screen attributes
const int SCREEN_WIDTH = 320;
const int SCREEN_HEIGHT = 240;
const int SCREEN_BPP = 32;
const int menuTextAmount = 4;
const int FRAMES_PER_SECOND=30;
//The surfaces
SDL_Surface *menuBackground = NULL;
SDL_Surface *background = NULL;
SDL_Surface *gameartImg = NULL;
SDL_Rect gameart[ 4 ];
SDL_Surface *gameboard = NULL;
SDL_Surface *menuMessage[menuTextAmount];
SDL_Surface *message = NULL;
SDL_Surface *messageFromOpponentDisplay = NULL;
SDL_Surface *messageWinsAndLoses = NULL;
std::string messageFromOpponent;
std::string menuMessageText[] = {"Press START to Play Game"," Press Y to View Credits", "Made by Nathan Hurde[theweirdn8]","www.PawByte.com"};
SDL_Surface *screen = NULL;
//The Game
//External Game Variables
const float gameVersion= 1.04;
std::string gameCaptionTitle = "PawByte - Tic Tac Toe";
//Map/Room Variables
int CURRENT_AREA = 0;
int MAIN_MENU = 0;
int GAME_CREDITS = 1;
int GAME_BOARD = 2;
int BOARD_PXWIDTH = 1;
int BOARD_PXHEIGHT =1;
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
//Credits Variables
//Credits alpha
int creditsAlpha = SDL_ALPHA_OPAQUE;
std::string creditsCaptions[]={"Game Created By","Game Designers ","Game Programmers ","Porting Team","Game Art","Game Music ","Thanks for Playing "};
int creditsMaxNumber[]={2,1,1,2,4,3,3};
std::string creditsNames[7][4]={
{"Nathan Hurde(theweirdn8)","PawByte.com","",""},
{"Nathan Hurde(theweirdn8)"," "," ",""},
{"Nathan Hurde(theweirdn8)"," "," ",""},
{"GCW-Zero Zear","RG 300 - JOCO_MKNZ (a.k.a Poligraf)"," ",""},
{"jimn346 from nationofdesign.com",
"Adzel from nationofdesign.com",
"Game Font - DroidSansMono",
"Released under the Apache License"},
{"Kevin Macleod from incompetech.com","Main Menu by Kevin Macleod","Credits Page by Kevin Macleod","Main Gameplay by Kevin Macleod"},
{"You are Awesome!","Support Open Source!","Love Linux",""}
};
int creditsCaptionCounter=0;
int creditsNamesCounter=0;
int creditsTiming=10;
int creditsExtraLoopage=1;
int creditsECounter=0;
int creditsTimeCounter=0;
SDL_Surface *creditsCaptionDisplay = NULL;
SDL_Surface *creditsNameDisplay = NULL;
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
//Game Variables
int gameSquare[9];
int gameToken;
bool isPlayerTurn;
int turnNumber=0;
int gameDifficulty;
int gameRoom;
bool gameCompleted=false;
int playerWins=0;
int computerWins=0;
int currentSlot=0;
int nRanSquare=0;
int mouse_x=0, mouse_y=0;
int fakeTimer=0;
//The event structure
SDL_Event event;
//The font
TTF_Font *font = NULL;
TTF_Font *creditsFontTitle = NULL;
//The color of the font
SDL_Color textColor = { 0, 0, 0 };
SDL_Color redColor = {255, 0, 0};
SDL_Color whiteColor = {255, 255, 255};
//The music that will be played
Mix_Music *bgdMusic[3];
std::ofstream myfile;
//messages from the PC
std::string opponentOpening[]={"Do not waste my time","Lets get this show on the road.","I wonder how I will beat you...",
"Lets go!", "Lets have a clean fight.", "This should be fun." ,"Try to beat me!", "Prepare to Lose","Let's give the crowd a show","My mission is to win","My code shall not fail me!"};
std::string opponentFirstMove[]={"There","YOLO!","Bring it on!","Feel the power!","9 squares, your blood, my code","Try to beat that","Ah, my 1st turn.","Time to start with a bang!","There you go.","Here we go..","I choose this one."};
std::string opponentBlock[]={"Beep Boop!","Nice try ole' sport.","My defenses are stronger than your will!","So predictable.","My A.I detected that.","I'm not falling for that","Nice try!","Try harder next time","Wham!","Haha.","Naughty, naughty."};
std::string opponentLose[]={"GG","I lost...", "Good job ole' sport!","Well played.","YOLO","A worthy opponent.","Good game.","Congratulations.","I let you win.","Come on, rematch!","Oh no!"};
std::string opponentWin[]={"No autographs please.","Welcome to Bronze V Rank of TTT Players","Your best wasn't good enough","I win, as usual.","Good game.","Well played.","Rematch?","You were a good opponent.","GG","Well, that was fun.","I win!"};
std::string opponentTieGame[]={"Rematch?","Jeepers, a tie!","What?! My legendary skills failed me.","I'm tired with this nerd?!","This is worst than losing.","Ha ha, you tied to horribly programmed A.I.","That was sick!","Good game.","Lets go again.","Well thats unusual.","Cool, us legends tied."};
int oTextArraySize= 10;
std::string winsandLosesText = "";
int random(int low,int high)
{
int randnum = (rand() % (high-low + 1)) + low;
return randnum;
}
std::string floattostring(float in)
{
std::ostringstream converter;
converter << in;
return converter.str();
}
std::string inttostring(int in)
{
std::ostringstream converter;
converter << in;
return converter.str();
}
void resetCredits()
{
creditsAlpha = SDL_ALPHA_OPAQUE;
creditsCaptionCounter=0;
creditsNamesCounter=0;
creditsTiming=100;
creditsExtraLoopage=10;
creditsECounter=0;
creditsTimeCounter=0;
}
void updateCreditsNameText()
{
creditsCaptionDisplay = TTF_RenderText_Solid( creditsFontTitle ,creditsCaptions[creditsCaptionCounter].c_str(), whiteColor );
creditsNameDisplay = TTF_RenderText_Solid( font,(creditsNames[creditsCaptionCounter][creditsNamesCounter]).c_str(), whiteColor );
//Set surface alpha
SDL_SetAlpha( creditsNameDisplay, SDL_SRCALPHA, creditsAlpha );
}
void updateOpponentText(std::string textInput)
{
messageFromOpponent=textInput;
messageFromOpponentDisplay = TTF_RenderText_Solid( font,messageFromOpponent.c_str(), whiteColor );
}
void updateWinsText()
{
winsandLosesText.clear();
winsandLosesText.append("Wins: ");
winsandLosesText.append(inttostring(playerWins));
winsandLosesText.append(" Loses: ");
winsandLosesText.append(inttostring(computerWins));
if( messageWinsAndLoses!=NULL)
{
SDL_FreeSurface(messageWinsAndLoses);
}
messageWinsAndLoses = TTF_RenderText_Solid( font,winsandLosesText.c_str(), redColor );
}
void startGame()
{
for(int iLoop=0;iLoop<9;iLoop++)
{
gameSquare[iLoop]=-1;
}
if(random(0,100)>50)
{
isPlayerTurn=true;
}
else
{
isPlayerTurn=false;
}
gameCompleted=false;
turnNumber=0;
updateOpponentText(opponentOpening[random(0,oTextArraySize)]);
updateWinsText();
}
void playBackgroundMusic()
{
Mix_HaltMusic();
Mix_PlayMusic( bgdMusic[CURRENT_AREA], -1 );
}
void goToMenu()
{
startGame();
CURRENT_AREA = MAIN_MENU;
playBackgroundMusic();
}
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old surface
SDL_FreeSurface( loadedImage );
//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 255, 000, 255 ) );
}
}
//Return the optimized surface
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}
void drawRectangle(int x1, int y1, int x2, int y2, int color)
{
int xx = std::min(x1,x2);
int yy = std::min(y1,y2);
int w = std::max(x1,x2)-xx;
int h = std::max(y1,y2)-yy;
SDL_Rect rect = {xx,yy,w,h};
SDL_FillRect(screen, &rect, color);
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE | SDL_RESIZABLE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Initialize SDL_ttf
if( TTF_Init() == -1 )
{
return false;
}
//Initialize SDL_mixer
if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
{
return false;
}
//Set the window caption
gameCaptionTitle.append(" - Version - ");
gameCaptionTitle.append(floattostring(gameVersion));
SDL_WM_SetCaption( gameCaptionTitle.c_str(), NULL );
//sets the background music to nulls/undeclared
for(int iLoop=0;iLoop<3;iLoop++)
{
bgdMusic[iLoop]=NULL;
}
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the background images
menuBackground = load_image("resources/gfx/menu_background.png");
background = load_image("resources/gfx/background.png");
gameartImg = load_image("resources/gfx/gameart.png");
gameboard = load_image("resources/gfx/gameboard.png");
//Open the font(s)
font = TTF_OpenFont( "resources/font/DroidSansMono.ttf", 14 );
creditsFontTitle = TTF_OpenFont( "resources/font/DroidSansMono.ttf", 18 );
//If there was a problem in loading the background
if( background == NULL )
{
/*
Commented out for GCW-Zero version
myfile.open ("errors.txt");
myfile << "Unable to load background.\n";
myfile.close();
*/
return false;
}
if(gameboard==NULL)
{
/*
Commented out for GCW-Zero version
myfile.open ("errors.txt");
myfile << "Unable to load gameboard.\n";
myfile.close();
*/
return false;
}
else
{
BOARD_PXWIDTH=gameboard->w;
BOARD_PXHEIGHT=gameboard->h;
}
if( gameartImg== NULL )
{
/*
Commented out for GCW-Zero version
myfile.open ("errors.txt");
myfile << "Unable to game art! \n";
myfile.close();
*/
return false;
}
//If there was an error in loading the font
if( font == NULL )
{
/*
Commented out for GCW-Zero version
myfile.open ("errors.txt");
myfile << "Unable to load font\n";
myfile.close();
*/
return false;
}
bgdMusic[0] = Mix_LoadMUS( "resources/sfx/bckgdm0.ogg" );
bgdMusic[1] = Mix_LoadMUS( "resources/sfx/bckgdm1.ogg" );
bgdMusic[2] = Mix_LoadMUS( "resources/sfx/bckgdm2.ogg" );
//If there was a problem loading the music
for(int iBugMus=0;iBugMus<3;iBugMus++)
{
if(bgdMusic[0]==NULL)
{
/*
Commented out for GCW-Zero version
myfile.open ("errors.txt");
myfile << "Unable to load music Index Number" << iBugMus << ".\n";
myfile.close();
*/
return false;
}
}
//If everything loaded fine
/*
Commented out for GCW-Zero version
myfile.open ("errors.txt");
myfile << "Everything loaded successfully! \n";
myfile.close();
*/
return true;
}
void clean_up()
{
//Free the surfaces
SDL_FreeSurface( background );
SDL_FreeSurface(menuBackground);
SDL_FreeSurface( gameartImg );
SDL_FreeSurface( gameboard);
SDL_FreeSurface( message );
for(int icleanUpMenu=0;icleanUpMenu<menuTextAmount;icleanUpMenu++)
{
SDL_FreeSurface(menuMessage[icleanUpMenu]);
}
SDL_FreeSurface( messageFromOpponentDisplay );
SDL_FreeSurface( messageWinsAndLoses );
//Free the sound effects
//Free the music
for(int iLoopM=0;iLoopM<3;iLoopM++)
{
Mix_FreeMusic(bgdMusic[iLoopM]);
}
//Close the font
TTF_CloseFont( font );
TTF_CloseFont( creditsFontTitle );
//Quit SDL_mixer
Mix_CloseAudio();
//Quit SDL_ttf
TTF_Quit();
//Quit SDL
SDL_Quit();
}
int getWinningSquare(int squareBoxType)
{
//diagnol line victories
//the first diagnol line check
if((gameSquare[4]==squareBoxType)&&(gameSquare[8]==squareBoxType))
{
if(gameSquare[0]<0)
{
return 0;
}
}
if((gameSquare[0]==squareBoxType)&&(gameSquare[8]==squareBoxType))
{
if(gameSquare[4]<0)
{
return 4;
}
}
if((gameSquare[0]==squareBoxType)&&(gameSquare[4]==squareBoxType))
{
if(gameSquare[8]<0)
{
return 8;
}
}
//the second diagnol line check
if((gameSquare[4]==squareBoxType)&&(gameSquare[6]==squareBoxType))
{
if(gameSquare[2]<0)
{
return 2;
}
}
if((gameSquare[2]==squareBoxType)&&(gameSquare[6]==squareBoxType))
{
if(gameSquare[4]<0)
{
return 4;
}
}
if((gameSquare[2]==squareBoxType)&&(gameSquare[4]==squareBoxType))
{
if(gameSquare[6]<0)
{
return 6;
}
}
//row checking
for(int i=0;i<9;i++)
{
//first row check
if((i==0)||(i==3)||(i==6))
{
if((gameSquare[i+1]==squareBoxType)&&(gameSquare[i+2]==squareBoxType))
{
if(gameSquare[i]<0)
{
return i;
}
}
}
if((i==1)||(i==4)||(i==7))
{
//second row check
if((gameSquare[i-1]==squareBoxType)&&(gameSquare[i+1]==squareBoxType))
{
if(gameSquare[i]<0)
{
return i;
}
}
}
if((i==2)||(i==5)||(i==8))
{
//third row check
if((gameSquare[i-2]==squareBoxType)&&(gameSquare[i-1]==squareBoxType))
{
if(gameSquare[i]<0)
{
return i;
}
}
}
if(i<3)
{
//first column check
if((gameSquare[i+3]==squareBoxType)&&(gameSquare[i+6]==squareBoxType))
{
if(gameSquare[i]<0)
{
return i;
}
}
}
if((i>=3)&&(i<=5))
{
//second column check
if((gameSquare[i-3]==squareBoxType)&&(gameSquare[i+3]==squareBoxType))
{
if(gameSquare[i]<0)
{
return i;
}
}
}
if(i>=6)
{
//third column check
if((gameSquare[i-3]==squareBoxType)&&(gameSquare[i-6]==squareBoxType))
{
if(gameSquare[i]<0)
{
return i;
}
}
}
}
return -1;
}
bool getVictory(int squareBoxType)
{
//diagnol line victories
//the first diagnol line check
if((gameSquare[4]==squareBoxType)&&(gameSquare[8]==squareBoxType))
{
if(gameSquare[0]==squareBoxType)
{
return true;
}
}
if((gameSquare[0]==squareBoxType)&&(gameSquare[8]==squareBoxType))
{
if(gameSquare[4]==squareBoxType)
{
return true;
}
}
if((gameSquare[0]==squareBoxType)&&(gameSquare[4]==squareBoxType))
{
if(gameSquare[8]==squareBoxType)
{
return true;
}
}
//the second diagnol line check
if((gameSquare[4]==squareBoxType)&&(gameSquare[6]==squareBoxType))
{
if(gameSquare[2]==squareBoxType)
{
return true;
}
}
if((gameSquare[2]==squareBoxType)&&(gameSquare[6]==squareBoxType))
{
if(gameSquare[4]==squareBoxType)
{
return true;
}
}
if((gameSquare[2]==squareBoxType)&&(gameSquare[4]==squareBoxType))
{
if(gameSquare[6]==squareBoxType)
{
return true;
}
}
//row checking
for(int i=0;i<9;i++)
{
//first row check
if((i==0)||(i==3)||(i==6))
{
if((gameSquare[i+1]==squareBoxType)&&(gameSquare[i+2]==squareBoxType))
{
if(gameSquare[i]==squareBoxType)
{
return true;
}
}
}
if((i==1)||(i==4)||(i==7))
{
//second row check
if((gameSquare[i-1]==squareBoxType)&&(gameSquare[i+1]==squareBoxType))
{
if(gameSquare[i]==squareBoxType)
{
return true;
}
}
}
if((i==2)||(i==5)||(i==8))
{
//third row check
if((gameSquare[i-2]==squareBoxType)&&(gameSquare[i-1]==squareBoxType))
{
if(gameSquare[i]==squareBoxType)
{
return true;
}
}
}
if(i<3)
{
//first column check
if((gameSquare[i+3]==squareBoxType)&&(gameSquare[i+6]==squareBoxType))
{
if(gameSquare[i]==squareBoxType)
{
return true;
}
}
}
if((i>=3)&&(i<=5))
{
//second column check
if((gameSquare[i-3]==squareBoxType)&&(gameSquare[i+3]==squareBoxType))
{
if(gameSquare[i]==squareBoxType)
{
return true;
}
}
}
if(i>=6)
{
//third column check
if((gameSquare[i-3]==squareBoxType)&&(gameSquare[i-6]==squareBoxType))
{
if(gameSquare[i]==squareBoxType)
{
return true;
}
}
}
}
return false;
}
bool boardIsFull()
{
for(int iLoop=0;iLoop<9;iLoop++)
{
if(gameSquare[iLoop]==-1)
{
return false;
}
}
return true;
}
void checkForCatGame()
{
if(boardIsFull()==true)
{
if((getVictory(0)==false)&&(getVictory(1)==false))
{
//game tied
gameCompleted=true;
updateOpponentText(opponentTieGame[random(0,oTextArraySize)]);
isPlayerTurn=true;
}
}
}
bool mouseBetween(double x1, double y1, double x2,double y2)
{
if((mouse_x>=x1)&&(mouse_x<=x2))
{
if((mouse_y>=y1)&&(mouse_y<=y2))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
void playerPickSlot()
{
if(gameSquare[currentSlot]==-1)
{
gameSquare[currentSlot]=0;
isPlayerTurn=false;
if(turnNumber==0)
{
updateOpponentText(opponentFirstMove[random(0,oTextArraySize)]);
}
else
{
updateOpponentText(" ");
}
if(getVictory(0))
{
gameCompleted=true;
updateOpponentText(opponentLose[random(0,oTextArraySize)]);
playerWins+=1;
isPlayerTurn=true;
}
else
{
checkForCatGame();
turnNumber+=1;
}
}
}
void getGameCreditsInput()
{
//If a key was pressed
if( event.type == SDL_KEYUP )
{
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
case SDLK_LALT:
goToMenu();
break;
default:
// goToMenu();
break;
}
}
}
bool getGameMenuInput(bool quit)
{
//If a key was pressed
if( event.type == SDL_KEYUP )
{
switch(event.key.keysym.sym)
{
case SDLK_SPACE:
startGame();
resetCredits();
CURRENT_AREA = GAME_CREDITS;
playBackgroundMusic();
quit=false;
break;
case SDLK_RETURN:
case SDLK_x:
case SDLK_v:
case SDLK_LCTRL:
startGame();
CURRENT_AREA = GAME_BOARD;
playBackgroundMusic();
quit= false;
break;
case SDLK_ESCAPE:
quit= true;
break;
default:
quit= false;
break;
}
}
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
return quit;
}
void getPlayerInput()
{
//If a key was pressed
if( event.type == SDL_KEYUP )
{
switch(event.key.keysym.sym)
{
case SDLK_LEFT:
case SDLK_TAB:
if(currentSlot>0)
{
currentSlot-=1;
}
break;
case SDLK_RIGHT:
case SDLK_BACKSPACE:
if(currentSlot<8)
{
currentSlot+=1;
}
break;
case SDLK_UP:
case SDLK_w:
if(currentSlot>=3)
{
currentSlot-=3;
}
break;
case SDLK_DOWN:
case SDLK_s:
if(currentSlot<=5)
{
currentSlot+=3;
}
break;
case SDLK_LCTRL:
case SDLK_LSHIFT:
case SDLK_x:
playerPickSlot();
break;
case SDLK_ESCAPE:
goToMenu();
break;
default:
break;
}
}
int ipjValue = 0;
//If a mouse button was released
if( event.type == SDL_MOUSEMOTION )
{
for(int iLoop=0;iLoop<3;iLoop+=1)
{
for(int jLoop=0;jLoop<3;jLoop+=1)
{
mouse_x=event.motion.x;
mouse_y=event.motion.y;
if(mouseBetween(SCREEN_WIDTH/2 -BOARD_PXWIDTH/3+(iLoop*(BOARD_PXWIDTH/3)), SCREEN_HEIGHT/2-BOARD_PXHEIGHT/3+(jLoop*(BOARD_PXHEIGHT/3)),SCREEN_WIDTH/2 +(iLoop*(BOARD_PXWIDTH/3)),SCREEN_HEIGHT/2 +(jLoop*(BOARD_PXHEIGHT/3))))
{
ipjValue=iLoop+(jLoop*3);
currentSlot=ipjValue;
}
}
}
}
if(event.type==SDL_MOUSEBUTTONUP)
{
//If the left mouse button was pressed
if( event.button.button == SDL_BUTTON_LEFT )
{
playerPickSlot();
}
}
}
void getPlayerGameDoneInput()
{
//If the left mouse button was pressed
if(event.type == SDL_MOUSEBUTTONUP)
{
if( event.button.button == SDL_BUTTON_LEFT )
{
startGame();
}
}
//If a key was pressed
if( event.type == SDL_KEYUP )
{
switch(event.key.keysym.sym)
{
case SDLK_RETURN:
case SDLK_SPACE:
case SDLK_x:
case SDLK_y:
startGame();
break;
case SDLK_ESCAPE:
goToMenu();
break;
default:
break;
}
}
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
goToMenu();
}
fakeTimer=0;
}
void computerAi()
{
int possibleSquare;
int iiiLoop=0;
if(isPlayerTurn==false)
{
possibleSquare=getWinningSquare(1);
//if the computer is not about to win
if(possibleSquare<0)
{
//select a diffrent square
//if the player is about to win
possibleSquare=getWinningSquare(0);
if(possibleSquare>0)
{
gameSquare[possibleSquare]=1;
if(getVictory(1))
{
gameCompleted=true;
updateOpponentText(opponentBlock[random(0,oTextArraySize)]+" "+opponentWin[random(0,oTextArraySize)]);