Checkerboard design from zeros matrix
2 views (last 30 days)
Show older comments
I have to program an 8x8 matrix randomly with 0's(blank), 1's(red), 2's(black) and one 3(king). What I'm stuck on is writing if statements regarding the possible moves for the king from wherever it is placed. Also compiling my x(c)=0 statement so it isn't so cluttered. As of right now I have;
x=zeros(8,8);
for j=1:8
for k=1:8
x(j,k)=randi([0,2]);
end
end
x([2,4,6,8,9,11,13,15,18,20,22,24,25,27,29,31,34,36,38,40,41,43,45,47,50,52,54,56,57,59,61,63])=0
x(randi(numel(x)))=3
disp(xf);
When input this I get this;
1 0 0 0 2 0 1 0
0 2 0 1 0 1 0 0
1 0 2 0 0 0 0 0
0 3 0 1 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 2 0 1 0 1 0
0 1 0 0 0 0 0 1
From this obviously the 3 can only go down right/left but i'm not sure on how to even start the statements.
2 Comments
Jan
on 26 Sep 2018
Replace:
x=zeros(8,8);
for j=1:8
for k=1:8
x(j,k)=randi([0,2]);
end
end
by the much easier:
x = randi([0,2], 8, 8);
Then focus on the question:
What I'm stuck on is writing if statements regarding the possible moves
for the king from wherever it is placed.
You write:
From this obviously the 3 can only go down right/left
No, this is not obvious. You didn't mention the rules yet. Therefore it is not clear, what you want to implement and which "if statements" you mean.
What does "compiling my x(c)=0 statement" mean? There is no "x(c)=0" statement in the posted code.
It is not clear also, what you call a "Checkerboard design". Checkerboards contain black and white fields, but your board hast 3 colors.
Which problem do you want to solve?
Answers (1)
Steven Lord
on 26 Sep 2018
Consider the row and column numbers of the squares where you are allowed to place pieces. Do you see anything interesting relating those two? [Hint: think even or odd.] If you still don't see it, take a look at the chessboard example on this Wikipedia page.
So that will tell you which squares must be blank or 0. [Personally, unless the assignment requires it I'd make the squares where pieces can't be NaN and squares where pieces could be but aren't right now 0.] Fill in the rest with numbers between 0 and 2. Finally, find squares where pieces could be and randomly (with randi or randperm) select one of those squares for your king.
4 Comments
Guillaume
on 26 Sep 2018
Edited: Guillaume
on 26 Sep 2018
Seems fairly straightforward to me:
- Look at each of the 8 adjacent square, one by one. For each adjacent square:
- is the square outside the board -> cannot go there
- is the square occupied by an opponent piece -> can capture the piece
- is the square occupied by your own piece -> cannot go there
- is the square empty -> can move there
That's one for loop and 4 if statements.
Steven Lord
on 26 Sep 2018
The mod function will help with even number detection.
If your king is at coordinates (x, y) what are the coordinates of the squares to which it is allowed by the rules of checkers to move, written in terms of x and y?
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!