0

I'm doing some Neuroevolution simulations to teach R to play a simple snake game.

I have a game board as a 10x10 matrix filled with integers from 0 to 2. 0 being empty, 1 the player and 2 is the reward ("apple").

Currently i'm drawing the game by simply using the image-function. Which gives me kind of a boring view of the board. Since I later need to present the results that I'm getting, i'd like to know if there are cooler ways to do this.

Is there a way to plot this matrix so that every 1 would be converted into a image (like pacman or something) and every two would convert into a apple? Using ggplot2 or any similar packages is an option.

Small example of what I'm currently using:

#Initialize empty board
gameBoard <- matrix(0, nrow=10, ncol=10)
#Randomly select 20 spots for apples and assign 2 to those spots
appleSpots <- sample(10*10, 20)
gameBoard[appleSpots] <- 2
#Put player in the middle of the board
gameBoard[5,5] <- 1

#Plot the board
image(gameBoard)
  • Would plotting an apple over the matrix image do it for you? – Roman Luštrik Jan 02 '18 at 17:14
  • Yes, most likely that would be enough. Though I'm not sure how to get the placement right. Have to take a look at that. – Joni Keto-Tokoi Jan 02 '18 at 17:18
  • With ggplot2, you could make the board with `geom_raster`, then add images with [`geom_image`](https://stackoverflow.com/a/43391265/4497050) or other means ([emoGG](https://github.com/dill/emoGG) is simple, if you like). You'll need to rearrange your data to long form to get started if you want to use ggplot. – alistaire Jan 02 '18 at 18:02
  • 1
    The grid package documentation has a bunch of illustrations showing how to place chess piece images. – IRTFM Jan 02 '18 at 18:11

0 Answers0