Advertisement
Guest User

Snake-scoreboard?

a guest
Jan 15th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.24 KB | None | 0 0
  1. // Hra Snake.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <conio.h>
  7. #include <windows.h>
  8. #include <cstdlib>
  9. #include <stdio.h>
  10. #define MAX 10
  11. #pragma warning(disable:4996)
  12.  
  13. using namespace std;
  14. bool gameOver;
  15. const int width = 40;
  16. const int height = 20;
  17. int x, y, appleX, appleY, score;
  18. int choice, choice2;
  19. int tailX[100], tailY[100];
  20. int nTail;
  21. typedef struct
  22. {
  23.     int score;
  24.     char name[20];
  25. }Player;
  26. enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN};
  27. eDirecton dir;
  28. void Setup()
  29. {
  30.     gameOver = false;
  31.     dir = STOP;
  32.     x = width / 2;
  33.     y = height / 2;
  34.     appleX = rand() % width;
  35.     appleY = rand() % height;
  36.     score = 0;
  37. }
  38. void Draw()
  39. {
  40.     system("cls");
  41.     for (int i = 0; i < width + 2; i++)
  42.         cout << "#";
  43.     cout << endl;
  44.  
  45.     for (int i = 0; i < height; i++)
  46.     {
  47.         for (int j = 0; j < width; j++)
  48.         {
  49.             if (j == 0)
  50.                 cout << "#";
  51.             if (i == y && j == x)
  52.                 cout << "O";
  53.             else if (i == appleY && j == appleX)
  54.                 cout << "A";
  55.             else
  56.             {
  57.                 bool print = false;
  58.                 for (int k = 0; k < nTail; k++)
  59.                 {
  60.                     if (tailX[k] == j && tailY[k] == i)
  61.                     {
  62.                         cout << "o";
  63.                         print = true;
  64.                     }
  65.                 }
  66.                 if (!print)
  67.                     cout << " ";
  68.             }
  69.  
  70.             if (j == width - 1)
  71.                 cout << "#";
  72.         }
  73.         cout << endl;
  74.     }
  75.  
  76.     for (int i = 0; i < width + 2; i++)
  77.         cout << "#";
  78.         cout << endl;
  79.         cout << "Score:" << score << endl;
  80. }
  81. void Input()
  82. {
  83.     if (_kbhit())
  84.     {
  85.         switch (_getch())
  86.         {
  87.         case 'K':
  88.             dir = LEFT;
  89.             break;
  90.         case 'M':
  91.             dir = RIGHT;
  92.             break;
  93.         case 'H':
  94.             dir = UP;
  95.             break;
  96.         case 'P':
  97.             dir = DOWN;
  98.             break;
  99.         case 'x':
  100.             gameOver = true;
  101.             break;
  102.         }
  103.     }
  104. }
  105. void Logic()
  106. {
  107.     int moveX = tailX[0];
  108.     int moveY = tailY[0];
  109.     int move2X, move2Y;
  110.     tailX[0] = x;
  111.     tailY[0] = y;
  112.     for (int i = 1; i < nTail; i++)
  113.     {
  114.         move2X = tailX[i];
  115.         move2Y = tailY[i];
  116.         tailX[i] = moveX;
  117.         tailY[i] = moveY;
  118.         moveX = move2X;
  119.         moveY = move2Y;
  120.     }
  121.     switch (dir)
  122.     {
  123.     case LEFT:
  124.         x--;
  125.         break;
  126.     case RIGHT:
  127.         x++;
  128.         break;
  129.     case UP:
  130.         y--;
  131.         break;
  132.     case DOWN:
  133.         y++;
  134.         break;
  135.     default:
  136.         break;
  137.     }
  138.     if (x > width || x < 0 || y > height || y < 0)
  139.     {
  140.         gameOver = true;
  141.     }
  142.     //if (x >= width) x = 0; else if (x < 0) x = width - 1;
  143.     //if (y >= height) y = 0; else if (y < 0) y = height - 1;
  144.     for (int i = 0; i < nTail; i++)
  145.         if (tailX[i] == x && tailY[i] == y)
  146.         {
  147.             gameOver = true;
  148.         }
  149.     if (x == appleX && y == appleY)
  150.     {
  151.         score += 10;
  152.         appleX = rand() % width;
  153.         appleY = rand() % height;
  154.         nTail++;
  155.     }
  156. }
  157. void AddScore(int score)
  158. {
  159.     FILE *scoreboardFile = fopen("scoreboard.txt", "a");
  160.     Player player;
  161.     printf("\n\nWrite your name: ");
  162.     scanf("%s", player.name);
  163.     fprintf(scoreboardFile, "name: %s \score: %d\n\n\n", player.name, player.score);
  164.     fclose(scoreboardFile);
  165. }
  166. void ShowScore(Player player[])
  167. {
  168.     FILE *scoreboardFile = fopen("scoreboard.txt", "r");
  169.     int i, num;
  170.     //system("cls")
  171.     if(scoreboardFile==NULL)
  172.     {
  173.         printf("\nERROR opening scoreboard");
  174.         Sleep(1000);
  175.         return;
  176.     }
  177.     for (i = 0; i < MAX; i++)
  178.     {
  179.         num = fscanf(scoreboardFile, "%s %d", player[i].name, &player[i].score);
  180.         if (num < 2)
  181.         {
  182.             player[i+1].name[0] = '\0';
  183.             player[i+1].score = 0;
  184.             break;
  185.         }
  186.         printf("name: %s, score: %d\n\n", player[i].name, player[i].score);
  187.     }
  188.     printf("\n\n\n");
  189.     system("pause");
  190.     fclose(scoreboardFile);
  191. }
  192. void SortScore(Player player[])
  193. {
  194.     int i, j;
  195.     Player temp;
  196.     char tempName[20];
  197.     int howMany = 0;
  198.     FILE *scoreboardFile = fopen("scoreboard.txt", "r");
  199.     for (i = 0; i<MAX && !feof(scoreboardFile); i++)
  200.     {
  201.         fscanf(scoreboardFile, "%s %d%*c", player[i].name, &player[i].score);
  202.         printf("name: %s, points: %d\n", player[i].name, player[i].score);
  203.         howMany++;
  204.     }
  205.     printf("\n\n\nhow many: %d\n\n\n", howMany);
  206.     for (i = 0; i<howMany; i++)
  207.     {
  208.         for (j = 0; j<howMany - 1; j++)
  209.         {
  210.             if (player[j].score > player[j + 1].score)
  211.             {
  212.                 temp = player[j];
  213.                 player[j] = player[j + 1];
  214.                 player[j + 1] = temp;
  215.             }
  216.         }
  217.     }
  218.     fclose(scoreboardFile);
  219.  
  220. }
  221. int main()
  222. {
  223.     gotostart:
  224.     {
  225.         int choice = 0;
  226.         int choice2 = 0;
  227.         system("cls");
  228.         std::cout << "============Snake============\n\n" << std::endl;
  229.         std::cout << "(1): Press 1 to start game" << std::endl;
  230.         std::cout << "(2): Press 2 to see scoreboard" << std::endl;
  231.         std::cout << "(3): Press 3 to exit" << std::endl;
  232.         std::cin >> choice;
  233.         switch (choice)
  234.         {
  235.         case 1:
  236.             Setup();
  237.             while (!gameOver)
  238.             {
  239.                 Draw();
  240.                 Input();
  241.                 Logic();
  242.                 Sleep(50);
  243.             }
  244.             while (gameOver)
  245.             {
  246.                 system("cls");
  247.                 printf("\n\n             GAME OVER");
  248.                 printf("\n\n\nYour score is: %d", score);
  249.                 Sleep(1000);
  250.                 gotostart2:
  251.                 int choice2 = 0;
  252.                 system("cls");
  253.                 std::cout << "GAMEOVER" << std::endl;
  254.                 std::cout << "(1): Press 1 to menu" << std::endl;
  255.                 std::cout << "(2): Press 2 to exit" << std::endl;
  256.                 std::cin >> choice2;
  257.                 switch (choice2)
  258.                 {
  259.                 case 1:
  260.                     goto gotostart;
  261.                     return 0;
  262.                     break;
  263.                 case 2:
  264.                     break;
  265.                 default:
  266.                     goto gotostart2;
  267.                     return 0;
  268.                     break;
  269.                 }
  270.             }
  271.             break;
  272.         case 2:
  273.             ShowScore(0);
  274.             break;
  275.         case 3:
  276.             break;
  277.         default:
  278.             goto gotostart;
  279.             return 0;
  280.             break;
  281.         }
  282.        
  283.     }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement