Docsity

Análisis de Casos de Éxito en el Desarrollo de Software - Prof. Quintero, Ejercicios de Programación C

Vista previa parcial del texto

¡Descarga Análisis de Casos de Éxito en el Desarrollo de Software - Prof. Quintero y más Ejercicios en PDF de Programación C solo en Docsity!

SOFTWARE PARA INGENIERIA

ELABORADO POR

CARLOS JULIO PENARANDA

CÓDIGO:

GRUPO:

203036_

TUTOR:

DIEGO FERNANDO VASCO

UNIVERSIDAD NACIONAL ABIERTA Y A DISTANCIA UNAD

MARCO TEÓRICO.

REQUERIMIENTOS DEL PROGRAMA.

Para identificar de manera clara los requerimientos funcionales del juego batalla naval se

utiliza el diagrama de casos de usos.

Descripción de los requerimientos funcionales.

Nombre R1-Disparar Actor Jugador

Resumen Permite seleccionar una posición de la matriz para realizar el disparo.

Entrada Posición en x de la casilla, posición en y de la casilla.

Resultad

o

El disparo en el tablero se ha realizado, actualizando la casilla como disparo

realizado.

 Función leer reglas: Esta función permite mostrar las reglas de puntuación del

juego batalla naval.

 Función disparos realizados: Esta función permite obtener la cantidad de disparos

realizados por el jugador.

 Función barcos hundidos: Esta función permite obtener la cantidad de barcos

hundidos.

 Función puntos logrados: Esta función permite obtener la cantidad de puntos

logrados.

 Función casilla existe: Esta función permite saber s una casilla existe en el mapa,

ideal para que los barcos no queden por fuera de la matriz.

 Función casilla ocupada: Esta función permite saber si una casilla esta ocupada o

no, recibe como parámetro la posición en la columna como en la fila, ideal para

cuando se realizan los disparos o cuando se están ubicando los barcos para que

no queden superpuestos.

 Función escoger posición: Esta función permite ubicar los barcos de manera

horizontal o vertical, recibe como parámetro esta orientación.

CÓDIGO.

function battleship(action) % Software para ingenieria % Estudiante: Carlos Julio Penaranda % Grupo: 203036_ % cedula: 72131709 if nargin== action = 'create'; end % if strcmp(action, 'create') battle; % initialize chart filled = [0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0]; type = ['M','M','M','M','M','M','M','M','M','M',... 'M','M','M','M','M','M','M','M','M','M',... 'M','M','M','M','M','M','M','M','M','M',...

'M','M','M','M','M','M','M','M','M','M',...

'M','M','M','M','M','M','M','M','M','M',...

'M','M','M','M','M','M','M','M','M','M',...

'M','M','M','M','M','M','M','M','M','M',...

'M','M','M','M','M','M','M','M','M','M',...

'M','M','M','M','M','M','M','M','M','M',...

'M','M','M','M','M','M','M','M','M','M'];

% Matriz de 10 x 10 astart = rand(1); astart = 100*astart; astart = astart - mod(astart,1); % Get direction direction = rand(1); if direction >. direction = 'horizontal'; elseif direction >. direction = 'vertical'; end if strcmp(direction,'horizontal') == 1 apoint1 = astart + 1; apoint2 = astart + 2; apoint3 = astart + 3; apoint4 = astart + 4; if mod(apoint4,10) < 5 & mod(apoint4,10) > 0 apoint1 = astart - 1; apoint2 = astart - 2; apoint3 = astart - 3; apoint4 = astart - 4; end elseif strcmp(direction,'vertical') == 1 apoint1 = astart + 10; apoint2 = apoint1 + 10; apoint3 = apoint2 + 10; apoint4 = apoint3 + 10; if apoint4 > 100 apoint1 = astart - 10; apoint2 = apoint1 - 10; apoint3 = apoint2 - 10; apoint4 = apoint3 - 10; end elseif strcmp(direction,'diagonal_up_slope') == 1 if astart < 41 apoint1 = astart + 9; % must reverse direction apoint2 = apoint1 + 9; apoint3 = apoint2 + 9; apoint4 = apoint3 + 9; if mod(astart,10) < 5 & mod(astart,10) > 0 apoint1 = astart + 11; apoint2 = apoint1 + 11; apoint3 = apoint2 + 11; apoint4 = apoint3 + 11; end else apoint1 = astart - 9; apoint2 = apoint1 - 9;

% Juego start_again = 1; while start_again == 1 xx = 'a'; while strcmp(xx,'a') == 1 bstart = rand(1); bstart = 100*bstart; bstart = bstart - mod(bstart,1); if filled(bstart) == 0 xx = 'b'; end end % Get direction direction = rand(1); if direction >. direction = 'horizontal'; elseif direction >. direction = 'vertical'; end if strcmp(direction,'horizontal') == 1 bpoint1 = bstart + 1; bpoint2 = bstart + 2; bpoint3 = bstart + 3; if mod(bpoint3,10) < 4 & mod(bpoint3,10) > 0 % Ran off the end of the plot bpoint1 = bstart - 1; bpoint2 = bstart - 2; bpoint3 = bstart - 3; end elseif strcmp(direction,'vertical') == 1 bpoint1 = bstart + 10; bpoint2 = bpoint1 + 10; bpoint3 = bpoint2 + 10; if bpoint3 > 100 bpoint1 = bstart - 10; bpoint2 = bpoint1 - 10; bpoint3 = bpoint2 - 10; end elseif strcmp(direction,'diagonal_up_slope') == 1 if bstart < 31 bpoint1 = bstart + 9; % must reverse direction bpoint2 = bpoint1 + 9; bpoint3 = bpoint2 + 9; if mod(bstart,10) < 4 & mod(bstart,10) > 0 bpoint1 = bstart + 11; % must change to down slope bpoint2 = bpoint1 + 11; bpoint3 = bpoint2 + 11; end else bpoint1 = bstart - 9; bpoint2 = bpoint1 - 9; bpoint3 = bpoint2 - 9; if mod(bstart,10) > 7 | mod(bstart,10) == 0 bpoint1 = bstart + 9; bpoint2 = bpoint1 + 9; bpoint3 = bpoint2 + 9;

if bstart > 77 bpoint1 = bstart - 11; bpoint2 = bpoint1 - 11; bpoint3 = bpoint2 - 11; end end end else % diagonal down_slope if bstart > 70 bpoint1 = bstart - 11; % must reverse direction bpoint2 = bpoint1 - 11; bpoint3 = bpoint2 - 11; if mod(bstart,10) < 4 & mod(bstart,10) > 0 bpoint1 = bstart - 9; % must change to down slope bpoint2 = bpoint1 - 9; bpoint3 = bpoint2 - 9; end else bpoint1 = bstart + 11 ; bpoint2 = bpoint1 + 11; bpoint3 = bpoint2 + 11; if mod(bstart,10) > 7 | mod(bstart,10) == 0 bpoint1 = bstart - 11; bpoint2 = bpoint1 - 11; bpoint3 = bpoint2 - 11; if bstart < 31 bpoint1 = bstart + 9; bpoint2 = bpoint1 + 9; bpoint3 = bpoint2 + 9; end end end end if filled(bpoint1) == 1 | filled(bpoint2) == 1 | filled(bpoint3) == 1 start_again = 1; else start_again = 0; filled(bstart) = 1; filled(bpoint1) = 1; filled(bpoint2) = 1; filled(bpoint3) = 1; type(bstart) = 'B'; type(bpoint1) = 'B'; type(bpoint2) = 'B'; type(bpoint3) = 'B'; end end % Cruiser start_again = 1; while start_again == 1 xx = 'a'; while strcmp(xx,'a') == 1 cstart = rand(1); cstart = 100*cstart; cstart = cstart - mod(cstart,1);

if mod(cstart,10) > 8 | mod(cstart,10) == 0 cpoint1 = cstart - 11; cpoint2 = cpoint1 - 11; if cstart < 21 cpoint1 = cstart + 9; cpoint2 = cpoint1 + 9; end end end end if filled(cpoint1) == 1 | filled(cpoint2) == 1 start_again = 1; else start_again = 0; filled(cstart) = 1; filled(cpoint1) = 1; filled(cpoint2) = 1; type(cstart) = 'C'; type(cpoint1) = 'C'; type(cpoint2) = 'C'; end end % Submarine start_again = 1; while start_again == 1 xx = 'a'; while strcmp(xx,'a') == 1 sstart = rand(1); sstart = 100*sstart; sstart = sstart - mod(sstart,1); if filled(sstart) == 0 xx = 'b'; end end % Get direction direction = rand(1); if direction >. direction = 'horizontal'; elseif direction >. direction = 'vertical'; end if strcmp(direction,'horizontal') == 1 spoint1 = sstart + 1; spoint2 = sstart + 2; if mod(spoint2,10) < 3 & mod(spoint2,10) > 0 % Ran off the end of the plot spoint1 = sstart - 1; spoint2 = sstart - 2; end elseif strcmp(direction,'vertical') == 1 spoint1 = sstart + 10; spoint2 = spoint1 + 10; if spoint2 > 100 spoint1 = sstart - 10; spoint2 = spoint1 - 10; end

elseif strcmp(direction,'diagonal_up_slope') == 1 if sstart < 21 spoint1 = sstart + 9; % must reverse direction spoint2 = spoint1 + 9; if mod(sstart,10) < 3 & mod(sstart,10) > 0 spoint1 = sstart + 11; % must change to down slope spoint2 = spoint1 + 11; end else spoint1 = sstart - 9; spoint2 = spoint1 - 9; if mod(sstart,10) > 8 | mod(sstart,10) == 0 spoint1 = sstart + 9; spoint2 = spoint1 + 9; if sstart > 88 spoint1 = sstart - 11; spoint2 = spoint1 - 11; end end end else if sstart > 80 spoint1 = sstart - 11; % must reverse direction spoint2 = spoint1 - 11; if mod(sstart,10) < 3 & mod(sstart,10) > 0 spoint1 = sstart - 9; % must change to down slope spoint2 = spoint1 - 9; end else spoint1 = sstart + 11 ; spoint2 = spoint1 + 11; if mod(sstart,10) > 8 | mod(sstart,10) == 0 spoint1 = sstart - 11; spoint2 = spoint1 - 11; if bstart < 21 spoint1 = sstart + 9; spoint2 = spoint1 + 9; end end end end if filled(spoint1) == 1 | filled(spoint2) == 1 start_again = 1; else start_again = 0; filled(sstart) = 1; filled(spoint1) = 1; filled(spoint2) = 1; type(sstart) = 'S'; type(spoint1) = 'S'; type(spoint2) = 'S'; end end % Destroyer start_again = 1; while start_again == 1

end if filled(dpoint1) == 1 start_again = 1; else start_again = 0; filled(dstart) = 1; filled(dpoint1) = 1; type(dstart) = 'D'; type(dpoint1) = 'D'; end end for z = 1: if filled(z) == 1 color = ['[1 0 0]']; else color = ['[1 1 1]']; end button = ['bs_pushbutton',num2str(z)]; set(findobj('tag',button),'Userdata',str2num(color)); set(findobj('tag','bs_bar'),'Userdata',type); set(findobj('tag','bs_a'),'Userdata',5); set(findobj('tag','bs_b'),'Userdata',4); set(findobj('tag','bs_c'),'Userdata',3); set(findobj('tag','bs_d'),'Userdata',2); set(findobj('tag','bs_s'),'Userdata',3); end % Set initial color for each button to GRAY (real color hidden until user clicks on a button) for x = 1: button = ['bs_pushbutton',num2str(x)]; set(findobj('tag',button),'BackgroundColor',[0.7 0.7 0.7]); end end %######################################################################## ################################################### if isempty(findobj('tag','bs')) % if program is closed down while running return end %######################################################################## ################################################### if strcmp(action,'quit') % Exit the Game close(findobj('tag','bs')); % Close down the GUI close all; return; end %######################################################################## ################################################### if strcmp(action, 'start_over') % set all background colors back to gray for x = 1: button = ['bs_pushbutton',num2str(x)]; set(findobj('tag',button),'BackgroundColor',[0.7 0.7 0.7]);

set(findobj('tag',button),'Visible','on'); end set(findobj('tag','bs_hit_miss'),'Userdata',17); set(findobj('tag','bs_num_moves'),'String','0'); set(findobj('tag','bs_num_moves'),'Userdata',0); set(findobj('tag','bs_instructions'),'String','Good luck!'); % re-jumble all the warships filled = [0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0,... 0,0,0,0,0,0,0,0,0,0]; type = ['M','M','M','M','M','M','M','M','M','M',... 'M','M','M','M','M','M','M','M','M','M',... 'M','M','M','M','M','M','M','M','M','M',... 'M','M','M','M','M','M','M','M','M','M',... 'M','M','M','M','M','M','M','M','M','M',... 'M','M','M','M','M','M','M','M','M','M',... 'M','M','M','M','M','M','M','M','M','M',... 'M','M','M','M','M','M','M','M','M','M',... 'M','M','M','M','M','M','M','M','M','M',... 'M','M','M','M','M','M','M','M','M','M']; % % Aircraft Carrier % Get Single Random Starting Point astart = rand(1); astart = 100*astart; astart = astart - mod(astart,1); % Get direction direction = rand(1); if direction >. direction = 'horizontal'; elseif direction >. direction = 'vertical'; end if strcmp(direction,'horizontal') == 1 apoint1 = astart + 1; apoint2 = astart + 2; apoint3 = astart + 3; apoint4 = astart + 4; if mod(apoint4,10) < 5 & mod(apoint4,10) > 0 % Ran off the end of the plot apoint1 = astart - 1; apoint2 = astart - 2; apoint3 = astart - 3; apoint4 = astart - 4; end elseif strcmp(direction,'vertical') == 1 apoint1 = astart + 10; apoint2 = apoint1 + 10; apoint3 = apoint2 + 10; apoint4 = apoint3 + 10;

apoint4 = apoint3 - 11; if astart < 41 apoint1 = astart + 9; apoint2 = apoint1 + 9; apoint3 = apoint2 + 9; apoint4 = apoint3 + 9; end end end end filled(astart) = 1; filled(apoint1) = 1; filled(apoint2) = 1; filled(apoint3) = 1; filled(apoint4) = 1; type(astart) = 'A'; type(apoint1) = 'A'; type(apoint2) = 'A'; type(apoint3) = 'A'; type(apoint4) = 'A'; % Battleship start_again = 1; while start_again == 1 xx = 'a'; while strcmp(xx,'a') == 1 bstart = rand(1); bstart = 100*bstart; bstart = bstart - mod(bstart,1); if filled(bstart) == 0 xx = 'b'; end end % Get direction direction = rand(1); if direction >. direction = 'horizontal'; elseif direction >. direction = 'vertical'; end if strcmp(direction,'horizontal') == 1 bpoint1 = bstart + 1; bpoint2 = bstart + 2; bpoint3 = bstart + 3; if mod(bpoint3,10) < 4 & mod(bpoint3,10) > 0 % Ran off the end of the plot bpoint1 = bstart - 1; bpoint2 = bstart - 2; bpoint3 = bstart - 3; end elseif strcmp(direction,'vertical') == 1 bpoint1 = bstart + 10; bpoint2 = bpoint1 + 10; bpoint3 = bpoint2 + 10; if bpoint3 > 100 bpoint1 = bstart - 10;

bpoint2 = bpoint1 - 10; bpoint3 = bpoint2 - 10; end elseif strcmp(direction,'diagonal_up_slope') == 1 if bstart < 31 bpoint1 = bstart + 9; % must reverse direction bpoint2 = bpoint1 + 9; bpoint3 = bpoint2 + 9; if mod(bstart,10) < 4 & mod(bstart,10) > 0 bpoint1 = bstart + 11; % must change to down slope bpoint2 = bpoint1 + 11; bpoint3 = bpoint2 + 11; end else bpoint1 = bstart - 9; bpoint2 = bpoint1 - 9; bpoint3 = bpoint2 - 9; if mod(bstart,10) > 7 | mod(bstart,10) == 0 bpoint1 = bstart + 9; bpoint2 = bpoint1 + 9; bpoint3 = bpoint2 + 9; if bstart > 77 bpoint1 = bstart - 11; bpoint2 = bpoint1 - 11; bpoint3 = bpoint2 - 11; end end end else % diagonal down_slope if bstart > 70 bpoint1 = bstart - 11; % must reverse direction bpoint2 = bpoint1 - 11; bpoint3 = bpoint2 - 11; if mod(bstart,10) < 4 & mod(bstart,10) > 0 bpoint1 = bstart - 9; % must change to down slope bpoint2 = bpoint1 - 9; bpoint3 = bpoint2 - 9; end else bpoint1 = bstart + 11 ; bpoint2 = bpoint1 + 11; bpoint3 = bpoint2 + 11; if mod(bstart,10) > 7 | mod(bstart,10) == 0 bpoint1 = bstart - 11; bpoint2 = bpoint1 - 11; bpoint3 = bpoint2 - 11; if bstart < 31 bpoint1 = bstart + 9; bpoint2 = bpoint1 + 9; bpoint3 = bpoint2 + 9; end end end end if filled(bpoint1) == 1 | filled(bpoint2) == 1 | filled(bpoint3) == 1 start_again = 1;

cpoint1 = cstart - 9; cpoint2 = cpoint1 - 9; if mod(cstart,10) > 8 | mod(cstart,10) == 0 cpoint1 = cstart + 9; cpoint2 = cpoint1 + 9; if cstart > 88 cpoint1 = cstart - 11; cpoint2 = cpoint1 - 11; end end end else % diagonal down_slope if cstart > 80 cpoint1 = cstart - 11; % must reverse direction cpoint2 = cpoint1 - 11; if mod(cstart,10) < 3 & mod(cstart,10) > 0 cpoint1 = cstart - 9; % must change to down slope cpoint2 = cpoint1 - 9; end else cpoint1 = cstart + 11 ; cpoint2 = cpoint1 + 11; if mod(cstart,10) > 8 | mod(cstart,10) == 0 cpoint1 = cstart - 11; cpoint2 = cpoint1 - 11; if cstart < 21 cpoint1 = cstart + 9; cpoint2 = cpoint1 + 9; end end end end if filled(cpoint1) == 1 | filled(cpoint2) == 1 start_again = 1; else start_again = 0; filled(cstart) = 1; filled(cpoint1) = 1; filled(cpoint2) = 1; type(cstart) = 'C'; type(cpoint1) = 'C'; type(cpoint2) = 'C'; end end % Submarine start_again = 1; while start_again == 1 xx = 'a'; while strcmp(xx,'a') == 1 sstart = rand(1); sstart = 100*sstart; sstart = sstart - mod(sstart,1); if filled(sstart) == 0 xx = 'b'; end end

% Get direction direction = rand(1); if direction >. direction = 'horizontal'; elseif direction >. direction = 'vertical'; end if strcmp(direction,'horizontal') == 1 spoint1 = sstart + 1; spoint2 = sstart + 2; if mod(spoint2,10) < 3 & mod(spoint2,10) > 0 % Ran off the end of the plot spoint1 = sstart - 1; spoint2 = sstart - 2; end elseif strcmp(direction,'vertical') == 1 spoint1 = sstart + 10; spoint2 = spoint1 + 10; if spoint2 > 100 spoint1 = sstart - 10; spoint2 = spoint1 - 10; end elseif strcmp(direction,'diagonal_up_slope') == 1 if sstart < 21 spoint1 = sstart + 9; % must reverse direction spoint2 = spoint1 + 9; if mod(sstart,10) < 3 & mod(sstart,10) > 0 spoint1 = sstart + 11; % must change to down slope spoint2 = spoint1 + 11; end else spoint1 = sstart - 9; spoint2 = spoint1 - 9; if mod(sstart,10) > 8 | mod(sstart,10) == 0 spoint1 = sstart + 9; spoint2 = spoint1 + 9; if sstart > 88 spoint1 = sstart - 11; spoint2 = spoint1 - 11; end end end else % diagonal down_slope if sstart > 80 spoint1 = sstart - 11; % must reverse direction spoint2 = spoint1 - 11; if mod(sstart,10) < 3 & mod(sstart,10) > 0 spoint1 = sstart - 9; % must change to down slope spoint2 = spoint1 - 9; end else spoint1 = sstart + 11 ; spoint2 = spoint1 + 11; if mod(sstart,10) > 8 | mod(sstart,10) == 0 spoint1 = sstart - 11; spoint2 = spoint1 - 11;