flipkarma.com€¦ · web viewfinally, when all the stickers are turned, player with higher score...
TRANSCRIPT
TRIBHUVAN UNIVERSITY
INSTITUTE OF ENGINEERINGPulchowk Campus
Lalitpur, Nepal
A Report on C-Programming Project
Submitted By Submitted To Names:- Department of Bikash Gupta(070/BCT/512) Electronics and Computer EngineeringManish Chandra(070/BCT/519) Shrawan Bk(070/BCT/544)
1
TABLE OF CONTENTS TOPICS PAGE NUMBER
1)Acknowledgement………………………………….3
2)Abstract of the project……………………………...4
3)Background…………………………………………5
4)Problem analysis……………………………………6
5)Algorithm…………………………………………7-13
6)Flowchart………………………………………….14-29
7)Code……………………………………………….30-43
8)Compiling, testing & debugging…………………..43
9)Output snapshots…………………………………...43-46
10)Discussion & conclusion………………………….47
2
Acknowledgement
Although this project has been developed in a group of three, it would not have been possible without the help of my other fellow friends and teachers. We would like to thank our friend Nabin Bhattarai for his kind advice on how to make the project more practical. We offer our special gratitude towards Baburam Dawadi Sir who has contributed to the project with his valuable comments, suggestions, time and energy. Although his presence during the construction of the project would have proved very much helpful, his instructions for the project during the pre-project development phase have been very much appreciated. We would also like to thank our senior Prakash, who encouraged us during the project development.
In addition to all the help from our teachers, we received a lot of help and support from our friends. We believe it would be an injustice not to thank our friends especially Sagar Aryal. Their suggestions and comments have been most helpful for the development of the program and they deserve a significant part of the credit that goes to this program.
Last but not the least, we will always be indebted to our mother and father, without whom even thinking of developing this project was absurd.
3
ABSTRACT OF THE PROJECT
This game, though has been developed for C-Programming project, has also been developed with motto of encouragement. The basic layout of the program is very typical. There is a Main Menu, almost like a “Table of Contents” in a book. Upon entering the “play game”, there is an option to choose between “play with computer” and “play with human”. Sticker game is very popular in real world as “wrestling sticker” game. It’s just an attempt to make a similar game. Since we didn’t use graphics, it was nearly impossible for us to include such stickers in the game. So, we chose to use ASCII characters. Keeping in mind that, the logic of the game should be similar with the real one, we have especially focused on that matter.
Thus, by this project, we believe that enthusiasm will be created among new generation to develop games on their own with much eagerness. The main motto of our project is all hidden in it.
4
BACKGROUND
This project was primarily designed for a semester project for “Computer Programming“. This project, as its title “Sticker Game” suggests, is an attempt to develop a program that is not only a précis of the semester course but also a way to show that the mini games played during childhood can also be played virtually.
We will not boast that the project is complete by itself and bug-free at its present version; however, I believe it can serve as a starting point for future developers to be enthusiastic to learn C programming and then develop even better “Sticker Game”.
In my experience, many children and even adults have keen interest in playing computer games. It’s just an attempt from our side to create enthusiasm in them to develop games or other equivalents by themselves.
Therefore this project has been purposely developed to fit reasonably within the boundaries of the course. It uses simple sub routines and functions so that the user may be able to use the project with ease and then re-develop it with even more ease.
Although the project seems very simple, with no or less effort on the developer’s part, I assure you that it was a challenge and at the same time a pleasure to tackle it. This program was purposely developed to be as simple as possible but at the same time highly encouraging.
I sincerely hope the program fulfils its purpose and proves helpful to anyone who might use it.
Anyone wishing to improve and upgrade the program is very welcome to do so.
5
PROBLEM ANALYSIS
In this project, we will make two mostly used user defined functions: gotoxy() and sleep(). Sleep will pause the program execution for the specified time. Gotoxy() will set the cursor position at the specified location.
We shall display the shapes of the stickers with “-,_,|”, which is contained in function player1(). For the situation when the user would want to play with computer, we shall use time function as seed to get random values. At first, player will choose a number printed at the back of the sticker and the contents of the sticker will be characters equivalent to ASCII values of 1 to 5. If player would want to turn the card that has already been turned, we will display error message and ask the player to turn another unturned sticker by using goto statement. After that, computer turns the random sticker. If both the stickers match, we shall increase the score of the player who has turned the sticker at last, by the number of stickers that has already been turned; and decrease the same amount in another player’s score. Finally, when all the stickers are turned, player with higher score will win.
When the user will choose to play versus human, the same thing is repeated for first player. But for second player, when player will choose a number printed at the back of the sticker, the contents of the sticker will be characters equivalent to ASCII values of 1 to 5. If any of the players would want to turn the card that has already been turned, we will display error message and ask the players to turn another unturned sticker by using goto statement. After comparing the scores as that for single player, the winner’s name is displayed.
To get assured that computer (or second player) doesnot turn the same sticker as turned by first player, we shall use sleep() function so that time() function when it will be called, will not necessarily generate the same random number as that by first player.
Again we shall use goto statement when the player will want to play the game again.
6
ALGORITHM
For header:
Step-1: Start.Step-2: Include header files windows.h, stdio.h, conio.h, time.h, stdlib.h, string.h.Step-3: Declare functions gotoxy(int x, int y), sleep(time_t delay), player1(), stkr(),
stkr_com(), main_single_player(), main_two_player() & error(). Now for gotoxy function:Step-4: Initialize X- and Y- coordinates by 0 and 0 respectively.Step-5: Assign the value of argument x to X-coordinate and that of y to Y-
coordinate.Step-6: Set the cursor to that corresponding coordinate.Now for sleep function:Step-7: Declare timer0 and timer1 in time_t datatype.Step-8: Till the difference between the present time and the time when function was
called is less than the argument value, call time(&timer1).Now for stkr function:Step-9: Generate a random number between 1 & 5 using system time as seed.Step-10: Return the value.Now for stkr_com function:Step-11: Repeat the steps-9 & 10.Now for player1 function:Step-12: Display stickers’ shapes, i.e.
(a) Display upper horizontal line with ‘_’. for (a=1;a<=78;a++) Display ‘_’
(b) Display vertical lines with ‘|’. for (a=1;a<=100;a++) Display ‘|\t’ (c) Display the rightmost vertical line with ‘|’.
(d) Display lower horizontal line with ‘-’. for (a=0;a<=79;a++) Display ‘-’
(e)Display the number at the centre of the back of the stickers of player 1. for (a=1;a<=5;a++) { printf("%d",a); }
(f) Display the number at the centre of the back of the stickers of player 2 or Computer.
for (a=1;a<=5;a++) { printf("%d",a);
}Now for error function:Step-13: Display "THE STICKER HAS ALREADY BEEN TURNED".
7
Step-14: Display “ENTER OTHER UNTURNED STICKER'S NUMBER".Step-15: End.
For two_player.c
Step-1: Start.For main_two_player function:Step-2: Declare a, b, e, f, count_com, count_pla, *p1[30], *p2[30], c[5] & u[5].Step-3: Clear the screen.Step-4: Read both players’ names.Step-5: Clear the screen.Step-6: Call player1() function.Step-7: gotoxy(0,22).Step-8: Display heading of player1’s score, i.e.
printf("%s's score",&p1); & then display ‘0’ below it.
Step-9: gotoxy(63,22).Step-10: Display heading of player2’s score, i.e.
printf("%s's score",&p2); & then display ‘0’ below it.
Step-11: Take a loop from b=0 to b=4 with increment +1.Step-12: Call function stkr_com() and store the returned value in t2.Step-13: gotoxy(0,14) and display a blank line.Step-14: Display “It’s <1st Player’s Name>’s turn” at the centre of 12th row.Step-15: Display “ENTER THE STICKER'S NUMBER YOU WANT TO TURN
AND PRESS ENTER:”.Step-16: Define a label PLAY:.Step-17: gotoxy(0,14) & get the entered sticker’s number and store it in c[b].Step-18: gotoxy(0,14) & display three blank lines.Step-19: For second or above attempts, if the player desires to turn the same sticker
that s/he has already turned, (a)Call error() function & (b)goto PLAY (i.e. step-16).
Step-20: If the player enters 1,2,3,4 or 5,(a) Pause for 1.2 seconds(b) Display the contents of the sticker by virtually turning it, i.e.
Take a loop from a=0 to a=5 with increment +1 if(a<3) (i) Go at the centre of the sticker (ii) Call function stkr() and display the returned value else (i) Go to one line above the centre of sticker (ii) Call function stkr() and display the returned value. (c)Else
(i) Display “ENTER THE NUMBER FROM 1 TO 5". (ii) goto PLAY.
Step-21: Call function stkr() and store the returned value in t1.Step-22: If for player1 it’s second or above attempt of turning the sticker
check if (t1==t2)
8
If it’s true (a) gotoxy(0,18)
(b) Display at the centre of row "IT'S A MATCH". (c) count_pla+=1+b; count_com-=(b+1);
(d) Display 2nd player’s score at the location (71,23), i.e. printf(" %d ",count_com);
(e) Display 1st player’s score at the location (8,23), i.e. printf(" %d ",count_pla); else Display "\t\t\t\tNO MATCH " at the location (0,18).Step-23: goto the location (0,12) and display “ITS <SECOND PLAYER'S NAME>’s
TURN” and goto new line.Step-24: Take a loop from f=0 to f=4.Step-25: Define a label PLAY1:.Step-26: gotoxy(0,14) & assign the number that second player enters to u[f].Step-27: gotoxy(0,14) & display three blank lines.Step-28: gotoxy(0,15).Step-29: For second or above attempts of player2, if the player desires to turn the
same sticker that s/he has already turned, (a)Call error() function & (b)goto PLAY1 (i.e. step-25).
Step-30: If player2 enters 1,2,3,4 or 5,(a) Pause for 2.5 seconds.(b) Display the contents of the sticker by virtually turning it, i.e.
Take a loop from a=0 to a=5 with increment +1 if(a<3) (i)Go at the centre of the sticker (ii)call function stkr_com() and display the returned value else (i)Go to one line above the centre of sticker (ii)call function stkr_com() and display the returned value (c) Else (i) ) Display “ENTER THE NUMBER FROM 1 TO 5". (ii) goto PLAY1.
Step-31: Goto step-32 without completing the loop started in step-24.Step-32: Increase the value of f by 1.Step-33: Call function stkr_com() and store the returned value in t2.Step-34: Check if (t2==t1) If it’s true,
(a) gotoxy(0,18) (b) Display at the centre of row "IT'S A MATCH". (c) count_com+=1+b; count_pla-=(b+1);
(d) Display 2nd player’s score at the location (71,23), i.e. printf(" %d ",count_com);
(e) Display 1st player’s score at the location (8,23), i.e. printf(" %d ",count_pla); else
9
Display "\t\t\t\tNO MATCH " at the location (0,18).Step-35: Check if(b==4) If it’s true,
(a) Display "PRESS ANY KEY TO VIEW THE WINNER” at (30,20).(b) Take any character from user.
Step-36: Goto step-11.Step-37: Check if(count_com>count_pla)
(a) If it’s true,(i) Clear the screen.(ii) Display “THE WINNER IS <Player2’s name>” at (30,10).(iii) Display "\npress any key to continue.....").
(b) Else if(count_pla>count_com)(i) Clear the screen.(ii) Displayer “THE WINNER IS <Player1’s name>” at (30,10).(iii) Display "\npress any key to continue.....").
(c) else
(i) Clear the screen.(ii) Display “THE GAME ENDED IN A DRAW” at (30,10).(iii) Display "\npress any key to continue.....").
Step-38: Take a character from the user. Step-39: Stop.
For single_player.c
Step-1: Start.For main_single_player functionStep-2: Declare a, b, e, f, count_com, count_pla, *p1[30], c[5], u[5].Step-3: Clear the screen.Step-4: Read the player’s name.Step-5: Clear the screen.Step-6: Call player1() function.Step-7: gotoxy(0,22).Step-8: Display heading of player1’s score, i.e.
printf("%s's score",&p1); & then display ‘0’ below it.
Step-9: gotoxy(63,22).Step-10: Display heading of computer’s score & then display ‘0’ below it.Step-11: Repeat steps-11 to 22 of two_player.c.Step-12: goto the location (0,12) and display “ITS Computer’s
TURN” and goto new line.Step-13: Take a loop from f=0 to f=4.Step-14: Define a label PLAY1:.Step-15: Generate a random number between 1 and 5 & assign it to u[f].Step-16: For second or above attempts, if the computer generates same random
number that it has already generated, goto PLAY1 (i.e. step-14).Step-17: If the computer generates 1,2,3,4 or 5,
(a) Pause for 2.5 seconds.
10
(b) Display the contents of the sticker by virtually turning it, i.e.Take a loop from a=0 to a=5 with increment +1
if(a<3) (i) Go at the centre of the sticker (ii) Call function stkr_com() and display the returned value else (i) Go to one line above the centre of sticker (ii) Call function stkr_com() and display the returned value.
Step-18: Goto step-19 without completing the loop started in step-13.Step-19: Increase the value of f by 1.Step-20: Call function stkr_com() and store the returned value in t2.Step-21: Check if (t2==t1) If it’s true,
(a) gotoxy(0,18) (b) Display at the centre of row "IT'S A MATCH". (c) count_com+=1+b; count_pla-=(b+1);
(f) Display computer’s score at the location (71,23), i.e. printf(" %d ",count_com);
(g) Display 1st player’s score at the location (8,23), i.e. printf(" %d ",count_pla); else Display "\t\t\t\tNO MATCH " at the location (0,18).Step-22: Check if(b==4) If it’s true,
(c) Display "PRESS ANY KEY TO VIEW THE WINNER” at (30,20).(d) Take any character from user.
Step-23: Goto step-11.Step-24: Check if(count_com>count_pla)
(a) If it’s true,(i) Clear the screen.(ii) Displayer “THE WINNER IS COMPUTER” at (30,10).(iii) Display "\npress any key to continue.....").
(b) Else if(count_pla>count_com)(iv) Clear the screen.(v) Displayer “THE WINNER IS <Player1’s name>” at (30,10).(vi) Display "\npress any key to continue.....").
(c) else
(iv) Clear the screen.(v) Display “THE GAME ENDED IN A DRAW” at (30,10).(vi) Display "\npress any key to continue.....").
Step-38: Take a character from the user. Step-39: Stop.
For main:
11
S tep-1 : StartStep-2: Include header.h, single_player.c, two_player.c files.Step-3: Declare m, e, k.Step-4: Define label PLAY0:.Step-5: gotoxy(30,5) & display "MAIN MENU”.Step-6: gotoxy(28,6) & display ”*************".Step-7: gotoxy(30,10) & display"1.PLAY GAME".Step-8: gotoxy(30,11) & display "2.HOW TO PLAY".Step-9: gotoxy(30,12) & display "3.EXIT".Step-10: Leaving two blank lines, display "\t\tENTER THE CORRESPONDING
NUMBER TO YOUR CHOICE:”.Step-11: Take corresponding number from user and assign it to e.Step-12: (A) If choice is ‘1’,
(a) Clear the screen. (b) gotoxy(30,10) & display "1.PLAY VERSUS HUMAN". (c) gotoxy(30,11) & display "2.PLAY VERSUS COMPUTER".
(d) Leave two blank lines & display "\t\tENTER THE CORRESPONDING NUMBER TO YOUR CHOICE:".
(e)Assign user’s choice to k. (i) If choice is ‘1’, call main_two_player() function. (ii) If choice is ‘2’, call main_single_player() function.
(iii)Else display "PRESS ONLY THE CORRESPONDING NUMBER".
(f) Goto step-13.(B) If choice is ‘2’,
(a) Clear the screen.(b) Display the rule of the game, i.e.
printf("\n\nTHERE ARE A TOTAL OF TEN STICKERS, FIVE YOURS AND FIVE OTHER PLAYER'S (OR COMPUTER'S), TURNED IN SUCH A WAY THAT ITS BACK PORTION FACES US. TURN ANY STICKER AS YOU DESIRE BY PRESSING THE NUMBER PRINTED ON ITS BACK. THEN OTHER PLAYER (OR COMPUTER) TURNS ANOTHER STICKER. IF BOTH THE STICKERS MATCH, THE PLAYER WHO TURNS THE STICKER LATER, GETS POINTS WHICH IS EQUAL TO THE MAXIMUM NUMBER OF STICKERS TURNED TILL THAT MATCH. MOREOVER, ANOTHER PLAYER GETS THE SAME NUMBER OF NEGATIVE POINTS. PLAYER WITH HIGHER SCORE AFTER THE TURN OF ALL TEN STICKERS, WINS.");(c) Leave two blank lines and display "PRESS ANY KEY TO GO BACK TO MAIN MENU"
(d) Take a character from user. (e) Clear the screen.
(f) goto PLAY0 (i.e. step-4). (g) goto step-13.
(C) If choice is ‘3’, goto step-23. (D) Else
(a) Display "\n\t\tPRESS ONLY THE CORRESPONDING NUMBER".
12
(b) Goto step-13.Step-13: Clear the screen.Step-14: Display “DO YOU WANT TO PLAY AGAIN?".Step-15: Display "1.YES".Step-16: Display "2.NO".Step-17: Define a label PLAY3:.Step-18: Display "\t\tENTER THE CORRESPONDING NUMBER TO YOUR
CHOICE:"Step-19: Assign the user’s choice to m.Step-20: (A) If choice is ‘1’,
(a) Clear the screen.(b) Goto PLAY0 (i.e. step-4).(c) Goto step-21.
(B) If choice is ‘2’, goto step-21. (C) Else display "PRESS 1 OR 2" & goto PLAY3.Step-21: Take a character from user.Step-22: Return 0.Step-23: Stop.
13
FLOWCHART
FOR HEADER
14
start
include <stdio.h><conio.h><time.h><stdlib.h><windows.h><string.h>
Declare gotoxy(int,int), sleep(time_t), player1(),stkr(),stkr_com(),main_single_player(void),main_two_player(void) ,error()
COORD coord={0,0}
void gotoxy(int x, int y)
coord.X=x, coord.Y=y
Set cursor to corresponding location
void sleep(time_t delay)
Declare timer0,timer1
Call time(&timer0)
time(&timer1)
if((timer1-timer0)<delay)
int stkr()
T
F
1
15
srand(time(NULL))
int x1=((rand()) % 5)
int x=x1+1
return x
int stkr_com()
srand(time(NULL))
int y1=((rand()) % 5)
int y=y1+1
return y
void player1()
int a
Display ' '
for (a=1;a<=78;a++)
Display '_'
1
2
16
Display "\n"
for (a=1;a<=100;a++)
Display '|'
for (a=1;a<=10;a++)
gotoxy(79,a)
Display '|'
for (a=0;a<=79;a++)
gotoxy(a,11)
Display '-'
int j=4,s=44
for (a=1;a<=5;a++)
gotoxy(j,6)
Display a
j+=8
2
3
17
for (a=1;a<=5;a++)
gotoxy(s,6)
Display a
s+=8
void error()
Display "THE STICKER HAS ALREADY BEEN TURNED.
ENTER OTHER UNTURNED STICKER'S NUMBER"
3
Stop
FOR SINGLE_PLAYER.C
18
Start
include "header.h"
void main_single_player(void)
Declare a,b,e,f=0, count_com=0, count_pla=0,*p1[30]
Clear the screen
Read p1
Player1()
gotoxy(0,22)
Display “p1’s score” and ‘0’ below it
gotoxy(63,22)
Display "Comp's score" and ‘0’ below it
for (b=0;b<=4;b++) 4
19
int t2=stkr_com()
int c[5]
gotoxy(0,12)
Display “IT’S p1’s TURN”
Display “ENTER THE STICKER'S NUMBER YOU WANT TO TURN AND PRESS ENTER:”
Read c[b]
If(b>0) and player turns the same sticker that has already been turned
error()
If(c[b]==1||c[b]==2||c[b]==3||c[b]==4||c[b]==5
sleep(1.2)
Display the contents of the sticker
"ENTER THE NUMBER FROM 1 TO 5
4
FT
TF
5
20
int t1=stkr()
if(b>0)
if (t1==t2)
count_pla+=1+b, count_com-=(b+1)
Display count_com, count_pla below their respective headings
gotoxy(0,18)
Display “NO MATCH”
Display “ITS Computer's TURN”
int u[5]
for(;f<=4;f++)
Generate a random number between 1 and 5 and assign it to u[f]
If(f>0) and computer generates the same random number that it has already generated
5
T
F T
T
7
F
21
If(u[f]==1||u[f]==2||u[f]==3||u[f]==4||u[f]==5
sleep(2.5)
Display the contents of the sticker
f+=1
t2=stkr_com()
if(t2==t1)
count_com+=1+b, count_pla-=(b+1)
Display count_pla, count_com below their respective headings
gotoxy(0,18)
Display “NO MATCH”
if(b==4)
Display "PRESS ANY KEY TO VIEW THE WINNER"
7
T
T
F
T
8
9
4
22
if(count_com>count_pla)
Clear the screen
Display "THE WINNER IS COMPUTER"
Display “press any key to continue....."
if(count_pla>count_com)
Clear the screen
Display “THE WINNER IS p1”
Display “press any key to continue.....”
Clear the screen
Display “THE GAME ENDED IN A DRAW”
Display “press any key to continue.....”
getch()
9
T
F
T
F
Stop
FOR TWO_PLAYER.C
23
Declare a,b,e,f=0, count_com=0, count_pla=0, *p1[30],*p2[30]
Read p1,p2
for(;f<=4;f++)
Read u[f]
If(f>0) and the player turns the same sticker that has already been turned
error()
If(u[f]==1||u[f]==2||u[f]==3||u[f]==4||u[f]==5
sleep(2.5)
Display the contents of the sticker
Repeat the same steps for player1
T
F
T
F
10
11
24
Display "ENTER THE NUMBER FROM 1 TO 5"
f+=1
t2=stkr_com()
if(t2==t1)
count_com+=1+b, count_pla-=(b+1)
Display count_com, count_pla below their respective headings
gotoxy(0,18)
Display “NO MATCH”
if(b==4)
Display “PRESS ANY KEY TO VIEW THE WINNER"
getch()
10
11
T
F
T
12
25
if(count_com>count_pla)
Display ”THE WINNER IS p2”
Display “press any key to continue....."
if(count_pla>count_com)
Display “THE WINNER IS p1”
Display “press any key to continue....."
Display “THE GAME ENDED IN A DRAW"
Display “press any key to continue....."
getch()
12
T
F
T
F
FOR MAIN.C
26
include "header.h", "single_player.c", "two_player.c”
Declare m, e ,k
Display "MAIN MENU"
Display "\n*************"
gotoxy(30,10)
Display "1.PLAY GAME"
Display "\n2.HOW TO PLAY"
Display “\n3.EXIT”
Display “ENTER THE CORRESPONDING NUMBER TO YOUR CHOICE:"
e=getch()
14
17
27
If(e==1)
Display "1.PLAY VERSUS HUMAN"
Display "2.PLAY VERSUS COMPUTER"
Display “ENTER THE CORRESPONDING NUMBER TO YOUR CHOICE:"
k=getch()
If(k==1)
main_two_player()
If(k==2)
main_single_player()
Display "PRESS ONLY THE CORRESPONDING NUMBER"
14
T
TF
TF
19
15F
28
If(e==2)
Display the rule of the game
Display “PRESS ANY KEY TO GO BACK TO MAIN MENU"
getch()
if(e==3)
Display “PRESS ONLY THE CORRESPONDING NUMBER”
Display "DO YOU WANT TO PLAY AGAIN?"
Display "1.YES\n2.NO"
Display “ENTER THE CORRESPONDING NUMBER TO YOUR CHOICE:"
m=getch()
15
T
F
17
18T
F
19
20
21
29
if(m==1)
if(m==2)
Display "PRESS 1 OR 2"
20
17T
F
F
21
Stop
CODE
FOR HEADER
#ifndef HEADER_H#define HEADER_H#include <stdio.h>#include <conio.h>#include <time.h>#include <stdlib.h>#include<windows.h>#include<string.h>
void gotoxy(int,int);void sleep(time_t);void player1();int stkr();int stkr_com();void main_single_player(void);void main_two_player(void);void error();COORD coord={0,0}; //initialize the cursor posiion to (0,0)void gotoxy(int x, int y){ coord.X=x; coord.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);//set the cursor position to corresponding address}void sleep(time_t delay)//pause the screen for specified time{ time_t timer0,timer1; time(&timer0); do { time(&timer1); } while((timer1-timer0)<delay);}int stkr(){ srand(time(NULL));//take seed from time() function int x1=((rand()) % 5); int x=x1+1;//generate random number between 1 and 5 return x;}int stkr_com(){ srand(time(NULL)); int y1=((rand()) % 5);
30
int y=y1+1; return y;}void player1(){ int a; printf("%c",' '); for (a=1;a<=78;a++) printf("%c",'_');//display upper horizontal line with '-' printf("\n"); for (a=1;a<=100;a++) printf("%c\t",'|');//display vertical lines with'|' for (a=1;a<=10;a++) { gotoxy(79,a); printf("%c",'|');//display rightmost vertical line with '|' } for (a=0;a<=79;a++) { gotoxy(a,11); printf("%c",'-');//display lower horizontal line with '-' } int j=4; for (a=1;a<=5;a++) { gotoxy(j,6); printf("%d",a);//display sticker's numbers at their centre j+=8; } int s=44; for (a=1;a<=5;a++) { gotoxy(s,6); printf("%d",a); s+=8; }}void error(){ puts("THE STICKER HAS ALREADY BEEN TURNED"); puts("ENTER OTHER UNTURNED STICKER'S NUMBER");}#endif
31
FOR SINGLE_PLAYER.C
#include "header.h"
void main_single_player(void){ int a,b,e,f=0; int count_com=0; int count_pla=0; char *p1[30]; system("cls"); printf("ENTER PLAYER 1'S NAME(don't use space): "); scanf("%s",&p1); system("cls"); player1();//display stickers gotoxy(0,22); printf("%s's score",&p1);//display heading of player1 score gotoxy(8,23); printf("%c",'0'); gotoxy(63,22); printf("Comp's score");//display heading of computer score gotoxy(71,23); printf("%c",'0'); for (b=0;b<=4;b++)//loop to turn both player's and computer's chosen sticker { int t2=stkr_com(); gotoxy(0,14); puts("\t\t\t\t\t"); int c[5]; gotoxy(0,12); printf("\t\t\t\tITS %s's TURN \n",&p1); puts("\tENTER THE STICKER'S NUMBER YOU WANT TO TURN AND PRESS ENTER:"); PLAY: gotoxy(0,14); scanf("%d",&c[b]);//store th chosen number in c[b] gotoxy(0,14); printf(" "); printf("\n "); printf("\n "); gotoxy(0,15); if(b==1 && c[1]==c[0])//if player tries to turn the sticker that has already been turned, take c[b] again { error(); goto PLAY; } else if(b==2 && (c[2]==c[0] || c[2]==c[1])) { error();
32
goto PLAY; } else if(b==3 && (c[3]==c[0] || c[3]==c[1] || c[3]==c[2])) { error(); goto PLAY; } else if(b==4 && (c[4]==c[0] || c[4]==c[1] || c[4]==c[2] || c[4]==c[3])) { error(); goto PLAY; } switch (c[b]) { case 1: case 2: case 3: case 4: case 5: { sleep(1.2); for(a=0;a<6;a++)//display the contents of the sticker { if(a<3) { gotoxy(7*c[b]+c[b]-3-a,6); printf("%c",stkr()); } else { gotoxy(7*c[b]+c[b]-a,5); printf("%c",stkr()); } } break; } default: { puts("ENTER THE NUMBER FROM 1 TO 5"); goto PLAY; break; } } int t1=stkr(); if(b>0)//for 2nd or above attempts of player1 { if (t1==t2)//if player1 and computer turns the similar sticker { gotoxy(0,18); puts("\t\t\t\tIT'S A MATCH");
33
count_pla+=1+b;//increase player1's score by number of stickers turned before the match count_com-=(b+1);//decrease computer's score by same number gotoxy(71,23); printf(" %d ",count_com); gotoxy(8,23); printf(" %d ",count_pla); } else { gotoxy(0,18); puts("\t\t\t\tNO MATCH "); } } gotoxy(0,12); printf("\t\t\t\tITS Computer's TURN \n"); gotoxy(0,13); printf("\t\t\t\t\t\t\t\t\t"); printf("\n"); int u[5]; for(;f<=4;f++) { PLAY1: srand(time(NULL)); int r1=rand()%5; u[f]=r1+1;//generate random number between 1 and 5 if(f==1 && u[1]==u[0])//if for second or above attempts, if same random number is generated {
goto PLAY1;//generate another random number } else if(f==2 && (u[2]==u[0] || u[2]==u[1])) {
goto PLAY1; } else if(f==3 && (u[3]==u[0] || u[3]==u[1] || u[3]==u[2])) { goto PLAY1; } else if(f==4 && (u[4]==u[0] || u[4]==u[1] || u[4]==u[2] || u[4]==u[3])) {
goto PLAY1; } switch(u[f]) { case 1: case 2:
34
case 3: case 4: case 5: { sleep(2.5);//pause for 2.5 seconds for(a=0;a<6;a++)//display the contents of the sticker { if(a<3) { gotoxy(7*(u[f]+5)+(u[f]+5)-3-a,6); printf("%c",stkr_com()); } else { gotoxy(7*(u[f]+5)+(u[f]+5)-a,5); printf("%c",stkr_com()); } } break; } } break; } f+=1; t2=stkr_com(); if(t2==t1) { gotoxy(0,18); puts("\t\t\t\tIT'S A MATCH"); count_com+=1+b; count_pla-=(b+1); gotoxy(8,23); printf(" %d ",count_pla); gotoxy(71,23); printf(" %d ",count_com); } else { gotoxy(0,18); puts("\t\t\t\tNO MATCH "); } if(b==4) { gotoxy(30,20); puts("PRESS ANY KEY TO VIEW THE WINNER"); getch();//take a character from user } } if(count_com>count_pla) {
35
system("cls"); gotoxy(30,10); printf("THE WINNER IS COMPUTER"); puts("\npress any key to continue....."); } else if(count_pla>count_com) { system("cls"); gotoxy(30,10); printf("THE WINNER IS %s",&p1); puts("\npress any key to continue....."); } else { system("cls"); gotoxy(30,10); printf("THE GAME ENDED IN A DRAW"); puts("\npress any key to continue....."); }getch();}
FOR TWO_PLAYER.C
void main_two_player(void){ int a,b,e,f=0; int count_com=0; int count_pla=0; char *p1[30],*p2[30]; system("cls"); printf("ENTER PLAYER 1'S NAME(don't use space): "); scanf("%s",(&p1)); printf("ENTER PLAYER 2'S NAME(don't use space): "); scanf("%s",&p2); system("cls"); player1(); gotoxy(0,22); printf("%s's score",&p1); gotoxy(8,23); printf("%c",'0'); gotoxy(63,22); printf("%s's score",&p2); gotoxy(71,23); printf("%c",'0'); for (b=0;b<=4;b++) { int t2=stkr_com(); gotoxy(0,14);
36
puts("\t\t\t\t\t"); int c[5]; gotoxy(0,12); printf("\t\t\t\tITS %s's TURN \n",&p1); puts("\tENTER THE STICKER'S NUMBER YOU WANT TO TURN AND PRESS ENTER:"); PLAY: gotoxy(0,14); scanf("%d",&c[b]); gotoxy(0,14); printf(" "); printf("\n "); printf("\n "); gotoxy(0,15); if(b==1 && c[1]==c[0]) { error(); goto PLAY; } else if(b==2 && (c[2]==c[0] || c[2]==c[1])) { error(); goto PLAY; } else if(b==3 && (c[3]==c[0] || c[3]==c[1] || c[3]==c[2])) { error(); goto PLAY; } else if(b==4 && (c[4]==c[0] || c[4]==c[1] || c[4]==c[2] || c[4]==c[3])) { error(); goto PLAY; } switch (c[b]) { case 1: case 2: case 3: case 4: case 5: { sleep(1.2); for(a=0;a<6;a++) { if(a<3) { gotoxy(7*c[b]+c[b]-3-a,6); printf("%c",stkr()); }
37
else { gotoxy(7*c[b]+c[b]-a,5); printf("%c",stkr()); } } break; } default: { puts("ENTER THE NUMBER FROM 1 TO 5"); goto PLAY; break; } } int t1=stkr(); if(b>0) { if (t1==t2) { gotoxy(0,18); puts("\t\t\t\tIT'S A MATCH"); count_pla+=1+b; count_com-=(b+1); gotoxy(71,23); printf(" %d ",count_com);//display player2's score below its heading gotoxy(8,23); printf(" %d ",count_pla);//display player1's score below its heading } else { gotoxy(0,18); puts("\t\t\t\tNO MATCH "); } } gotoxy(0,12); printf("\t\t\t\tITS %s's TURN \n",&p2); printf("\n");
int u[5]; for(;f<=4;f++) { PLAY1: gotoxy(0,14); scanf("%d",&u[f]);//store player2's choice in u[f] gotoxy(0,14); printf(" "); printf("\n "); printf("\n "); gotoxy(0,15);
38
if(f==1 && u[1]==u[0])//if for 2nd or above attempts, if player2 chooses same sticker, take another value of u[f] { error(); goto PLAY1; } else if(f==2 && (u[2]==u[0] || u[2]==u[1])) { error(); goto PLAY1; } else if(f==3 && (u[3]==u[0] || u[3]==u[1] || u[3]==u[2])) { error(); goto PLAY1; } else if(f==4 && (u[4]==u[0] || u[4]==u[1] || u[4]==u[2] || u[4]==u[3])) { error(); goto PLAY1; } switch(u[f]) { case 1: case 2: case 3: case 4: case 5: { sleep(2.5);//pause for 2.5 seconds for(a=0;a<6;a++)//display the contents of the sticker { if(a<3) { gotoxy(7*(u[f]+5)+(u[f]+5)-3-a,6); printf("%c",stkr_com()); } else { gotoxy(7*(u[f]+5)+(u[f]+5)-a,5); printf("%c",stkr_com()); } } break; } default: { puts("ENTER THE NUMBER FROM 1 TO 5"); goto PLAY1; break;
39
} } break; } f+=1; t2=stkr_com(); if(t2==t1) { gotoxy(0,18); puts("\t\t\t\tIT'S A MATCH"); count_com+=1+b; count_pla-=(b+1); gotoxy(8,23); printf(" %d ",count_pla);//display player1's score below its heading gotoxy(71,23); printf(" %d ",count_com);//display player2's score below its heading } else { gotoxy(0,18); puts("\t\t\t\tNO MATCH "); } if(b==4)//after last sticker is turned { gotoxy(30,20); puts("PRESS ANY KEY TO VIEW THE WINNER"); getch();//take a character from user and display the winner } } if(count_com>count_pla) { system("cls"); gotoxy(30,10); printf("THE WINNER IS %s",&p2); puts("\npress any key to continue....."); } else if(count_pla>count_com) { system("cls"); gotoxy(30,10); printf("THE WINNER IS %s",&p1); puts("\npress any key to continue....."); } else { system("cls"); gotoxy(30,10); printf("THE GAME ENDED IN A DRAW"); puts("\npress any key to continue....."); }
40
getch();}
FOR MAIN
#include "header.h"#include "single_player.c"#include "two_player.c"int main(){ int m; char e,k; PLAY0: gotoxy(30,5); puts("MAIN MENU"); gotoxy(28,6); printf("%s","*************"); gotoxy(30,10); puts("1.PLAY GAME");//display options gotoxy(30,11); puts("2.HOW TO PLAY");//display options gotoxy(30,12); puts("3.EXIT");//display options printf("\n\n"); printf("\t\tENTER THE CORRESPONDING NUMBER TO YOUR CHOICE:"); e=getch(); switch(e) { case '1': { system("cls"); gotoxy(30,10); puts("1.PLAY VERSUS HUMAN"); gotoxy(30,11); puts("2.PLAY VERSUS COMPUTER"); printf("\n\n"); printf("\t\tENTER THE CORRESPONDING NUMBER TO YOUR CHOICE:"); k=getch(); switch(k) { case '1': { main_two_player();//call the function for two players break; } case '2': { main_single_player();//call the function for single player break;
41
} default: { puts("PRESS ONLY THE CORRESPONDING NUMBER"); break; } } break; } case '2'://display rule { system("cls"); printf("\n\nTHERE ARE A TOTAL OF TEN STICKERS, FIVE YOURS AND FIVE OTHER PLAYER'S (OR COMPUTER'S), TURNED IN SUCH A WAY THAT ITS BACK PORTION FACES US. TURN ANY STICKER AS YOU DESIRE BY PRESSING THE NUMBER PRINTED ON ITS BACK. THEN OTHER PLAYER (OR COMPUTER) TURNS ANOTHER STICKER. IF BOTH THE STICKERS MATCH, THE PLAYER WHO TURNS THE STICKER LATER, GETS POINTS WHICH IS EQUAL TO THE MAXIMUM NUMBER OF STICKERS TURNED TILL THAT MATCH. MOREOVER, ANOTHER PLAYER GETS THE SAME NUMBER OF NEGATIVE POINTS. PLAYER WITH HIGHER SCORE AFTER THE TURN OF ALL TEN STICKERS, WINS."); puts("\n\nPRESS ANY KEY TO GO BACK TO MAIN MENU"); getch(); system("cls"); goto PLAY0; break; } case '3': exit(1);//goto the end of the program default: { puts("\n\t\tPRESS ONLY THE CORRESPONDING NUMBER"); goto PLAY0; break; } } system("cls"); puts("DO YOU WANT TO PLAY AGAIN?"); puts("1.YES"); puts("2.NO"); PLAY3: printf("\t\tENTER THE CORRESPONDING NUMBER TO YOUR CHOICE:"); m=getch(); switch(m) { case '1': system("cls"); goto PLAY0;//start a new game break;
42
case '2': break; default: puts("PRESS 1 OR 2"); goto PLAY3; }getch();return 0;}
COMPILING, TESTING AND DEBUGGING
The program generated the output without any error.
OUTPUT SNAPSHOTS
43
44
45
46
DISCUSSION AND CONCLUSION
Thus the objective of using all the concepts of C is nearly accomplished. Structure can also be used in the program by some replacements. Many bugs can be pointed out in this game as we, the developers are just the beginners. By giving a long name in the game, the game window may look unattractive. The use of graphics library could have made the game look even more attractive, but we chose to be confined within the syllabus.
In conclusion, we still want to emphasize that the program is not complete by itself. There is still a lot of room for improvement. Graphics may be added to program to make it more attractive. The mouse cursor may be initialized in order to make the program even more interactive.
This is an open source program and therefore everybody is welcome to develop it. Future developers are very welcome to add their ideas to the program and improvise it.
Hopefully, all the users of this program will find it encouraging.
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101