Created
September 21, 2016 11:16
-
-
Save devils-ey3/18684bf59b9d9096b11dc3ca1bb1657a to your computer and use it in GitHub Desktop.
This is my first year C project. TicTacToe game by c.
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <conio.h> | |
#include <windows.h> | |
int password(); | |
int pass_system(); | |
void printBoard(); | |
void change_player(); | |
void input(); | |
int mainInput(int pass); | |
void run_game(); | |
int win(); | |
void input(); | |
int input_from_user(); | |
void change_background_and_textColour(); | |
void menu(); | |
void go(int n); | |
void start_game(); | |
void check_draw(); | |
void draw_boarD(); | |
void player_first(); | |
void put_X_O(char ch,int pos); | |
void sub_main(); | |
void main_s(); | |
/* Global variables */ | |
char board[9]= {'1','2','3','4','5','6','7','8','9'}; | |
int password(); | |
int pass_system(); | |
int n,j=1; | |
char player='X'; | |
char ch,ch1[80]; | |
int i_for_loop,check_user_input_pass=-1; | |
int boarD[10] = {2,2,2,2,2,2,2,2,2,2}; | |
int turn = 1,flag = 0; | |
int playeR,comp; | |
COORD coord= {0,0}; | |
/* Global variables */ | |
int main () | |
{ | |
change_background_and_textColour(); | |
int i; | |
for (;;) | |
{ | |
system("cls"); | |
sub_main(); | |
getch(); | |
system("cls"); | |
check_user_input_pass=0; | |
printf("Press 1 for play again\nPress 2 Exit\nInput : "); | |
i=input_from_user(); | |
if (i==1) | |
{ | |
check_user_input_pass++; | |
for (i=0;i<9;i++) | |
board[i]='1'+i; | |
continue; | |
} | |
else break; | |
} | |
system("cls"); | |
printf("Thanks for playing :)\nBye Bye"); | |
getch(); | |
return 0; | |
} | |
/** | |
Testing data | |
1 | |
674 | |
1 | |
1 | |
2 | |
4 | |
5 | |
1 | |
1 | |
674 | |
1 | |
1 | |
2 | |
4 | |
5 | |
*/ | |
void sub_main() | |
{ | |
printf("Press 1 for play user vs user \nPress 2 for play user vs pc\nPromote : "); | |
int userchoise; | |
userchoise=input_from_user(); | |
if (userchoise==1 || userchoise==2) | |
{ | |
if (!password(userchoise)) | |
{ | |
printf("The game is terminated"); | |
return 0; | |
} | |
} | |
else | |
printf("You can play"); | |
if (userchoise==1) | |
run_game(); | |
else | |
{ | |
main_s(); | |
} | |
} | |
void change_background_and_textColour() | |
{ | |
int value,value1; | |
printf("Give input for change text color\nBackground change automatically\nPress"); | |
printf("\nBlack = 1\nGreen = 2\nAqua = 3\nRed = 4\nPurple = 5\nYellow = 6\nWhite = 7\nGray = 8\nLight Blue = 9\n"); | |
printf("Color = "); | |
value=input_from_user(); | |
switch(value) | |
{ | |
case 1 : | |
system("COLOR 60"); | |
break; | |
case 2 : | |
system("COLOR 0A"); | |
break; | |
case 3 : | |
system("COLOR C3"); | |
break; | |
case 4 : | |
system("COLOR 74"); | |
break; | |
case 5 : | |
system("COLOR E5"); | |
break; | |
case 6 : | |
system("COLOR B6"); | |
break; | |
case 7 : | |
system("COLOR 17"); | |
break; | |
case 8 : | |
system("COLOR C8"); | |
break; | |
case 9 : | |
system("COLOR A9"); | |
break; | |
} | |
system("cls"); | |
check_user_input_pass++; | |
} | |
int password (int userchoise) ///for password matching | |
{ | |
char pass[80]="674",pass1[80]="658"; | |
int increment=3; | |
printf("Enter the password : "); | |
pass_system(); | |
if (userchoise==1) | |
{ | |
while (strcmp(pass,ch1) && increment>0) | |
{ | |
if (increment==1) | |
{ | |
printf("Sorry ^_^ .. Next time please\n"); | |
return 0; | |
} | |
system("cls"); | |
printf("You have %d chance left \n",increment-1); | |
--increment; | |
printf("Enter password : "); | |
pass_system(); | |
} | |
return 1; | |
} | |
else if (userchoise==2) | |
{ | |
while (strcmp(pass1,ch1) && increment>0) | |
{ | |
if (increment==1) | |
{ | |
printf("Sorry ^_^ ..\n Next time please\n"); | |
return 0; | |
} | |
system("cls"); | |
printf("You have %d chance left \n",increment-1); | |
--increment; | |
printf("Enter password : "); | |
pass_system(); | |
} | |
return 1; | |
} | |
} | |
int pass_system() /// for password input | |
{ | |
i_for_loop=0; | |
while ( (ch=getch()) !='\r' && i_for_loop <79 ) ///scan single character but not displayed in console | |
{ | |
printf("*"); | |
ch1[i_for_loop++]=ch; ///put the character into | |
} | |
ch1[i_for_loop]='\0'; | |
printf("\n"); | |
return ch1[i_for_loop]; | |
} | |
void printBoard() | |
{ | |
system("cls"); | |
int i; | |
printf("SIMPLE TIC TAC TOE GAME By C\n\tUSER VS USER\n\n"); | |
printf("+*-*+*-*+*-*+\n"); | |
for (i=0; i<9; i++) | |
{ | |
if (i!=0 && i%3==0) | |
printf("\n+---+---+---+\n|"); | |
if (i==0) | |
printf("|"); | |
printf(" %c |",board[i]); | |
} | |
printf("\n+*-*+*-*+*-*+\n"); | |
} | |
void call_error() | |
{ | |
printf("Field is already in use try again!"); | |
input(); | |
} | |
int mainInput(int pass) | |
{ | |
if (pass==1) | |
{ | |
if (board[pass-1]=='1') | |
{ | |
board[pass-1]=player; | |
} | |
else | |
call_error(); | |
} | |
else if (pass==2) | |
{ | |
if (board[pass-1]=='2') | |
{ | |
board[pass-1]=player; | |
} | |
else | |
call_error(); | |
} | |
else if (pass==3) | |
{ | |
if (board[pass-1]=='3') | |
{ | |
board[pass-1]=player; | |
} | |
else | |
call_error(); | |
} | |
else if (pass==4) | |
{ | |
if (board[pass-1]=='4') | |
{ | |
board[pass-1]=player; | |
} | |
else | |
call_error(); | |
} | |
else if (pass==5) | |
{ | |
if (board[pass-1]=='5') | |
{ | |
board[pass-1]=player; | |
} | |
else | |
call_error(); | |
} | |
else if (pass==6) | |
{ | |
if (board[pass-1]=='6') | |
{ | |
board[pass-1]=player; | |
} | |
else | |
call_error(); | |
} | |
else if (pass==7) | |
{ | |
if (board[pass-1]=='7') | |
{ | |
board[pass-1]=player; | |
} | |
else | |
call_error(); | |
} | |
else if (pass==8) | |
{ | |
if (board[pass-1]=='8') | |
{ | |
board[pass-1]=player; | |
} | |
else | |
call_error(); | |
} | |
else if (pass==9) | |
{ | |
if (board[pass-1]=='9') | |
{ | |
board[pass-1]=player; | |
} | |
else | |
call_error(); | |
} | |
} | |
int input_from_user() | |
{ | |
char a[4]; | |
int k=0,g; | |
while (k==0) | |
{ | |
fgets(a, 3, stdin); | |
fflush(stdin); | |
if(strlen(a) != 2) | |
{ | |
printf("Invalid input\n"); | |
printf("Input again : "); | |
continue; | |
} | |
if( sscanf(a, "%d", &g) != 1) | |
{ | |
printf("Invalid input\n"); | |
printf("Input again : "); | |
continue; | |
} | |
if (check_user_input_pass!=0) | |
if( g < 1 || g > 9) | |
{ | |
printf("Invalid range\n"); | |
printf("Input again : "); | |
continue; | |
} | |
if (check_user_input_pass==0) | |
if( g < 1 || g > 2) | |
{ | |
printf("Invalid range\n"); | |
printf("Input again : "); | |
continue; | |
} | |
k = 1; | |
} | |
return g; | |
} | |
void input() | |
{ | |
int g,k=0; | |
printf("\n\nIts player %d trun who can put %c\nPress the number of the board : ",j,player); | |
mainInput(input_from_user()); | |
} | |
void change_player() | |
{ | |
if (player == 'X') | |
{ | |
player = 'O'; | |
j=2; | |
} | |
else | |
{ | |
player = 'X'; | |
j=1; | |
} | |
} | |
int win() | |
{ | |
if (( board[0]=='X' && board[1]=='X' && board[2]=='X' ) || ( board[3]=='X' && board[4]=='X' && board[5]=='X' ) | |
||( board[6]=='X' && board[7]=='X' && board[8]=='X' ) || ( board[0]=='X' && board[3]=='X' && board[6]=='X' ) | |
||( board[1]=='X' && board[4]=='X' && board[7]=='X' ) || ( board[2]=='X' && board[5]=='X' && board[8]=='X' ) | |
||( board[0]=='X' && board[4]=='X' && board[8]=='X' ) || ( board[2]=='X' && board[4]=='X' && board[6]=='X' )) | |
return 1; | |
if (( board[0]=='O' && board[1]=='O' && board[2]=='O' ) || ( board[3]=='O' && board[4]=='O' && board[5]=='O' ) | |
||( board[6]=='O' && board[7]=='O' && board[8]=='O' ) || ( board[0]=='O' && board[3]=='O' && board[6]=='O' ) | |
||( board[1]=='O' && board[4]=='O' && board[7]=='O' ) || ( board[2]=='O' && board[5]=='O' && board[8]=='O' ) | |
||( board[0]=='O' && board[4]=='O' && board[8]=='O' ) || ( board[2]=='O' && board[4]=='O' && board[6]=='O' )) | |
return 2; | |
return 3; | |
} | |
void run_game() | |
{ | |
n=0; | |
printBoard(); | |
printf("\n\nPress 1 for playing with O\nPress 2 for playing with X\n\nInput : "); | |
int m=input_from_user(); | |
if (m==1) | |
player='O'; | |
else if (m==2) | |
player='X'; | |
check_user_input_pass++; | |
system("cls"); | |
printBoard(); | |
while(1) | |
{ | |
n++; | |
input(); | |
printBoard(); | |
change_player(); | |
if (n>4) | |
{ | |
if (win()==1) | |
{ | |
printf("\n****** 1st player win who is 'X' ******\n"); | |
break; | |
} | |
else if(win()==2) | |
{ | |
printf("\n****** 2nd player win who is 'O' ******\n"); | |
break; | |
} | |
else if (win()==3 && n==9) | |
{ | |
printf("\n****** The game is draw ******\n"); | |
break; | |
} | |
} | |
} | |
} | |
// Program is finished for user vs user | |
void gotoxy(int x,int y) | |
{ | |
coord.X=x; | |
coord.Y=y; | |
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord); | |
} | |
void main_s() | |
{ | |
system("cls"); | |
menu(); | |
getch(); | |
} | |
void menu() | |
{ | |
int choice; | |
system("cls"); | |
printf("\n--------Your Choice--------"); | |
printf("\n1 : Play with X"); | |
printf("\n2 : Play with O"); | |
printf("\n3 : Exit"); | |
printf("\nEnter your choice:>"); | |
scanf("%d",&choice); | |
turn = 1; | |
switch (choice) | |
{ | |
case 1: | |
playeR = 1; | |
comp = 0; | |
player_first(); | |
break; | |
case 2: | |
playeR = 0; | |
comp = 1; | |
start_game(); | |
break; | |
case 3: | |
exit(1); | |
default: | |
menu(); | |
} | |
} | |
int make2() | |
{ | |
if(boarD[5] == 2) | |
return 5; | |
if(boarD[2] == 2) | |
return 2; | |
if(boarD[4] == 2) | |
return 4; | |
if(boarD[6] == 2) | |
return 6; | |
if(boarD[8] == 2) | |
return 8; | |
return 0; | |
} | |
int make4() | |
{ | |
if(boarD[1] == 2) | |
return 1; | |
if(boarD[3] == 2) | |
return 3; | |
if(boarD[7] == 2) | |
return 7; | |
if(boarD[9] == 2) | |
return 9; | |
return 0; | |
} | |
int posswin(int p) | |
{ | |
// p==1 then X p==0 then O | |
int i; | |
int check_val,pos; | |
if(p == 1) | |
check_val = 18; | |
else | |
check_val = 50; | |
i = 1; | |
while(i<=9)//row check | |
{ | |
if(boarD[i] * boarD[i+1] * boarD[i+2] == check_val) | |
{ | |
if(boarD[i] == 2) | |
return i; | |
if(boarD[i+1] == 2) | |
return i+1; | |
if(boarD[i+2] == 2) | |
return i+2; | |
} | |
i+=3; | |
} | |
i = 1; | |
while(i<=3)//column check | |
{ | |
if(boarD[i] * boarD[i+3] * boarD[i+6] == check_val) | |
{ | |
if(boarD[i] == 2) | |
return i; | |
if(boarD[i+3] == 2) | |
return i+3; | |
if(boarD[i+6] == 2) | |
return i+6; | |
} | |
i++; | |
} | |
if(boarD[1] * boarD[5] * boarD[9] == check_val) | |
{ | |
if(boarD[1] == 2) | |
return 1; | |
if(boarD[5] == 2) | |
return 5; | |
if(boarD[9] == 2) | |
return 9; | |
} | |
if(boarD[3] * boarD[5] * boarD[7] == check_val) | |
{ | |
if(boarD[3] == 2) | |
return 3; | |
if(boarD[5] == 2) | |
return 5; | |
if(boarD[7] == 2) | |
return 7; | |
} | |
return 0; | |
} | |
void go(int n) | |
{ | |
if(turn % 2) | |
boarD[n] = 3; | |
else | |
boarD[n] = 5; | |
turn++; | |
} | |
void player_first() | |
{ | |
int pos; | |
check_draw(); | |
draw_boarD(); | |
gotoxy(30,18); | |
printf("Your Turn :> "); | |
scanf("%d",&pos); | |
if(boarD[pos] != 2) | |
player_first(); | |
if(pos == posswin(player)) | |
{ | |
go(pos); | |
draw_boarD(); | |
gotoxy(30,20); | |
//textcolor(128+RED); | |
printf("playeR Wins"); | |
getch(); | |
exit(0); | |
} | |
go(pos); | |
draw_boarD(); | |
start_game(); | |
} | |
void start_game() | |
{ | |
// p==1 then X p==0 then O | |
if(posswin(comp)) | |
{ | |
go(posswin(comp)); | |
flag = 1; | |
} | |
else if(posswin(player)) | |
go(posswin(player)); | |
else if(make2()) | |
go(make2()); | |
else | |
go(make4()); | |
draw_boarD(); | |
if(flag) | |
{ | |
gotoxy(30,20); | |
//textcolor(128+RED); | |
printf("Computer wins"); | |
getch(); | |
} | |
else | |
player_first(); | |
} | |
void check_draw() | |
{ | |
if(turn > 9) | |
{ | |
gotoxy(30,20); | |
//textcolor(128+RED); | |
printf("Game Draw"); | |
getch(); | |
exit(0); | |
} | |
} | |
void draw_boarD() | |
{ | |
int j; | |
for(j=9; j<17; j++) | |
{ | |
gotoxy(35,j); | |
printf("| |"); | |
} | |
gotoxy(28,11); | |
printf("-----------------------"); | |
gotoxy(28,14); | |
printf("-----------------------"); | |
for(j=1; j<10; j++) | |
{ | |
if(boarD[j] == 3) | |
put_X_O('X',j); | |
else if(boarD[j] == 5) | |
put_X_O('O',j); | |
} | |
} | |
void put_X_O(char ch,int pos) | |
{ | |
int m; | |
int x = 31, y = 10; | |
m = pos; | |
if(m > 3) | |
{ | |
while(m > 3) | |
{ | |
y += 3; | |
m -= 3; | |
} | |
} | |
if(pos % 3 == 0) | |
x += 16; | |
else | |
{ | |
pos %= 3; | |
pos--; | |
while(pos) | |
{ | |
x+=8; | |
pos--; | |
} | |
} | |
gotoxy(x,y); | |
printf("%c",ch); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment