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 comentario

Image Analyst
Image Analyst el 2 de Feb. de 2016
Editada: Image Analyst el 2 de Feb. de 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).

Iniciar sesión para comentar.

Respuestas (1)

Guillaume
Guillaume el 2 de Feb. de 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 comentarios

Gordon Price
Gordon Price el 2 de Feb. de 2016
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';
Gordon Price
Gordon Price el 2 de Feb. de 2016
Great thank you so much. As this project evolves I'm sure i'll run into more problems. Thanks again.

Iniciar sesión para comentar.

Categorías

Más información sobre Word games en Centro de ayuda y File Exchange.

Preguntada:

el 2 de Feb. de 2016

Comentada:

el 2 de Feb. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by