User:DanBron/Game of Nim: Difference between revisions

Added MATLAB example
(Added MATLAB example)
Line 41:
'''Notes on execution''': Just define <tt>nim</tt> in the REPL and call it as per the example.<br/>
'''Golf score''': 23 bytes (including the assigment, i.e. naming the function; 18 bytes for the function definition).
 
=={{header|MATLAB}}==
Full game implemented in standard method with two computer players
<lang MATLAB>function GameOfNim
board = [4 7 2 2 1];
fprintf('Board: [ %s]\n', sprintf('%d ', board))
turnA = true;
while sum(board)
if turnA % Player A - uses strategy, makes first possible move
nimMove = NextMoveNim(board);
movePlace = find(nimMove, 1);
if movePlace % An optimal move is possible
board(movePlace) = board(movePlace)-nimMove(movePlace);
else % No optimal move - move randomly
possMoves = find(board);
movePlace = possMoves(randi(length(possMoves)));
board(movePlace) = board(movePlace)-randi(board(movePlace));
end
fprintf('Player A: [ %s]\n', sprintf('%d ', board))
else % Player B - moves randomly
possMoves = find(board);
movePlace = possMoves(randi(length(possMoves)));
board(movePlace) = board(movePlace)-randi(board(movePlace));
fprintf('Player B: [ %s]\n', sprintf('%d ', board))
end
turnA = ~turnA;
end
end
 
function nimMove = NextMoveNim(board)
boardNimSum = CalcNimSum(board);
placeNimSums = zeros(size(board));
for k = 1:length(board)
placeNimSums(k) = CalcNimSum([board(k) boardNimSum]);
end
movePlaces = placeNimSums < board;
nimMove = zeros(size(board));
nimMove(movePlaces) = board(movePlaces)-placeNimSums(movePlaces);
end
 
function nimSum = CalcNimSum(board)
boardBinary = dec2bin(board)==49; % Shortcut to avoid using de2bi()
nimBinary = boardBinary(1, :);
for k = 2:length(board)
nimBinary = xor(nimBinary, boardBinary(k, :));
end
nimSum = bin2dec(char(nimBinary+48)); % Shortcut to avoid using bi2de()
end</lang>
{{out}}
From a first-player advantage game (non-zero initial Nim-sum)
<pre>Board: [ 4 7 2 2 1 ]
Player A: [ 4 5 2 2 1 ]
Player B: [ 3 5 2 2 1 ]
Player A: [ 3 2 2 2 1 ]
Player B: [ 3 2 2 0 1 ]
Player A: [ 1 2 2 0 1 ]
Player B: [ 1 2 2 0 0 ]
Player A: [ 0 2 2 0 0 ]
Player B: [ 0 2 0 0 0 ]
Player A: [ 0 0 0 0 0 ]</pre>
From a zero initial Nim-sum game
<pre>Board: [ 3 4 7 ]
Player A: [ 3 4 3 ]
Player B: [ 3 0 3 ]
Player A: [ 1 0 3 ]
Player B: [ 1 0 1 ]
Player A: [ 1 0 0 ]
Player B: [ 0 0 0 ]</pre>
Human-readable version of the "next move" function (with whitespace and descriptive variables)
<lang MATLAB>function nim(decBoard)
binBoard = de2bi(decBoard);
allSum = mod(sum(binBoard), 2);
for k = 1:numel(decBoard)
placeSum(k) = bi2de(binBoard(k, :)+allSum == 1);
end
moves = decBoard-placeSum;
moves(moves < 0) = 0;
disp(moves)
end</lang>
Code golf version of "next move" function (104 characters, including newlines). Requires Communications System Toolbox for de2bi() and bi2de() functions.
<lang MATLAB>function n(d)
b=de2bi(d);a=mod(sum(b),2);for k=1:numel(d)
p(k)=bi2de(b(k,:)+a==1);end
m=d-p;m(m<0)=0</lang>
No toolbox requirements (121 characters, including newlines)
<lang MATLAB>function n(d)
b=dec2bin(d)==49;a=mod(sum(b),2);for k=1:numel(d)
p(k)=bin2dec(char(b(k,:)+a==1)+48);end
m=d-p;m(m<0)=0</lang>
{{out}}
Usage
<pre>>> n([4 7 2 2 1])
 
m =
 
0 2 2 2 0
>> n([3 4 5])
 
m =
 
2 0 0</pre>
 
 
=={{header|Racket}}==
Anonymous user