20-11-2008, 20:25
Bem pessoal, vou vos deixar aqui uma das beladades da programaçao, o jogo da forca feito em turbo c++, por mim.
Agora vou so meter o codigo, feito no programa, mas se quiserem o executavel, peçam, que eu meto em rar, faço upload e meto aqui...
bem as partes que tem a descriçao em ingles, que sao 2, estao assim, porque foram retiradas do help, pois como tinha que fazer o mesmo codigo, nao fazia sentido, ter que escrever o codigo todo, por isso copiei essas duas partes...
Bem em caso de duvidas, tirem as partes do programa, que eu vou esclarecendo!!
PROGRAMA:[/i]
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <string.h>
int getcol(int col)
{
switch(col)
{
case 0: return 0; //BLACK;
case 1: return 4; //RED;
case 2: return 2; //GREEN
case 3: return 6; //BROWN
case 4: return 1; //BLUE;
case 5: return 5; //MAGENTA;
case 6: return 3; //CYAN;
case 7: return 7; //LIGHTGRAY;
case 8: return 8; //DARKGRAY;
case 9: return 12; //LIGHTRED;
case 10:return 10; //LIGHTGREEN
case 11:return 14; //YELLOW;
case 12:return 9; //LIGHTBLUE;
case 13:return 13; //LIGHTMAGENTA
case 14:return 11; //LIGHTCYAN;
case 15:return 15; //WHITE;
}
return col;
}
int loadbitmap(char *filename)
{
FILE *ptr=NULL; //file handle to open bitmap file
int width,height; //width and height of the bitmap
unsigned long temp=0,i=0,j=0; //some variables i need
unsigned long ww;
ptr=fopen(filename,"rb"); //open the bitmap file
if(!ptr) return 0; //if its not there return
width=0;height=0; //initialise wisth and height to zero
fseek(ptr,18,SEEK_SET); //got to offset 18 in file
for(i=0x1;i<=0x10000;i*=0x100) //read the width
{
temp=fgetc(ptr);
width+=(temp*i);
}
fseek(ptr,22,SEEK_SET); //go to offset 22 in file
for(i=0x1;i<=0x10000;i*=0x100) //read the height
{
temp=fgetc(ptr);
height+=(temp*i);
}
ww=width; //ww is the number of reads to make for each horizontal line
if(ww%2) //ww has to be half of width - since each pixel is only 4 bits of information
ww++;ww/=2; //just getting the correct value
if(ww%4) //now - ww is stored as sets of 4 pixels each - i think !?! o this is the adjustment made
ww=(ww/4)*4+4; //if width is less than ww*2 we ignore what we read
fseek(ptr,119,SEEK_SET); //Ok! offset 119 - lets read the pixels -
//remember the bitmap is stroed up - side - down
int ch,ch1,ch2;
for(i=0;i<height;i++)
for(j=0;j<ww;j++)
{
ch=fgetc(ptr); //each character read is 2 pixels - yes 4 bits per pixel - so 16 colors
ch1=ch;ch2=ch; //find those colors using bitwise ops
ch1=ch1&(0xf0);ch1=ch1>>4; // ~
ch2=ch2&(0x0f); // ~
if(j*2<width) //ok so put the first pixel read on screen
putpixel((j)*2,(height-1-i),getcol(ch1));
if(j*2+1<width) //put the second pixel read on screen
putpixel((j)*2+1,(height-1-i),getcol(ch2));
}
fclose(ptr); //close the file handle
return 1;
}
void desenha_forca(){
line(150,330,280,330);
line(150,326,280,326);
line(213,326,213,50);
line(217,326,217,54);
line(213,50,348,50);
line(217,54,344,54);
line(348,50,348,100);
line(344,54,344,100);
}
void escreve_estado(int falhas){
char msg[100];
setfillstyle(EMPTY_FILL, getmaxcolor());
setcolor(EGA_BLACK);
bar(430,160,540,270);
setcolor(EGA_YELLOW);
rectangle(420,150,550,280);
sprintf(msg,
" %d falhas",
falhas);
outtextxy(445,170,msg);
outtextxy(480,215,"&");
sprintf(msg,
"%d tentativas"
,9-falhas );
outtextxy(440,260,msg);
}
int verifica_char(char* str, char c){
return(1);
}
void desenha_menu_principal(){
setcolor(getmaxcolor());
cleardevice();
loadbitmap("forca.bmp");
setcolor(EGA_BLACK);
settextstyle(GOTHIC_FONT,HORIZ_DIR,0);
outtextxy(400,80, "1- Jogar");
setcolor(EGA_BROWN);
outtextxy(400,140, "2- instru‡oes");
setcolor(EGA_GREEN);
outtextxy(400,200, "3- sair");
}
void desenha_menu_secundario(){
setcolor(EGA_WHITE);
settextstyle(DEFAULT_FONT,HORIZ_DIR,0);
loadbitmap("imgmenu.bmp");
outtextxy(180,20, "POR FAVOR ESCOLHA UM DOS SEGUINTES TEMAS!");
}
void desenha_parte(int n){
if (n==1)
circle(350,150,50);
if (n==2)
line(350,200,350,280);
if (n==3)
line(350,230,300,190);
if (n==4)
line(350,230,400,190);
if (n==5)
line(350,280,300,330);
if (n==6)
line(350,280,400,330);
if (n==7)
circle(340,146,6);
if (n==8)
circle(360,146,6);
if (n==9)
arc(350,190,0,172,15);
}
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy,i,x;
int pos;
int radius = 100;
char *fname[] = { "EMPTY_FILL",
"SOLID_FILL",
"LINE_FILL",
"LTSLASH_FILL",
"SLASH_FILL",
"BKSLASH_FILL",
"LTBKSLASH_FILL",
"HATCH_FILL",
"XHATCH_FILL",
"INTERLEAVE_FILL",
"WIDE_DOT_FILL",
"CLOSE_DOT_FILL",
"USER_FILL"
};
char c,d,e;
FILE *stream;
char string[]= " jogo da forca";
char msg[80];
char palavra[25];
char pal[25];
char s[3];
char letras[26]="";
int falhas=0;
char aux[80];
int y, li, au;
int encontrou;
randomize();
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
do{
desenha_menu_principal();
do{
c=getch();
if(c<='0' || c>'4'){
setcolor(EGA_BLUE);
settextstyle(DEFAULT_FONT,HORIZ_DIR,0);
outtextxy(180,5,"Nao identificado, por favor tente novamente!");
delay(500);
}
}while(c<='0' || c>'4');
cleardevice();
if(c == '1'){
desenha_menu_secundario();
do{
d=getch();
if(d<='0' || d>'5')
setcolor(EGA_BLACK);
outtextxy(150,450,"Nao identificado, por favor tente novamente");
}while(d<='0' || d>'5');
cleardevice();
if(d == '1'){
stream=fopen("paises.txt", "r");
outtextxy(190,20,"O tema selecionado foi : PAISES");
}
if(d == '2'){
stream=fopen("animais.txt", "r");
outtextxy(190,20,"O tema selecionado foi : ANIMAIS");
}
if(d == '3'){
stream=fopen("prof.txt", "r");
outtextxy(190,20,"O tema selecionado foi : PROFICCOES");
}
if(d == '4'){
stream=fopen("ditador.txt", "r");
outtextxy(190,20,"O tema selecionado foi : DITADORES");
}
if(d == '5'){
stream=fopen("capitais.txt", "r");
outtextxy(190,20,"O tema selecionado foi : CAPITAIS");
}
li=0;
while(!feof(stream)){
fgets(s,200,stream);
li++;
}
rewind(stream);
// escolher a palavra
au = random (li);
for (i=0;i<au;i++){
fgets(palavra,200,stream); // ler pergunta
}
falhas=0;
fclose(stream);
pal[strlen(palavra)-1]='\0';
for (i=0;i<strlen(palavra)-1;i++)
pal[i]=' ';
palavra[strlen(palavra)-1]='\0';
desenha_forca();
// escrever as letras
s[1]='\0';
for(i=0 ; i < strlen(palavra) ;i++)
line(20+i*15,100,30+i*15,100);
for(s[0]='A',x=10;s[0]<='Z';s[0]++){
outtextxy(x,400,s);
x+=24;
}
for (i=12;i<=620;i+=24)
circle(i, 403, 8);
//for do fim!
for(;{
//le a letra que o utilizador introduziu
do{
e=getch();
if( !((e>='a' && e<= 'z') || (e>='A' && e<= 'Z'))){
outtextxy(280,450,"DIGITE APENAS LETRAS");
delay(1000);
setfillstyle(EMPTY_FILL, getmaxcolor());
setcolor(EGA_BLACK);
bar(270,430,500,500);
}
}while( !((e>='a' && e<= 'z') || (e>='A' && e<= 'Z')) );
if (e>='a' && e<='z'){
e=e-('a'-'A');
pos=e-'A';
setcolor(EGA_BLACK);
circle(12+pos*24,403,8);
}
setcolor(EGA_BLUE);
encontrou=0;
//detetar letra na palavra
for(i=0;i<strlen(palavra);i++){
if (e==palavra[i]){
encontrou = 1;
sprintf(aux,"%c",e);
outtextxy(20+i*15,85,aux);
pal[i]=e;
}
}
if (!encontrou){
falhas++;
desenha_parte(falhas);
escreve_estado(falhas);
}
if (falhas>=9){
cleardevice();
loadbitmap("loser.bmp");
setcolor(EGA_BLACK);
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(50,440,"carregue enter para voltar ao menu!");
getch();
break;
}
else
if (!strcmp(palavra,pal)){
cleardevice();
loadbitmap("winner.bmp");
setcolor(EGA_WHITE);
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(50,440,"carregue enter para voltar ao menu!");
getch();
break;
}
} /* for */
}
if(c == '2'){ // mostrar instrucoes
cleardevice();
setcolor(EGA_LIGHTGREEN);
settextstyle(DEFAULT_FONT,HORIZ_DIR,0);
stream=fopen("inst.txt", "r");
outtextxy(20,450,"por favor! assim que ler as instrucoes carregue enter para voltar ao menu!");
y=10;
while(!feof(stream)){
fgets(msg, 200, stream);
msg[strlen(msg)-1]='\0';
outtextxy(20,y,msg);
y=y+10;
}
fclose(stream);
getch();
}
if(c == '3')
exit(0);
}while(1);
getch();
closegraph();
return 0;
}
Agora vou so meter o codigo, feito no programa, mas se quiserem o executavel, peçam, que eu meto em rar, faço upload e meto aqui...
bem as partes que tem a descriçao em ingles, que sao 2, estao assim, porque foram retiradas do help, pois como tinha que fazer o mesmo codigo, nao fazia sentido, ter que escrever o codigo todo, por isso copiei essas duas partes...
Bem em caso de duvidas, tirem as partes do programa, que eu vou esclarecendo!!
PROGRAMA:[/i]
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <string.h>
int getcol(int col)
{
switch(col)
{
case 0: return 0; //BLACK;
case 1: return 4; //RED;
case 2: return 2; //GREEN
case 3: return 6; //BROWN
case 4: return 1; //BLUE;
case 5: return 5; //MAGENTA;
case 6: return 3; //CYAN;
case 7: return 7; //LIGHTGRAY;
case 8: return 8; //DARKGRAY;
case 9: return 12; //LIGHTRED;
case 10:return 10; //LIGHTGREEN
case 11:return 14; //YELLOW;
case 12:return 9; //LIGHTBLUE;
case 13:return 13; //LIGHTMAGENTA
case 14:return 11; //LIGHTCYAN;
case 15:return 15; //WHITE;
}
return col;
}
int loadbitmap(char *filename)
{
FILE *ptr=NULL; //file handle to open bitmap file
int width,height; //width and height of the bitmap
unsigned long temp=0,i=0,j=0; //some variables i need
unsigned long ww;
ptr=fopen(filename,"rb"); //open the bitmap file
if(!ptr) return 0; //if its not there return
width=0;height=0; //initialise wisth and height to zero
fseek(ptr,18,SEEK_SET); //got to offset 18 in file
for(i=0x1;i<=0x10000;i*=0x100) //read the width
{
temp=fgetc(ptr);
width+=(temp*i);
}
fseek(ptr,22,SEEK_SET); //go to offset 22 in file
for(i=0x1;i<=0x10000;i*=0x100) //read the height
{
temp=fgetc(ptr);
height+=(temp*i);
}
ww=width; //ww is the number of reads to make for each horizontal line
if(ww%2) //ww has to be half of width - since each pixel is only 4 bits of information
ww++;ww/=2; //just getting the correct value
if(ww%4) //now - ww is stored as sets of 4 pixels each - i think !?! o this is the adjustment made
ww=(ww/4)*4+4; //if width is less than ww*2 we ignore what we read
fseek(ptr,119,SEEK_SET); //Ok! offset 119 - lets read the pixels -
//remember the bitmap is stroed up - side - down
int ch,ch1,ch2;
for(i=0;i<height;i++)
for(j=0;j<ww;j++)
{
ch=fgetc(ptr); //each character read is 2 pixels - yes 4 bits per pixel - so 16 colors
ch1=ch;ch2=ch; //find those colors using bitwise ops
ch1=ch1&(0xf0);ch1=ch1>>4; // ~
ch2=ch2&(0x0f); // ~
if(j*2<width) //ok so put the first pixel read on screen
putpixel((j)*2,(height-1-i),getcol(ch1));
if(j*2+1<width) //put the second pixel read on screen
putpixel((j)*2+1,(height-1-i),getcol(ch2));
}
fclose(ptr); //close the file handle
return 1;
}
void desenha_forca(){
line(150,330,280,330);
line(150,326,280,326);
line(213,326,213,50);
line(217,326,217,54);
line(213,50,348,50);
line(217,54,344,54);
line(348,50,348,100);
line(344,54,344,100);
}
void escreve_estado(int falhas){
char msg[100];
setfillstyle(EMPTY_FILL, getmaxcolor());
setcolor(EGA_BLACK);
bar(430,160,540,270);
setcolor(EGA_YELLOW);
rectangle(420,150,550,280);
sprintf(msg,
" %d falhas",
falhas);
outtextxy(445,170,msg);
outtextxy(480,215,"&");
sprintf(msg,
"%d tentativas"
,9-falhas );
outtextxy(440,260,msg);
}
int verifica_char(char* str, char c){
return(1);
}
void desenha_menu_principal(){
setcolor(getmaxcolor());
cleardevice();
loadbitmap("forca.bmp");
setcolor(EGA_BLACK);
settextstyle(GOTHIC_FONT,HORIZ_DIR,0);
outtextxy(400,80, "1- Jogar");
setcolor(EGA_BROWN);
outtextxy(400,140, "2- instru‡oes");
setcolor(EGA_GREEN);
outtextxy(400,200, "3- sair");
}
void desenha_menu_secundario(){
setcolor(EGA_WHITE);
settextstyle(DEFAULT_FONT,HORIZ_DIR,0);
loadbitmap("imgmenu.bmp");
outtextxy(180,20, "POR FAVOR ESCOLHA UM DOS SEGUINTES TEMAS!");
}
void desenha_parte(int n){
if (n==1)
circle(350,150,50);
if (n==2)
line(350,200,350,280);
if (n==3)
line(350,230,300,190);
if (n==4)
line(350,230,400,190);
if (n==5)
line(350,280,300,330);
if (n==6)
line(350,280,400,330);
if (n==7)
circle(340,146,6);
if (n==8)
circle(360,146,6);
if (n==9)
arc(350,190,0,172,15);
}
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy,i,x;
int pos;
int radius = 100;
char *fname[] = { "EMPTY_FILL",
"SOLID_FILL",
"LINE_FILL",
"LTSLASH_FILL",
"SLASH_FILL",
"BKSLASH_FILL",
"LTBKSLASH_FILL",
"HATCH_FILL",
"XHATCH_FILL",
"INTERLEAVE_FILL",
"WIDE_DOT_FILL",
"CLOSE_DOT_FILL",
"USER_FILL"
};
char c,d,e;
FILE *stream;
char string[]= " jogo da forca";
char msg[80];
char palavra[25];
char pal[25];
char s[3];
char letras[26]="";
int falhas=0;
char aux[80];
int y, li, au;
int encontrou;
randomize();
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
do{
desenha_menu_principal();
do{
c=getch();
if(c<='0' || c>'4'){
setcolor(EGA_BLUE);
settextstyle(DEFAULT_FONT,HORIZ_DIR,0);
outtextxy(180,5,"Nao identificado, por favor tente novamente!");
delay(500);
}
}while(c<='0' || c>'4');
cleardevice();
if(c == '1'){
desenha_menu_secundario();
do{
d=getch();
if(d<='0' || d>'5')
setcolor(EGA_BLACK);
outtextxy(150,450,"Nao identificado, por favor tente novamente");
}while(d<='0' || d>'5');
cleardevice();
if(d == '1'){
stream=fopen("paises.txt", "r");
outtextxy(190,20,"O tema selecionado foi : PAISES");
}
if(d == '2'){
stream=fopen("animais.txt", "r");
outtextxy(190,20,"O tema selecionado foi : ANIMAIS");
}
if(d == '3'){
stream=fopen("prof.txt", "r");
outtextxy(190,20,"O tema selecionado foi : PROFICCOES");
}
if(d == '4'){
stream=fopen("ditador.txt", "r");
outtextxy(190,20,"O tema selecionado foi : DITADORES");
}
if(d == '5'){
stream=fopen("capitais.txt", "r");
outtextxy(190,20,"O tema selecionado foi : CAPITAIS");
}
li=0;
while(!feof(stream)){
fgets(s,200,stream);
li++;
}
rewind(stream);
// escolher a palavra
au = random (li);
for (i=0;i<au;i++){
fgets(palavra,200,stream); // ler pergunta
}
falhas=0;
fclose(stream);
pal[strlen(palavra)-1]='\0';
for (i=0;i<strlen(palavra)-1;i++)
pal[i]=' ';
palavra[strlen(palavra)-1]='\0';
desenha_forca();
// escrever as letras
s[1]='\0';
for(i=0 ; i < strlen(palavra) ;i++)
line(20+i*15,100,30+i*15,100);
for(s[0]='A',x=10;s[0]<='Z';s[0]++){
outtextxy(x,400,s);
x+=24;
}
for (i=12;i<=620;i+=24)
circle(i, 403, 8);
//for do fim!
for(;{
//le a letra que o utilizador introduziu
do{
e=getch();
if( !((e>='a' && e<= 'z') || (e>='A' && e<= 'Z'))){
outtextxy(280,450,"DIGITE APENAS LETRAS");
delay(1000);
setfillstyle(EMPTY_FILL, getmaxcolor());
setcolor(EGA_BLACK);
bar(270,430,500,500);
}
}while( !((e>='a' && e<= 'z') || (e>='A' && e<= 'Z')) );
if (e>='a' && e<='z'){
e=e-('a'-'A');
pos=e-'A';
setcolor(EGA_BLACK);
circle(12+pos*24,403,8);
}
setcolor(EGA_BLUE);
encontrou=0;
//detetar letra na palavra
for(i=0;i<strlen(palavra);i++){
if (e==palavra[i]){
encontrou = 1;
sprintf(aux,"%c",e);
outtextxy(20+i*15,85,aux);
pal[i]=e;
}
}
if (!encontrou){
falhas++;
desenha_parte(falhas);
escreve_estado(falhas);
}
if (falhas>=9){
cleardevice();
loadbitmap("loser.bmp");
setcolor(EGA_BLACK);
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(50,440,"carregue enter para voltar ao menu!");
getch();
break;
}
else
if (!strcmp(palavra,pal)){
cleardevice();
loadbitmap("winner.bmp");
setcolor(EGA_WHITE);
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(50,440,"carregue enter para voltar ao menu!");
getch();
break;
}
} /* for */
}
if(c == '2'){ // mostrar instrucoes
cleardevice();
setcolor(EGA_LIGHTGREEN);
settextstyle(DEFAULT_FONT,HORIZ_DIR,0);
stream=fopen("inst.txt", "r");
outtextxy(20,450,"por favor! assim que ler as instrucoes carregue enter para voltar ao menu!");
y=10;
while(!feof(stream)){
fgets(msg, 200, stream);
msg[strlen(msg)-1]='\0';
outtextxy(20,y,msg);
y=y+10;
}
fclose(stream);
getch();
}
if(c == '3')
exit(0);
}while(1);
getch();
closegraph();
return 0;
}