سلام!

کد بازی XO را می توانید در ادامه مشاهده کنید. کد بقیه ی پروژه ها به مرور زمان در وبلاگ قرار خواهند گرفت.

File: board.h


#ifndef BOARD_H
#define BOARD_H

#include "SDL/SDL.h"
#include "SDL/SDL_gfxPrimitives.h"
#include "SDL/SDL_ttf.h"

#include <string>

using namespace std;

class Board {
    private:

//        Width and height of a block of table
        const int _blockWidth, _blockHeight;
//        Represents turn; 1 for O and -1 for X
        int _flag;
//        Represents the winner
        int _winner;

//        Table contents
        int **table;

        const static int x = -1;
        const static int o = 1;

        void drawTable( SDL_Surface* );
        void draw( SDL_Surface* );
        int startGame( SDL_Surface* );
        void drawX( SDL_Surface*, int x, int y );
        void drawO( SDL_Surface*, int x, int y );
        void putPiece( SDL_Surface*, int, int );

        int blockWidth();
        int blockHeight();
        int flag();
        string winner();

        void setBlockWidth( int = 0 );
        void setBlockHeight( int = 0 );
        void setFlag(int = 0 );
        void setWinner( int );

    public:

        Board( SDL_Surface* );
        ~Board();
        int run( SDL_Surface* );
        bool checkWinning();
        void showWinner( SDL_Surface* );


};

#endif

File: board.cpp


#include "board.h"

Board::Board ( SDL_Surface* screen ) : _blockWidth( screen->w / 3 ) , _blockHeight( screen->h / 3 ) {

//    Initializing flag
    this->setFlag( o );

//    Making table
    table = new int* [ 3 ];
    for ( int i = 0; i < 3; i++ ){
        table[i] = new int[3];
        for ( int j = 0; j < 3; j++ )
            table[i][j] = 0;
    }
}

Board::~Board() {
    SDL_Quit();
    for ( int i = 0; i < 3; i++ )
        delete [] table[i];
    delete[] table;
}

int Board::run ( SDL_Surface *screen ) {
    this->drawTable( screen );
    return this->startGame( screen );
}

int Board::startGame( SDL_Surface* screen ){
    SDL_Event event;
    while ( 1 ) {
        while ( SDL_PollEvent( &event ) ) {

            if ( event.type == SDL_QUIT )
                return 0;

            if ( event.type == SDL_MOUSEBUTTONDOWN ) {
                this->putPiece( screen, event.button.x / this->blockWidth(), event.button.y / this->blockHeight() );
                this->setFlag( this->flag() * -1 );
                this->draw( screen );
                SDL_Flip( screen );
                if ( this->checkWinning() )
                    return 1;

            }
        }
    }
}

bool Board::checkWinning() {

//    Checking vertical and horizontal winning
    for ( int i = 0; i < 3; i++ ){
        if ( table[i][0] == table[i][1] && table[i][1] == table[i][2] && table[i][0] != 0){
            this->setWinner( table[i][0] );
            return true;
        }
        if ( table[0][i] == table[1][i] && table[1][i] == table[2][i] && table[0][i] != 0){
            this->setWinner( table[0][i] );
            return true;
        }
    }

//    Checking diagonal winning
    if ( table[0][0] == table[1][1] && table[1][1] == table[2][2] && table[0][0] != 0 ) {
        this->setWinner( table[0][0] );
        return true;
    }
    if ( table[0][2] == table[1][1] && table[1][1] == table[2][0] && table[0][2] != 0 ) {
        this->setWinner( table[0][2] );
        return true;
    }
    return false;

}

void Board::showWinner( SDL_Surface* screen ) {

//    Initializing SDL_ttf library, font and color
    TTF_Init();
    TTF_Font *font = TTF_OpenFont( "fonts/Arial.ttf", 54 );
    SDL_Color color = { 255, 255, 255 };

//    The text that is going to be shown
    string text;
    text.append( this->winner() ).append( " Win!" );

//    Out message surface
    SDL_Surface *message = TTF_RenderText_Solid( font, text.c_str(), color );

//    Position of the message
    SDL_Rect place;
    place.x = ( screen->w - message->w ) / 2;
    place.y = ( screen->h - message->h ) / 2;

//    Applying message to window
    SDL_BlitSurface( message, NULL, screen, &place );
    SDL_Flip( screen );

//    TTF cleanup
    TTF_CloseFont( font );
    TTF_Quit();
}

void Board::draw( SDL_Surface* screen){
    for ( int i = 0; i < 3; i++ )
        for ( int j = 0; j < 3; j++ ){
            if ( table[j][i] == Board::x )
                this->drawX( screen, i, j );
            if ( table[j][i] == Board::o )
                this->drawO( screen, i, j );
        }
}

void Board::drawX( SDL_Surface* screen, int x, int y ){
/*
This line:

\
 \
  \
   \

*/
    lineRGBA( screen, x * this->blockWidth() + this->blockWidth() / 10, y * this->blockHeight() + this->blockHeight() / 10, ( x + 1 ) * this->blockWidth() - this->blockWidth() / 10, ( y + 1 ) * this->blockHeight() - this->blockHeight() / 10, 255, 255, 255, 255 );
/*
This line:

   /
  /
 /
/

*/
    lineRGBA( screen, ( x + 1 ) * this->blockWidth() - this->blockWidth() / 10, y * this->blockHeight() + this->blockHeight() / 10, x * this->blockWidth() + this->blockWidth() / 10, ( y + 1 ) * this->blockHeight() - this->blockHeight() / 10, 255, 255, 255, 255 );
}

void Board::drawO( SDL_Surface* screen, int x, int y ){
    ellipseRGBA( screen, x * this->blockWidth() + this->blockWidth() / 2, y * this->blockHeight() + this->blockHeight() / 2, this->blockWidth() / 2 - 10, this->blockHeight() / 2 - 10, 255, 255, 255, 255 );
}

void Board::putPiece( SDL_Surface* screen, int x, int y ) {
    if ( table[y][x] != 0 )
        return;
    table[y][x] = this->flag();
}

void Board::drawTable( SDL_Surface *screen ) {
    for ( int i = 1; i < 3; i++ ) {
//        Vertical lines
        lineRGBA( screen, i * this->blockWidth(), 0, i * this->blockWidth(), screen->h, 255, 255, 255, 255 );
//        Horizontal lines
        lineRGBA( screen, 0, i * this->blockHeight(), screen->w, i * this->blockHeight(), 255, 255, 255, 255 );
    }
    SDL_Flip( screen );
}

int Board::blockWidth () {
    return this->_blockWidth;
}

int Board::blockHeight () {
    return this->_blockHeight;
}

int Board::flag () {
    return this->_flag;
}

string Board::winner () {
    if ( this->_winner == x )
        return "X";
    if ( this->_winner == o )
        return "O";
    else return "";
}

void Board::setFlag( int flag ) {
    this->_flag = flag;
}

void Board::setWinner( int winner ) {
    this->_winner = winner;
}

File: main.cpp


#include <iostream>

#include "SDL/SDL.h"

#include "board.h"

using namespace std;

void closeProgram( Board *board ) {
    delete board;
    exit( 0 );
}

int main () {
    
    SDL_Init( SDL_INIT_EVERYTHING );

    SDL_Surface *screen = SDL_SetVideoMode( 300, 300, 32, SDL_HWSURFACE );
    Board *board = new Board( screen );

    if ( !board->run( screen ) ){
        closeProgram( board ) ;
    }

    board->showWinner( screen );
    

    SDL_Event event;

    while ( 1 ) {
        while ( SDL_PollEvent( &event ) )
            if ( event.type == SDL_QUIT )
                closeProgram( board ) ;
    }

    return 0;

}

File: makefile

run: all
    ./XO
all: main.o board.o
    g++ main.o board.o -o XO -Wall -g -lSDL -lSDL_gfx -lSDL_ttf
main.o: board.o main.cpp
    g++ -c main.cpp
board.o: board.h board.cpp
    g++ -c board.cpp
clean:
    rm -rf *.o XO

فولدر مربوط به این پروژه را می توانید از این جا دریافت نمایید.

موفق باشید!