How do I create an x by y grid and how do I color each grid section?

I am currently creating the game stratego on matlab. I am not sure how to physically create the game board though. I am looking for something quite simple, just a grid pattern where I can manually color each square whatever color I want. Please help

5 Kommentare

When I plug in your code, I get the checkered board, but all the white spaces are green, and your A(10,10)=4 statement doesn't change anything. Do you have any suggestions, getting rid of the colormap turns the white spaces to yellow and the black spaces to blue. I am not sure if that helps or not. Please let me know what I did wrong and how I can fix it. Thanks
jonas
jonas am 27 Okt. 2018
Bearbeitet: jonas am 27 Okt. 2018
Put the line
A(10,10) = 4
before the line with pcolor. Sorry for the confusion.
That works better, but I think I may not be understanding something about how the code works. I though that A(10,10) = a number with that square turning whatever number it corresponds to in the color pallet. So when I try to turn that square blue
A(10,10) = 2.8
but that just turns the center square green and everything else normal. And when I do
if true
% A(10,10) = 2.5
then it turns the center square green, then every other white square red and I am not sure why. While I have your attention, if I put multiple A(x,y) = statements to change multiple tiles different colors will I get an error statement? If so how would I change multiple squares?
jonas
jonas am 27 Okt. 2018
Bearbeitet: jonas am 27 Okt. 2018
Well you understood how this works much faster than most people do. The only thing I forgot to mention is that the colorscale scales automatically, meaning that the max/min in your matrix become the the top/bottom colors of your colorbar. You need to set the CLim so that the max/min stays at [0 4], even if those numbers are not present in your matrix.
A = repmat([repmat([1 0],1,10);repmat([0 1],1,10)],10,1)
set(gca,'clim',[0 4])
A(10,10)=3;
h = pcolor(A)
cmap = [0 0 0;1 1 1;1 0 0;0 0 1;0 1 0];
colormap(cmap)
set(gca,'clim',[0 4])
In addition, something you will probably find useful is that you can change the colors after plotting by this simple syntax
h.CData(2,4) = 3
assuming the handle to your pcolor is called h. This will be useful for you when you move your pieces over the board. You can write a callback function for that.
pcolor() is really surf() followed by view(2).
pcolor() interpolates face colors according to the surrounding four nodes.
If you want face colors directly you should be using imagesc()

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

jonas
jonas am 26 Okt. 2018
Bearbeitet: jonas am 26 Okt. 2018
You could use pcolor, here's a checkerboard example
A = repmat([repmat([1 0],1,10);repmat([0 1],1,10)],10,1)
pcolor(A)
You can build a color palette like this
cmap = [0 0 0;1 1 1;1 0 0;0 0 1;0 1 0];
colormap(cmap)
Now you can paint with these colors, by assigning values between, for example, 0 and 4 (you have 5 colors).
A(10,10)=4;
Makes the square at [10;10] green

Weitere Antworten (0)

Kategorien

Mehr zu Board games finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by