Simples joguinho em C++ para exemplificar o uso de ncurses

Iniciado por Diego_Rocha, 12 de Agosto de 2007, 15:50

tópico anterior - próximo tópico

Diego_Rocha

Fiz esse simples joguinho para mostrar como usar o ncurses,


#include <iostream>
#include <string>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <ncurses.h>

class Roleta
{
private:
std::string roleta1, roleta2, roleta3, roleta4; //slots

std::string format;

bool win, exec;

int credits, games, rraposta, aposta, pay;

WINDOW *screen;
int y, x;

public:
Roleta()
{
exec = true;

credits = 51;
games = 0;
rraposta = aposta = 1;

win = false;

srand( time(NULL) );

init_curses();
_init_colors();
noecho();
curs_set(0);
getmaxyx( screen, y, x );

if ( x < 120 && y < 35 )
{
wattron( screen, COLOR_PAIR(1));
format = "Sorry try increase the resolution to play this game :)";
mvwaddstr( screen,(y/2),(x/2)-24, format.c_str() );
wattroff( screen, COLOR_PAIR(1));
refresh();
close();
}
}

~Roleta()
{

resetty();
endwin();
}

bool init_curses( )
{
if ( ( screen = initscr() ) == NULL )
{
std::cout << "Erro ao iniciar window!" << std::endl;
return false;
}
savetty();
refresh();
return true;
}

void _init_colors()
{
if (has_colors())
{
start_color();
if ( use_default_colors() != ERR )
{
init_pair( 1, COLOR_RED, -1 );
init_pair( 2, COLOR_CYAN, -1 );
init_pair( 3, COLOR_BLUE, -1 );
}
}
}

std::string sorted()
{
a:  //goto
int myrandnumber = rand() % 20 + 1;
if ( myrandnumber < 5 )
return "apple";
else if( myrandnumber > 5 && myrandnumber < 9 )
return "**BONUS**";
else if ( myrandnumber > 9 && myrandnumber < 13 )
return "lemon";
else if ( myrandnumber > 13 && myrandnumber < 20 )
return "oranje";
else
goto a;

}

void update()
{
char tt[5];

sprintf( tt, "%d", games );
wattron( screen, COLOR_PAIR(2) );
format = "Games: ";
mvwaddstr( screen, 2, 4, format.c_str() );
mvwaddstr( screen, 2, 12, tt );
sprintf( tt, "%d", credits );
format = "Credits: ";
mvwaddstr( screen, 3, 4, format.c_str() );
mvwaddstr( screen, 3, 14, tt );
wattroff( screen, COLOR_PAIR(2) );
refresh();
}

void make_game()
{
char c;
int i = 0;
char tt[5];

while ( c != 'q' )
{
switch( c )
{
case 65:
if ( rraposta < credits )
rraposta++;
break;
case 66:
if ( rraposta > 1 )
rraposta--;
break;
case 10:
run();
break;
}

if ( i == 0 )
run();

aposta = credits < rraposta ? credits : rraposta;
rraposta = aposta;

wattron( screen, COLOR_PAIR(2));
sprintf( tt, "%d     ", aposta );
mvwaddstr( screen, (y/2)-6, 4, "BET = multiplies");
mvwaddstr( screen, (y/2)-6, 22, tt );
wattroff( screen, COLOR_PAIR(2));
refresh();

c = getch();
i++;
}

}

void run( )
{
clear();
int i = 0, j = -4;
bool printwin;
char tt[5];
std::string option;
win = false;



wattron( screen, COLOR_PAIR(2) );
mvwaddstr( screen, (y/2)-10, (x/2)-23,"**BONUS** = 50 credits");
mvwaddstr( screen, (y/2)-9, (x/2)-23, "orange    = 2 credits");
mvwaddstr( screen, (y/2)-9, (x/2)+6,"apple =  5 credits");
mvwaddstr( screen, (y/2)-10, (x/2)+6, "lemon = 10 credits");
wattroff( screen, COLOR_PAIR(2) );

mvwaddstr( screen, (y/2)-7, (x/2)-23, "+---------------------------------------------+");
mvwaddstr( screen, (y/2)-6, (x/2)-23, "|  Slot 1  |  Slot 2  |  Slot 3   |  Slot 4   |");
mvwaddstr( screen, (y/2)-5, (x/2)-23, "+---------------------------------------------+");

while ( i < 5 )
{
roleta1 = sorted();
roleta2 = sorted();
roleta3 = sorted();
roleta4 = sorted();
printwin = false;
if ( roleta1 == roleta2 && roleta2 == roleta3 && roleta3 == roleta4 )
{
if ( roleta1 == "**BONUS**" )
{
credits += 50 * aposta;
pay = 50 * aposta;
}
else if ( roleta1 == "apple" )
{
credits += 5 * aposta;
pay = 5 * aposta;
}
else if ( roleta1 == "lemon" )
{
credits += 10 * aposta;
pay = 10 * aposta;
}
else
{
credits += 2 * aposta;
pay = 2 * aposta;
}

win = true;
printwin = true;
}

sprintf( tt, "%d", pay );
option = tt;
if ( printwin )
wattron( screen, COLOR_PAIR(1));

format ="| "+roleta1;
mvwaddstr( screen, (y/2)+j, (x/2)-23, format.c_str() );
format ="| "+roleta2;
mvwaddstr( screen, (y/2)+j, (x/2)-12, format.c_str() );
format ="| "+roleta3;
mvwaddstr( screen, (y/2)+j, (x/2)-1, format.c_str() );
format ="| "+roleta4;
mvwaddstr( screen, (y/2)+j, (x/2)+11, format.c_str() );

if ( printwin )
{
if ( roleta1 == "**BONUS**" )
format = "| **** BONUS +50";
else
format = "| *** +"+option+" credit";
}
else
format = "|";

mvwaddstr( screen, (y/2)+j, (x/2)+23, format.c_str());

if ( printwin )
wattroff( screen, COLOR_PAIR(1));

mvwaddstr( screen,(y/2)+(++j),(x/2)-23, "+---------------------------------------------+");

refresh();
i++;
j++;

}
refresh();
games++;

if ( credits == 0 )
{
clear();
wattron( screen, COLOR_PAIR(1));
mvwaddstr( screen, (y/2), (x/2)-strlen("GAME OVER"), "GAME OVER");
wattroff( screen, COLOR_PAIR(1));
refresh();
close();
}

if ( !win )
credits -= aposta;

mvwaddstr( screen,(y/2)+(++j),(x/2)-11, "Type 'UP or DOWN' to increase or diminish bet");
mvwaddstr( screen,(y/2)+(++j),(x/2)-11, "Type 'enter' to spin");
mvwaddstr( screen,(y/2)+(++j),(x/2)-11, "Type 'q' to quit game");

update();
}

void close()
{
resetty();
endwin();
exit(0);
}
};

int main()
{
Roleta rol;
rol.make_game();
return 0;
}

/*compilar
#g++ roleta.cpp -o roleta -lncurses
#./roleta
*/


Espero q sirva pra alguma coisa t++