codice:
#include <iostream>
#include <algorithm>
#include <ctime>
#include <cstring>
#include <vector>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "button.h"
using namespace std;
const int SCREEN_WIDTH = 480;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
SDL_Surface *screen = NULL;
SDL_Surface *button = NULL;
SDL_Surface *x=NULL;
SDL_Surface *o=NULL;
SDL_Event event;
const char X = 'X';
const char O = 'O';
const char EMPTY = ' ';
const char tie = 'T';
const char noOne = 'N';
char turno = X;
SDL_Surface *load_image(string filename) {
SDL_Surface *loadedImage;
SDL_Surface *optimizedImage;
loadedImage = IMG_Load(filename.c_str());
if( loadedImage != NULL ) {
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
}
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL ) {
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( source, clip, destination, &offset );
}
bool init(){
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) {
return false;
}
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
if( screen == NULL ) {
return false;
}
SDL_WM_SetCaption( "Tris", NULL );
return true;
}
void clean_up() {
SDL_FreeSurface( screen );
SDL_Quit();
}
Button::Button( int x=5, int y=5, SDL_Surface* destination = NULL, int w=150, int h=150 )
{
//Set the button's attributes
box.x = x;
box.y = y;
box.w = w;
box.h = h;
button = load_image("button.bmp");
apply_surface( x, y, button, destination);
piece = EMPTY;
}
void Button::setpiece(char pezzo) {
piece = pezzo;
}
void Button::setbutton(SDL_Surface* sostituisco) { //per le mosse del pc ho dovuto creare la funzione apposta
button = sostituisco;
}
void Button::show() {
apply_surface(box.x,box.y,button,screen);
}
inline bool isLegal(int move, vector<Button> &tris) {
return (tris[move].getpiece() == EMPTY);
}
char winner(vector<Button>& tris) {
int tieIndex=0;
const int WIN[8][3] = { {0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6} };
for(int row = 0; row < 8; row++) {
//cout << tris[WIN[row][0]].getpiece() << tris[WIN[row][1]].getpiece()<< tris[WIN[row][2]].getpiece();
if( /*tris[WIN[row][0]].getpiece() != EMPTY*/(!isLegal(WIN[row][0],tris)) && (tris[WIN[row][0]].getpiece() == tris[WIN[row][1]].getpiece()) && (tris[WIN[row][1]].getpiece() == tris[WIN[row][2]].getpiece())) {
cout << tris[WIN[row][0]].getpiece();
return tris[WIN[row][0]].getpiece();
}
}
for(int i=0; i< 9; i++) {
if(tris[i].getpiece() != EMPTY) {
tieIndex += 1;
if(tieIndex == 9) {return tie;}
}
}
return noOne;
}
int pcMove(vector<Button> triss) { //non lavoro con references o puntatori ma con una copia perché ho da manipolare
int move=0;
for(move =0; move<9; move++) {
if(isLegal(move,triss)) {
triss[move].setpiece(O);
if(winner(triss) == O) {
//cout << triss[move].getpiece() << move << "\n";
return move;}
else {triss[move].setpiece(EMPTY);}
}
}
move=0;
for(move =0; move<9; move++) {
if(isLegal(move,triss)) {
triss[move].setpiece(X);
if(winner(triss) == X) {
return move;}
triss[move].setpiece(EMPTY);
}
}
const int BEST_MOVES[] = {4,0,2,6,8,1,3,5,7};
for(int i=0; i<9; i++) {
int move = BEST_MOVES[i];
if(isLegal(move,triss)) {
return move;
}
}
}
int main(int argc, char *args[]) {
bool quit = false;
bool done = false;
turno='X';
if( init() == false ) {
return 1;
}
vector<Button> tris(9);
Button button1(5,5,screen,150,150);
Button button2(160,5,screen,150,150);
Button button3(315,5,screen,150,150);
Button button4(5,160,screen,150,150);
Button button5(160,160,screen,150,150);
Button button6(315,160,screen,150,150);
Button button7(5,315,screen,150,150);
Button button8(160,315,screen,150,150);
Button button9(315,315,screen,150,150);
tris[0] = (button1); //il problema è k devo lavoraresolo con i tris[] o solo
tris[1] = (button2); //con i button anche in pcmove
tris[2] = (button3);
tris[3] = (button4);
tris[4] = (button5);
tris[5] = (button6);
tris[6] = (button7);
tris[7] = (button8);
tris[8] = (button9);
while( (quit == false) && (winner(tris) == noOne)) {
tris[0].show();
tris[1].show();
tris[2].show();
tris[3].show();
tris[4].show();
tris[5].show();
tris[6].show();
tris[7].show();
tris[8].show();
if( SDL_Flip( screen ) == -1 ) {
return 1;}
if(turno == X) {
if( (!done) && (SDL_PollEvent(&event))) {
switch(event.type) {
case SDL_MOUSEBUTTONDOWN: {
if( event.button.button == SDL_BUTTON_LEFT ) {
if( (button1.box.x < event.button.x) && ((button1.box.x + button1.box.w) > event.button.x) && (button1.box.y < event.button.y) && ((button1.box.y + button1.box.h) > event.button.y) ) {
if(tris[0].getpiece() == EMPTY) {
tris[0].setbutton(load_image("x.bmp"));
tris[0].setpiece(X);
done = true;
turno =O;
break;}
}
if( (button2.box.x < event.button.x) && ((button2.box.x + button2.box.w) > event.button.x) && (button2.box.y < event.button.y) && ((button2.box.y + button2.box.h) > event.button.y) ) {
if(tris[1].getpiece() == EMPTY) {
tris[1].setbutton(load_image("x.bmp"));
tris[1].setpiece(X);
turno =O;
done = true;
break;}
}
if( (button3.box.x < event.button.x) && ((button3.box.x + button3.box.w) > event.button.x) && (button3.box.y < event.button.y) && ((button3.box.y + button3.box.h) > event.button.y) ) {
if(tris[2].getpiece() == EMPTY) {
tris[2].setbutton(load_image("x.bmp"));
tris[2].setpiece(X);
turno =O;
done = true;
break;}
}
if( (button4.box.x < event.button.x) && ((button4.box.x + button4.box.w) > event.button.x) && (button4.box.y < event.button.y) && ((button4.box.y + button4.box.h) > event.button.y) ) {
if(tris[3].getpiece() == EMPTY) {
tris[3].setbutton(load_image("x.bmp"));
tris[3].setpiece(X);
turno =O;
done = true;
break;}
}
if( (button5.box.x < event.button.x) && ((button5.box.x + button5.box.w) > event.button.x) && (button5.box.y < event.button.y) && ((button5.box.y + button5.box.h) > event.button.y) ) {
if(tris[4].getpiece() == EMPTY) {
tris[4].setbutton(load_image("x.bmp"));
tris[4].setpiece(X);
turno =O;
done = true;
break;}
}
if( (button6.box.x < event.button.x) && ((button6.box.x + button6.box.w) > event.button.x) && (button6.box.y < event.button.y) && ((button6.box.y + button6.box.h) > event.button.y) ) {
if(tris[5].getpiece() == EMPTY) {
tris[5].setbutton(load_image("x.bmp"));
tris[5].setpiece(X);
turno =O;
done = true;
break;}
}
if( (button7.box.x < event.button.x) && ((button7.box.x + button7.box.w) > event.button.x) && (button7.box.y < event.button.y) && ((button7.box.y + button7.box.h) > event.button.y) ) {
if(tris[6].getpiece() == EMPTY) {
tris[6].setbutton(load_image("x.bmp"));
tris[6].setpiece(X);
turno =O;
done = true;
break;}
}
if( (button8.box.x < event.button.x) && ((button8.box.x + button8.box.w) > event.button.x) && (button8.box.y < event.button.y) && ((button8.box.y + button8.box.h) > event.button.y) ) {
if(tris[7].getpiece() == EMPTY) {
tris[7].setbutton(load_image("x.bmp"));
tris[7].setpiece(X);
turno =O;
done = true;
break;}
}
if( (button9.box.x < event.button.x) && ((button9.box.x + button9.box.w) > event.button.x) && (button9.box.y < event.button.y) && ((button9.box.y + button9.box.h) > event.button.y) ) {
if(tris[8].getpiece() == EMPTY) {
tris[8].setbutton(load_image("x.bmp"));
tris[8].setpiece(X);
turno =O;
done = true;
break;}
} //fine del "se mi trovo sopra"
}
continue;
}
case SDL_QUIT: {
quit = true;
break;}
}//fine switch
}
if(turno == O) {
int i = pcMove(tris);
tris[i].setbutton(load_image("o.bmp"));
tris[i].setpiece(O);
turno = X;
done = false;}
}
}
clean_up();
return 0;
}