0
I’m playing an old-fashioned game on C, I want to know how to check which player won by checking the lines horizontally and vertically and on the diagonals.
Here’s my program done so far:
#define tam 3
void desenhaTabuleiro(char M[tam][tam]);
void zeraTabuleiro(char M[tam][tam])
int joga(char M[tam][tam], int player);
int testaTabuleiro(char M[tam][tam], int linJogada, int colJogada);
int main() {
char M[tam][tam];
int player=0;
int retornoJoga;
int numJogada = 1;
zeraTabuleiro(M);
desenhaTabuleiro(M);
while(1){
retornoJoga=joga(M, player);
if(retornoJoga == 0)
player=!player;
if(numJogada >= 9){
printf("\n###FIM DE JOGO###");
break;
}
else
numJogada++;
desenhaTabuleiro(M);
}
return 0;
}
int testaTabuleiro(char M[tam][tam], int linJogada, int colJogada){
if (linJogada < 0 || linJogada > 2 || colJogada < 0 || colJogada > 2){
printf("#### JOGADA INVALIDA ####\n#MSG: Out of tabuleiro!!!\n\n");
return -1;
}
if(M[linJogada][colJogada] != 'V'){
printf("#### JOGADA INVALIDA ####\n#MSG: Esta posicao ja existe\n\n");
return -1;
}
s
return 0;
}
int joga(char M[tam][tam], int player){
int linJogada, colJogada;
int retornoTeste;
int retornoPosicao;
printf(":::: Player %d ::::\nEscolha a linha e a coluna: ",player+1);
scanf("%d %d",&linJogada,&colJogada);
retornoTeste=testaTabuleiro(M, linJogada, colJogada);
if(retornoTeste == -1){
return -1;
}
if(retornoTeste == 0){
if(player)
M[linJogada][colJogada] = 'O';
else
M[linJogada][colJogada] = 'X';
return 0;
}
}
void zeraTabuleiro(char M[tam][tam]){
int i,j;
for(i=0; i<tam; i++){
for(j=0; j<tam; j++){
M[i][j] = 'V';
}
}
}
void desenhaTabuleiro(char M[tam][tam]){
int i,j;
//system("cls");
for(i=0; i<tam; i++){
for(j=0; j<tam; j++){
if(j==0){
printf(" %c ",M[i][j]);
}
else{
if(j==1){
printf("| %c |",M[i][j]);
}
else{
if(j==2){
printf(" %c \n",M[i][j]);
if(i!=2){
printf("-----------\n",i,j);
}
}
}
}
}
}
}
Here’s the code I tried to make this check
int vencedor(char M[tam][tam]){
int i, j;
int soma = 0;
for(i=0; i<tam; i++){
for(j=0; j<tam; j++){
if(M[i][j] == 'O'){
soma += 1;
}
}
if(soma == 3){
break;
return 0;
}
else{
soma = 0;
}
}
}
But what have you tried? What problems have you faced? As it stands, it just seems a case of "solve the problem for me"...
– Gammeth
I’ll put the code I have
– Meeeefiu
I still can’t see what you tried to solve the problem... Also put the codes that went wrong... Or haven’t tried anything yet?
– Gammeth
I just put the code I tried
– Meeeefiu