Main.cpp 3.0 KB

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