board game any help?

34 Ansichten (letzte 30 Tage)
Adel Falah
Adel Falah am 7 Jan. 2021
Kommentiert: DGM am 13 Nov. 2025 um 23:44
I just need help in the start. This is my first course in matlab so I'd appreciate the help.
I have task with board game. My question:
How can I color my matrix randomly with rgb colors without using imshow(cause I didn't learn it in the course)?
Here is my code:
a=1:25;
if((length(a)>8))
M=ones(6);
c=[0.3,0.5,0.8];
for nn=1:3
%first itr:
a=1:25;
out=a(randperm(25,24));
a(out)=[];
x=1:6;
y=1:6;
[X,Y]=meshgrid(x,y);
X1=X(out);
Y1=Y(out);
for i=1:24
M(X1(i),Y1(i))=c(nn);
end
// hsv = [.6 1 1; .6 .7 1; .6 .5 1; .6 .3 1; .6 0 1];
//rgb = hsv2rgb(hsv);
%%figure;imshow(x,x,M)//didnt learn it got it from the internet
end
end
figure;pcolor(x,x,M)
I tried to do the code in 3 dimension zeros(5,5,3).
but couldn't plot it without imshow, I'm having trouble specially with the color (hsv2rgb) cause i have to have white colors in cells I didn't color.
Also when I tried to put soldiers (another requirement) as * in the matrix I had a big problem cause they can't set on the middle of the cell, they set on the edge,
So I would really like some help.
Thank you.

Antworten (2)

Image Analyst
Image Analyst am 8 Jun. 2022
Bearbeitet: Image Analyst am 13 Nov. 2025 um 17:55
Not sure what you want. Do you just want a randomly colored image? If so do
rgbImage = rand(5, 5, 3);
imshow(rgbImage, 'InitialMagnification', 2500);
axis('on', 'image')
Note: pcolor does not plot all the rows and columns in a tiled manner so pcolor of a 5 by 5 array will only show 4 by 4 "tiles".
values = rand(5,5) % Create a 5x5 array.
values = 5×5
0.9073 0.4728 0.6602 0.1790 0.3753 0.1587 0.4485 0.3155 0.0413 0.1903 0.2228 0.6363 0.0581 0.6035 0.1719 0.7284 0.2109 0.8952 0.7086 0.3799 0.7448 0.1206 0.8698 0.9561 0.1673
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
pcolor(values); % Shows 4x4, not 5x5
cmap = rand(32, 3); % Create a colormap with random colors.
colormap(cmap);
colorbar;

DGM
DGM am 12 Nov. 2025 um 20:18
I posted this in response to an AI-generated answer before they revealed themselves to be a spambot. That's why it references patch(). I figured I might as well keep the example.
Sure, pcolor() is problematic in this application, but I don't see why creating a bunch of patch() objects is the obvious solution. Just use image()/imagesc()/imshow().
% Board size
n = 5; % this was only 6 because OP was using pcolor()
M = rand(n,n,3); % random RGB colors for each cell
% Set some cells to white (e.g., uncolored)
mask = rand(n,n) < 0.3; % ~30% white
M(repmat(mask,[1 1 3])) = 1; % apply the mask
% display it
imshow(M,'initialmagnification','fit')
hold on
% place markers wherever they're needed
% you could use plot(), but using scatter() allows
% both edge and face color, improving visibility
markercolor = [1 1 0]; % pick something
populatedcells = rand(n,n) < 0.2; % figure out where you want them
[pcy pcx] = find(populatedcells);
hs = scatter(pcx,pcy,80,'filled', ...
'markeredgecolor',1-markercolor, ...
'markerfacecolor',markercolor, ...
'linewidth',2);
After that, there's no need to replot anything. Just update the xy data in hs.
Probably also:
  2 Kommentare
Image Analyst
Image Analyst am 13 Nov. 2025 um 17:55
Who/where is the bot post?
DGM
DGM am 13 Nov. 2025 um 23:44
I nuked it. It was internally contradictory AI generated garbage that was posted with zero effort for the purpose of later editing and inserting a bunch of spam links. The user who posted said answer also went and spam-ified all their other answers on the same day. I nuked all of them and reported the account.
If the answer is notably poor (Walter was the first to point out that the code didn't even match the text description) and it's clear to me that the answer was posted with insincere motives, I'm going to delete it.
For what it's worth to any readers, here is my own example of solving the problem using patch(). I suppose it has some advantages, but the setup isn't as convenient. I'm not going to implement the selective omission of tiles. That can be done by either setting entries in CT to a "background" color, or by removing entries from F.
n = 5; % same board size as before
CT = rand(n^2,3); % use a color table instead of an image
% construct vertex and face lists
% use the same vertex locations used by image()/imshow()
% these faces are in column-major order
[vx vy] = meshgrid(0.5:n+0.5);
V = [vx(:) vy(:)];
F = zeros(n^2,4);
k = 1;
for kx = 1:n
for ky = 1:n
F(k,:) = [ky ky+1 ky+1 ky] + (n+1)*[kx-1 kx-1 kx kx];
k = k+1;
end
end
% plot using a single patch() object
patch('faces',F,'vertices',V, ...
'facevertexcdata',CT,'facecolor','flat', ...
'edgecolor','k','linewidth',3);
hold on; axis equal tight
% place markers as before
markercolor = [1 1 0]; % pick something
populatedcells = rand(n,n) < 0.2; % figure out where you want them
[pcy pcx] = find(populatedcells);
hs = scatter(pcx,pcy,80,'filled', ...
'markeredgecolor',1-markercolor, ...
'markerfacecolor',markercolor, ...
'linewidth',2);
If I recall correctly, the original answer generated N^2 patch objects. This example only uses one.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Board games finden Sie in Help 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