[ create a new paste ] login | about

Link: http://codepad.org/WkO2O5tn    [ raw code | fork ]

C++, pasted on Dec 16:
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <time.h>
using namespace std;

int jouer(int argc, char**argv);
void clearScreen();
void attendreSaisie();

int main(int argc, char**argv){

    int choix = 0;

    while(1){

        clearScreen();

        cout << "\n === Motus ===" << endl;
        cout << "\n 1. Jouer" << endl;
        cout << " 2. Quitter" << endl;

        cout << "\n\n Choix: ";
        cin >> choix;

        switch(choix){
            case 1:
                jouer(argc, argv);
                break;
            case 2:
                return 0;
                break;
            default:
                return 0;
                break;
        }
    }

}

//intégration de la sdl
#include "SDL.h"
#include "SDL_ttf.h"

void drawRect(int x, int y, int w, int h, Uint32 color, SDL_Surface* ecran);
void drawBorders( SDL_Surface *ecran);

int jouer(int argc, char**argv){

    //initialisation de la sdl
    SDL_Surface *ecran = NULL, *lettre = NULL;
    TTF_Font *font = NULL;

    SDL_Init(SDL_INIT_VIDEO);
    TTF_Init();

    ecran = SDL_SetVideoMode(234, 261, 32, SDL_DOUBLEBUF);
    SDL_WM_SetCaption("Motus", NULL);

    //colori la surface en bleu
    SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 55, 122, 190));

    //dessine les bords
    drawBorders(ecran);

    //mise à jour de l'affichage
    SDL_Flip(ecran);

    //ouverture du fichier
    fstream fichier("mots.txt");
    string ligne = "", sMots = "";
    int nbrMots = 0;
    while(getline(fichier, ligne)){
        nbrMots++;
        sMots += ligne + " ";
    }

    //création du tableau qui va contenir les mots
    string mots[nbrMots];

    //insertion des mots dans le tableau
    int counterPos = 0;
    for( int i=0; i<nbrMots; i++ ){
        mots[i] = "";
        do{
            mots[i] += sMots[counterPos];
            counterPos++;
        }while(sMots[counterPos] != ' ');
        counterPos++;
    }

    //choisir un mot aléatoire
    srand(time(NULL));
    string motChoisi = mots[rand() % nbrMots-1];

    clearScreen();

    //demander la saisi du mot
    cout << "\n 1er essai:" << endl;
    cout << " Veuillez saisir un mot de huit lettres commencant par '";
    cout << motChoisi[0] << motChoisi[1] << motChoisi[2];
    cout << "': ";

    string motSaisi = "";
    //création du bg
    SDL_Surface *bg = SDL_CreateRGBSurface(SDL_HWSURFACE, 27, 35, 32, 0, 0, 0, 0);

    //position du bg et de la lettre
    SDL_Rect pos, posLetter;
    pos.x = 2, pos.y = 2;
    posLetter.x = 0, posLetter.y = 0;

    //initialisation de la police
    font = TTF_OpenFont("tahoma.ttf", 20);
    SDL_Color noir = {0, 0, 0};

    //pour chaque mot saisi (7 fois max)
    for( int i=0; i<7; i++ ){

        //si le joueur n'en est pas à son premier essai on va lui demande de REsaisir
        if( i>0 ) {
            cout << i+1 << "eme essai: ";
            cout << "\n Veuillez resaisir un mot de huit lettres: ";
        }

        //on demande de saisir un mot
        cin >> motSaisi;

        //verification si le mot fait bien huit lettres
        while( motSaisi.length() != 8 ){
            cout << "\n Vous n'avez pas saisi un mot de huit lettres, veuillez reessayer: ";
            cin >> motSaisi;
        }

        cout << "\n ";

        //comparaison et affichage graphique
        for( int j=0; j<8; j++ ){
            //condition pour colorier le bg
            if( motSaisi[j] == motChoisi[j] ){
                SDL_FillRect(bg, NULL, SDL_MapRGB(ecran->format, 94, 187, 70));
                SDL_BlitSurface(bg, NULL, ecran, &pos);
            } else if( motSaisi[j] == motChoisi[j-1] || motSaisi[j] == motChoisi[j+1] ) {
                SDL_FillRect(bg, NULL, SDL_MapRGB(ecran->format, 243, 131, 35));
                SDL_BlitSurface(bg, NULL, ecran, &pos);
            }

            //affichage des lettres
            stringstream sstr;
            sstr << motSaisi[j];
            string outputString = sstr.str();

            //to upper case
            transform(outputString.begin(), outputString.end(),outputString.begin(), ::toupper);
            lettre = TTF_RenderText_Blended(font, outputString.c_str(), noir);
            //on donne une bonne position aux lettres
            posLetter.x = pos.x + 13 - lettre->w/2;
            posLetter.y = pos.y + 4;
            //on parse les lettres sur l'écran
            SDL_BlitSurface(lettre, NULL, ecran, &posLetter);

            pos.x += 29;
        }

        //mise à jour de l'affichage
        SDL_Flip(ecran);

        //mise à jour de la position du bg
        pos.x = 2;
        pos.y += 37;

        //si le mot est trouvé
        if( motSaisi+(char)13 == motChoisi || motSaisi == motChoisi ){ //(char)13 retour chariot sous linux
            cout << "\n\n Correct! :D";
            cout << "\n\n\n ";
            attendreSaisie();
            SDL_FreeSurface(ecran);
            SDL_Quit();
            return 0;
        }

        //si le mot n'est pas trouvé au bout de 7 tentatives
        if( i==6 ){
            cout << "\n\n Vous avez echoue. :(" << endl;
            cout << " Le mot en question etait: " << motChoisi;
            cout << "\n\n\n ";
            attendreSaisie();
            SDL_FreeSurface(ecran);
            SDL_Quit();
            return 0;
        }

    }
    return 0;
}

void drawRect(int x, int y, int w, int h, Uint32 color, SDL_Surface* ecran) {
    SDL_Rect rect = {x,y,w,h};
    SDL_FillRect(ecran, &rect, color);
}

void drawBorders( SDL_Surface *ecran){
    int y = 0;
    for( int i=0; i<8; i++ ){
        drawRect(0, y, 234, 2, SDL_MapRGB(ecran->format, 255, 255, 255), ecran);
        y += 37;
    }
    int x = 0;
    for( int j=0; j<9; j++ ){
        drawRect(x, 0, 2, 261, SDL_MapRGB(ecran->format, 255, 255, 255), ecran);
        x += 29;
    }
}

void attendreSaisie(){
    #ifdef WIN32
        system("pause");
    #else
        //Code Linux ici
    #endif
}

void clearScreen(){
    #ifdef WIN32
        system("cls");
    #else
        system("clear");
    #endif
}


Create a new paste based on this one


Comments: