Tips on grids and matrix

3 views (last 30 days)
Melissa Hickman
Melissa Hickman on 3 Apr 2021
Answered: Arjun on 30 May 2025
Very confused on how to go about this:
I need to write a script for a "rock paper scissors game" that creates:
- a grid of 2D points with integer values, varying from 1 to N.
- an NxN matrix A that represents the initial distribution of the three populations (each population being "rock", "paper", or "scissors")
Not looking for answers, just point me in the right direction please! i am awful at matlab

Answers (1)

Arjun
Arjun on 30 May 2025
As I understand it, you need a 2D grid of integer values and an N×N matrix populated with a random distribution of "rock", "paper", and "scissors".
To create a 2D grid of points with values ranging from 1 to N, you can start by initializing an N×N cell array using MATLAB’s "cell" function. Then, use nested "for" loops to populate the grid with integer values. Please refer to the code segment below:
N=4;
grid = cell(N, N);
for i = 1:N
for j = 1:N
grid{i, j} = [i, j];
end
end
disp(grid);
{[1 1]} {[1 2]} {[1 3]} {[1 4]} {[2 1]} {[2 2]} {[2 3]} {[2 4]} {[3 1]} {[3 2]} {[3 3]} {[3 4]} {[4 1]} {[4 2]} {[4 3]} {[4 4]}
To generate the desired matrix with the initial distribution, you can again create an N×N cell array and use nested for loops to fill it. A random number generator such as "randi" can be used to assign values from 1 to 3, where 1 represents "rock", 2 represents "paper", and 3 represents "scissors". Please refer to the code section below:
N=4;
populations = {'rock', 'paper', 'scissors'};
A = cell(N, N);
for i = 1:N
for j = 1:N
A{i, j} = populations{randi(3)};
end
end
disp(A);
{'rock' } {'paper' } {'scissors'} {'rock' } {'paper' } {'paper' } {'paper' } {'scissors'} {'scissors'} {'paper' } {'scissors'} {'scissors'} {'rock' } {'scissors'} {'rock' } {'scissors'}
I hope this helps!

Categories

Find more on Just for fun in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!