I'm almost finished with this but got stuck in trapping it when
1)there is a winner
2) a player attempts to enter a position that has been previously filled
#include<stdio.h>
#include<conio.h>
display();
game(int turn, int board[3][3]);
output(int board[3][3]);
main()
{int turn=1, board[3][3]={0};
clrscr();
display();
while(turn <= 9)
{game(turn, board);
turn++;
output(board);
if(turn < 9)
game(turn, board);
output(board);
turn++;
}
getch();
}
display()
{printf("Player 1: X\nPlayer 2: O");
gotoxy(15,3); printf("1 2 3");
gotoxy(15,4); printf("4 5 6");
gotoxy(15,5); printf("7 8 9");
}
game(int turn,int board[3][3])
{int a=1,b,p=1,q=3, xo;
do
{if (turn % 2 == 1)
printf("\nP1(X): ");
if(turn % 2 == 0)
printf("\nP2(O): ");
scanf("%d", &xo);
}while(xo<1 || xo>9 );
for(b=0 ;b<3 ;b++)
{for(;a<=q ;a++)
{if(a == xo)
{if(turn % 2 == 1)
board[b][a-p]=1;
if(turn % 2 == 0)
board[b][a-p]=2;
}
}
q+=3;
p+=3;
}
}
output(int board[3][3])
{
int i, j, x, y=3;
for(i=0;i<3;i++)
{x=15;
for(j=0;j<3;j++)
{switch(board[i][j])
{case 0: gotoxy(x,y); printf("_");
x+=2;
break;
case 1: gotoxy(x,y); printf("X");
x+=2;
break;
case 2: gotoxy(x,y); printf("O");
x+=2;
break;
}
}
y++;
}
}
our instructions were to use arrays and reuse arrays if possible..
help guys..
TIA