Advertisement
Guest User

Untitled

a guest
Apr 14th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <Windows.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <conio.h>
  7. #define WALL 178
  8. #define EMPTY ' '
  9. #define FOOD '*'
  10. #define R (*root)
  11.  
  12. void cls();
  13.  
  14. void printSpace(int lvl);
  15.  
  16. void printSquare();
  17.  
  18. void gotoXY(int x, int y);
  19.  
  20. char snakeMaster[10][200];
  21.  
  22. void snakeMasterLogo()
  23. {
  24.     strcpy(snakeMaster[0],"                           _____ _   _          _  ________   __  __           _____ _______ ______ _____");
  25.     strcpy(snakeMaster[1],"                          / ____| \\ | |   /\\   | |/ /  ____| |  \\/  |   /\\    / ____|__   __|  ____|  __ \\");
  26.     strcpy(snakeMaster[2],"                         | (___ |  \\| |  /  \\  | ' /| |__    | \\  / |  /  \\  | (___    | |  | |__  | |__) |");
  27.     strcpy(snakeMaster[3],"                          \\___ \\| . ` | / /\\ \\ |  < |  __|   | |\\/| | / /\\ \\  \\___ \\   | |  |  __| |  _  /");
  28.     strcpy(snakeMaster[4],"                          ____) | |\\  |/ ____ \\| . \\| |____  | |  | |/ ____ \\ ____) |  | |  | |____| | \\ \\");
  29.     strcpy(snakeMaster[5],"                         |_____/|_| \\_/_/    \\_\\_|\\_\\______| |_|  |_/_/    \\_\\_____/   |_|  |______|_|  \\_\\");
  30. };
  31.  
  32. void printLogoAwal()
  33. {
  34.     int y = 10;
  35.     gotoXY(20,y);
  36.     for(int a = 0;a<6;a++)
  37.     {
  38.         for(int b = 0;b<150;b++)
  39.         {
  40.             printf("%c",snakeMaster[a][b]);
  41.             Sleep(10);
  42.         }
  43.         y++;
  44.         gotoXY(20,y);
  45.     }
  46.     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");
  47.     puts("Press enter to continue...");
  48.     getchar();
  49. }
  50.  
  51. void printLogoMenu()
  52. {
  53.     int y = 10;
  54.     gotoXY(20,y);
  55.     for(int a = 0;a<6;a++)
  56.     {
  57.         for(int b = 0;b<150;b++)
  58.         {
  59.             printf("%c",snakeMaster[a][b]);
  60.         }
  61.         y++;
  62.         gotoXY(20,y);
  63.     }
  64.     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");
  65. }
  66.  
  67. //UserData
  68.  
  69. typedef struct User
  70. {
  71.     char username[100];
  72.     char password[100];
  73.     char status[100];
  74.     int score;
  75.     User*right,*left;
  76. }User;
  77.  
  78. User *root;
  79.  
  80. void insertUser(User **root,char name[100],char password[100],int score,char status[]);
  81.  
  82. User* searchUser(User *root,char username[100]);
  83.  
  84. void deleteUser(User **root, char name[100]);
  85.  
  86. User* signIn();
  87.  
  88. void loadUser();
  89.  
  90. void resetFile();
  91.  
  92. void saveUser(User *root);
  93.  
  94. void insertNewUser();
  95.  
  96. void deleteExistingUser(User *userSekarang);
  97.  
  98. void printInOrder(User *root,int lvl);
  99.  
  100. int memberMenu();
  101.  
  102. int adminMenu();
  103.  
  104. int masterMenu();
  105.  
  106. //Game Snake
  107.  
  108. char map[40][120];
  109. struct Node
  110. {
  111.     int x;
  112.     int y;
  113.     Node* next,*prev;
  114. }*head,*tail,*curr;
  115.  
  116. void initMap();
  117.  
  118. void printMap();
  119.  
  120. void initNode(int x, int y);
  121.  
  122. void pushHead(int x, int y);
  123.  
  124. void pushTail(int x, int y);
  125.  
  126. void popTail();
  127.  
  128. int snakeGame(int userScore);
  129.  
  130. int main()
  131. {
  132.     snakeMasterLogo();
  133.     printLogoAwal();
  134.     loadUser();
  135.     system("cls");
  136.     cls();
  137.     User *user = signIn();
  138.     bool isMember = false;
  139.     bool isAdmin = false;
  140.     bool loop = true;
  141.     int choice;
  142.     if(strcmp(user->status,"admin") == 0)
  143.     {
  144.         isAdmin = true;
  145.     }
  146.     else
  147.     {
  148.         isMember = true;
  149.     }
  150.     do
  151.     {
  152.         system("cls");
  153.         printLogoMenu();
  154.         printf("Welcome,%s\n",user->username);
  155.         puts("==============================");
  156.         if(isMember)
  157.         {
  158.             choice = memberMenu();
  159.         }
  160.         else if(isAdmin)
  161.         {
  162.             choice = adminMenu();
  163.         }
  164.         switch(choice)
  165.         {
  166.         case 1:
  167.             int newScore;
  168.             newScore = snakeGame(user->score);
  169.             if(newScore>user->score)
  170.             {
  171.                 user->score = newScore;
  172.             }
  173.             break;
  174.         case 2:
  175.             system("cls");
  176.             cls();
  177.             printInOrder(root,0);
  178.             getchar();
  179.             break;
  180.         case 3:
  181.             if(isMember)loop = false;
  182.             else
  183.             {
  184.                 system("cls");
  185.                 cls();
  186.                 printInOrder(root,0);
  187.                 do
  188.                 {
  189.                     choice = masterMenu();
  190.                     switch(choice)
  191.                     {
  192.                     case 1:
  193.                         insertNewUser();
  194.                         break;
  195.                     case 2:
  196.                         deleteExistingUser(user);
  197.                         break;
  198.                     }
  199.                 }while(choice !=3);
  200.             }
  201.             break;
  202.         case 4:
  203.             loop = false;
  204.             break;
  205.         }
  206.     }while(loop == true);
  207.     resetFile();
  208.     saveUser(root);
  209.     return 0;
  210. }
  211.  
  212. void cls()
  213. {
  214.     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");
  215. }
  216.  
  217. void gotoXY(int x, int y)
  218. {
  219.     COORD cord;
  220.     cord.X = x;
  221.     cord.Y = y;
  222.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cord);
  223. }
  224.  
  225. void printSpace(int lvl)
  226. {
  227.     for(int a = 0;a<lvl;a++)
  228.     {
  229.        
  230.         printf("       ");
  231.        
  232.     }
  233. }
  234.  
  235. void printSquare()
  236. {
  237.     for(int a = 0;a<28;a++)
  238.     {
  239.         printf("%c",178);
  240.     }
  241.     printf("\n");
  242. }
  243.  
  244. //Game
  245. void initMap()
  246. {
  247.     for(int a = 0;a<40;a++)
  248.     {
  249.         for(int b = 0;b<120;b++)
  250.         {
  251.             if(a==0 || a==39 || b == 0 || b==119)
  252.             {
  253.                 map[a][b] = WALL;
  254.             }
  255.             else
  256.             {
  257.                 map[a][b] = EMPTY;
  258.             }
  259.         }
  260.     }
  261. }
  262.  
  263. void printMap()
  264. {
  265.     gotoXY(20,5);
  266.     for(int a = 0;a<40;a++)
  267.     {
  268.         for(int b = 0;b<120;b++)
  269.         {
  270.             printf("%c",map[a][b]);
  271.         }
  272.         gotoXY(20,a+6);
  273.     }
  274. }
  275.  
  276. void initNode(int x, int y)
  277. {
  278.     curr = (Node*)malloc(sizeof(Node));
  279.     curr->x = x;
  280.     curr->y = y;
  281.     curr->next = curr->prev = NULL;
  282. }
  283.  
  284. void pushHead(int x, int y)
  285. {
  286.     initNode(x,y);
  287.     if(!head)
  288.     {
  289.         head = tail = curr;
  290.     }
  291.     else
  292.     {
  293.         curr->next = head;
  294.         head->prev = curr;
  295.         head = curr;
  296.     }
  297. }
  298.  
  299. void pushTail(int x, int y)
  300. {
  301.     initNode(x,y);
  302.     curr->prev = tail;
  303.     tail->next = curr;
  304.     tail = curr;
  305. }
  306.  
  307. void popTail()
  308. {
  309.     curr = tail;
  310.     tail = curr->prev;
  311.     tail->next = NULL;
  312.     free(curr);
  313. }
  314.  
  315. int snakeGame(int userScore)
  316. {
  317.     system("cls");
  318.     srand(time(NULL));
  319.     initMap();
  320.     printMap();
  321.     bool isAlive = true;
  322.     int headX,headY;
  323.     int score = 0;
  324.     headX = 20;headY = 10;
  325.     int directionX = 1;
  326.     int directionY = 0;
  327.     char key = NULL;
  328.     pushHead(headX,headY);
  329.     map[headY][headX] = WALL;
  330.     gotoXY(20+headX,5+headY);
  331.     printf("%c",map[headY][headX]);
  332.     int randomX;
  333.     int randomY;
  334.     do
  335.     {
  336.         randomX = rand()%118+1;
  337.         randomY = rand()%38+1;
  338.     }while(randomX == headX+directionX && randomY== headY+directionY);
  339.     map[randomY][randomX] = FOOD;
  340.     gotoXY(20+randomX,5+randomY);
  341.     printf("%c",map[randomY][randomX]);
  342.    
  343.    
  344.     while(isAlive == true)
  345.     {
  346.         Sleep(30);
  347.         if(kbhit())
  348.         {
  349.             key = getch();
  350.         }
  351.         switch(key)
  352.         {
  353.         case 'w'://ATAS
  354.             if(directionY != 1)
  355.             {
  356.                 directionY= -1;
  357.                 directionX = 0;
  358.             }
  359.             break;
  360.         case 's'://BAWAH
  361.             if(directionY != -1)
  362.             {
  363.                 directionY= 1;
  364.                 directionX = 0;
  365.             }
  366.             break;
  367.         case 'd'://KANAN
  368.             if(directionX != -1)
  369.             {
  370.                 directionX = 1;
  371.                 directionY = 0;
  372.             }
  373.             break;
  374.         case 'a'://KIRI
  375.             if(directionX != 1)
  376.             {
  377.                 directionX= -1;
  378.                 directionY = 0;
  379.             }
  380.             break;
  381.         }
  382.         if(map[headY+directionY][headX+directionX] == EMPTY)
  383.         {
  384.             pushHead(headX+directionX,headY+directionY);
  385.             map[headY+directionY][headX+directionX] = WALL;
  386.             gotoXY(20+headX+directionX,5+headY+directionY);
  387.             printf("%c",map[headY+directionY][headX+directionX]);
  388.             map[tail->y][tail->x] = EMPTY;
  389.             gotoXY(20+tail->x,5+tail->y);
  390.             printf("%c",map[tail->y][tail->x]);
  391.             popTail();
  392.         }
  393.         else if(map[headY+directionY][headX+directionX] == FOOD)
  394.         {
  395.             pushHead(headX+directionX,headY+directionY);
  396.             map[headY+directionY][headX+directionX] = WALL;
  397.             gotoXY(20+headX+directionX,5+headY+directionY);
  398.             printf("%c",map[headY+directionY][headX+directionX]);
  399.             int randomX;
  400.             int randomY;
  401.             do
  402.             {
  403.                 randomX = rand()%118+1;
  404.                 randomY = rand()%38+1;
  405.             }while(randomX == headX+directionX && randomY== headY+directionY);
  406.             map[randomY][randomX] = FOOD;
  407.             gotoXY(20+randomX,5+randomY);
  408.             printf("%c",map[randomY][randomX]);
  409.             score +=1;
  410.             gotoXY(20+0,5+42);
  411.             printf("Score : %d",score);
  412.         }
  413.         else
  414.         {
  415.             isAlive = false;
  416.             gotoXY(20+0,5+42);
  417.             printf("               ");
  418.             gotoXY(20+0,5+42);
  419.             printf("Game Over");
  420.             if(score>userScore)
  421.             {
  422.                 gotoXY(20+0,6+42);
  423.                 printf("New High Score");
  424.             }
  425.             getchar();
  426.             break;
  427.         }
  428.         headY +=directionY;
  429.         headX +=directionX;
  430.     }
  431.     return score;
  432. }
  433.  
  434.  
  435. // User and Menu
  436.  
  437. void loadUser()
  438. {
  439.     FILE *fp = fopen("user.txt","r");
  440.     if(fp)
  441.     {
  442.         User *temp = (User*)malloc(sizeof(User));
  443.         while(!feof(fp))
  444.         {  
  445.             fscanf(fp,"%[^|]|%[^|]|%d|%[^\n]\n",temp->username,temp->password,&temp->score,temp->status);
  446.             insertUser(&root,temp->username,temp->password,temp->score,temp->status);
  447.         }
  448.         fclose(fp);
  449.     }
  450.     else
  451.     {
  452.         printf("File not found");
  453.     }
  454. }
  455.  
  456. void resetFile()
  457. {
  458.     FILE *fp = fopen("user.txt","w");
  459.     if(fp)
  460.     {
  461.         fprintf(fp,"");
  462.         fclose(fp);
  463.     }
  464. }
  465.  
  466. void saveUser(User *root)
  467. {
  468.     FILE *fp = fopen("user.txt","a");
  469.     if(fp)
  470.     {
  471.         if(!root)
  472.         {
  473.             return;
  474.         }
  475.         fprintf(fp,"%s|%s|%d|%s\n",root->username,root->password,root->score,root->status);
  476.         fclose(fp);
  477.         saveUser(root->left);
  478.         saveUser(root->right);
  479.     }
  480. }
  481.  
  482. void insertUser(User **root,char name[100],char password[100],int score,char status[])
  483. {
  484.     if(! R)
  485.     {
  486.         User *temp = (User*)malloc(sizeof(User));
  487.         strcpy(temp->username,name);
  488.         strcpy(temp->password,password);
  489.         temp->score = score;
  490.         strcpy(temp->status,status);
  491.         temp->left = temp->right = NULL;
  492.         R = temp;
  493.     }
  494.     else if(strcmp(name,R->username)<0)
  495.     {
  496.         insertUser(&R->left,name,password,score,status);
  497.     }
  498.     else if(strcmp(name,R->username)>0)
  499.     {
  500.         insertUser(&R->right,name,password,score,status);
  501.     }
  502. }
  503.  
  504. User* searchUser(User *root,char username[100])
  505. {
  506.     if(!root)
  507.     {
  508.         return NULL;
  509.     }
  510.     if(strcmp(username,root->username)<0)
  511.     {
  512.         return searchUser(root->left,username);
  513.     }
  514.     if(strcmp(username,root->username)>0)
  515.     {
  516.         return searchUser(root->right,username);
  517.     }
  518.     if(strcmp(username,root->username)==0)
  519.     {
  520.         return root;
  521.     }
  522.    
  523. }
  524.  
  525. void printInOrder(User *root,int lvl)
  526. {
  527.     if(!root)
  528.     {
  529.         return;
  530.     }
  531.     printInOrder(root->left,lvl+1);
  532.     if(lvl >0)
  533.     {
  534.         printSpace(lvl);printf("         ");printSquare();
  535.         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);
  536.         printSpace(lvl);printf("         ");printSquare();
  537.         puts("");
  538.     }
  539.     else
  540.     {
  541.         printSquare();
  542.         printf("%c%20s %c%3d %c\n",178,root->username,178,root->score,178);
  543.         printSquare();
  544.         puts("");
  545.     }
  546.     printInOrder(root->right,lvl+1);
  547. }
  548.  
  549. User* signIn()
  550. {
  551.     bool ada = false;
  552.     char username[100];
  553.     char password[100];
  554.     do
  555.     {
  556.         printf("Input your username : ");
  557.         gets(username);fflush(stdin);
  558.         printf("Input your password : ");
  559.         gets(password);fflush(stdin);
  560.         User *valid = NULL;
  561.         valid = searchUser(root,username);
  562.         if(valid)
  563.         {
  564.             if(strcmp(valid->username,username)==0 && strcmp(valid->password,password)==0)
  565.             {
  566.                 ada = true;
  567.                 return valid;
  568.             }
  569.         }
  570.         if(ada == false)
  571.         {
  572.             puts("Invalid Username / Password\n\n\n");
  573.         }
  574.     }while(ada == false);
  575. }
  576.  
  577. int memberMenu()
  578. {
  579.     int choice;
  580.     puts("1. Play Snake");
  581.     puts("2. View Score");
  582.     puts("3. Save and Exit");
  583.     do
  584.     {
  585.         printf("Choose:");
  586.         scanf("%d",&choice);fflush(stdin);
  587.     }while(choice<1 || choice>3);
  588.     return choice;
  589. }
  590.  
  591. int adminMenu()
  592. {
  593.     int choice;
  594.     puts("1. Play Snake");
  595.     puts("2. View Score");
  596.     puts("3. Master User");
  597.     puts("4. Save and Exit");
  598.     do
  599.     {
  600.         printf("Choose:");
  601.         scanf("%d",&choice);fflush(stdin);
  602.     }while(choice<1 || choice>4);
  603.     return choice;
  604. }
  605.  
  606. int masterMenu()
  607. {
  608.     system("cls");
  609.     cls();
  610.     printInOrder(root,0);
  611.     puts("\n\nMASTER USER");
  612.     puts("===========");
  613.     puts("1. Insert New User");
  614.     puts("2. Delete User");
  615.     puts("3. Back");
  616.     int choice;
  617.     do
  618.     {
  619.         printf("Choose : ");
  620.         scanf("%d",&choice);fflush(stdin);
  621.     }while(choice<1 || choice>3);
  622.     return choice;
  623. }
  624.  
  625. void deleteUser(User** root,char name[100])
  626. {
  627.     if(R==NULL)
  628.     {
  629.         return;
  630.     }
  631.     else if(strcmp(name,R->username) < 0)
  632.     {
  633.         deleteUser(&R->left,name);
  634.     }
  635.     else if(strcmp(name,R->username) > 0)
  636.     {
  637.         deleteUser(&R->right,name);
  638.     }
  639.     else
  640.     {
  641.         User *temp;
  642.         if(!R->right)
  643.         {
  644.             temp = R;
  645.             R = R->left;
  646.             free(temp);
  647.         }
  648.         else if(!R->left)
  649.         {
  650.             temp = R;
  651.             R = R->right;
  652.             free(temp);
  653.         }
  654.         else
  655.         {
  656.             temp = R->right;
  657.             while(temp->left)
  658.             {
  659.                 temp = temp->left;
  660.             }
  661.             strcpy(R->username,temp->username);
  662.             strcpy(R->password,temp->password);
  663.             R->score = temp->score;
  664.             strcpy(R->status,temp->status);
  665.             deleteUser(&R->right,temp->username);
  666.         }
  667.     }
  668. }
  669.  
  670. void insertNewUser()
  671. {
  672.     system("cls");
  673.     cls();
  674.     printInOrder(root,0);
  675.     char username[21];
  676.     char password[20];
  677.     char type[10];
  678.     int len;
  679.     bool valid;
  680.     do
  681.     {
  682.         printf("Input Username[3-20]: ");
  683.         gets(username);fflush(stdin);
  684.         len = strlen(username);
  685.     }while(len<3 || len>20);
  686.     do
  687.     {
  688.         valid = false;
  689.         printf("Input Password[substring of username]: ");
  690.         gets(password);fflush(stdin);
  691.         if(strstr(username,password)!= NULL)
  692.         {
  693.             valid = true;
  694.         }
  695.         else
  696.         {
  697.             puts("password must be a substring of the inputted username");
  698.         }
  699.     }while(valid == false);
  700.     do
  701.     {
  702.         valid = false;
  703.         printf("Input Type[admin|member]: ");
  704.         gets(type);fflush(stdin);
  705.         if(strcmp(type,"admin")==0 || strcmp(type,"member")==0)
  706.         {
  707.             valid = true;
  708.         }
  709.     }while(valid == false);
  710.     insertUser(&root,username,password,0,type);
  711.     puts("New User Inserted Successfully");
  712.     getchar();
  713. }
  714.  
  715. void deleteExistingUser(User *userSekarang)
  716. {
  717.     system("cls");
  718.     cls();
  719.     printInOrder(root,0);
  720.     puts("\n\n");
  721.     char username[100];
  722.     printf("Input Username to be deleted: ");
  723.     gets(username);
  724.     User *temp = searchUser(root,username);
  725.     if(!temp)
  726.     {
  727.         printf("User %d not found",username);
  728.  
  729.     }
  730.     else if(strcmp(userSekarang->username,username)==0)
  731.     {
  732.         puts("You cannot delete yourself");
  733.         getchar();
  734.     }
  735.     else
  736.     {
  737.         deleteUser(&root,username);
  738.     }
  739. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement