Jogo da velha melhorado
Publicado por Samuel Macedo 13/10/2004
[ Hits: 7.730 ]
Versão melhorada do jogo da velha que eu postei ha alguns dias atrás...
#include <stdio.h> int ctrl[3][3]; char imp[3][3]; int ctrlf(int l1,int c1,int l2,int c2,int param) { int cti,ctj,chp1=0,chp2=0,chv=0,cmp,ctvelha=0; if ( param == 1 ){ --l1; --c1; if(ctrl[l1][c1]!= 0) return 5; ctrl[l1][c1] = 1; imp[l1][c1] = 'X'; } else{ --l2; --c2; if(ctrl[l2][c2]!= 0) return 5; ctrl[l2][c2] = 2; imp[l2][c2] = 'O'; } for(cti=0;cti<3;cti++){ for(ctj=0;ctj<3;ctj++){ if(ctrl[cti][ctj] == 1){ ++chp1; } if(ctrl[cti][ctj] == 2){ ++chp2; } if(ctrl[cti][ctj] == 0){ chv = 1; } } if (chp1 == 3) return 1; if (chp2 == 3) return 2; chp1 = 0; chp2 = 0; }//checagem linha for(cti=0;cti<3;cti++){ for(ctj=0;ctj<3;ctj++){ if(ctrl[ctj][cti] == 1){ ++chp1; } if(ctrl[ctj][cti] == 2){ ++chp2; } } if (chp1 == 3) return 1; if (chp2 == 3) return 2; chp1 = 0; chp2 = 0; }//checagem coluna for(cti = 0;cti<3;cti++){ if(ctrl[cti][cti] == 1){ ++chp1; } if(ctrl[cti][cti] == 2){ ++chp2; } } if (chp1 == 3) return 1; if (chp2 == 3) return 2; chp1 = 0; chp2 = 0; // checagem diag. principal for(cti=0;cti<3;cti++){ for(ctj=0;ctj<3;ctj++){ if(cti + ctj == 2){ if(ctrl[cti][ctj] == 1){ ++chp1; } if(ctrl[cti][ctj] == 2){ ++chp2; } } } } if (chp1 == 3) return 1; if (chp2 == 3) return 2; chp1 = 0; chp2 = 0; //checagem diag. sec. if (chv == 0){ return 3; } return 0; } impjogo() { int impi, impj; system("clear"); for(impi=0;impi<3;impi++){ for(impj=0;impj<3;impj++){ printf("%c ",imp[impi][impj]); } printf("\n"); } } int central() { int i,j,j1l,j1c,j2l,j2c,result,param,perg; for(i=0;i<3;i++){ for(j=0;j<3;j++){ ctrl[i][j] = 0; imp[i][j] = '*'; } } do{ do{ impjogo(); if (result == 5) puts("Posicao ja ocupada! Jogue novamente:"); printf("==Jogador 1==\n"); do{ printf("Linha: "); scanf("%d",&j1l); if(j1l>3||j1l<1){ puts("Nao existe essa linha!"); puts("Digita de novo, animal:"); } }while(j1l>3||j1l<1); do{ printf("Coluna: "); scanf("%d",&j1c); if(j1c>3||j1c<1){ puts("Nao existe essa coluna!"); puts("Digita de novo, animal:"); } }while(j1c>3||j1c<1); result = ctrlf(j1l,j1c,j2l,j2c,1); if ( result != 0 && result != 5) break; }while(result==5); do{ impjogo(); if (result == 5) puts("Posicao ja ocupada! Jogue novamente:"); printf("==Jogador 2==\n"); do{ printf("Linha: "); scanf("%d",&j2l); if(j2l>3||j2l<1){ puts("Nao existe essa linha!"); puts("Digita de novo, animal:"); } }while(j2l>3||j2l<1); do{ printf("Coluna: "); scanf("%d",&j2c); if(j2c>3||j2c<1){ puts("Nao existe essa coluna!"); puts("Digita de novo, animal:"); } }while(j2c>3||j2c<1); result = ctrlf(j1l,j1c,j2l,j2c,2); }while(result==5); }while(result == 0); impjogo(); if (result == 1){ puts("JOGADOR 1 GANHOU"); } if (result == 2){ puts("JOGADOR 2 GANHOU"); } if (result == 3){ puts("VELHA..."); } puts("Jogar novamente? ( 0 - Sim / qualquer outra tecla - Nao )"); scanf("%d",&perg); return perg; } main() { while(central()==0){ } }
Inserir e remover caracter da matriz
Instalar e Configurar o Slackware Linux em 2025
Como configurar os repositórios do apt no Debian 12 em 2025
Passkeys: A Evolução da Autenticação Digital
Instalação de distro Linux em computadores, netbooks, etc, em rede com o Clonezilla
Configurando o Conky para iniciar corretamente no sistema
3 configurações básicas que podem melhorar muito a sua edição pelo editor nano
Como colorir os logs do terminal com ccze
Instalação Microsoft Edge no Linux Mint 22
Como configurar posicionamento e movimento de janelas no Lubuntu (Openbox) com atalhos de teclado
Ingress NGINX Controller CVSS base score of 9.8 (2)
Impossível corrigir problemas, você manteve (hold) pacotes quebrados. (2)
Linux Mint não conecta Wi-Fi sem fio (18)