Indexing into an array for a scorecard.

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

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).

Sign in to comment.

Answers (1)

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

Lets say I wanted to place an 'x' where they player chooses to save. So using your example I wanted an 'x' for player 2 column and aces row. I didn't quite understand the code you sent, but after a few minutes of playing with it and displaying the variables at the end it's beginning to make sense. I really appreciate your help. Thanks.
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';
Great thank you so much. As this project evolves I'm sure i'll run into more problems. Thanks again.

Sign in to comment.

Categories

Find more on Word games in Help Center and File Exchange

Asked:

on 2 Feb 2016

Commented:

on 2 Feb 2016

Community Treasure Hunt

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

Start Hunting!