User:Buibanakrb1998
- include <windows.h>
- include <stdio.h>
- include <stdlib.h>
- include <time.h>
- include <stdio.h>
- include <time.h>
- include <stdlib.h>
- include <conio.h>
- include <time.h>
- include <ctype.h>
- include <time.h>
- include <windows.h>
- include <process.h>
- include <unistd.h>
- include <dos.h>
//#include<conio.h> int splitnumber_rand[4]; int splitnumber_in[4]; int targetnumber; int number_correct; int HIT=0; int BLOW=0; void gotoxy(int x, int y) {
COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
} void Print() {
gotoxy(8,12); printf("\tWELCOM TO GAME GUESSNUMBER.(press any key to continue)\n"); gotoxy(40,13); getch(); system("cls"); printf("\tGame instructions:\n"); printf("\n-> Use arrow keys to move the snake.\n\n-> You will be provided foods at the several coordinates of the screen which you have to eat. Everytime you eat a food the length of the snake will be increased by 1 element and thus the score.\n\n-> Here you are provided with three lives. Your life will decrease as you hit the wall or snake's body.\n\n-> YOu can pause the game in its middle by pressing any key. To continue the paused game press any other key once again\n\n-> If you want to exit press esc. \n"); printf("\n\nPress any key to play game..."); if(getch()==27) exit(0);
} void sleep(int mseconds) {
clock_t goal = mseconds + clock(); while (goal > clock());
}
void load(){
int row,col,r,c,q; gotoxy(41,14); printf("loading..."); gotoxy(35,15); for(r=1;r<=20;r++){ sleep(200);//to display the character slowly printf("%c",177);} //getch(); //exit(0);
} int check_different(int x,int m1[4]) { m1[3]=x%10; m1[2]=(x/10)%10; m1[1]=(x/100)%10; m1[0]=(x/1000);
if(m1[0]!=m1[1]&&m1[0]!=m1[2]
&&m1[0]!=m1[3]&&m1[1]!=m1[2]&& m1[1]!=m1[3]&&m1[2]!=m1[3]) { return 1; } else { return 0; } } void random_and_check(void) { while(1) { int target; target = 1000 + rand() % (9999 + 1 - 1000);
if(check_different(target,splitnumber_rand)) { targetnumber=target; break; }
} for (int i=0;i<4;i++) { //printf("%d",splitnumber_rand[i]); } gotoxy(0,0); printf (" %d\n",targetnumber); } void draw(void) { for(int i = 1; i <= 33; i++) {
for(int k = 1 ; k <= 92; k++){ if(i == 1 || k == 1 || i == 33 || k == 92){ printf("*"); } else{ printf(" "); } } printf("\n"); }
} char* NhapChuoi (const int k) { char *kq=new char[k+1]; // t?o m?ng int n,index=0; do { loop:; n=getch(); // lay ma ASCII //if ( ( (n>='0' && n<='9') || (n>='A' && n<='Z') || (n>='a' && n<='z') || n==' ' || n=='.' ) && index<k) // Neu thuoc 0-9, A-Z, a-z ho?c d?u . ho?c kho?ng tr?ng && chu?i dang có ít hon k ký t? thì cho phép if((n>='0' && n<='9') && index<k) {
printf("%c",char(n));
kq[index++]=n; // Luu vào m?ng
} else if (n=='\b' && index>0) // N?u nh?n d?u BackSpace mà chu?i không r?ng {
printf("\b \b"); // Xóa 1 ký t? trên màn hình
kq[--index]=0; // Xóa 1 ký t? trong chu?i kq } if (n==13 && index==0) goto loop; // N?u nh?n Enter mà chua nh?p gì thì k cho phép } while (n!=13); // C? l?p l?i trong khi chua nh?p Enter
kq[index]=0; // K?t thúc chu?i number_correct = atoi(kq); //return kq; } void input_and_check(void) { char term; gotoxy(0,1); draw(); while(1) {
gotoxy(35,4); printf("Please! input your number\n"); gotoxy(46,5); fflush(stdin); NhapChuoi(4); if(check_different(number_correct,splitnumber_in)==0||number_correct<1000) { //continue; //printf("%d",num); char s2[]="Sorry, Please! input again your number"; gotoxy(31,10);
printf("Sorry, Please! input again your number");
gotoxy(31,10); sleep(2000); for(int r=0;r<=strlen(s2);r++) {
printf("%c",32);
} gotoxy(46,5);
for(int r=0;r<=3;r++)
{
printf("%c",32);
} } else {
//printf("The number is in the correct format, Thank you!\n"); //num_correct=num; for(int i=0;i<=3;i++) { for(int j=0;j<=3;j++) { if((splitnumber_rand[i] == splitnumber_in[j])&& i==j) { HIT++;
} else if((splitnumber_rand[i] == splitnumber_in[j])&& i!=j) { BLOW++; } } } gotoxy(43,7);
printf("HIT = %d",HIT); gotoxy(43,8); printf("BLOW= %d",BLOW); //draw(); if(HIT<4) { HIT=0; BLOW=0; gotoxy(35,13); printf("Do you want to try again?"); gotoxy(35,14); printf("if yes please press 'y'"); gotoxy(35,15); printf("if no please press 'n'"); gotoxy(45,16); int n; n=getch(); if(n==121) { gotoxy(46,5); for(int r=0;r<=3;r++)
{
printf("%c",32);
}
continue;
} else if(n==110)
{ //printf("%c",s1[0]); // break; exit(0);
}
}
else if(HIT == 4)
{
gotoxy(43,10);
printf("YOU WIN");
sleep(300);
HIT=0;
BLOW=0; gotoxy(35,13); printf("Do you want to play again?"); gotoxy(35,14); printf("if yes please press 'y'"); gotoxy(35,15); printf("if no please press 'n'"); gotoxy(45,16); int n; n=getch(); if(n==121) { int e=20; gotoxy(46,5); for(int r=0;r<=3;r++)
{
printf("%c",32);
} gotoxy(43,10);
for(int r=0;r<=6;r++)
{
printf("%c",32);
} random_and_check();
continue;
} else if(n==110)
{ //printf("%c",s1[0]); //break; exit(0);
} }
} }
} int main (void) { srand((int)time(0)); HANDLE hConsoleColor;
hConsoleColor = GetStdHandle(STD_OUTPUT_HANDLE); HWND console = GetConsoleWindow(); RECT r; GetWindowRect(console, &r); //stores the console's current dimensions MoveWindow(console, r.left, r.top, 700, 600, TRUE); // 800 width, 100 height SetConsoleTextAttribute(hConsoleColor, 10); Print(); system("cls"); load(); system("cls"); random_and_check(); input_and_check();
}