Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. const computeMatrix = (size, cellSize, state) => {
  2. const cell = repeat(' ', cellSize);
  3.  
  4. // Init the matrix with black cells.
  5. let matrix = range(size).map(
  6. () => range(size).map(
  7. () => chalk.bgBlack(cell)
  8. )
  9. );
  10.  
  11. // If there is some food, paint it in magenta.
  12. if (state.food !== undefined) {
  13. const {x, y} = state.food.coords;
  14. matrix[y][x] = chalk.bgMagenta(cell);
  15. }
  16.  
  17. // Paint the snake body and then the head in a brighter color.
  18. initial(state.snake.body).forEach(({x, y}) => {
  19. matrix[y][x] = (state.snake.status === Status.ALIVE)
  20. ? chalk.bgGreen(cell)
  21. : chalk.bgRed(cell);
  22. });
  23. const {x, y} = last(state.snake.body);
  24. matrix[y][x] = (state.snake.status === Status.ALIVE)
  25. ? chalk.bgGreenBright(cell)
  26. : chalk.bgRedBright(cell);
  27.  
  28. // Paint white borders around the arena.
  29. const whiteCell = chalk.bgWhite(cell);
  30. const whiteLine = range(size + 2).map(() => whiteCell);
  31. matrix = [
  32. whiteLine,
  33. ...matrix.map(row => [
  34. whiteCell,
  35. ...row,
  36. whiteCell
  37. ]),
  38. whiteLine,
  39. ];
  40. return matrix.map(row => row.join('')).join('\n')
  41. };
  42.  
  43. const Arena = ({size, cellSize = 3, state}) => (
  44. <>
  45. <Box>Score: {state.score}</Box>
  46. <Box>{computeMatrix(size, cellSize, state)}</Box>
  47. </>
  48. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement