Created
October 8, 2016 06:30
-
-
Save adeen-s/d454afcb2d54bfde7762c5358c6b28ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Made by Adeen | |
| // Initial Version on 8/10/2016 | |
| //On Ubuntu 16.04LTS using Atom editor and gcc toolchain | |
| #include<stdio.h> | |
| #include<stdlib.h> | |
| #include<stdbool.h> | |
| int a[3][3]; | |
| int input() { | |
| int n,i,j; | |
| while(true) { | |
| scanf("%d",&n); | |
| n--; | |
| i=n/3; | |
| j=n%3; | |
| if(a[i][j]==0) { | |
| a[i][j]=2; | |
| break; | |
| } | |
| else | |
| continue; | |
| } | |
| return 0; | |
| } | |
| int display() { | |
| int i,j; | |
| system("clear"); | |
| for(i=0;i<3;i++) { | |
| for(j=0;j<3;j++) { | |
| if(a[i][j]==0) | |
| printf("|_|"); | |
| if(a[i][j]==1) | |
| printf("|O|"); | |
| if(a[i][j]==2) | |
| printf("|X|"); | |
| } | |
| printf("\n"); | |
| } | |
| return 0; | |
| } | |
| int play() { | |
| int i,j; | |
| for(i=0;i<3;i++) | |
| for(j=0;j<3;j++) { | |
| if(a[i][j]==0) { | |
| a[i][j]=1; | |
| return 0; | |
| } | |
| } | |
| return 0; | |
| } | |
| int check() { | |
| int i,j,t=0,cpu=0,user=0; | |
| //-------------------Loop for Horizontal line checking begins------------------- | |
| for(i=0;i<3;i++) { | |
| cpu=0;user=0; | |
| for(j=0;j<3;j++) { | |
| if(a[i][j]==1) | |
| cpu++; | |
| if(a[i][j]==2) | |
| user++; | |
| if(cpu==3) { | |
| printf("Sorry, You Lost\n"); | |
| return 1; | |
| } | |
| if(user==3) { | |
| printf("Congratulations !! You Won\n"); | |
| return 1; | |
| } | |
| } | |
| } | |
| //-------------------Loop for Horizontal Line checking Ends--------------------- | |
| //-------------------Loop for Vertical Line Checking Begins--------------------- | |
| for(i=0;i<3;i++) { | |
| cpu=0;user=0; | |
| for(j=0;j<3;j++) { | |
| if(a[j][i]==1) | |
| cpu++; | |
| if(a[j][i]==2) | |
| user++; | |
| if(cpu==3) { | |
| printf("Sorry, You Lost\n"); | |
| return 1; | |
| } | |
| if(user==3) { | |
| printf("Congratulations !! You Won\n"); | |
| return 1; | |
| } | |
| } | |
| } | |
| //-------------------Loop for vertical Line Checking Ends----------------------- | |
| //-------------------Checking for Diagonal Lines begins------------------------- | |
| if(a[0][0]==1 && a[1][1]==1 && a[2][2]==1) { | |
| printf("Sorry, You Lost\n"); | |
| return 1; | |
| } | |
| if(a[0][0]==2 && a[1][1]==2 && a[2][2]==2) { | |
| printf("Congratulations !! You Won\n"); | |
| return 1; | |
| } | |
| if(a[0][2]==1 && a[1][1]==1 && a[2][0]==1) { | |
| printf("Sorry, You Lost\n"); | |
| return 1; | |
| } | |
| if(a[0][2]==2 && a[1][1]==2 && a[2][0]==2) { | |
| printf("Congratulations !! You Won\n"); | |
| return 1; | |
| } | |
| //-------------------Diagonal Line checking ends-------------------------------- | |
| for(i=0;i<3;i++) | |
| for(j=0;j<3;j++) | |
| if(a[i][j]!=0) | |
| t++; | |
| if(t==9) | |
| return 1; | |
| return 0; | |
| } | |
| int main() { | |
| int i,j,c; | |
| system("clear"); | |
| printf(" Welcome\n To mark your square, press the corresponding number key\n The top left square is 1 and the bottom right square is 9\n"); | |
| printf(" You will play as 'X' and the Computer will be 'O'\n You will play first\nPress ENTER key to continue..."); | |
| getchar(); | |
| system("clear"); | |
| //The Matrix values will indicate the board. 0 for empty, 1 for O and 2 for X | |
| for(i=0;i<3;i++) | |
| for(j=0;j<3;j++) | |
| a[i][j]=0; | |
| while(true) { | |
| display(); | |
| c=check(); | |
| if(c==1) | |
| return 0; | |
| input(); | |
| display(); | |
| c=check(); | |
| if(c==1) | |
| return 0; | |
| display(); | |
| play(); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment