Code covered by the BSD License  

Highlights from
Flood Game

image thumbnail
from Flood Game by Alessandro Masullo
Simple flood Game. Try to flood the matrix with the same symbol.

game.m
%
%
%%%% Flood Game by Alessandro Masullo, 2013 %%%%
%
clc, clear
N = 20;        % Game size
sym = '0+.-*'; % Symbols to be used in the game
diffic = 6;    % Game difficulty [1-10]

color = ceil(rand(N) * (numel(sym) - 1)); % Random game
path = zeros(numel(color), 1);            % User path, contains filled cells
path(1) = 1;                              % Initial path
user = color(1,1);                        % Initial user choise

max_attempt = ceil(N * (3-diffic/3));

%
%%%% The game %%%%
%
for attempt = 1:max_attempt
    % Check in the neighboring points of the path for user input elements
	for i = 1:length(path)
		if path(i) > 0
			[A, B] = ind2sub([N N], path(i));
			if A < N && color(A+1, B) == user
                path(sub2ind([N N], A+1, B)) = sub2ind([N N], A+1, B);
            end
			if A > 1 && color(A-1, B) == user
                path(sub2ind([N N], A-1, B)) = sub2ind([N N], A-1, B);
            end
			if B < N && color(A, B+1) == user
                path(sub2ind([N N], A, B+1)) = sub2ind([N N], A, B+1);
            end
			if B > 1 && color(A, B-1) == user
                path(sub2ind([N N], A, B-1)) = sub2ind([N N], A, B-1); 
            end
		end
    end
    
    % Update the matrix with the new filled elements
	color(path > 0) = user;
    
    % Display the game matrix
    clc
	for si = 1:N
		for sj = 1:N
			fprintf('%s ', sym(color(si,sj)))
		end
		fprintf('\n')
    end
    
    % Check for the win
    if numel(find(path > 0)) == numel(color)
        win = ['"$+-.%%09<718>@CJJMUOPLJD?;50'
               'RFJG72>DEMTNEIQEFPP34(7(''''+%&'
               '"'',.*")4;:32=?AEJJWRPNIHA><3 '
               'KFSC17AFIRRGENKDJR)6.55''5''6.D'];
        winm = double(reshape(win,2,116/2));
        yy = spline(1:length(winm), winm(2,:), 1:0.1:length(winm));
        xx = spline(1:length(winm), winm(1,:), 1:0.1:length(winm));
        plot(65, 57, 'or', xx, yy, 'LineWidth', 2)
        break
    end
    
    % User input
    while true
        user_in = input(sprintf('Game (%d/%d)\nInsert symbol ''%s'': ', ...
                                [attempt max_attempt sym]), 's');
        if ischar(user_in) && not(isempty(strfind(sym, user_in)))
            user = find(sym == user_in(1));
            break
        end
    end
end

disp('Game over!')

Contact us