Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <Windows.h>
- #include <string.h>
- #include <time.h>
- #include <conio.h>
- #define WALL 178
- #define EMPTY ' '
- #define FOOD '*'
- #define R (*root)
- void cls();
- void printSpace(int lvl);
- void printSquare();
- void gotoXY(int x, int y);
- char snakeMaster[10][200];
- void snakeMasterLogo()
- {
- strcpy(snakeMaster[0]," _____ _ _ _ ________ __ __ _____ _______ ______ _____");
- strcpy(snakeMaster[1]," / ____| \\ | | /\\ | |/ / ____| | \\/ | /\\ / ____|__ __| ____| __ \\");
- strcpy(snakeMaster[2]," | (___ | \\| | / \\ | ' /| |__ | \\ / | / \\ | (___ | | | |__ | |__) |");
- strcpy(snakeMaster[3]," \\___ \\| . ` | / /\\ \\ | < | __| | |\\/| | / /\\ \\ \\___ \\ | | | __| | _ /");
- strcpy(snakeMaster[4]," ____) | |\\ |/ ____ \\| . \\| |____ | | | |/ ____ \\ ____) | | | | |____| | \\ \\");
- strcpy(snakeMaster[5]," |_____/|_| \\_/_/ \\_\\_|\\_\\______| |_| |_/_/ \\_\\_____/ |_| |______|_| \\_\\");
- };
- void printLogoAwal()
- {
- int y = 10;
- gotoXY(20,y);
- for(int a = 0;a<6;a++)
- {
- for(int b = 0;b<150;b++)
- {
- printf("%c",snakeMaster[a][b]);
- Sleep(10);
- }
- y++;
- gotoXY(20,y);
- }
- printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
- puts("Press enter to continue...");
- getchar();
- }
- void printLogoMenu()
- {
- int y = 10;
- gotoXY(20,y);
- for(int a = 0;a<6;a++)
- {
- for(int b = 0;b<150;b++)
- {
- printf("%c",snakeMaster[a][b]);
- }
- y++;
- gotoXY(20,y);
- }
- printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
- }
- //UserData
- typedef struct User
- {
- char username[100];
- char password[100];
- char status[100];
- int score;
- User*right,*left;
- }User;
- User *root;
- void insertUser(User **root,char name[100],char password[100],int score,char status[]);
- User* searchUser(User *root,char username[100]);
- void deleteUser(User **root, char name[100]);
- User* signIn();
- void loadUser();
- void resetFile();
- void saveUser(User *root);
- void insertNewUser();
- void deleteExistingUser(User *userSekarang);
- void printInOrder(User *root,int lvl);
- int memberMenu();
- int adminMenu();
- int masterMenu();
- //Game Snake
- char map[40][120];
- struct Node
- {
- int x;
- int y;
- Node* next,*prev;
- }*head,*tail,*curr;
- void initMap();
- void printMap();
- void initNode(int x, int y);
- void pushHead(int x, int y);
- void pushTail(int x, int y);
- void popTail();
- int snakeGame(int userScore);
- int main()
- {
- snakeMasterLogo();
- printLogoAwal();
- loadUser();
- system("cls");
- cls();
- User *user = signIn();
- bool isMember = false;
- bool isAdmin = false;
- bool loop = true;
- int choice;
- if(strcmp(user->status,"admin") == 0)
- {
- isAdmin = true;
- }
- else
- {
- isMember = true;
- }
- do
- {
- system("cls");
- printLogoMenu();
- printf("Welcome,%s\n",user->username);
- puts("==============================");
- if(isMember)
- {
- choice = memberMenu();
- }
- else if(isAdmin)
- {
- choice = adminMenu();
- }
- switch(choice)
- {
- case 1:
- int newScore;
- newScore = snakeGame(user->score);
- if(newScore>user->score)
- {
- user->score = newScore;
- }
- break;
- case 2:
- system("cls");
- cls();
- printInOrder(root,0);
- getchar();
- break;
- case 3:
- if(isMember)loop = false;
- else
- {
- system("cls");
- cls();
- printInOrder(root,0);
- do
- {
- choice = masterMenu();
- switch(choice)
- {
- case 1:
- insertNewUser();
- break;
- case 2:
- deleteExistingUser(user);
- break;
- }
- }while(choice !=3);
- }
- break;
- case 4:
- loop = false;
- break;
- }
- }while(loop == true);
- resetFile();
- saveUser(root);
- return 0;
- }
- void cls()
- {
- printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
- }
- void gotoXY(int x, int y)
- {
- COORD cord;
- cord.X = x;
- cord.Y = y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cord);
- }
- void printSpace(int lvl)
- {
- for(int a = 0;a<lvl;a++)
- {
- printf(" ");
- }
- }
- void printSquare()
- {
- for(int a = 0;a<28;a++)
- {
- printf("%c",178);
- }
- printf("\n");
- }
- //Game
- void initMap()
- {
- for(int a = 0;a<40;a++)
- {
- for(int b = 0;b<120;b++)
- {
- if(a==0 || a==39 || b == 0 || b==119)
- {
- map[a][b] = WALL;
- }
- else
- {
- map[a][b] = EMPTY;
- }
- }
- }
- }
- void printMap()
- {
- gotoXY(20,5);
- for(int a = 0;a<40;a++)
- {
- for(int b = 0;b<120;b++)
- {
- printf("%c",map[a][b]);
- }
- gotoXY(20,a+6);
- }
- }
- void initNode(int x, int y)
- {
- curr = (Node*)malloc(sizeof(Node));
- curr->x = x;
- curr->y = y;
- curr->next = curr->prev = NULL;
- }
- void pushHead(int x, int y)
- {
- initNode(x,y);
- if(!head)
- {
- head = tail = curr;
- }
- else
- {
- curr->next = head;
- head->prev = curr;
- head = curr;
- }
- }
- void pushTail(int x, int y)
- {
- initNode(x,y);
- curr->prev = tail;
- tail->next = curr;
- tail = curr;
- }
- void popTail()
- {
- curr = tail;
- tail = curr->prev;
- tail->next = NULL;
- free(curr);
- }
- int snakeGame(int userScore)
- {
- system("cls");
- srand(time(NULL));
- initMap();
- printMap();
- bool isAlive = true;
- int headX,headY;
- int score = 0;
- headX = 20;headY = 10;
- int directionX = 1;
- int directionY = 0;
- char key = NULL;
- pushHead(headX,headY);
- map[headY][headX] = WALL;
- gotoXY(20+headX,5+headY);
- printf("%c",map[headY][headX]);
- int randomX;
- int randomY;
- do
- {
- randomX = rand()%118+1;
- randomY = rand()%38+1;
- }while(randomX == headX+directionX && randomY== headY+directionY);
- map[randomY][randomX] = FOOD;
- gotoXY(20+randomX,5+randomY);
- printf("%c",map[randomY][randomX]);
- while(isAlive == true)
- {
- Sleep(30);
- if(kbhit())
- {
- key = getch();
- }
- switch(key)
- {
- case 'w'://ATAS
- if(directionY != 1)
- {
- directionY= -1;
- directionX = 0;
- }
- break;
- case 's'://BAWAH
- if(directionY != -1)
- {
- directionY= 1;
- directionX = 0;
- }
- break;
- case 'd'://KANAN
- if(directionX != -1)
- {
- directionX = 1;
- directionY = 0;
- }
- break;
- case 'a'://KIRI
- if(directionX != 1)
- {
- directionX= -1;
- directionY = 0;
- }
- break;
- }
- if(map[headY+directionY][headX+directionX] == EMPTY)
- {
- pushHead(headX+directionX,headY+directionY);
- map[headY+directionY][headX+directionX] = WALL;
- gotoXY(20+headX+directionX,5+headY+directionY);
- printf("%c",map[headY+directionY][headX+directionX]);
- map[tail->y][tail->x] = EMPTY;
- gotoXY(20+tail->x,5+tail->y);
- printf("%c",map[tail->y][tail->x]);
- popTail();
- }
- else if(map[headY+directionY][headX+directionX] == FOOD)
- {
- pushHead(headX+directionX,headY+directionY);
- map[headY+directionY][headX+directionX] = WALL;
- gotoXY(20+headX+directionX,5+headY+directionY);
- printf("%c",map[headY+directionY][headX+directionX]);
- int randomX;
- int randomY;
- do
- {
- randomX = rand()%118+1;
- randomY = rand()%38+1;
- }while(randomX == headX+directionX && randomY== headY+directionY);
- map[randomY][randomX] = FOOD;
- gotoXY(20+randomX,5+randomY);
- printf("%c",map[randomY][randomX]);
- score +=1;
- gotoXY(20+0,5+42);
- printf("Score : %d",score);
- }
- else
- {
- isAlive = false;
- gotoXY(20+0,5+42);
- printf(" ");
- gotoXY(20+0,5+42);
- printf("Game Over");
- if(score>userScore)
- {
- gotoXY(20+0,6+42);
- printf("New High Score");
- }
- getchar();
- break;
- }
- headY +=directionY;
- headX +=directionX;
- }
- return score;
- }
- // User and Menu
- void loadUser()
- {
- FILE *fp = fopen("user.txt","r");
- if(fp)
- {
- User *temp = (User*)malloc(sizeof(User));
- while(!feof(fp))
- {
- fscanf(fp,"%[^|]|%[^|]|%d|%[^\n]\n",temp->username,temp->password,&temp->score,temp->status);
- insertUser(&root,temp->username,temp->password,temp->score,temp->status);
- }
- fclose(fp);
- }
- else
- {
- printf("File not found");
- }
- }
- void resetFile()
- {
- FILE *fp = fopen("user.txt","w");
- if(fp)
- {
- fprintf(fp,"");
- fclose(fp);
- }
- }
- void saveUser(User *root)
- {
- FILE *fp = fopen("user.txt","a");
- if(fp)
- {
- if(!root)
- {
- return;
- }
- fprintf(fp,"%s|%s|%d|%s\n",root->username,root->password,root->score,root->status);
- fclose(fp);
- saveUser(root->left);
- saveUser(root->right);
- }
- }
- void insertUser(User **root,char name[100],char password[100],int score,char status[])
- {
- if(! R)
- {
- User *temp = (User*)malloc(sizeof(User));
- strcpy(temp->username,name);
- strcpy(temp->password,password);
- temp->score = score;
- strcpy(temp->status,status);
- temp->left = temp->right = NULL;
- R = temp;
- }
- else if(strcmp(name,R->username)<0)
- {
- insertUser(&R->left,name,password,score,status);
- }
- else if(strcmp(name,R->username)>0)
- {
- insertUser(&R->right,name,password,score,status);
- }
- }
- User* searchUser(User *root,char username[100])
- {
- if(!root)
- {
- return NULL;
- }
- if(strcmp(username,root->username)<0)
- {
- return searchUser(root->left,username);
- }
- if(strcmp(username,root->username)>0)
- {
- return searchUser(root->right,username);
- }
- if(strcmp(username,root->username)==0)
- {
- return root;
- }
- }
- void printInOrder(User *root,int lvl)
- {
- if(!root)
- {
- return;
- }
- printInOrder(root->left,lvl+1);
- if(lvl >0)
- {
- printSpace(lvl);printf(" ");printSquare();
- printSpace(lvl);printf(" %c%c%c%c%c%c%c%c% 20s %c%3d %c\n",178,178,178,178,178,178,178,178,root->username,178,root->score,178);
- printSpace(lvl);printf(" ");printSquare();
- puts("");
- }
- else
- {
- printSquare();
- printf("%c%20s %c%3d %c\n",178,root->username,178,root->score,178);
- printSquare();
- puts("");
- }
- printInOrder(root->right,lvl+1);
- }
- User* signIn()
- {
- bool ada = false;
- char username[100];
- char password[100];
- do
- {
- printf("Input your username : ");
- gets(username);fflush(stdin);
- printf("Input your password : ");
- gets(password);fflush(stdin);
- User *valid = NULL;
- valid = searchUser(root,username);
- if(valid)
- {
- if(strcmp(valid->username,username)==0 && strcmp(valid->password,password)==0)
- {
- ada = true;
- return valid;
- }
- }
- if(ada == false)
- {
- puts("Invalid Username / Password\n\n\n");
- }
- }while(ada == false);
- }
- int memberMenu()
- {
- int choice;
- puts("1. Play Snake");
- puts("2. View Score");
- puts("3. Save and Exit");
- do
- {
- printf("Choose:");
- scanf("%d",&choice);fflush(stdin);
- }while(choice<1 || choice>3);
- return choice;
- }
- int adminMenu()
- {
- int choice;
- puts("1. Play Snake");
- puts("2. View Score");
- puts("3. Master User");
- puts("4. Save and Exit");
- do
- {
- printf("Choose:");
- scanf("%d",&choice);fflush(stdin);
- }while(choice<1 || choice>4);
- return choice;
- }
- int masterMenu()
- {
- system("cls");
- cls();
- printInOrder(root,0);
- puts("\n\nMASTER USER");
- puts("===========");
- puts("1. Insert New User");
- puts("2. Delete User");
- puts("3. Back");
- int choice;
- do
- {
- printf("Choose : ");
- scanf("%d",&choice);fflush(stdin);
- }while(choice<1 || choice>3);
- return choice;
- }
- void deleteUser(User** root,char name[100])
- {
- if(R==NULL)
- {
- return;
- }
- else if(strcmp(name,R->username) < 0)
- {
- deleteUser(&R->left,name);
- }
- else if(strcmp(name,R->username) > 0)
- {
- deleteUser(&R->right,name);
- }
- else
- {
- User *temp;
- if(!R->right)
- {
- temp = R;
- R = R->left;
- free(temp);
- }
- else if(!R->left)
- {
- temp = R;
- R = R->right;
- free(temp);
- }
- else
- {
- temp = R->right;
- while(temp->left)
- {
- temp = temp->left;
- }
- strcpy(R->username,temp->username);
- strcpy(R->password,temp->password);
- R->score = temp->score;
- strcpy(R->status,temp->status);
- deleteUser(&R->right,temp->username);
- }
- }
- }
- void insertNewUser()
- {
- system("cls");
- cls();
- printInOrder(root,0);
- char username[21];
- char password[20];
- char type[10];
- int len;
- bool valid;
- do
- {
- printf("Input Username[3-20]: ");
- gets(username);fflush(stdin);
- len = strlen(username);
- }while(len<3 || len>20);
- do
- {
- valid = false;
- printf("Input Password[substring of username]: ");
- gets(password);fflush(stdin);
- if(strstr(username,password)!= NULL)
- {
- valid = true;
- }
- else
- {
- puts("password must be a substring of the inputted username");
- }
- }while(valid == false);
- do
- {
- valid = false;
- printf("Input Type[admin|member]: ");
- gets(type);fflush(stdin);
- if(strcmp(type,"admin")==0 || strcmp(type,"member")==0)
- {
- valid = true;
- }
- }while(valid == false);
- insertUser(&root,username,password,0,type);
- puts("New User Inserted Successfully");
- getchar();
- }
- void deleteExistingUser(User *userSekarang)
- {
- system("cls");
- cls();
- printInOrder(root,0);
- puts("\n\n");
- char username[100];
- printf("Input Username to be deleted: ");
- gets(username);
- User *temp = searchUser(root,username);
- if(!temp)
- {
- printf("User %d not found",username);
- }
- else if(strcmp(userSekarang->username,username)==0)
- {
- puts("You cannot delete yourself");
- getchar();
- }
- else
- {
- deleteUser(&root,username);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement