Posted by : Waqas Javed Saturday, 17 January 2015

This coding demonstrate the usage of  only basic data type and basic command(if, for, while functions, etc) to make a practical game that could be run. The code just uses the basic commands. This program also demonstrate the usage of function.
Comments are added to let you understand the program still if you have any further query you can comment below. Thanks hope this will help you.

#include<stdio.h>
#include<conio.h>

//Player turns
int turn(int &user, int &t, int &a1, int &a2, int &a3, int &a4, int &a5, int &a6, int &a7, int &a8, int &a9) {
int pos;

if(user%2 == 1){printf("Player1\n");}
if(user%2 == 0){printf("Player2\n");}
printf("Enter the postion You Want to selct: ");
scanf_s("%d", &pos);


if(pos == 1 && a1 == 0){//chk avalability of player selected box
if(user%2 == 1){//if player1 chosed box
a1 = 1; //box = 1 for p1 chek the box
user++; //chng user for next turn
t++; //turn count
} else if (user%2 == 0) {//if player 2 chosed box
a1 = 2; //box = 2 for p2 checked the box
user++;
t++;
}
} else if (pos == 1 && a1 != 0){//if box is already filled
printf("Already checked!\n");
}

//same structure for next 9 boxes
if(pos == 2 && a2 == 0){
if(user%2 == 1){
a2 = 1;
user++;
t++;
} else if (user%2 == 0) {
a2 = 2;
user++;
t++;
}
} else if (pos == 2 && a2 != 0){
printf("Already checked!\n");
}

if(pos == 3 && a3 == 0){
if(user%2 == 1){
a3 = 1;
user++;
t++;
} else if (user%2 == 0) {
a3 = 2;
user++;
t++;
}
} else if (pos == 3 && a3 != 0){
printf("Already checked!\n");
}

if(pos == 4 && a4 == 0){
if(user%2 == 1){
a4 = 1;
user++;
t++;
} else if (user%2 == 0) {
a4 = 2;
user++;
t++;
}
} else if (pos == 4 && a4 != 0){
printf("Already checked!\n");
}

if(pos == 5 && a5 == 0){
if(user%2 == 1){
a5 = 1;
user++;
t++;
} else if (user%2 == 0) {
a5 = 2;
user++;
t++;
}
} else if (pos == 5 && a5 != 0){
printf("Already checked!\n");
}

if(pos == 6 && a6 == 0){
if(user%2 == 1){
a6 = 1;
t++;
user++;
} else if (user%2 == 0) {
a6 = 2;
user++;
t++;
}
} else if (pos == 6 && a6 != 0){
printf("Already checked!\n");
}

if(pos == 7 && a7 == 0){
if(user%2 == 1){
a7 = 1;
user++;
t++;
} else if (user%2 == 0) {
a7 = 2;
user++;
t++;
}
} else if (pos == 7 && a7 != 0){
printf("Already checked!\n");
}

if(pos == 8 && a8 == 0){
if(user%2 == 1){
a8 = 1;
user++;
t++;
} else if (user%2 == 0) {
a8 = 2;
user++;
t++;
}
} else if (pos == 8 && a8 != 0){
printf("Already checked!\n");
}

if(pos == 9 && a9 == 0){
if(user%2 == 1){
a9 = 1;
user++;
t++;
} else if (user%2 == 0) {
a9 = 2;
user++;
t++;
}
} else if (pos == 9 && a9 != 0){
printf("Already checked!\n");
}

return 0;

}

//check if any player had made the combination
int win_cheker(int &a1, int &a2, int &a3, int &a4, int &a5, int &a6, int &a7, int &a8, int &a9){
//chk first row for p1 else p2
if(a1 == a2 && a2 == a3 && a3 == 1){
printf("winning combination\n");
printf("\n%d %d %d\n* * *\n* * *\n ", a1, a2, a3);
return 1;
} else if (a1 == a2 && a2 == a3 && a3 == 2) {
printf("winning combination\n");
printf("\n%d %d %d\n* * *\n* * *\n ", a1, a2, a3);
return 2;
}

//chk 2nd row for p1 else p2
if(a4 == a5 && a5 == a6 && a6 == 1){
printf("winning combination\n");
printf("\n* * *\n%d %d %d\n* * *\n ", a4, a5, a6);
return 1;
} else if(a4 == a5 && a5 == a6 && a6 == 2) {
printf("winning combination\n");
printf("\n* * *\n%d %d %d\n* * *\n ", a4, a5, a6);
return 2;
}

//chk 3rd row for p1 else p2
if(a7 == a8 && a8 == a9 && a9 == 1){
printf("winning combination\n");
printf("\n|* * *\n|* * *\n|%d %d %d\n ", a7, a8, a9);
return 1;
} else if(a7 == a8 && a8 == a9 && a9 == 2) {
printf("winning combination\n");
printf("\n* * *\n* * *\n%d %d %d\n ", a7, a8, a9);
return 2;
}

//chk 1st column for p1 else p2
if(a1 == a4 && a4 == a7 && a7 == 1){
printf("winning combination\n");
printf("\n%d * *\n%d * *\n%d * *\n ", a1, a4, a7);
return 1;
} else if(a1 == a4 && a4 == a7 && a7 == 2) {
printf("winning combination\n");
printf("\n%d * *\n%d * *\n%d * *\n ", a1, a4, a7);
return 2;
}

//chk 2nd column for p1 else p2
if(a2 == a5 && a5 == a8 && a8 == 1){
printf("winning combination\n");
printf("\n* %d *\n* %d *\n* %d *\n ", a2, a5, a8);
return 1;
} else if(a2 == a5 && a5 == a8 && a8 == 2) {
printf("winning combination\n");
printf("\n* %d *\n* %d *\n* %d *\n ", a2, a5, a8);
return 2;
}

//chk 3st column for p1 else p2
if(a3 == a6 && a6 == a9 && a9 == 1){
printf("winning combination\n");
printf("\n* * %d\n* * %d\n* * %d\n", a3, a6, a9);
return 1;
} else if(a3 == a6 && a6 == a9 && a9 == 2) {
printf("winning combination\n");
printf("\n* * %d\n* * %d\n* * %d\n", a3, a6, a9);
return 2;
}

//chk diagnol-1 for p1 else p2
if(a1 == a5 && a5 == a9 && a9 == 1){
printf("winning combination\n");
printf("\n%d * *\n* %d *\n* * %d\n", a1, a5, a9);
return 1;
} else if(a1 == a5 && a5 == a9 && a9 == 2) {
printf("winning combination\n");
printf("\n%d * *\n* %d *\n* * %d\n", a1, a5, a9);
return 2;
}

//chk diagnol-2 for p1 else p2
if(a3 == a5 && a5 == a7 && a7 == 1){
printf("winning combination\n");
printf("\n* * %d\n* %d *\n%d * *\n", a3, a5, a7);
return 1;
} else if(a3 == a5 && a5 == a7 && a7 == 2) {
printf("winning combination\n");
printf("\n* * %d\n* %d *\n%d * *\n", a3, a5, a7);
return 2;
}
printf("\n");
return 0;
}

//the main structure of progrmam
void main() {
int chk = 0; //turn to 1 if p1 wins. or turns to 2 if p2 wins
int user = 1; /*user determination by remainder method, incrementing user value after
every turn, using user%2 formula 0 for p-1, 1 for player-2*/
int t = 0; //count turns made
int a1 = 0; //a1-a9 position of box. 0 for empty box, 1 for p1 checked, 2 for p2 checked
int a2 = 0;
int a3 = 0;
int a4 = 0;
int a5 = 0;
int a6 = 0;
int a7 = 0;
int a8 = 0;
int a9 = 0;

printf("\n%d %d %d\n%d %d %d\n%d %d %d\n\n", a1, a2, a3, a4, a5, a6, a7, a8, a9);

while(chk == 0 && t < 9){

//call turn function
turn(user, t, a1, a2, a3, a4, a5, a6, a7, a8, a9);

//show the grid
printf("\n%d %d %d\n%d %d %d\n%d %d %d\n ", a1, a2, a3, a4, a5, a6, a7, a8, a9);

//chk for win combination
chk = win_cheker(a1, a2, a3, a4, a5, a6, a7, a8, a9);
}

if(chk == 1) {
printf("player1 you won");
} else if (chk == 2) {
printf("player2 you won");
} else {
printf("Match Tie");
}

_getch();

}

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Contact Us

Stay updated for more codes if we u want any code write us by commenting below or email us.
If you want to hire us for your semester project email us at above given email or call or message
Email: 14bscswjaved@seecs.edu.pk
Phone no: 0308-8109708

Popular Post

Powered by Blogger.

- Copyright © BSCS -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -