Fail struct


SUBMITTED BY: Guest

DATE: Dec. 16, 2013, 12:37 a.m.

FORMAT: C++

SIZE: 6.1 kB

HITS: 783

  1. #include <iostream>
  2. #include <windows.h>
  3. #include <cstdlib>
  4. #include <ctime>
  5. using namespace std;
  6. struct data
  7. {
  8. int score;
  9. int hiscore;
  10. int pX,pY;
  11. int fX,fY;
  12. int eX,eY;
  13. string hiname;
  14. }game;
  15. int random(int nMin, int nMax)
  16. {
  17. return ((rand() % (nMax - nMin)) + 1) + nMin;
  18. }
  19. bool checkBlock(int pX, int pY)
  20. {
  21. if (pX % 2 != 0 && pY % 2 != 0)
  22. return true;
  23. else
  24. return false;
  25. }
  26. int main()
  27. {
  28. //bool repeat = false;
  29. bool dead = false;
  30. bool f = false;
  31. bool first = true;
  32. string s;
  33. string name;
  34. string in = "";
  35. game.hiscore = 0;
  36. srand(time(0));
  37. while (1)
  38. {
  39. system("cls");
  40. if (dead)
  41. {
  42. dead = false;
  43. cout << "You Lose!\n";
  44. cout << "Wanna play again? (y/n) ";
  45. do
  46. {
  47. cin >> in;
  48. }
  49. while (!(in == "y" || in == "n"));
  50. if (in == "n")
  51. return 0;
  52. first = true;
  53. }
  54. system("cls");
  55. cout << "Highscore: " << game.hiname << " (" << game.hiscore << ")" << "\n\n";
  56. cout << "Masukkan nama player: ";
  57. cin >> name;
  58. game.score = 0;
  59. s = "";
  60. do
  61. {
  62. game.pX = random(2,9);
  63. game.pY = random(2,9);
  64. } while ((checkBlock(game.pX,game.pY)));
  65. do
  66. {
  67. game.eX = random(2,9);
  68. game.eY = random(2,9);
  69. if (!(game.eX == game.pX && game.eY == game.pY) && !checkBlock(game.eX,game.eY))
  70. f = true;
  71. } while (f != true);
  72. f = false;
  73. do
  74. {
  75. game.fX = random(2,9);
  76. game.fY = random(2,9);
  77. if (!(game.fX == game.pX && game.fY == game.pY) && !checkBlock(game.fX,game.fY))
  78. f = true;
  79. } while (f != true);
  80. f = false;
  81. Sleep(100);
  82. while (dead != true)
  83. {
  84. system("cls");
  85. cout << "Player: " << name << "\n";
  86. cout << "Score: " << game.score << "\n";
  87. if ((game.pX > game.eX) && game.eY % 2 == 0)
  88. {
  89. game.eX++;
  90. if ( game.pY > game.eY)
  91. {
  92. }
  93. }
  94. else if (game.pX < game.eX && game.eY % 2 == 0)
  95. {
  96. game.eX--;
  97. }
  98. if ((game.pY > game.eY) && game.eX % 2 == 0)
  99. {
  100. game.eY++;
  101. }
  102. else if (game.pY < game.eY && game.eX % 2 == 0)
  103. {
  104. game.eY--;
  105. }
  106. if (game.pX == game.fX && game.pY == game.fY)
  107. {
  108. do
  109. {
  110. game.fX = random(2,9);
  111. game.fY = random(2,9);
  112. if (!(game.fX == game.pX && game.fY == game.pY) && !checkBlock(game.fX,game.fY))
  113. f = true;
  114. } while (f != true);
  115. f = false;
  116. game.score += 5;
  117. }
  118. if (game.eX == game.pX && game.eY == game.pY)
  119. {
  120. game.pX = -1;
  121. game.pY = -1;
  122. dead = true;
  123. }
  124. for (int i = 1; i <= 11; i++)
  125. {
  126. for (int j = 1; j <= 11; j++)
  127. {
  128. if (i == 1 || i == 11)
  129. s += "=";
  130. else if (j == 1 || j == 11)
  131. s += "|";
  132. else if ( (i % 2 != 0 && (i != 1 || i != 11)) && (j % 2 != 0 && (j != 1 || j != 11)) )
  133. s += "X";
  134. else if (i == game.pX && j == game.pY)
  135. s += name[0];
  136. else if (i == game.fX && j == game.fY)
  137. s += "*";
  138. else if (i == game.eX && j == game.eY)
  139. s += "#";
  140. else
  141. s += " ";
  142. }
  143. s += "\n";
  144. }
  145. cout << s;
  146. s = "";
  147. if (first)
  148. {
  149. first = false;
  150. Sleep(2000);
  151. }
  152. if (GetAsyncKeyState(VK_UP))
  153. {
  154. game.pX--;
  155. if ( checkBlock(game.pX,game.pY) || game.pX == 1)
  156. game.pX++;
  157. //delay = true;
  158. }
  159. else if (GetAsyncKeyState(VK_DOWN))
  160. {
  161. game.pX++;
  162. if ( checkBlock(game.pX,game.pY) || game.pX == 11)
  163. game.pX--;
  164. //delay = true;
  165. }
  166. else if (GetAsyncKeyState(VK_LEFT))
  167. {
  168. game.pY--;
  169. if ( checkBlock(game.pX,game.pY) || game.pY == 1)
  170. game.pY++;
  171. //delay = true;
  172. }
  173. else if (GetAsyncKeyState(VK_RIGHT))
  174. {
  175. game.pY++;
  176. if ( checkBlock(game.pX,game.pY) || game.pY == 11)
  177. game.pY--;
  178. }
  179. cout << "\n\n" << "Location: " << game.eX << "," << game.eY;
  180. Sleep(150);
  181. }
  182. if (game.score > game.hiscore)
  183. {
  184. game.hiscore = game.score;
  185. game.hiname = name;
  186. }
  187. }
  188. return 0;
  189. }

comments powered by Disqus