/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
#define DIM 3
#define ESPACO ' '
void
inic (char s[][DIM])
{
int i, j;
for (i = 0; i < DIM; i++)
{
for (j = 0; j < DIM; j++)
{
s[i][j] = ESPACO;
}
}
}
void
mostra (char s[DIM][DIM])
{
int i, j;
for (i = 0; i < DIM; i++)
{
for (j = 0; j < DIM; j++)
{
printf ("%c %c", s[i][j], j == DIM - 1 ? ' ' : '|');
}
if (i != DIM - 1)
printf ("\n-----------");
putchar ('\n');
}
}
/*
xxx x** **x acontece quando: "ganha = 0 if( i==j ^^ v[i][j]= || ) then ganha=1"
*** x** *x*
*** x** x**
ganha se tiver uma linha ou coluna ou diagonal completa
linha (i) linha = 1 for(j=0;j < DIM;j++)
coluna
diagonal
*/
int linha(char m[DIM][DIM]){ //same symbol
for (int i=0; i<DIM;i++){
int counter=0;
char ch=m[i][0];
for(int j = 0; j<DIM; j++){
// int // X O X != ESPAÇO ^^
if(m[i][j]==ch){
counter++;
if(counter == 3){
puts("Vitória Horizontal!");
return 1;
}
}
}
}
puts("NL");
return 0;
}
int diagonal(char m[DIM][DIM]){
int counter,i=0;
char ch=m[i][0];
for (int j=0; i<DIM;i++,j++){
if(m[i][j]==ch){
counter++;
if(counter == 3){
puts("Vitória Diagonal!");
return 1;
}
}
}
puts("NB");
return 0;
}
int coluna(char m[DIM][DIM]){ //same symbol
for (int j=0; j<DIM;j++){
int counter=0;
char ch=m[0][j];
for(int i = 0; j<DIM; i++){
// int // X O X != ESPAÇO ^^
if(m[i][j]==ch){
counter++;
if(counter == 3){
printf("Vitória Vertical!\n");
return 1;
}
}
}
}
puts("NV");
return 0;
}
int ganha(char m[DIM][DIM]){
return linha(m) || coluna(m) || diagonal(m);
}
int
main ()
{
/* Experiences
char s[DIM][DIM];
char v[][DIM]={'c','F',' ',' ',' ','X',' ',' ',' '};
*/
char Velha[DIM][DIM];
// char Nova[5][5];
// mostra(Nova);
//mostra(Velha);
int posx, posy;
char ch = '0';
int n_jogadas = 0;
inic (Velha);
while (1)
{
mostra (Velha);
printf ("\nIntroduza a Posição de Jogo Linha Coluna: ");
scanf ("%d %d", &posx, &posy);
posx--;
posy--; //ou --
if (Velha[posx][posy] == ESPACO) /* Casa Livre */
{
Velha[posx][posy] = ch = (ch == '0') ? 'X' : '0';
n_jogadas++;
}
else
printf ("Posição já ocupada\nJogue Novamente!!!\n");
if (n_jogadas == DIM * DIM || ganha(Velha))
break; /* Acabar o Programa */
}
mostra (Velha);
ganha(Velha);
/* Experiences
mostra(v);
mostra(s);
return 0;
*/
return 0;
}