Indexing into an array for a scorecard.
Show older comments
I'm creating a scorecard for a game. I have a matrix that's created using if statements based on the amount of players. So if there's 2 players but you start over and add a 3rd it would add another column to track scores.
x = {[],[],player{1},player{2};
[1],'Aces',[],[];
[2],'Twos',[],[];
[3],'Threes',[],[];
[4],'Fours',[],[];
[5],'Fives',[],[];
[6],'Sixes',[],[];
[7],'Bonus',[],[];
[8],'3 of a kind',[],[];
[9],'4 of a kind',[],[];
[10],'Full House',[],[];
[11],'Small Straight',[],[];
[12],'Large Straight',[],[];
[13],'Yahtzee',[],[];
[14],'Chance',[],[];
[15],'Yahtzee Bonus',[],[];
[16],'Total',[],[]};
disp(x);
So the problem I'm having is lets say after the round the player chooses to save his score. How could I index into the matrix with the players score. I'm confused because there's multiple players. If it's player 2's turn and he wants to save Aces, I want to be able to check if he's saved a score there and if he hasn't put it there.
1 Comment
Image Analyst
on 2 Feb 2016
Edited: Image Analyst
on 2 Feb 2016
What is inside the player cell? What is player{1}? A number? A structure? An array? What?
What column contains the scores?
Why not use a table instead? Easier and less overhead (more efficient).
Answers (1)
Guillaume
on 2 Feb 2016
Note that x is a cell array, not a matrix.
Also, it's not a particularly good idea to store the metainformation (column and row names) in the same array as the content. Personally, I'd get rid of the first row and column and, if required, store them into two other cell arrays.
Anyway, to answer: "If it's player 2's turn and he wants to save Aces, I want to be able to check if he's saved a score there and if he hasn't put it there":
player_number = 2; %input
card = 'Aces'; %input
score = 20; %input
card_row = find(strcmp(x(:, 2), card));
player_column = player_number + 2;
if isempty(x(card_row, player_column))
%no score for player and card
x(card_row, player_column) = score;
else
%already a score in there
%what do you want to do
end
3 Comments
Gordon Price
on 2 Feb 2016
Guillaume
on 2 Feb 2016
The first two lines of the code find the row and column corresponding to the score. The if test to see if that score is empty, if it is it puts a value in there. I put a number but you could just as well put a 'x' in there.
x(card_row, player_column) = 'x';
Gordon Price
on 2 Feb 2016
Categories
Find more on Word games 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!