Checkerboard design from zeros matrix

2 Ansichten (letzte 30 Tage)
vincent stevenson
vincent stevenson am 26 Sep. 2018
Kommentiert: Steven Lord am 26 Sep. 2018
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 Kommentare
Jan
Jan am 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?
vincent stevenson
vincent stevenson am 26 Sep. 2018
What I wanted to find was an easier way to write was my x(2,4,6,8,....) so I didn't have to write all 32 numbers out. The numbers aren't color based but piece based. So a 0 is an unoccupied space on the board while a 1 serves as a red piece, a 2 as a black piece and 3 as a red king. Its not so much about their colors but their roles in the game of checkers. So in the position the 3 is in the up and left spot is blocked by a wall, up right has a 1 behind the 2 but the bottom left/right is free for movement. I'm just stuck on conveying that in to if statements.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Steven Lord
Steven Lord am 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 Kommentare
Guillaume
Guillaume am 26 Sep. 2018
Bearbeitet: Guillaume am 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
Steven Lord am 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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming 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