Creating a game board for 15-puzzle

8 次查看(过去 30 天)
For this project, we are to make a the game 'Umpteen Puzzle' in Matlab. It is supposed to be a general function so the user can enter the dimensions of the case, and the function will return the case of that size with tiles placed in initial position. Tried starting by just doing 16 tiles in a 4x4 with one of the tiles being 0 (the blank space). Cant figure out how to do random though for 0-15 can only manually put each number in a spot. How can i use randperm or randi to generate random matrix of 0-15? And from there, I need to display it as an image to be used as the gameboard. Any help would be appreciated.

采纳的回答

James Tursa
James Tursa 2015-2-17
编辑:James Tursa 2015-2-17
You can use reshape to get the random integers into the matrix shape you want. E.g.,
tiles = reshape(randperm(16)-1,4,4)
For the display, I think you will need to provide more details of exactly what look you are after. Just displaying the tiles variable to the screen shows a grid of the numbers, which may be adequate to "play" the game.
  1 个评论
Cale Cortney
Cale Cortney 2015-2-17
Hi James, thanks for your response!
This is what the project instructions state: "The user should be prompted to enter a number of rows that is greater than 1 and a number of columns that is greater than 1. The tiles should be randomly arranged in the case, but the puzzle must be solvable via simple sliding moves."
"function [ ourCase ] = createCase( numRows , numCols ) After the user enters how large the case should be, the case needs to be created and populated with tiles. createCase should receive the dimensions of the case and return a case of that size (with the tiles placed in their initial positions."
We managed to find code for this puzzle online via the file exchange. We downloaded it to use it for reference and to help us get started. This image is what displays after Running the function. I think that's kind of more of what were looking for. When typing disp(tiles) , the matrix just appears in the command window and no 'image', per se, is produced.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Number games 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by