placing spots on graph

1 visualizzazione (ultimi 30 giorni)
James Webber
James Webber il 4 Gen 2022
Risposto: Prateekshya il 26 Set 2023
% Demo by Image Analyst, December, 2021.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
numSpaces = 6;
myBoard = zeros(numSpaces+1,numSpaces+1);
computersBoard = zeros(numSpaces+1,numSpaces+1);
%-----------------------------------------------------------------------
% YOUR BOARD.
subplot(1, 2, 1);
pcolor(myBoard);
colormap([1,1,1])
axis square
title('Your Board', 'FontSize', 15)
% Set up the outside tick labels.
xticks(0.5 : numSpaces + 0.5)
xtl = {'0', '1', '2', '3', '4', '5', '6'};
xticklabels(xtl)
yticks(0.5 : numSpaces + 0.5)
ytl = {'0', 'F', 'E', 'D', 'C', 'B', 'A'};
yticklabels(ytl)
% Put up grid locations in the cells.
counter = 1;
for row = 1 : numSpaces
for col = 1 : numSpaces
str = sprintf('%d', counter);
xt = row + 0.5;
yt = numSpaces - col + 1.5;
text(xt, yt, str, 'Color', 'r', 'FontSize', 8, 'FontWeight','bold', 'HorizontalAlignment','center')
counter = counter + 1;
end
end
%-----------------------------------------------------------------------
% COMPUTER'S BOARD.
subplot(1, 2, 2);
pcolor(computersBoard);
axis square
title('Computer Board', 'FontSize', 15)
% Set up the outside tick labels.
xticks(0.5 : numSpaces + 0.5)
xticklabels(xtl)
yticks(0.5 : numSpaces + 0.5)
yticklabels(ytl)
% Put up grid locations in the cells.
% This has the numbers going in the opposite direction.
counter = 1;
for col = 1 : numSpaces
for row = 1 : numSpaces
str = sprintf('%d', counter);
xt = row + 0.5;
yt = numSpaces - col + 1.5;
text(xt, yt, str, 'Color', 'r', 'FontSize', 8, 'FontWeight','bold', 'HorizontalAlignment','center')
counter = counter + 1;
end
end
%i have attached a screenshot of what the program does when you run it
% this is my code for the grids how do i write code so that when i type in
% a certian number of the grid it will chage to a diffrent colour??
%i would like it to be for both grids the computer and player 1 but to
%input the number sepertally

Risposte (1)

Prateekshya
Prateekshya il 26 Set 2023
Hi James,
As per my understanding, you are trying to indicate the moves of the player and the computer alternatively according to the number they have chosen. This can be done by reverse calculating the values of "xt" and "yt" from a given number present in the board. You can consider the following example:
Create a utility function that returns the value of "xt" and "yt" for both the boards according to an input argument. Here "boardType = 0" indicates your board and "boardType = 1" indicates the computer's board. This value can be changed according to the requirement.
function [xt, yt] = getPosition(inputNumber, numSpaces, boardType)
rowVal = floor((inputNumber-1)/numSpaces)+1; % Find out the value of the row
colVal = mod(inputNumber,numSpaces); % Find out the value of the column
if colVal == 0 colVal = 6; end % Modify column value to 6 if it is 0
% Swap the values if the board is for the computer
if boardType == 1
temp = rowVal;
rowVal = colVal;
colVal = temp;
end
xt = rowVal+0.5; % Calculate xt
yt = numSpaces-colVal+1.5; % Calculate yt
end
This function can be called to calculate the "xt" and "yt" values each time you want to change the colour. Now you can choose the subplot according to the requirement and use the "text" function to change the colour of the font. Here are some examples:
inputNumber = 36;
[xt, yt] = getPosition(inputNumber,numSpaces,0); % For your board
subplot(1,2,1) % Select the corresponding subplot
text(xt, yt, sprintf('%d',inputNumber) , 'Color', 'b', 'FontSize', 8, 'FontWeight','bold', 'HorizontalAlignment','center')
inputNumber = 17;
[xt, yt] = getPosition(inputNumber,numSpaces,1); % For computer's board
subplot(1,2,2) % Select the corresponding subplot
text(xt, yt, sprintf('%d',inputNumber), 'Color', 'g', 'FontSize', 8, 'FontWeight','bold', 'HorizontalAlignment','center')
The above code gives the following result:
I hope this helps!

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by