1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
#include <stdio.h>
int a [2] [2] =
{
0 , 0 ,0 , 0 , 0 ,0 , 0 , 0 , 0
};
int pos1 = 0 , pos2 = 0, flag = 0 , max = 0;
void fill(int flag)
{
int l = 0 , m = 0 , n = 0;
while(max <= 9)
{
if(flag % 2 == 0)
{
B:
printf ("Player 1 enter pos1 and pos2 varying between 0 to 2\n");
scanf ("%d %d", &pos1 , &pos2); //Scanf needs the address of the variable
if(a [pos1][pos2] == 4)
{
printf ("Entry is invalid/already occupied\n");
goto B;
}
else
{
a[pos1][pos2] = 3;
flag++;
max++;
l = check_row (pos1 , pos2);
m = check_column (pos1 , pos2);
if(l ==1 || m==1)
{
result(flag);
}
if((pos1 - pos2) % 2 ==0 )
{
n = check_diagonal(pos1 , pos2);
if(n == 1)
{
result(flag);
}
}
}
}
else
{
A:
printf ("Player 2 enter pos1 and pos2 varying between 0 to 2\n");
scanf ("%d %d", &pos1 , &pos2); //Scanf needs the address of the variable
if(a[pos1][pos2] == 3)
{
printf ("Entry is invalid/already occupied\n");
goto A;
}
else
{
a[pos1][pos2] = 4;
flag++;
max++;
check_row (pos1 , pos2);
check_column (pos1 , pos2);
if(l == 1 || m == 1)
{
result(flag);
}
if((pos1 - pos2) % 2 == 0)
{
check_diagonal(pos1 , pos2);
if(n == 1)
{
result(flag);
}
}
}
}
}
}
int check_diagonal(pos1 , pos2)
{
int i , count = 0;
if(pos1 == pos2)
{
for(i = 0 ; i < 2 ; i++)
{
if(a [i][i] == a[i+1][i+1])
{
count++;
}
}
if(count == 2)
{
return 1;
}
}
else
{
i = 1;
if(a[i][i]== a[i+1][i-1] == a[i-1][i+1])
{
return 1;
}
}
}
int check_row(int pos1 , int pos2)
{
if(a[pos1][0] == a[pos1][1] ==a[pos1][2])
{
return 1;
}
}
int check_column(int pos1 , int pos2)
{
if(a[0][pos2] == a[1][pos2] == a[2][pos2])
{
return 1;
}
}
void result(int flag)
{
int k = 0;
if(flag % 2==0)
{
k = 1;
}
else
{
k = 2;
}
printf ("Game over player %d wins,k");
}
int main (int argc , char* argv []) //Don't use void main
{
int i = 0;
printf ("Game starts with player1\n");
fill (flag);
return 0;
}
|