Advertisement
vinci98gl2

TrisSDLv1.0

Mar 3rd, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.81 KB | None | 0 0
  1. https://www.dropbox.com/sh/zjwy9ja39actcyo/AABECnjO7kIQzbBNcoPM-zIta?dl=0
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <SDL/SDL.h>
  6. #include <windows.h>
  7. #include <stdbool.h>
  8. #define TRE 3
  9.  
  10. //The attributes of the screen
  11. const int SCREEN_WIDTH = 640;
  12. const int SCREEN_HEIGHT = 480;
  13. const int SCREEN_BPP = 16;
  14. //le immagini
  15. SDL_Surface* screen = NULL;
  16. SDL_Surface* sfondo = NULL;
  17. SDL_Surface* croce = NULL;
  18. SDL_Surface* palla = NULL;
  19. SDL_Surface* win = NULL;
  20. //gli eventi
  21. SDL_Event event;
  22. //variabili globali del TRIS
  23. int griglia[TRE][TRE]={{0},{0}}; //matrice
  24. int player=1; //giocatore attuale
  25.  
  26. void init(void)
  27. {   //Start SDL
  28.     SDL_Init( SDL_INIT_EVERYTHING );
  29.  
  30.     //Set up screen
  31.     screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
  32. }
  33.  
  34. void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination );
  35. void deinit(void);
  36. void mostra(void);
  37. void impostacoordinate(int x,int y);
  38. //prototipi del TRIS
  39. void insert(void);
  40. void cambiaplayer(void);
  41. bool controllowin(void);
  42.  
  43. int main( int argc, char* args[] )
  44. {
  45.    int turno=0; //indica il turno corrente
  46.    
  47.    init();
  48.    
  49.    //carico le immagini
  50.    sfondo = SDL_LoadBMP( "grigliatrisvuota.bmp" );
  51.    croce = SDL_LoadBMP( "croce.bmp" );
  52.    palla = SDL_LoadBMP( "palla.bmp" );
  53.    win = SDL_LoadBMP("win.bmp");
  54.    //inserisco la griglia sullo schermo
  55.    SDL_BlitSurface( sfondo, NULL, screen, NULL );
  56.    SDL_Flip( screen ); //mostro lo schermo
  57.    SDL_Delay(1000); //un secondo di attesa
  58.    
  59.    for(;(event.type!=SDL_QUIT)&&(turno<9);SDL_Delay(250))
  60.    {  
  61.       SDL_PollEvent( &event ); //cattura l'evento
  62.      
  63.       if( event.type == SDL_KEYDOWN )
  64.       {
  65.          insert();
  66.          mostra();
  67.          cambiaplayer();
  68.          turno++;
  69.          controllowin();
  70.          if(controllowin())turno=9;
  71.       }
  72.       SDL_Flip( screen ); //mostro lo schermo
  73.    }
  74.    SDL_Delay(3000);
  75.    deinit();
  76.    //Quit SDL
  77.    SDL_Quit();
  78.    return 0;
  79. }
  80. void deinit(void)
  81. {    
  82.    SDL_FreeSurface( sfondo );
  83.    SDL_FreeSurface( croce );
  84.    SDL_FreeSurface( palla );
  85. }
  86.  
  87. void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
  88. {
  89.     //Make a temporary rectangle to hold the offsets
  90.     SDL_Rect offset;
  91.    
  92.     //Give the offsets to the rectangle
  93.     offset.x = x;
  94.     offset.y = y;
  95.    
  96.     //Blit the surface
  97.     SDL_BlitSurface( source, NULL, destination, &offset );
  98. }
  99.  
  100. void insert(void)
  101. {
  102.    switch(event.key.keysym.sym)
  103.    {
  104.       case SDLK_KP9: if(griglia[2][2]==0) griglia[2][2]=player;
  105.               break;
  106.       case SDLK_KP8: if(griglia[2][1]==0) griglia[2][1]=player;
  107.               break;
  108.       case SDLK_KP7: if(griglia[2][0]==0) griglia[2][0]=player;
  109.               break;
  110.       case SDLK_KP6: if(griglia[1][2]==0) griglia[1][2]=player;
  111.               break;
  112.       case SDLK_KP5: if(griglia[1][1]==0) griglia[1][1]=player;
  113.               break;
  114.       case SDLK_KP4: if(griglia[1][0]==0) griglia[1][0]=player;
  115.               break;
  116.       case SDLK_KP3: if(griglia[0][2]==0) griglia[0][2]=player;
  117.               break;
  118.       case SDLK_KP2: if(griglia[0][1]==0) griglia[0][1]=player;
  119.               break;
  120.       case SDLK_KP1: if(griglia[0][0]==0) griglia[0][0]=player;
  121.               break;
  122.       default: ;
  123.    }
  124. }
  125. void cambiaplayer(void)
  126. {
  127.    if(player==1) player=2;
  128.    else player=1;
  129. }
  130.  
  131. void mostra(void)
  132. {  int y,x;
  133.    for(y=2;y>=0;y--)
  134.    {
  135.       for(x=0;x<TRE;x++)
  136.       {
  137.          if(griglia[y][x]==player)
  138.             impostacoordinate(x,y);
  139.       }
  140.    }
  141. }
  142.  
  143. void impostacoordinate(int x,int y)
  144. {  
  145.    int riga,colonna;
  146.    switch(x)
  147.    {
  148.       case 0:colonna=0;break;
  149.       case 1:colonna=SCREEN_WIDTH-150*2.75;break;
  150.       case 2:colonna=SCREEN_WIDTH-150;break;
  151.    }
  152.    switch(y)
  153.    {
  154.       case 0:riga=SCREEN_HEIGHT-115;break;
  155.       case 1:riga=SCREEN_HEIGHT-115*2.5;break;
  156.       case 2:riga=0;break;
  157.    }
  158.    if(player==1)apply_surface(colonna,riga,croce,screen);
  159.    if(player==2)apply_surface(colonna,riga,palla,screen);
  160. }
  161.  
  162. bool controllowin(void)
  163. {  int z;
  164.    for(z=0;z<TRE;z++)
  165.    {
  166.       if((griglia[z][0]==griglia[z][1])&&(griglia[z][1]==griglia[z][2])&&(griglia[z][0]!=0))
  167.       {  apply_surface(0,SCREEN_HEIGHT/2-70,win,screen);
  168.          return true;
  169.       }
  170.       if((griglia[0][z]==griglia[1][z])&&(griglia[1][z]==griglia[2][z])&&(griglia[0][z]!=0))
  171.       {  apply_surface(0,SCREEN_HEIGHT/2-70,win,screen);
  172.          return true;
  173.       }
  174.    }
  175.    if((griglia[0][2]==griglia[1][1])&&(griglia[1][1]==griglia[2][0])&&(griglia[1][1]!=0))
  176.    {  apply_surface(0,SCREEN_HEIGHT/2-70,win,screen);
  177.       return true;
  178.    }
  179.    if((griglia[0][0]==griglia[1][1])&&(griglia[1][1]==griglia[2][2])&&(griglia[1][1]!=0))
  180.    {  apply_surface(0,SCREEN_HEIGHT/2-70,win,screen);
  181.       return true;
  182.    }
  183.    return false;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement