Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int checkwin (char t[10], char c)
- {
- int x,i;
- x=0;
- if ((t[1]==t[5]) && (t[5]==t[9]) && (t[9]==c))
- {
- x=1;
- return x;
- }
- if ((t[3]==t[5]) && (t[5]==t[7]) && (t[7]==c))
- {
- x=1;
- return x;
- }
- for (i=1;i<=3;i++)
- {
- if ((t[i]==t[i+3]) && (t[i+3]==t[i+6]) && (t[i+3]==c))
- {
- x=1;
- return x;
- }
- }
- for (i=1;i<=7;i=i+3)
- {
- if ((t[i]==t[i+1]) && (t[i+1]==t[i+2]) && (t[i+2]==c))
- {
- x=1;
- return x;
- printf ("ahla");
- }
- }
- return x;
- }
- int update (char t[10] ,int n,int i)
- {
- if (i==1)
- {
- t[n]='X';
- }else
- {
- t[n]='O';
- }
- return t;
- }
- int turn (int i)
- {
- if (i==1)
- {
- i=2;
- }else
- {
- i=1;
- }
- return i;
- }
- void gamepad (char t[10])
- {
- printf (" | |\n");
- printf (" %c | %c | %c \n",t[1],t[2],t[3]);
- printf (" | |\n");
- printf ("-------------------------\n");
- printf (" | |\n");
- printf (" %c | %c | %c \n",t[4],t[5],t[6]);
- printf (" | |\n");
- printf ("-------------------------\n");
- printf (" | |\n");
- printf (" %c | %c | %c \n",t[7],t[8],t[9]);
- printf (" | |\n");
- }
- int reset (char t[10])
- {
- int i;
- for (i=1;i<=9;i++)
- {
- t[i]=i+48;
- }
- }
- int draw (char t[10])
- {
- int i,test;
- i=0;
- test=1;
- for (i=1;i<=9;i++)
- {
- if (t[i]!='X' && t[i]!='O')
- {
- test=0;
- }
- }
- return test;
- }
- int main()
- {
- char t[10] = {'0','1','2','3','4','5','6','7','8','9'};
- int i,n,win1,win2,win0;
- char res;
- i=1;
- do{
- do{
- printf ("\n Welcome to TicTacToe \n");
- printf ("\nPlayer 1: X ///// Player 2: O\n");
- gamepad (t);
- do{
- printf ("\nPlayer %d choose a column: ",i);
- scanf ("%d",&n);
- }while (t[n]=='X' || t[n]=='O');
- update(t,n,i);
- if (checkwin (t,'X')==1)
- {
- win1=1;
- }
- if (checkwin (t,'O')==1)
- {
- win2=1;
- }
- i=turn(i);
- win0=draw(t);
- system("cls");
- } while (win1==0 && win2==0 && win0==0);
- if (win1==1)
- {
- system("color B");
- gamepad (t);
- win1=0;
- printf ("Player 1 won good job");
- }
- if (win2==1)
- {
- system("color A");
- gamepad (t);
- win2=0;
- printf ("Player 2 won good job");
- }
- if (win0==1)
- {
- system("color C");
- gamepad (t);
- win0=0;
- printf ("Draw");
- }
- do{
- printf ("\nReplay another game? (Y/N)");
- scanf ("%c",&res);
- }while((toupper(res)!='N') && (toupper(res)!='Y'));
- system("cls");
- reset (t);
- }while (toupper(res)!='N');
- printf ("Thanks for using my program <3");
- }
Add Comment
Please, Sign In to add comment