Você está na página 1de 10

#include <iostream.

h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <iostream.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define TOP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4

void score(int s=1)


{
static unsigned long sc=0;
window(70,22,78,23);
if(s)
{
sc=sc+100;
cprintf(" ");
window(70,22,78,23);
cprintf("%ld",sc);
}
else
sc=0;
}
void status(char *s,int c=WHITE)
{
window(70,23,78,25);
textcolor(c);
cprintf("%s",s);
for(int x=strlen(s);x<7;x++)
cprintf(" ");
textcolor(WHITE);
}
// tic tac toe

char square[10] = {'o','1','2','3','4','5','6','7','8','9'};

int checkwin();
void board();
int checkwin()
{
if (square[1] == square[2] && square[2] == square[3])

return 1;
else if (square[4] == square[5] && square[5] == square[6])

return 1;
else if (square[7] == square[8] && square[8] == square[9])

return 1;
else if (square[1] == square[4] && square[4] == square[7])

return 1;
else if (square[2] == square[5] && square[5] == square[8])

return 1;
else if (square[3] == square[6] && square[6] == square[9])

return 1;
else if (square[1] == square[5] && square[5] == square[9])

return 1;
else if (square[3] == square[5] && square[5] == square[7])

return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
&& square[4] != '4' && square[5] != '5' && square[6] != '6'
&& square[7] != '7' && square[8] != '8' && square[9] != '9')

return 0;
else
return -1;
}

/*******************************************************************
FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
********************************************************************/

void board()
{
clrscr();
cout << "\n\n\tTic Tac Toe\n\n";

cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
cout << endl;

cout << " | | " << endl;


cout << " " << square[1] << " | " << square[2] << " | " << square[3] <<
endl;

cout << "_____|_____|_____" << endl;


cout << " | | " << endl;

cout << " " << square[4] << " | " << square[5] << " | " << square[6] <<
endl;

cout << "_____|_____|_____" << endl;


cout << " | | " << endl;

cout << " " << square[7] << " | " << square[8] << " | " << square[9] <<
endl;

cout << " | | " << endl << endl;


}
// hangman

const int MAXLENGTH=80;


const int MAX_TRIES=5;
const int MAXROW=7;
int letterFill (char, char[], char[]);
void initUnknown (char[], char[]);

//choose and copy a word from array of words randomly


randomize();
int n=random(10);
strcpy(word,words[n]);

// Initialize the secret word with the * character.


initUnknown(word, unknown);

// welcome the user


cout << "\n\nWelcome to hangman...Guess a country Name";
cout << "\n\nEach letter is represented by a star.";
cout << "\n\nYou have to type only one letter in one try";
cout << "\n\nYou have " << MAX_TRIES << " tries to try and guess the word.";
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

// Loop until the guesses are used up


while (num_of_wrong_guesses < MAX_TRIES)
{
cout << "\n\n" << unknown;
cout << "\n\nGuess a letter: ";
cin >> letter;
// Fill secret word with letter if the guess is correct,
// otherwise increment the number of wrong guesses.
if (letterFill(letter, word, unknown)==0)
{
cout << endl << "Whoops! That letter isn't in there!" << endl;
num_of_wrong_guesses++;
}
else
{
cout << endl << "You found a letter! Isn't that exciting!" <<
endl;
}
// Tell user how many guesses has left.
cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
cout << " guesses left." << endl;
// Check if they guessed the word.
if (strcmp(word, unknown) == 0)
{
cout << word << endl;
cout << "Yeah! You got it!";
break;
}
}
if(num_of_wrong_guesses == MAX_TRIES)
{
cout << "\nSorry, you lose...you've been hanged." << endl;
cout << "The word was : " << word << endl;
}
getch();
return 0;
}

/* Take a one character guess and the secret word, and fill in the
unfinished guessword. Returns number of characters matched.
Also, returns zero if the character is already guessed. */
int letterFill (char guess, char secretword[], char guessword[])
{
int i;
int matches=0;
for (i = 0; secretword[i]!='\0'; i++)
{
// Did we already match this letter in a previous guess?
if (guess == guessword[i])
return 0;
// Is the guess in the secret word?
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}

// Initialize the unknown word

void initUnknown (char word[], char unknown[])


{
int i;
int length = strlen(word);
for (i = 0; i < length; i++)
unknown[i]='*';
unknown[i]='\0';
}
void main();
{
cout<<" welcome to gaming zone
"<<endl<<endl<<endl;
cout<<" 1. snake ";
cout<<" 2. tic tac toe ";
cout<<" 3. hangman ";
cin>>x;
if (int x=1)
{
void main()
{
clrscr();
_setcursortype(_NOCURSOR);
textcolor(WHITE);
cprintf("\n
�������������������������������������������������������������������������Ŀ ");
for(int i=0;i<17;i++)
cprintf(" �
� ");
cprintf("
��������������������������������������������������������������������������� ");
cprintf(" ����������������������Ŀ Press 'x' to Exit
�����������������Ŀ ");
cprintf(" � S N A K E G A M E � Press 'p' to Pause and Play � SCORE :
0 � ");
cprintf(" ������������������������ � STATUS:
Playing � ");
cprintf("
������������������� ");
int snkx[20],snky[20],d=LEFT,p=0,sz=6,restart=1;
while(1)
{
if(restart)
{
for(i=0;i<6;i++)
snkx[i]=30+i*2;
for(i=6;i<20;i++)
snkx[i]=0;
for(i=0;i<20;i++)
snky[i]=9;
sz=6;
p=0;
d=LEFT;
restart=0;
score();
window(6,3,76,19);
for(i=0;i<610;i++)
cprintf(" ");
}
int x,y;
if(p!=1)
{
randomize();
do{
x=random(70)+6;
}while(x%2==1);
y=random(16)+3;
p=1;
window(x,y,x+1,y+1);
textcolor(LIGHTGREEN);
cprintf("%c",1);
textcolor(WHITE);
}
while(!kbhit())
{
status("Playing",LIGHTGREEN);
window(snkx[sz-1],snky[sz-1],snkx[sz-1]+2,snky[sz-1]+2);
cprintf(" ");
for(i=0;i<sz-1;i++)
{
window(snkx[i],snky[i],snkx[i]+1,snky[i]+1);
cprintf("#");
}
for(i=sz-1;i>=1;i--)
{
snkx[i]=snkx[i-1];
snky[i]=snky[i-1];
}
switch(d)
{
case RIGHT : snkx[0]+=2; break;
case LEFT : snkx[0]-=2; break;
case TOP : snky[0]-=1; break;
case DOWN : snky[0]+=1;
}
delay(100);
if(x==snkx[0] && y==snky[0])
{
int f=0;
do{
do{
x=random(70)+6;
}while(x%2==1);
y=random(16)+3;
for(i=0;i<sz;i++)
if(snkx[i]==x && snky[i]==y)
f=1;
}while(f);
p=1;
window(x,y,x+1,y+1);
textcolor(LIGHTGREEN);
cprintf("%c",1);
textcolor(WHITE);

score();
sz++;

switch(d)
{
case RIGHT : snkx[sz-1]+=2;
snky[sz-1]=snky[sz-2];
break;
case LEFT : snkx[sz-1]-=2;
snky[sz-1]=snky[sz-2];
break;
case TOP : snky[sz-1]-=1;
snkx[sz-1]=snkx[sz-2];
break;
case DOWN : snky[sz-1]+=1;
snkx[sz-1]=snkx[sz-2];
}
}
int sflag=0;
for(i=0;i<sz-1;i++)
if(snkx[sz-1]==snkx[i] && snky[sz-1]==snky[i])
{
sflag=1;
break;
}
if(snkx[0]<5 || snkx[0]>76 || snky[0]<3 || snky[0]>19)
sflag=1;
if(sflag)
{
textcolor(LIGHTRED);
for(i=1;i<sz;i++)
{
window(snkx[i],snky[i],snkx[i]+1,snky[i]+1);
cprintf("#");
}
status("Stopped",LIGHTRED);
score(0);
restart=1;
break;
}
}
char ch=getch();
switch(tolower(ch))
{
case 'x' : return;
case 'p' :
status("Paused");
getch();
break;
case 0 : {
char chh=getch();
switch(chh)
{
case 72 :{
if(d==LEFT || d== RIGHT)
d=TOP;
} break;
case 80 :{
if(d==LEFT || d== RIGHT)
d=DOWN;
} break;
case 75 :{
if(d==TOP || d==DOWN)
d=LEFT;
} break;
case 77 :{
if(d==TOP || d==DOWN)
d=RIGHT;
}
}
} break;
}
}

// Project ends here

else if(x==2)
{
int main()
{
int player = 1,i,choice;

char mark;
do
{
board();
player=(player%2)?1:2; //This line of code will set player to 1 if
player originally was odd,
and to 2 if it was even.

cout << "Player " << player << ", enter a number: ";
cin >> choice;
mark=(player == 1) ? 'X' : 'O';
/*boolean expression ? a : b;

If this expression is true, then a else b


It's often used as the
right-hand expression in assignment operators. In your case player is being
assigned 1 or 2 based on whether they are even or odd.*/

if (choice == 1 && square[1] == '1')

square[1] = mark;
else if (choice == 2 && square[2] == '2')

square[2] = mark;
else if (choice == 3 && square[3] == '3')

square[3] = mark;
else if (choice == 4 && square[4] == '4')

square[4] = mark;
else if (choice == 5 && square[5] == '5')

square[5] = mark;
else if (choice == 6 && square[6] == '6')

square[6] = mark;
else if (choice == 7 && square[7] == '7')

square[7] = mark;
else if (choice == 8 && square[8] == '8')

square[8] = mark;
else if (choice == 9 && square[9] == '9')

square[9] = mark;
else
{
cout<<"Invalid move ";

player--;
cin.ignore();
cin.get();
}
i=checkwin();

player++;
}while(i==-1);
board();
if(i==1)

cout<<"==>\aPlayer "<<--player<<" win ";


else
cout<<"==>\aGame draw";

cin.ignore();
cin.get();
return 0;
}
}
else if(x==3)
{
int main ()
{ clrscr();
cout<<"HINT:The countries are india, pakistan, nepal, indonesia, ethopia,
phillipines,malaysia,australia,iran, oman" ;
char unknown [MAXLENGTH];
char letter;
int num_of_wrong_guesses=0;
char word[MAXLENGTH];
char words[][MAXLENGTH] =
{
"india",
"pakistan",
"nepal",
"malaysia",
"philippines",
"australia",
"iran",
"ethiopia",
"oman",
"indonesia"
};

//choose and copy a word from array of words randomly


randomize();
int n=random(10);
strcpy(word,words[n]);

// Initialize the secret word with the * character.


initUnknown(word, unknown);

// welcome the user


cout << "\n\nWelcome to hangman...Guess a country Name";
cout << "\n\nEach letter is represented by a star.";
cout << "\n\nYou have to type only one letter in one try";
cout << "\n\nYou have " << MAX_TRIES << " tries to try and guess the word.";
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

// Loop until the guesses are used up


while (num_of_wrong_guesses < MAX_TRIES)
{
cout << "\n\n" << unknown;
cout << "\n\nGuess a letter: ";
cin >> letter;
// Fill secret word with letter if the guess is correct,
// otherwise increment the number of wrong guesses.
if (letterFill(letter, word, unknown)==0)
{
cout << endl << "Whoops! That letter isn't in there!" << endl;
num_of_wrong_guesses++;
}
else
{
cout << endl << "You found a letter! Isn't that exciting!" <<
endl;
}
// Tell user how many guesses has left.
cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
cout << " guesses left." << endl;
// Check if they guessed the word.
if (strcmp(word, unknown) == 0)
{
cout << word << endl;
cout << "Yeah! You got it!";
break;
}
}
if(num_of_wrong_guesses == MAX_TRIES)
{
cout << "\nSorry, you lose...you've been hanged." << endl;
cout << "The word was : " << word << endl;
}
getch();
return 0;
}

/* Take a one character guess and the secret word, and fill in the
unfinished guessword. Returns number of characters matched.
Also, returns zero if the character is already guessed. */

int letterFill (char guess, char secretword[], char guessword[])


{
int i;
int matches=0;
for (i = 0; secretword[i]!='\0'; i++)
{
// Did we already match this letter in a previous guess?
if (guess == guessword[i])
return 0;
// Is the guess in the secret word?
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}

else
cout<<"incorrect choice";
getch();
}

Você também pode gostar