izukuboi

Untitled

Jul 9th, 2023
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <windows.h>
  4. #include <vector>
  5. #include <cmath>
  6.  
  7. bool gameOver;
  8. const int width = 20;
  9. const int height = 20;
  10. int x, y, fruitX, fruitY, score;
  11. int tailX[100], tailY[100];
  12. int nTail;
  13. enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
  14. eDirection dir;
  15.  
  16. const int inputSize = (width * height) + 1 + (2 * nTail);
  17. const int hiddenSize = 16;
  18. const int outputSize = 4;
  19.  
  20. struct NeuralNetwork
  21. {
  22. std::vector<std::vector<double>> weights1;
  23. std::vector<std::vector<double>> weights2;
  24. };
  25.  
  26. NeuralNetwork neuralNetwork;
  27.  
  28. void InitializeNeuralNetwork()
  29. {
  30. neuralNetwork.weights1.resize(hiddenSize, std::vector<double>(inputSize, 0.0));
  31. neuralNetwork.weights2.resize(outputSize, std::vector<double>(hiddenSize, 0.0));
  32.  
  33. // Randomly initialize the weights
  34. for (int i = 0; i < hiddenSize; i++)
  35. {
  36. for (int j = 0; j < inputSize; j++)
  37. {
  38. neuralNetwork.weights1[i][j] = (2.0 * rand() / RAND_MAX) - 1.0;
  39. }
  40. }
  41.  
  42. for (int i = 0; i < outputSize; i++)
  43. {
  44. for (int j = 0; j < hiddenSize; j++)
  45. {
  46. neuralNetwork.weights2[i][j] = (2.0 * rand() / RAND_MAX) - 1.0;
  47. }
  48. }
  49. }
  50.  
  51. std::vector<double> FlattenInput()
  52. {
  53. std::vector<double> input(inputSize, 0.0);
  54.  
  55. // Set the positions of the apples
  56. input[(fruitY * width) + fruitX] = 1.0;
  57.  
  58. // Set the length of the snake's body
  59. input[width * height] = static_cast<double>(nTail) / (width * height);
  60.  
  61. // Set the positions of the snake's body segments
  62. for (int i = 0; i < nTail; i++)
  63. {
  64. input[(tailY[i] * width) + tailX[i]] = 1.0;
  65. }
  66.  
  67. return input;
  68. }
  69.  
  70. std::vector<double> GetOutput(std::vector<double>& input)
  71. {
  72. std::vector<double> hidden(hiddenSize, 0.0);
  73. std::vector<double> output(outputSize, 0.0);
  74.  
  75. // Calculate hidden layer values
  76. for (int i = 0; i < hiddenSize; i++)
  77. {
  78. for (int j = 0; j < inputSize; j++)
  79. {
  80. hidden[i] += neuralNetwork.weights1[i][j] * input[j];
  81. }
  82. hidden[i] = 1.0 / (1.0 + exp(-hidden[i]));
  83. }
  84.  
  85. // Calculate output layer values
  86. for (int i = 0; i < outputSize; i++)
  87. {
  88. for (int j = 0; j < hiddenSize; j++)
  89. {
  90. output[i] += neuralNetwork.weights2[i][j] * hidden[j];
  91. }
  92. output[i] = 1.0 / (1.0 + exp(-output[i]));
  93. }
  94.  
  95. return output;
  96. }
  97.  
  98. int GetMaxIndex(const std::vector<double>& values)
  99. {
  100. int maxIndex = 0;
  101. double maxValue = values[0];
  102. for (int i = 1; i < values.size(); i++)
  103. {
  104. if (values[i] > maxValue)
  105. {
  106. maxValue = values[i];
  107. maxIndex = i;
  108. }
  109. }
  110. return maxIndex;
  111. }
  112.  
  113. void Setup()
  114. {
  115. gameOver = false;
  116. dir = STOP;
  117. x = width / 2;
  118. y = height / 2;
  119. fruitX = rand() % width;
  120. fruitY = rand() % height;
  121. score = 0;
  122. }
  123.  
  124. void Draw()
  125. {
  126. system("cls");
  127. for (int i = 0; i < width + 2; i++)
  128. std::cout << "#";
  129. std::cout << std::endl;
  130.  
  131. for (int i = 0; i < height; i++)
  132. {
  133. for (int j = 0; j < width; j++)
  134. {
  135. if (j == 0)
  136. std::cout << "#";
  137. if (i == y && j == x)
  138. std::cout << "O";
  139. else if (i == fruitY && j == fruitX)
  140. std::cout << "F";
  141. else
  142. {
  143. bool printTail = false;
  144. for (int k = 0; k < nTail; k++)
  145. {
  146. if (tailX[k] == j && tailY[k] == i)
  147. {
  148. std::cout << "o";
  149. printTail = true;
  150. }
  151. }
  152. if (!printTail)
  153. std::cout << " ";
  154. }
  155. if (j == width - 1)
  156. std::cout << "#";
  157. }
  158. std::cout << std::endl;
  159. }
  160.  
  161. for (int i = 0; i < width + 2; i++)
  162. std::cout << "#";
  163. std::cout << std::endl;
  164. std::cout << "Score:" << score << std::endl;
  165. }
  166.  
  167. void Input()
  168. {
  169. if (_kbhit())
  170. {
  171. switch (_getch())
  172. {
  173. case 'a':
  174. dir = LEFT;
  175. break;
  176. case 'd':
  177. dir = RIGHT;
  178. break;
  179. case 'w':
  180. dir = UP;
  181. break;
  182. case 's':
  183. dir = DOWN;
  184. break;
  185. case 'x':
  186. gameOver = true;
  187. break;
  188. }
  189. }
  190. }
  191.  
  192. void Logic()
  193. {
  194. int prevX = tailX[0];
  195. int prevY = tailY[0];
  196. int prev2X, prev2Y;
  197. tailX[0] = x;
  198. tailY[0] = y;
  199. for (int i = 1; i < nTail; i++)
  200. {
  201. prev2X = tailX[i];
  202. prev2Y = tailY[i];
  203. tailX[i] = prevX;
  204. tailY[i] = prevY;
  205. prevX = prev2X;
  206. prevY = prev2Y;
  207. }
  208.  
  209. switch (dir)
  210. {
  211. case LEFT:
  212. x--;
  213. break;
  214. case RIGHT:
  215. x++;
  216. break;
  217. case UP:
  218. y--;
  219. break;
  220. case DOWN:
  221. y++;
  222. break;
  223. }
  224.  
  225. if (x >= width)
  226. x = 0;
  227. else if (x < 0)
  228. x = width - 1;
  229.  
  230. if (y >= height)
  231. y = 0;
  232. else if (y < 0)
  233. y = height - 1;
  234.  
  235. for (int i = 0; i < nTail; i++)
  236. {
  237. if (tailX[i] == x && tailY[i] == y)
  238. gameOver = true;
  239. }
  240.  
  241. // Update the input vector with the flattened game state
  242. std::vector<double> input = FlattenInput();
  243.  
  244. // Get the output from the neural network
  245. std::vector<double> output = GetOutput(input);
  246.  
  247. // Determine the direction to move based on the output
  248. int maxIndex = GetMaxIndex(output);
  249. switch (maxIndex)
  250. {
  251. case 0:
  252. dir = LEFT;
  253. break;
  254. case 1:
  255. dir = RIGHT;
  256. break;
  257. case 2:
  258. dir = UP;
  259. break;
  260. case 3:
  261. dir = DOWN;
  262. break;
  263. }
  264.  
  265. if (x == fruitX && y == fruitY)
  266. {
  267. score += 10;
  268. fruitX = rand() % width;
  269. fruitY = rand() % height;
  270. nTail++;
  271. }
  272. }
  273.  
  274. int main()
  275. {
  276. InitializeNeuralNetwork();
  277. Setup();
  278. while (!gameOver)
  279. {
  280. Draw();
  281. Input();
  282. Logic();
  283. Sleep(10); // Pause for a short time to control game speed
  284. }
  285. return 0;
  286. }
  287.  
Advertisement
Add Comment
Please, Sign In to add comment