Create game board in matlab

6 次查看(过去 30 天)
Krish Desai
Krish Desai 2015-11-26
回答: arushi 2024-8-28
I want to create a board that displays for the user to see. I then want the user to be able to enter an x and y coordinate and replace that value with the value from another board.
Example: Board user sees:
1 2 3 4 5
1 x x x x x
2 x x x x x
3 x x x x x
4 x x x x x
5 x x x x x
Actual board:
1 2 3 4 5
1 x x x x x
2 x x x k x
3 k x x x x
4 x x k x x
5 x x x x x
So if the user enters in x=3 and y=4 the new board the user sees is:
1 2 3 4 5
1 x x x x x
2 x x x x x
3 x x x x x
4 x x k x x
5 x x x x x

回答(1 个)

arushi
arushi 2024-8-28
Hi Krish,
To implement a system where a user can enter coordinates to update a visible board with values from another board, you can follow these steps :
% Initialize the boards
visibleBoard = repmat('x', 5, 5);
actualBoard = [
'x', 'x', 'x', 'x', 'x';
'x', 'x', 'x', 'k', 'x';
'k', 'x', 'x', 'x', 'x';
'x', 'x', 'k', 'x', 'x';
'x', 'x', 'x', 'x', 'x'
];
% Function to display the board
function displayBoard(board)
disp(' 1 2 3 4 5');
for i = 1:size(board, 1)
fprintf('%d ', i);
fprintf('%s ', board(i, :));
fprintf('\n');
end
end
% Main loop
while true
% Display the current visible board
disp('Current Board:');
displayBoard(visibleBoard);
% Get user input
x = input('Enter the x coordinate (1-5): ');
y = input('Enter the y coordinate (1-5): ');
% Validate input
if x < 1 || x > 5 || y < 1 || y > 5
disp('Invalid coordinates. Please enter values between 1 and 5.');
continue;
end
% Update the visible board with the value from the actual board
visibleBoard(y, x) = actualBoard(y, x);
% Display the updated board
disp('Updated Board:');
displayBoard(visibleBoard);
% Check if the user wants to continue
cont = input('Do you want to enter another coordinate? (y/n): ', 's');
if strcmpi(cont, 'n')
break;
end
end
Hope this helps.

类别

Help CenterFile Exchange 中查找有关 Board games 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by