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
|
/*Tic tac toe
*************
Summer 2011
/************************************************************************
********************************Headers**********************************
************************************************************************/
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "Screen.h"
#include "Board.h"
#include <string>
/************************************************************************
********************************Globals**********************************
************************************************************************/
Board gameBoard;
Screen mainScreen;
//mainScreen.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Computer AI
class Comp
{
public:
int HandleEvents();
Comp();
private:
};
Comp::Comp()
{
}
int Comp::HandleEvents()
{
// block top row
// attack
// random
if( gameBoard.Bbox[ 0 ][ 0 ] == 0 )
{
mainScreen.ApplySurface( 0, 0, gameBoard.O, mainScreen.screen );
//Update the screen
SDL_Flip( mainScreen.screen );
gameBoard.Bbox[0][0] = 2;
return 0;
}
else if( gameBoard.Bbox[ 0 ][ 1 ] == 0)
{
mainScreen.ApplySurface( 213, 0, gameBoard.O, mainScreen.screen );
//Update the screen
SDL_Flip( mainScreen.screen );
gameBoard.Bbox[0][1] = 2;
return 0;
}
if( gameBoard.Bbox[ 0 ][ 2 ] == 0 )
{
mainScreen.ApplySurface( 426, 0, gameBoard.O, mainScreen.screen );
//Update the screen
SDL_Flip( mainScreen.screen );
gameBoard.Bbox[0][2] = 2;
return 0;
}
if( gameBoard.Bbox[ 1 ][ 0 ] == 0)
{
mainScreen.ApplySurface( 0, 160, gameBoard.O, mainScreen.screen );
//Update the screen
SDL_Flip( mainScreen.screen );
gameBoard.Bbox[1][0] = 2;
return 0;
}
if( gameBoard.Bbox[ 1 ][ 1 ] == 0 )
{
mainScreen.ApplySurface( 213, 160, gameBoard.O,mainScreen.screen );
//Update the screen
SDL_Flip( mainScreen.screen );
gameBoard.Bbox[1][1] = 2;
return 0;
}
if( gameBoard.Bbox[ 1 ][ 2 ] == 0)
{
mainScreen.ApplySurface( 426, 160,gameBoard.O, mainScreen.screen );
//Update the screen
SDL_Flip(mainScreen.screen );
gameBoard.Bbox[1][2] = 2;
return 0;
}
if( gameBoard.Bbox[ 2 ][ 0 ] == 0 )
{
mainScreen.ApplySurface( 0, 320, gameBoard.O, mainScreen.screen );
//Update the screen
SDL_Flip( mainScreen.screen );
gameBoard.Bbox[2][0] = 2;
return 0;
}
if( gameBoard.Bbox[ 2 ][ 1 ] == 0 )
{
mainScreen.ApplySurface( 213, 320, gameBoard.O, mainScreen.screen );
//Update the screen
SDL_Flip( mainScreen.screen );
gameBoard.Bbox[2][1] = 2;
return 0;
}
if( gameBoard.Bbox[ 2 ][ 2 ] == 0 )
{
mainScreen.ApplySurface( 426, 320, gameBoard.O,mainScreen.screen );
//Update the screen
SDL_Flip( mainScreen.screen );
gameBoard.Bbox[2][2] = 2;
return 0;
}
return 1;
}
/************************************************************************
********************************Functions********************************
************************************************************************/
void CleanUp()
{
//Free the surfaces
SDL_FreeSurface( mainScreen.background );
SDL_FreeSurface( gameBoard.X );
SDL_FreeSurface( gameBoard.O );
SDL_Quit();
}
bool LoadFiles()
{
//Load the images
mainScreen.background = mainScreen.LoadImage( "background.bmp" );
gameBoard.X = mainScreen.LoadImage( "x.bmp" );
gameBoard.O = mainScreen.LoadImage( "o.bmp" );
//Open the font
mainScreen.font = TTF_OpenFont( "lazy.ttf", 28 );
// IF there was an error in loading the font
if( mainScreen.font == NULL )
{
std::cout << "Font loading failed!";
return false;
}
return true;
}
/************************************************************************
********************************Main************************************
************************************************************************/
int main( int argc, char* args[] )
{
//quit flag
bool gameDone = false;
if( mainScreen.Init() == 1 )
{
std::cout << "Initialization failed";
return 1;
}
if( LoadFiles() == 1 )
{
std::cout << "File loading failed";
return 1;
}
// Set all the spaces to unoccupied
for( int i = 0; i < 4; i++ )
{
for(int j = 0; j < 4; j++ )
{
gameBoard.Bbox[i][j] = 0;
}
}
//Render the text
mainScreen.message = TTF_RenderText_Solid( mainScreen.font, "You win!", mainScreen.textColor );
// Draw the board
mainScreen.ApplySurface( 0, 0, mainScreen.background, mainScreen.screen );
//Update the screen
if( SDL_Flip( mainScreen.screen ) == -1 )
{
std::cout << "SDL_Flip failed ";
return 1;
}
Comp myComp;
while( !gameDone )
{
while ( SDL_PollEvent( &gameBoard.event ) )
{
if(gameBoard.event.type == SDL_MOUSEBUTTONDOWN )
{
//Handle user events
gameBoard.HandleEvents();
if( gameBoard.CheckForWinner() == true )
{
mainScreen.ApplySurface( 216, 160, mainScreen.message, mainScreen.screen );
SDL_Delay(2000);
gameDone = true;
}
//Handle the comps evets
myComp.HandleEvents();
}
if( gameBoard.event.type == SDL_QUIT)
{
gameDone = true;
}
}
}
//Update the screen
if( SDL_Flip(mainScreen.screen ) == -1 )
return 1;
CleanUp();
std::cin.get();
std::cin.get();
return 0;
}
|