Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5.  
  6. namespace Bandersnatch
  7. {
  8. public class Player
  9. {
  10. private Stack<int> history;
  11. private int index;
  12.  
  13. private Graph story;
  14.  
  15.  
  16. public Player(Graph story)
  17. {
  18. Stack<int> history = new Stack<int>();
  19. this.story = story;
  20. this.index = story.GetBegin();
  21. }
  22.  
  23. public void Game()
  24. {
  25. int status;
  26. history.Push(index);
  27. do
  28. {
  29. status = GetNextAction();
  30. } while (story.GetNode(status).GetType() != NodeType.End);
  31. Console.WriteLine(story.GetNode(status).GetMessage());
  32. }
  33.  
  34. private int GetInput(ConsoleKeyInfo input, int limit, ref int line)
  35. {
  36. if (input.Key == ConsoleKey.UpArrow && line > 1)
  37. return --line;
  38. if (input.Key == ConsoleKey.DownArrow && line < limit)
  39. return ++line;
  40. if (input.Key == ConsoleKey.Q)
  41. return -2;
  42. if (input.Key == ConsoleKey.Enter)
  43. {
  44. history.Push(line - 1);
  45. return line - 1;
  46. }
  47. return line;
  48. }
  49.  
  50. private int GetNextAction()
  51. {
  52. int i = history.Peek();
  53. Node actualNode = story.GetNode(i);
  54. Dictionary<int, string> neighbours = actualNode.GetNeighbours();
  55. int limit = neighbours.Count;
  56. int line = 1;
  57.  
  58. if (limit == 0)
  59. return -1;
  60.  
  61. ConsoleKeyInfo input;
  62. do
  63. {
  64. Console.WriteLine($"{actualNode.GetMessage()}\n");
  65.  
  66. Console.SetCursorPosition(0, line);
  67. Console.Write('>');
  68.  
  69. Console.SetCursorPosition(4, 1);
  70. foreach (KeyValuePair<int, string> item in neighbours)
  71. {
  72. Console.Write(item.Value);
  73. Console.SetCursorPosition(4, Console.CursorTop + 1);
  74. }
  75.  
  76. input = Console.ReadKey();
  77. line = GetInput(input, limit, ref line);
  78. Console.Clear();
  79.  
  80. } while (input.Key != ConsoleKey.Q || input.Key != ConsoleKey.Enter);
  81. return line;
  82. }
  83.  
  84. private void DisplaySave()
  85. {
  86. throw new NotImplementedException();
  87. }
  88.  
  89. public bool LoadSave(string save)
  90. {
  91. throw new NotImplementedException();
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement