Main.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <conio.h>
  4. #include "Const.h"
  5. #include "Game.h"
  6. #include "Snake.h"
  7. #include "Field.h"
  8. #include "Food.h"
  9. int main()
  10. {
  11. snake_size = 5;// >0 Âêëþ÷àåò ãîëîâó + ðàçìåð õâîñòà (åñëè 1 - òî òîëüêî ãîëîâà)
  12. //timeout = 200; //> 100 (ms), 1000 - áóäåò çàäåðæèâàòü âûïîëíåíèå ïðîãðàììû íà 1 ñåêóíäó
  13. //Çàïîëíåíèå ìàññèâà êîîðäèíàò èíäåêñàìè îò 1 äî L; snake_x[0] = 1
  14. // snake_x[L-1] - íà÷àëî õâîñòà
  15. // snake_x[L-snake_size] - ïîëå, ãäå íàõîäèòñÿ ïóñòîé ñèìâîë
  16. for (int i = 0; i < L; i++)
  17. {
  18. snake_x[i] = i + 1;
  19. }
  20. //Çàäàíèå ðàíäîìíîãî íà÷àëüíîãî ïîëîæåíèÿ ãîëîâû çìåéêè
  21. srand(time(0));
  22. int step = rand() % L + 1;
  23. //step = 13;
  24. //snake_x[0] = 13;
  25. for (int i = 0; i < step - 1; i++)
  26. {
  27. int tmp = snake_x[0];
  28. for (int j = 0; j < L - 1; j++)
  29. {
  30. snake_x[j] = snake_x[j + 1];
  31. }
  32. snake_x[L - 1] = tmp;
  33. }
  34. for (int i = 0; i < columns; i++)//çàïîëíåíèå ïîëÿ ïóñòûìè ñèìâîëàìè
  35. {
  36. field[i] = field_symbol;
  37. }
  38. field[0] = field[columns - 1] = '*';//óñòàíîâêà ãðàíèö
  39. field[snake_x[0]] = head_symbol;//óñòààíîâêà ãîëîâû
  40. // óñòàíîâêà õâîñòà, åñëè åñòü
  41. for (int i = 1; i < snake_size; i++)
  42. {
  43. field[snake_x[L - i]] = tail_symbol;
  44. }
  45. stop
  46. for (int i = 0; i < columns; i++)
  47. {
  48. std::cout << border_symbol;
  49. }
  50. std::cout << std::endl;
  51. for (int i = 0; i < columns; i++)
  52. {
  53. std::cout << field[i];
  54. }
  55. std::cout << std::endl;
  56. for (int i = 0; i < columns; i++)
  57. {
  58. std::cout << border_symbol;
  59. }
  60. std::cout << std::endl;
  61. std::cout << "Press control key" << std::endl;//5) Âûâîä ïðèãëàøåíèÿ ê èãðå.
  62. //char key = _getch();
  63. //ïîÿâëåíèå åäû íà ïîëå
  64. do
  65. {
  66. food_x = rand() % (columns - 2) + 1;
  67. } while (field[food_x] != field_symbol);
  68. field[food_x] = food_symbol;
  69. food_flag = true;
  70. while (true)
  71. {
  72. system("cls");//ïîëíàÿ î÷èñòêà êîíñîëè
  73. //ñìåùàåì ïîëîæåíèå ãîëîâû çìåéêè
  74. int tmp = snake_x[0];
  75. for (int j = 0; j < L - 1; j++)
  76. {
  77. snake_x[j] = snake_x[j + 1];
  78. }
  79. snake_x[L - 1] = tmp;
  80. if (field[snake_x[0]] == field_symbol)//âïåðåäè ïóñòî
  81. {
  82. //óñòàíàâëèâàåì ïîëîæåíèå ãîëîâû
  83. field[snake_x[0]] = head_symbol;
  84. //÷èñòèì "õâîñò"
  85. field[snake_x[L - snake_size]] = field_symbol;
  86. if (snake_size > 1)
  87. {
  88. //ñèìâîë õâîñòà âìåñòî ãîëîâû
  89. field[snake_x[L - 1]] = tail_symbol;
  90. }
  91. }
  92. if (field[snake_x[0]] == food_symbol)//âïåðåäè åäà
  93. {
  94. //óñòàíàâëèâàåì ïîëîæåíèå ãîëîâû
  95. field[snake_x[0]] = head_symbol;
  96. if (snake_size > 1)
  97. {
  98. //ñèìâîë õâîñòà âìåñòî ãîëîâû
  99. field[snake_x[L - 1]] = tail_symbol;
  100. }
  101. food_flag = false;//åäû íåò
  102. snake_size++; //óâåëè÷åíèå ðàçìåðà çìåéêè
  103. }
  104. if (!food_flag && snake_size < L)//åñëè íåò åäû è çìåéêà äîñòèãëà ìàêñèìàëüíîãî ðàçìåðà
  105. {
  106. do
  107. {
  108. food_x = rand() % (columns - 2) + 1;
  109. } while (field[food_x] != field_symbol);
  110. field[food_x] = food_symbol;//óñòàíîâèòü åäó
  111. food_flag = true;//åäà åñòü
  112. }
  113. if (!food_flag && snake_size == L)//åñëè íåò åäû è çìåéêà ìàêñèìàëüíîãî ðàçìåðà
  114. {
  115. std::cout << "The End" << std::endl;//âûâåñòè ñîîáùåíèå îá îêîí÷àíèè èãðû
  116. break;//âûéòè èç öèêëà
  117. }
  118. //ïå÷àòü
  119. for (int i = 0; i < columns; i++)
  120. {
  121. std::cout << border_symbol;
  122. }
  123. std::cout << std::endl;
  124. for (int i = 0; i < columns; i++)
  125. {
  126. std::cout << field[i];
  127. }
  128. std::cout << std::endl;
  129. for (int i = 0; i < columns; i++)
  130. {
  131. std::cout << border_symbol;
  132. }
  133. std::cout << std::endl;
  134. Sleep(timeout);
  135. }
  136. return 0;//êîä çàâåðøåíèÿ ïðèëîæåíèÿ
  137. }