Ir ao conteúdo
  • Cadastre-se

Placar de campeonato em c


Xandrules

Posts recomendados

Ola estou com um problemas pois tenho um trabalho a concluir sobre um campeonato de futebol onde tem um cadasstro de usuario geração de confronto entre jogadores

onde quando ha um vencedor contabiliza 3 pontos, empate 1 ponto, derrota 0 pontos, gols pro, gols contra, numero de vitorias, numero de derrotas, ja fiz uma parte do codigo so que nao consigo atribuir ao mesmo usuario dados do jogo anterior com o do jogo posterior esta criando um para cada jogo.

# include <stdio.h>
# include <windows.h>


typedef struct
{
	char jogador1[20];
	char jogador2[20];
}Treg_cadastro;

typedef struct
{
    char player1[20];
    char player2[20];
	int pontos1=0;
	int pontos2=0;
	int placar1=0;
	int placar2=0;
	int vitoria1=0;
	int vitoria2=0;
	int empate1=0;
	int empate2=0;
	int derrota1=0;
	int derrota2=0;
	int golspro1=0;
	int golspro2=0;
	int golscon1=0;
	int golscon2=0;
	
}Treg_jogo;

typedef FILE*Treg_arq; 
typedef FILE*Tarq_jogo;

void cadastro();
void consulta_jogadores();
void jogo();
void placar();
void ranking();

int main()
{
	int opcao;
	Treg_cadastro reg_cadastro;
	Treg_arq arq_cadastro;	//ARQUIVO USADO CADASTRO.BIN
	Treg_jogo reg_jogo;		
	Tarq_jogo arq_jogo;
	char escolha;
	do
    {
        system("color 1a");
        system("cls");
        printf("\n\t\t====================================================\n");
        printf("\t\t1 - \tCADASTRAR JOGADORES");
        printf("\n\t\t====================================================\n");
		printf("\t\t2 - \tCONSULTAR JOGADORES");
		printf("\n\t\t====================================================\n");
		printf("\t\t3 - \tDISPUTAR JOGO");
		printf("\n\t\t====================================================\n");
		printf("\t\t4 - \tPLACARES DOS JOGOS");
		printf("\n\t\t====================================================\n");
		printf("\t\t5 - \tRANKING");
		printf("\n\t\t====================================================\n");
		printf("\t\t6 - \tAPAGAR CADASTROS");
		printf("\n\t\t====================================================\n");
		printf("\t\t7 - \tSAIR");
		printf("\n\t\t====================================================\n");

        printf("\t\t ===>");

        scanf("%i", &opcao);

        system("cls");

        switch (opcao)
        {
        case 1 :
        	cadastro();
            break;
            
        case 2 :
        	consulta_jogadores();
            break;
            
        case 3 :
            jogo();            
            break;
            
        case 4 :
		    placar();
            break;
            
        case 5 :
            
            break;
            
        case 6:
        	  printf("\n\n\t\tDESEJA APAGAR OS CADASTROS ? S/N");
			  printf("\n\t\t===>");	
        	  fflush(stdin);
        	  scanf("%c",&escolha);
        	  if(escolha =='s' || escolha == 'S')
        	  {
			  
		      	system("del cadastros.bin");//CASO QUEIRA LIMPAR O ARQUIVO E TODOS OS USUARIOS
		      	arq_cadastro = fopen("cadastros.bin","rb");
		      	fread(&reg_cadastro, sizeof(Treg_cadastro), 1, arq_cadastro); 
		      
		      	if(arq_cadastro == NULL)
		      	{
		      		printf("\n\n\t\t\tARQUIVO APAGADO COM SUCESSO");
		      		Sleep(3000);
			  	}
			  	fclose(arq_cadastro);
			    
			  }
			  printf("\n\n\t\tDESEJA APAGAR OS DADOS ? S/N");
			  printf("\n\t\t===>");	
        	  fflush(stdin);
        	  scanf("%c",&escolha);
        	  if(escolha =='s' || escolha == 'S')
        	  {
			  
		      	system("del placar.bin");//CASO QUEIRA LIMPAR O ARQUIVO E TODOS OS DADOS
		      	arq_jogo = fopen("placar.bin","rb");
		      	fread(&reg_jogo, sizeof(Treg_jogo), 1, arq_jogo); 
		      
		      	if(arq_cadastro == NULL)
		      	{
		      		printf("\n\n\t\t\tARQUIVO APAGADO COM SUCESSO");
		      		Sleep(3000);
			  	}
			  	fclose(arq_jogo);
			  
			  }
			  main();//RETORNA PARA O MENU PRINCIPAL
			break; 
			
		case 7:
			break;   

        default:
            printf("\n\nOpcao Invalida!!!\n\n");
            system("pause");
            
        }
    }
    while (opcao != 6);


	
	return 0;
}
void cadastro()
{
	Treg_cadastro reg_cadastro;
	Treg_arq arq_cadastro;//ARQUIVO USADO CADASTRO.BIN
	
	char nome[20],nome2[20];
	char escolha;
	
	printf("\n\n\t\t ENTRE COM O NOME DO JOGADOR ");//CADASTRAR JOGADOR 1
	printf("\n\t\t ====>");
	scanf("%s",&reg_cadastro.jogador1);
	printf("\n\t\t DESEJA CADASTRAR MAIS UM JOGADOR? S/N");
	
	fflush(stdin);
	printf("\n\t\t ====>");
	scanf("%c",&escolha);
		if(escolha == 's' || escolha == 'S')
		{
	
		printf("\n\t\t ENTRE COM O NOME DO JOGADOR ");//CADASTRAR JOGADOR 2
		printf("\n\t\t ====>");
		scanf("%s",&reg_cadastro.jogador2);
  		}
   
		arq_cadastro = fopen("cadastros.bin","ab");//ABRI O ARQUIVO PARA GRAVAR O NOVO USUARIO CASO O ARQUIVO NAO EXISTA E CRIADO UM NOVO
    		if(arq_cadastro==NULL)
    		{
      	 		 //caso de algum problema no arquivo entra aqui e volta para o inicio sem travar o programa
      	 		 printf("problema com o arquivo de cadastros!\n\n");
     		   	 system("pause");
        		 main();
    		}
    	fwrite(&reg_cadastro, sizeof(Treg_cadastro), 1, arq_cadastro);//GRAVA O ARQUIVO COM OS USUARIOS

    	fclose(arq_cadastro);
    
}
void consulta_jogadores()
{
	Treg_cadastro reg_cadastro;
	Treg_arq arq_cadastro;
	
	arq_cadastro = fopen("cadastros.bin","r+b");//ABRE O ARQUIVO 
	if ( arq_cadastro == NULL)
	{
		printf("\n\nARQUIVO INVALIDO\n\n");
		system("pause");
		main();
	}
		else
		{
		fread(&reg_cadastro, sizeof(Treg_cadastro), 1, arq_cadastro); //LE O ARQUIVO
			while(!feof(arq_cadastro))
			{
				printf("\n%s",reg_cadastro.jogador1);//MOSTRA TODOS OS USUARIOS CADASTRADOS
				printf("\n%s",reg_cadastro.jogador2);
				fread(&reg_cadastro, sizeof(Treg_cadastro), 1, arq_cadastro);
			}
			fclose(arq_cadastro);
		}
	printf("\n\n");
    system("pause");
}
void jogo()
{
	Treg_cadastro reg_cadastro;
	Treg_jogo reg_jogo;
	Treg_arq arq_cadastro;	//ARQUIVO USADO CADASTRO.BIN	
	Tarq_jogo arq_jogo;//ARQUIVO PLACAR.BIN
	int placar1=0,placar2=0;
	
	char jogador1[20];
	char jogador2[20];
	int opcao,i = 1,j=1;
	char escolha;
	
		
	printf("\n\n\t\t ENTRE COM O NOME DO PLAYER 1");
	printf("\n\t\t ====>");//BUSCA O USUARIO
	scanf("%s",&jogador1);
	printf("\n\t\t ENTRE COM O NOME DO PLAYER 2");
	printf("\n\t\t ====>");
	scanf("%s",&jogador2);
	
    arq_cadastro = fopen("cadastros.bin","r+b");//ABRE O ARQUIVO PARA LEITURA
    
    reg_jogo.pontos1 =0;
    reg_jogo.pontos2 =0;
     
     if ( arq_cadastro == NULL)//SE O ARQUIVO NAO EXISTIR OU ESTIVER VAZIO
	{
		printf("\n\nARQUIVO INVALIDO\n\n");
		system("pause");
		main();
	}
		else
		{
	     	fread(&reg_cadastro, sizeof(Treg_cadastro), 1, arq_cadastro); 
	     	system("cls");
			while(!feof(arq_cadastro))
			{
				if(strcmp(jogador1,reg_cadastro.jogador1)==0) //COMPARA PARA VER SE O USUARIO DIGITADO ESTA CADASTRADO COMO JOGADOR 1
				{
					printf("\t\t%s X",reg_cadastro.jogador1);
					strcpy(reg_jogo.player1,reg_cadastro.jogador1);//SE ENCONTRAR COPIA PARA O REGISTRO JOGO
					i = 2;
				}
					else if(strcmp(jogador1,reg_cadastro.jogador2)==0)//COMPARA PARA VER SE O USUARIO DIGITADO ESTA CADASTRADO COMO JOGADOR 2
					{
						printf("\t\t%s X",reg_cadastro.jogador2);
						strcpy(reg_jogo.player1,reg_cadastro.jogador2);//SE ENCONTRAR COPIA PARA O REGISTRO JOGO
						i=3;
					} 
				    						
						if(strcmp(jogador2,reg_cadastro.jogador1)==0) //COMPARA PARA VER SE O USUARIO DIGITADO ESTA CADASTRADO COMO JOGADOR 1
						{
							printf(" %s",reg_cadastro.jogador1);
							strcpy(reg_jogo.player2,reg_cadastro.jogador1);//SE ENCONTRAR COPIA PARA O REGISTRO JOGO
							j = 3;
						}
							else if(strcmp(jogador2,reg_cadastro.jogador2)==0)//COMPARA PARA VER SE O USUARIO DIGITADO ESTA CADASTRADO COMO JOGADOR 2
							{
								printf(" %s",reg_cadastro.jogador2);
								strcpy(reg_jogo.player2,reg_cadastro.jogador2);//SE ENCONTRAR COPIA PARA O REGISTRO JOGO
								j=2;
							} 
			 
					fread(&reg_cadastro, sizeof(Treg_cadastro), 1, arq_cadastro);
			    }
			system("cls");		
		}
		if((i == 2 && j == 2 )|| (i == 3 && j == 3)||(i == 2 && j ==3) ||(i == 3 && j ==2))//SE ESTIVER CADASTRADO INICIA O JOGO
		{
			
			printf("\n\n\t\t\t %s X %s",reg_jogo.player1,reg_jogo.player2);
			printf("\n\n O JOGO INICIOU PARA FINALIZAR DIGITE A OPCAO CORRESPONDENTE !!!\n");
            
            system("color 1a");
       		
       		fflush(stdin);
       		
        	printf("\t1 - JOGADOR 1 VENCEU\n");
			printf("\t2 - JOGADOR 2 VENCEU\n");
			printf("\t3 - EMPATE\n");
			printf("\t4 - JOGO CANCELADO\n");
					
		    scanf("%i",&opcao);
		
			switch (opcao)
			{
				case 1:
					  //JOGADOR1 VENCEU ENTRA COM O VALOR DO JOGADOR 1 PRIMEIRO
					  arq_jogo = fopen("placar.bin","a+b");
					  do
					  {
					  
					  if((i == 2 && j == 2 )|| (i == 3 && j == 3)||(i == 2 && j ==3) ||(i == 3 && j ==2))
					  {
					  	printf("\n\n\t\tENTRE COM O PLACAR DO JOGO\n");
					  	
					  	printf("%s ",reg_jogo.player1);
					  	scanf("%i",&reg_jogo.placar1);
					  	printf("\n");
					  	printf("%s ",reg_jogo.player2);
					  	scanf("%i",&reg_jogo.placar2); 	
					  	
					  	
					  		if(reg_jogo.placar1 > reg_jogo.placar2 )//TESTE PARA VERIFICAR SE O JOGADOR 1 VENCEU
					  		{
						 			  	
					  			reg_jogo.pontos1 = reg_jogo.pontos1 + 3;//PONTOS + 3 PARA O VENCEDOR
					  			reg_jogo.pontos2 = reg_jogo.pontos2 + 0;
					  	
					  			reg_jogo.vitoria1 = reg_jogo.vitoria1 + 1;//VITORIA = 1 PARA O VENCEDOR
					  			reg_jogo.vitoria2 = reg_jogo.vitoria2 + 0;
					  			
					  			reg_jogo.empate1 = reg_jogo.empate1 + 0;//EMPATE NAO SE APLICA NESSE CASO
					  			reg_jogo.empate2 = reg_jogo.empate2 + 0;
					  			
					  			reg_jogo.derrota1 = reg_jogo.derrota1 + 0;//DERROTA + 1 PARA O PERDEDOR 
					  			reg_jogo.derrota2 = reg_jogo.derrota2 + 1;
										
					  			reg_jogo.golspro1 = reg_jogo.golspro1 + reg_jogo.placar1;//RECEBE OS GOLS FEITOS
					  			reg_jogo.golspro2 = reg_jogo.golspro2 + reg_jogo.placar2;
					  			
					  			reg_jogo.golscon1 = reg_jogo.golscon1 + reg_jogo.placar2;//RECEBE OS GOLS SOFRIDOS
					  			reg_jogo.golscon2 = reg_jogo.golscon2 + reg_jogo.placar1;
					  								  			
								if((reg_jogo.placar1 - reg_jogo.placar2 )>= 3)//SE O JOGADOR FOR HUMILHADO
								{
																					   					  			
					  			printf("\n\n\n\t\t\tPARABENS %s PELA GRANDE VITORIA",reg_jogo.player1);
					  			printf("\n\tHAHAHAHA %s FOI HUMILHADO HAHAHAHAHA PASSA O CONTROLE SEU FRACO\n",reg_jogo.player2);
					  			
					  		    }
					  		    else
								{
								  
					  		    printf("\n\n\n\t\t\tPARABENS %s PELA VITORIA",reg_jogo.player1);
					  		    printf("\n\n\n\t\t\tPASSA O CONTROLE %s KKKKKKKKKKKKKK\n",reg_jogo.player2);
					  		    
					  		    }
					   		}
					  	
					  }					  
					  else if(reg_jogo.placar1 <= reg_jogo.placar2)
					   {
					   		system("cls");
					   		printf("\n\n\t\tPLACAR INVALIDO POIS O JOGADOR %s VENCEU\n\n",reg_jogo.player1);
					   }
					  
				}while(reg_jogo.placar1 < reg_jogo.placar2);
				fwrite(&reg_jogo, sizeof(Treg_jogo), 1, arq_jogo);
				fclose(arq_jogo);
				system("pause");
				main();
				break;
					
				case 2:
					//JOGADOR2 VENCEU ENTRA COM O VALOR DO JOGADOR 2 PRIMEIRO
				break;
					
				case 3:
					//EMPATE 	
				break;
				
				case 4:
					//SAI DO JOGO POIS O JOGO NAO TEVE UM PLACAR	
				break;
			}
		}
		else if(i == 1 || j==1)
		{
			if(i==1)//SE NAO ESTIVER CADASTRADO SERA NECESSARIO CADASTRAR
			{			
				printf("\nANTES DE INICIAR E NECESSARIO CADASTRAR O JAGODOR %s\n",jogador1);
		    }
		    	if(j==1)//SE NAO ESTIVER CADASTRADO SERA NECESSARIO CADASTRAR
		    	{
		    		printf("\nANTES DE INICIAR E NECESSARIO CADASTRAR O JAGODOR %s\n",jogador2);
				}
					
						printf("\nDESEJA CADASTRAR O(S) JOGADOR(ES)?  S/N \n");
						
						fflush(stdin);
						scanf("%c",&escolha);
				
						if(escolha == 's' || escolha == 'S')
						{
							cadastro();//CHAMA A FUNCAO CADASTRO E CADASTRA E VAI PARA O MENU PRINCIPAL
						}
			
        			
		}
	
		
		fclose(arq_cadastro);
		
		
	printf("\n\n");
    system("pause");
}
void placar()
{
 Treg_cadastro reg_cadastro;
 Tarq_jogo arq_jogo; // ARQUIVO USADO PLACAR.BIN
 Treg_arq arq_cadastro;//ARQUIVO USADO CADASTRO.BIN	
 Treg_jogo reg_jogo;
 
 
 arq_jogo = fopen("placar.bin", "rb");
 if (arq_jogo == NULL)
 {
  printf("\n\nArquivo nao aberto - Fale com a equipe de TI");
 }
 else
 {
  fread(&reg_jogo, sizeof(Treg_jogo), 1, arq_jogo);   
  while (! feof(arq_jogo))
  {
   printf("\n\n");
   printf("\nPLAYER     : %s", reg_jogo.player1);      
   printf("\nPONTOS     : %i", reg_jogo.pontos1);
   printf("\nVITORIAS   : %i", reg_jogo.vitoria1);      
   printf("\nDERROTAS   : %i", reg_jogo.derrota1);      
   printf("\nEMPATES    : %i", reg_jogo.empate1);
   printf("\nGOLS PRO   : %i", reg_jogo.golspro1);  
   printf("\nGOLS CONTRA: %i", reg_jogo.golscon1);   
   printf("\n");
   
   printf("\nPLAYER     : %s", reg_jogo.player2);      
   printf("\nPONTOS     : %i", reg_jogo.pontos2);
   printf("\nVITORIAS   : %i", reg_jogo.vitoria2);      
   printf("\nDERROTAS   : %i", reg_jogo.derrota2);      
   printf("\nEMPATES    : %i", reg_jogo.empate2);
   printf("\nGOLS PRO   : %i", reg_jogo.golspro2);  
   printf("\nGOLS CONTRA: %i", reg_jogo.golscon2);   
    
    
   fread(&reg_jogo, sizeof(Treg_jogo), 1, arq_jogo);     
  }   
  fclose(arq_jogo);
 } 
 system("pause");
 //getch();
}
void ranking()
{
	
}

  

Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...