fork download
  1.  
  2. #include<iostream>
  3. #include<string>
  4. #include"olcConsoleGameEngine.h"
  5.  
  6. enum STATES {START, RECURSIVEGAME, STACKGAME, STACKAUTO, SCORES, CREDITS, EXIT};
  7.  
  8. struct move {
  9. move(int _from, int _to) {//int
  10. from = _from;
  11. to = _to;
  12. }
  13. int from, to;
  14. };
  15. std::vector<move> moveset;
  16.  
  17. void HanoiRecursion(int nDisks, int first, int middle, int last)
  18. {
  19. //nDisks work has iterator
  20. if (nDisks > 0)
  21. {
  22. HanoiRecursion(nDisks - 1, first, last, middle);
  23. moveset.push_back(move(first, middle));
  24.  
  25. HanoiRecursion(nDisks - 1, last, middle, first);
  26. }
  27. }
  28.  
  29.  
  30.  
  31. class mainEngine : public olcConsoleGameEngine {
  32. public:
  33. STATES state = START;
  34. std::vector<int> towers[3];
  35. int diskAmount;
  36.  
  37.  
  38. int mode;//0: start 1: activa recursividad 2:stack 3: stackAuto 4:creditos 5:scoreboard 6:salir
  39. int mouseHolder;//the disk size pickup by the mouse
  40. float timeMarker;
  41.  
  42. std::vector<std::wstring> players;
  43. bool gettingName;
  44.  
  45. mainEngine()
  46. {
  47. m_sAppName = L"LOS JUEGOS DE TORRE DE HANOI";
  48. }
  49.  
  50. bool OnUserCreate()
  51. {
  52. mode = 0;
  53. timeMarker = 0.0f;
  54. mouseHolder = 0;
  55. gettingName = false;
  56. return true;
  57. }
  58.  
  59.  
  60. bool OnUserUpdate(float fElapsedTime)
  61. {
  62. if (state == START)
  63. {
  64. if (GameStartMenu())
  65. {
  66. if (mode == 1)
  67. state = RECURSIVEGAME;
  68. else if (mode == 2)
  69. state = STACKGAME;
  70. else if (mode == 3)
  71. state = STACKAUTO;
  72. else if (mode == 4)
  73. state = CREDITS;
  74. else if (mode == 5)
  75. state = SCORES;
  76. else if (mode == 6)
  77. state = EXIT;
  78. }
  79. }
  80. else if (state == RECURSIVEGAME)
  81. {
  82. DrawRecursiveHanoiGame(fElapsedTime);
  83. }
  84. else if (state == STACKGAME)
  85. {
  86. DrawStackHanoiGame();
  87. }
  88. else if (state == STACKAUTO)
  89. {
  90. DrawStackAuto(fElapsedTime);
  91. }
  92. else if (state == SCORES)
  93. {
  94. DrawScores();
  95. if (gettingName)//TODO
  96. {
  97. GetUsersName(fElapsedTime);
  98. }
  99. else
  100. {
  101. int i = 1;
  102. for (auto n : players)
  103. {
  104. DrawString(ScreenWidth() / 2 - 30, ScreenHeight() / 2 + (i * 5), n, BG_DARK_YELLOW);
  105. i++;
  106. }
  107. }
  108. }
  109. else if (state == CREDITS)
  110. {
  111. DrawCredits();
  112. }
  113. else if (state == EXIT)
  114. return false;
  115.  
  116. return true;
  117. }
  118.  
  119.  
  120. bool GameStartMenu()
  121. {
  122. //background
  123. Fill(0, 0, ScreenWidth(), ScreenHeight(), ' ', BG_DARK_GREY);
  124.  
  125. //title
  126. DrawString(ScreenWidth() / 2 - 20, 10, L"LOS JUEGOS DE HANOI", FG_WHITE);
  127.  
  128. DrawString(10, 25, L"SELECCIONE LA CANTIDAD DE DISCOS", FG_WHITE);
  129.  
  130. //disks selection
  131. if (diskAmount < 2 || diskAmount > 8)
  132. diskAmount = 2;
  133.  
  134. if (m_keys[VK_UP].bReleased)
  135. diskAmount++;
  136. else if (m_keys[VK_DOWN].bReleased)
  137. diskAmount--;
  138.  
  139. std::wstring diskNumber = std::to_wstring(diskAmount);
  140. DrawString(45, 25, diskNumber, FG_WHITE);
  141.  
  142. DrawString(10, 30, L"PRESIONE 'R' PARA HANOI_RECURSIVO", FG_WHITE);//press R for recursiveHanoi
  143. DrawString(10, 40, L"PRESIONE 'S' PARA HANOI_STACK MANUAL", FG_WHITE);//press S for hanoiStack
  144. DrawString(10, 50, L"PRESIONE 'A' PARA HANOI_STACK AUTO", FG_WHITE);
  145. DrawString(10, 60, L"PRESIONE 'Q' PARA SCOREBOARD", FG_WHITE);//exit mode
  146. DrawString(10, 70, L"PRESIONE 'C' PARA CREDITOS", FG_WHITE);//creditos
  147. DrawString(10, 80, L"PRESIONE 'ESC' PARA SALIR", FG_WHITE);//exit mode
  148.  
  149. //letter for activating hanoiRecursive
  150. if (m_keys['R'].bReleased)
  151. {
  152. SetUpRecursiveHanoi(diskAmount);
  153. mode = 1;
  154. return true;
  155. }
  156.  
  157. //letter for activating hanoiStack manual
  158. if (m_keys['S'].bReleased)
  159. {
  160. SetUpStackHanoi(diskAmount);
  161. mode = 2;
  162. return true;
  163. }
  164.  
  165. //letter for activating hanoiStack auto
  166. if (m_keys['A'].bReleased)
  167. {
  168. SetUpStackAutoHanoiGame(diskAmount);
  169. mode = 3;
  170. return true;
  171. }
  172.  
  173. //letter for credits
  174. if (m_keys['C'].bReleased)
  175. {
  176. mode = 4;
  177. return true;
  178. }
  179.  
  180. //letter for SCORES
  181. if (m_keys['Q'].bReleased)
  182. {
  183. mode = 5;
  184. return true;
  185. }
  186. //letter for Exit
  187. if (m_keys[VK_ESCAPE].bReleased)
  188. {
  189. mode = 6;
  190. return true;
  191. }
  192.  
  193. return false;
  194. }
  195.  
  196. void SetUpRecursiveHanoi(int disks)
  197. {
  198. int towerA = 0;
  199. int towerB = 1;
  200. int towerC = 2;
  201. HanoiRecursion(disks, towerA, towerC, towerB);
  202.  
  203.  
  204. for (int i = 0; i < disks; i++)
  205. towers[0].push_back(disks - i);
  206. }
  207.  
  208.  
  209.  
  210. void DrawRecursiveHanoiGame(float deltaTime)
  211. {
  212. static float timer = 0;
  213. static int nmove = 0;
  214. timer += deltaTime;
  215.  
  216. if (timer > 0.5)
  217. {
  218. timer -= 0.5;
  219. if (nmove < moveset.size())
  220. {
  221. towers[moveset[nmove].to].push_back(towers[moveset[nmove].from].back());
  222. towers[moveset[nmove].from].pop_back();
  223.  
  224. towers[moveset[nmove].from].shrink_to_fit();
  225. towers[moveset[nmove].to].shrink_to_fit();
  226.  
  227. nmove++;
  228. }
  229. }
  230.  
  231. //Graphics
  232. DrawTowers(FG_BLACK, BG_DARK_MAGENTA);
  233.  
  234.  
  235. //menu
  236. if (m_keys['M'].bReleased)
  237. {
  238. ClearVectors();
  239. state = START;
  240. }
  241.  
  242. //end app
  243. else if (m_keys[VK_ESCAPE].bReleased)
  244. {
  245. ClearVectors();
  246. state = EXIT;
  247. }
  248.  
  249. else if (m_keys['C'].bReleased)
  250. {
  251. ClearVectors();
  252. state = CREDITS;
  253. }
  254. }
  255.  
  256.  
  257. void SetUpStackHanoi(int disks)
  258. {
  259. //insert disks on tower 0
  260. for (int i = 0; i < disks; i++)
  261. towers[0].push_back(disks - i);
  262. }
  263.  
  264.  
  265. void CheckMouseInput()
  266. {
  267. if (GetMouse(0).bReleased)
  268. {
  269. int mouseX = GetMouseX();
  270. int mouseY = GetMouseY();
  271.  
  272. if (mouseHolder != 0)
  273. {
  274. for (int i = 0; i < 3; i++)
  275. {
  276. if ((mouseX < i * 40 + 20) && (mouseX > i * 40 - 20) && (mouseY < 90 && mouseY > 65))//this means we have hit tower[i], or a box around tower[i] at least
  277. {
  278. if (towers[i].empty() || towers[i].back() > mouseHolder)
  279. {
  280. towers[i].push_back(mouseHolder);
  281.  
  282. std::wstring mouseContent = std::to_wstring(mouseHolder);
  283. std::wstring towerContent = std::to_wstring(i);
  284.  
  285. DrawString(20, 25, L"MOVER DISCO", FG_WHITE);
  286. DrawString(20, 30, L"A TORRE", FG_WHITE);
  287.  
  288. mouseHolder = 0;
  289. }
  290. }
  291. }
  292. }
  293. else
  294. {
  295. for (int i = 0; i < 3; i++)
  296. {
  297. if ((mouseX < i * 40 + 20) && (mouseX > i * 40 - 20) && (mouseY < 90 && mouseY > 65))
  298. {
  299. if (towers[i].empty() == false)
  300. {
  301. mouseHolder = towers[i].back();
  302. towers[i].pop_back();
  303.  
  304. std::wstring mouseContent = std::to_wstring(mouseHolder);
  305. std::wstring towerContent = std::to_wstring(i);
  306.  
  307. DrawString(15, 15, mouseContent, FG_WHITE);
  308. DrawString(15, 20, towerContent, FG_WHITE);
  309. }
  310. }
  311. }
  312. }
  313. }
  314.  
  315.  
  316.  
  317. }
  318.  
  319. void DrawStackHanoiGame()
  320. {
  321. //controls
  322. CheckMouseInput();
  323.  
  324. //graphics
  325. DrawTowers(BG_BLACK, BG_DARK_YELLOW);
  326.  
  327. //volver a menu
  328. if (m_keys['M'].bReleased)
  329. {
  330. ClearVectors();
  331. state = START;
  332. }
  333.  
  334. //end app
  335. else if (m_keys[VK_ESCAPE].bReleased)
  336. {
  337. ClearVectors();
  338. state = EXIT;
  339. }
  340.  
  341. //win the game
  342. if (towers[2].size() == diskAmount)
  343. {
  344. state = SCORES;
  345. }
  346.  
  347. }
  348.  
  349. void SetUpStackAutoHanoiGame(int disks)
  350. {
  351. HanoiRecursion(disks, 0, 2, 1);
  352.  
  353.  
  354. //carga la torre 0 con los discos
  355. for (int i = 0; i < disks; i++)
  356. towers[0].push_back(disks - i);
  357. }
  358.  
  359. void DrawStackAuto(float deltaTime)
  360. {
  361. static float timer = 0;
  362. static int nmove = 0;
  363. timer += deltaTime;
  364.  
  365. if (timer > 1.0)
  366. {
  367. timer -= 1.0;
  368. if (nmove < moveset.size())
  369. {
  370. towers[moveset[nmove].to].push_back(towers[moveset[nmove].from].back());
  371. towers[moveset[nmove].from].pop_back();
  372.  
  373. towers[moveset[nmove].from].shrink_to_fit();
  374. towers[moveset[nmove].to].shrink_to_fit();
  375.  
  376. nmove++;
  377. }
  378. }
  379.  
  380. //Graphics
  381. DrawTowers(BG_DARK_MAGENTA, BG_DARK_GREY);
  382.  
  383.  
  384. //menu
  385. if (m_keys['M'].bReleased)
  386. {
  387. ClearVectors();
  388. state = START;
  389. }
  390.  
  391. //end app
  392. else if (m_keys[VK_ESCAPE].bReleased)
  393. {
  394. ClearVectors();
  395. state = EXIT;
  396. }
  397.  
  398. }
  399.  
  400. void DrawTowers(short background, short disksColor)
  401. {
  402. Fill(0, 0, ScreenWidth(), ScreenHeight(), ' ', background);
  403. //torres texto
  404. DrawString(18 + (0 * 40), 90 - 30, L"TORRE 0", FG_DARK_BLUE);
  405. DrawString(18 + (1 * 40), 90 - 30, L"TORRE 1", FG_DARK_GREEN);
  406. DrawString(18 + (2 * 40), 90 - 30, L"TORRE 2", FG_DARK_RED);
  407.  
  408. //dibujar torres en pantalla
  409. for (int i = 0; i < 3; i++)
  410. {
  411. int xTowerCoord = 20 + (i * 40);
  412. int yTowerCoord = 90 - 25;
  413. short color;
  414. if (i == 0) color = BG_DARK_BLUE;
  415. if (i == 1) color = BG_DARK_GREEN;
  416. if (i == 2) color = BG_DARK_RED;
  417.  
  418. Fill(xTowerCoord, yTowerCoord, (xTowerCoord - 2) + 3, yTowerCoord + 25, ' ', color);
  419. }
  420.  
  421.  
  422. for (int i = 0; i < 3; i++)
  423. {
  424. //si aun hay movimientos en las torres
  425. if (towers[i].size() > 0)
  426. {
  427. for (int j = 0; j < towers[i].size(); j++)
  428. {
  429. //dibujar los discos
  430. int _size = towers[i].at(j);//torre i disco j
  431. int xdiskCoord = 20 + i * 40 - (_size + 4);
  432. int ydiskCoord = 90 - (j * 3);
  433. Fill(xdiskCoord, ydiskCoord, xdiskCoord + (_size + 4) * 2 + 1, ydiskCoord + 3, ' ', disksColor);
  434. }
  435. }
  436. }
  437.  
  438.  
  439. }
  440.  
  441. void DrawScores()
  442. {
  443. Fill(0,0, ScreenWidth(), ScreenHeight(), ' ', BG_BLACK);
  444.  
  445.  
  446. DrawString(ScreenWidth() /2 - 15, ScreenHeight()/2 - 30, L"FELICIDADES HAS GANADO!!!", FG_DARK_YELLOW);
  447.  
  448. DrawString(ScreenWidth() /2 - 30, ScreenHeight()/2 - 5, L"INSERTE SU NOMBRE: PRESIONE 'N' ", FG_DARK_YELLOW);
  449. DrawString(ScreenWidth() /2 - 30, ScreenHeight()/2, L"TABLA DE PUNTOS", FG_DARK_YELLOW);
  450.  
  451. DrawString(ScreenWidth() /2 + 20, ScreenHeight()/2 + 30, L"MENU, PRESIONA 'M'", FG_DARK_YELLOW);
  452. DrawString(ScreenWidth() /2 + 20, ScreenHeight()/2 + 35, L"SALIDA, PRESIONA 'ESC'", FG_DARK_YELLOW);
  453.  
  454.  
  455. //name input
  456. if (gettingName == false && m_keys['N'].bReleased)
  457. {
  458. gettingName = true;
  459. }
  460.  
  461. //volver a menu
  462. if (gettingName == false && m_keys['M'].bReleased)
  463. {
  464. ClearVectors();
  465. state = START;
  466. }
  467. //end app
  468. else if (gettingName == false && m_keys[VK_ESCAPE].bReleased)
  469. {
  470. ClearVectors();
  471. state = EXIT;
  472. }
  473. }
  474.  
  475. void ClearVectors()
  476. {
  477. moveset.clear();
  478.  
  479. for (int i = 0; i < 3; i++)
  480. towers[i].clear();
  481.  
  482. }
  483.  
  484. void DrawCredits()
  485. {
  486. Fill(0, 0, ScreenWidth(), ScreenHeight(), ' ', BG_DARK_BLUE);
  487. DrawString(ScreenWidth() / 2 - 30, ScreenHeight() / 2 - 10, L"DESARROLLADO POR:", FG_WHITE);
  488. DrawString(ScreenWidth() / 2 - 30, ScreenHeight() / 2, L"XAVIER LAMELA(SIR_ARTHUR_DAYNE)", FG_WHITE);
  489.  
  490. DrawString(ScreenWidth() / 2 - 30, ScreenHeight()/2 + 30, L"SPECIAL THANKS TO ZLEAPINGBEAR, WITHOUT YOU THIS PROYECT WONT BE COMPLETED", FG_WHITE);
  491.  
  492. if (m_keys['M'].bReleased)
  493. {
  494. ClearVectors();
  495. state = START;
  496. }
  497. //end app
  498. else if (m_keys[VK_ESCAPE].bReleased)
  499. {
  500. ClearVectors();
  501. state = EXIT;
  502. }
  503.  
  504. }
  505.  
  506.  
  507. void GetUsersName(float deltaTime)
  508. {
  509. DrawString(ScreenWidth() / 2 - 30, ScreenHeight() / 2 - 10, L"**INSERTANDO**...", FG_DARK_YELLOW);
  510. static std::wstring name = L"";
  511. static float keytimer = 0;
  512.  
  513. keytimer += deltaTime;
  514.  
  515. if (keytimer > 0.1)
  516. {
  517. for (int i = 0; i < 128; i++)
  518. {
  519. if (m_keys[i].bReleased)
  520. {
  521. name.push_back(i);
  522. keytimer = 0;
  523. }
  524. }
  525.  
  526. }
  527.  
  528. if (m_keys[VK_RETURN].bReleased)//out of input SUB-state
  529. {
  530. players.push_back(name);
  531. name = L"";
  532. gettingName = false;
  533. }
  534.  
  535. }
  536.  
  537. };
  538.  
  539.  
  540.  
  541. int main()
  542. {
  543. mainEngine game;
  544. const int w = 120;
  545. const int h = 100;
  546.  
  547. const int pixelSize = 10;
  548. if (game.ConstructConsole(w, h, pixelSize, pixelSize))
  549. game.Start();
  550. else
  551. std::cout << "ERROR DE CARGA\n";
  552.  
  553. return 0;
  554.  
  555. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:33: fatal error: olcConsoleGameEngine.h: No such file or directory
 #include"olcConsoleGameEngine.h"
                                 ^
compilation terminated.
stdout
Standard output is empty