Converting cell of strings to matrix of numbers for Chess Program (and java heap error!)

2 views (last 30 days)
I'm programming a chess game and I've got a cell of strings which the GUI uses to draw the boards, but want to convert this into an array for the calculations.
tmp = {...
'rook:2','knight:2','bishop:2','queen:2','king:2','bishop:2','knight:2','rook:2';...
'pawn:2','pawn:2','pawn:2','pawn:2','pawn:2','pawn:2','pawn:2','pawn:2';...
'none:0','none:0','none:0','none:0','none:0','none:0','none:0','none:0';...
'none:0','none:0','none:0','none:0','none:0','none:0','none:0','none:0';...
'none:0','none:0','none:0','none:0','none:0','none:0','none:0','none:0';...
'none:0','none:0','none:0','none:0','none:0','none:0','none:0','none:0';...
'pawn:1','pawn:1','pawn:1','pawn:1','pawn:1','pawn:1','pawn:1','pawn:1';...
'rook:1','knight:1','bishop:1','queen:1','king:1','bishop:1','knight:1','rook:1' };
What I want is a matrix that looks like:
[5 3 3 9 200 3 3 5...
1 1 1 1 1 1 1 1...
etc.
]
Where the numbers correspond to the relative value of the pieces. And I also need to be able to change these back again to a cell of strings.
Additionally, the reason for doing this is that at the moment I keep on getting "failed to retrieve exception message" if I look two steps ahead. The size of tmp is 7938mb and there are four additional versions of it which need to be constantly updated, so I wondered if this is what was causing the java heap storage to fail. I have already increased the storage in the .opts file to as much as I can, as well as downloading and using the jheapcl function and still no luck.
If you can help at all with either queries it would be much appreciated!
Thanks in advance,
Chris
  1 Comment
Guillaume
Guillaume on 26 Nov 2014
If the issue is one of memory used by matrices (it's certainly not clear from the limited information you've posted about the error), increasing the java heap storage is not going to help at all. If anything it's going to make things worse, since it's actually going to decrease the memory available for storing matrices (it's used for something else).

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 26 Nov 2014
It doesn't look like the colour of the pieces matter for your numerical matrix, so you would be better off storing that in a matrix or cell array separate to the pieces name. As it is, the first thing is to get rid of that information:
boardpieces = cellfun(@(e) regexp(e, '[^:]+', 'match', 'once'), tmp, 'UniformOutput', false)
You can then use the second output of ismember to index into a matrix of values:
pieces = {'none', 'pawn', 'knight', 'bishop', 'rook', 'queen', 'king'};
values = [0 1 3 3 5 9 200];
[~, idx] = ismember(boardpieces, pieces);
boardvalue = values(idx);
  1 Comment
Chris
Chris on 26 Nov 2014
Thanks very much, that solved my question perfectly. As for the other issue, it turns out it wasn't a problem with storage at all, but a typo which was sending it into a loop.

Sign in to comment.

More Answers (0)

Categories

Find more on Just for fun 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!