Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://www.dropbox.com/sh/zjwy9ja39actcyo/AABECnjO7kIQzbBNcoPM-zIta?dl=0
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <SDL/SDL.h>
- #include <windows.h>
- #include <stdbool.h>
- #define TRE 3
- //The attributes of the screen
- const int SCREEN_WIDTH = 640;
- const int SCREEN_HEIGHT = 480;
- const int SCREEN_BPP = 16;
- //le immagini
- SDL_Surface* screen = NULL;
- SDL_Surface* sfondo = NULL;
- SDL_Surface* croce = NULL;
- SDL_Surface* palla = NULL;
- SDL_Surface* win = NULL;
- //gli eventi
- SDL_Event event;
- //variabili globali del TRIS
- int griglia[TRE][TRE]={{0},{0}}; //matrice
- int player=1; //giocatore attuale
- void init(void)
- { //Start SDL
- SDL_Init( SDL_INIT_EVERYTHING );
- //Set up screen
- screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
- }
- void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination );
- void deinit(void);
- void mostra(void);
- void impostacoordinate(int x,int y);
- //prototipi del TRIS
- void insert(void);
- void cambiaplayer(void);
- bool controllowin(void);
- int main( int argc, char* args[] )
- {
- int turno=0; //indica il turno corrente
- init();
- //carico le immagini
- sfondo = SDL_LoadBMP( "grigliatrisvuota.bmp" );
- croce = SDL_LoadBMP( "croce.bmp" );
- palla = SDL_LoadBMP( "palla.bmp" );
- win = SDL_LoadBMP("win.bmp");
- //inserisco la griglia sullo schermo
- SDL_BlitSurface( sfondo, NULL, screen, NULL );
- SDL_Flip( screen ); //mostro lo schermo
- SDL_Delay(1000); //un secondo di attesa
- for(;(event.type!=SDL_QUIT)&&(turno<9);SDL_Delay(250))
- {
- SDL_PollEvent( &event ); //cattura l'evento
- if( event.type == SDL_KEYDOWN )
- {
- insert();
- mostra();
- cambiaplayer();
- turno++;
- controllowin();
- if(controllowin())turno=9;
- }
- SDL_Flip( screen ); //mostro lo schermo
- }
- SDL_Delay(3000);
- deinit();
- //Quit SDL
- SDL_Quit();
- return 0;
- }
- void deinit(void)
- {
- SDL_FreeSurface( sfondo );
- SDL_FreeSurface( croce );
- SDL_FreeSurface( palla );
- }
- void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
- {
- //Make a temporary rectangle to hold the offsets
- SDL_Rect offset;
- //Give the offsets to the rectangle
- offset.x = x;
- offset.y = y;
- //Blit the surface
- SDL_BlitSurface( source, NULL, destination, &offset );
- }
- void insert(void)
- {
- switch(event.key.keysym.sym)
- {
- case SDLK_KP9: if(griglia[2][2]==0) griglia[2][2]=player;
- break;
- case SDLK_KP8: if(griglia[2][1]==0) griglia[2][1]=player;
- break;
- case SDLK_KP7: if(griglia[2][0]==0) griglia[2][0]=player;
- break;
- case SDLK_KP6: if(griglia[1][2]==0) griglia[1][2]=player;
- break;
- case SDLK_KP5: if(griglia[1][1]==0) griglia[1][1]=player;
- break;
- case SDLK_KP4: if(griglia[1][0]==0) griglia[1][0]=player;
- break;
- case SDLK_KP3: if(griglia[0][2]==0) griglia[0][2]=player;
- break;
- case SDLK_KP2: if(griglia[0][1]==0) griglia[0][1]=player;
- break;
- case SDLK_KP1: if(griglia[0][0]==0) griglia[0][0]=player;
- break;
- default: ;
- }
- }
- void cambiaplayer(void)
- {
- if(player==1) player=2;
- else player=1;
- }
- void mostra(void)
- { int y,x;
- for(y=2;y>=0;y--)
- {
- for(x=0;x<TRE;x++)
- {
- if(griglia[y][x]==player)
- impostacoordinate(x,y);
- }
- }
- }
- void impostacoordinate(int x,int y)
- {
- int riga,colonna;
- switch(x)
- {
- case 0:colonna=0;break;
- case 1:colonna=SCREEN_WIDTH-150*2.75;break;
- case 2:colonna=SCREEN_WIDTH-150;break;
- }
- switch(y)
- {
- case 0:riga=SCREEN_HEIGHT-115;break;
- case 1:riga=SCREEN_HEIGHT-115*2.5;break;
- case 2:riga=0;break;
- }
- if(player==1)apply_surface(colonna,riga,croce,screen);
- if(player==2)apply_surface(colonna,riga,palla,screen);
- }
- bool controllowin(void)
- { int z;
- for(z=0;z<TRE;z++)
- {
- if((griglia[z][0]==griglia[z][1])&&(griglia[z][1]==griglia[z][2])&&(griglia[z][0]!=0))
- { apply_surface(0,SCREEN_HEIGHT/2-70,win,screen);
- return true;
- }
- if((griglia[0][z]==griglia[1][z])&&(griglia[1][z]==griglia[2][z])&&(griglia[0][z]!=0))
- { apply_surface(0,SCREEN_HEIGHT/2-70,win,screen);
- return true;
- }
- }
- if((griglia[0][2]==griglia[1][1])&&(griglia[1][1]==griglia[2][0])&&(griglia[1][1]!=0))
- { apply_surface(0,SCREEN_HEIGHT/2-70,win,screen);
- return true;
- }
- if((griglia[0][0]==griglia[1][1])&&(griglia[1][1]==griglia[2][2])&&(griglia[1][1]!=0))
- { apply_surface(0,SCREEN_HEIGHT/2-70,win,screen);
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement