%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Eng 101, Sec 200 % %Assignment 7: Battleship matlab % %filename: battleship.m % %v2.1a % %Notes: Single ship, 10x10board, with sound % % Mouse use % % This program runs BEST on Matlab5.3, 6 is slow % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %------Output rules/game setup------% disp('Welcome to Battleship, Matlab Style. By James Glettler') disp('Please select your options:') %Input options %useSounds = input('Would you like to have sound? (plays incorrectly on Matlab6 (1=yes,0=no) '); useSounds = 0; %Because I haven't Given you the sound files, plug them in if you want cheat = input('Would you like to cheat and have the ship location displayed in another window? (1=yes,0=no) '); %Board Size global SIZE = 10; %init board with zeros Board = zeros(SIZE); plotBoard = Board; plotBoard(SIZE + 1,:) = 0; %boost rows by one and assign as zeros plotBoard(:,SIZE + 1) = 1; %boost cols by one and assign as ones (pin high) Board = plotBoard; %return new boosted size to Blank main board figure(1); %work with figure 1 if useSounds [Y,fs,Nbits]=wavread('jp.wav'); sound(Y,fs,Nbits); end %------------Place Ship-------------% %Pick initial spot i = 1+SIZE*rand; j = 1+SIZE*rand; %Pick dir loop = 1; while (loop) %dir is done explicitly to maintain use of doubles for math functions temp = rand * 3; dx = 0; if (temp > 2) dx = 1; elseif (temp < 1) dx = -1; end temp = rand * 3; dy = 0; if (temp > 2) dy = 1; elseif (temp < 1) dy = -1; end i0 = dx * 4 + i; j0 = dy * 4 + j; if (i0 > 0 & i0 <= SIZE & j0 > 0 & j0 <= SIZE) loop = 0; end if (dx == 0 & dy == 0) loop = 1; end end for I = 0:3 Board(int32(i + I * dx),int32(j + I * dy))=1; %ship value is 1 end clear i0 % erase temp variables clear j0 clear temp clear dx clear dy disp('Ship Placed') targetsOpen = 4; %----------Disp Ship----------------% if cheat figure(2); pcolor(0:SIZE,0:SIZE,Board); %Make the plot itself title('True ship location, hidden board') axis off figure(1); end %------------Drop Bomb--------------% bombs = 25; while (bombs > 0 & targetsOpen > 0) %=======Disp showBoard titleMsg = sprintf('You have %d bombs left, please make your selection.',bombs); pcolor(0:SIZE,0:SIZE,plotBoard); %Make the plot itself axis off title(titleMsg) %=======Get mouse input & error check loop = 1; while loop [x,y] = ginput(1); jDrop = int32(x+1); iDrop = int32(y+1); if (jDrop > 0 & jDrop <= SIZE & iDrop > 0 & iDrop <= SIZE) loop = 0; end end %=======Check status of target & processes if (Board(iDrop,jDrop) == 1) %HIT disp('Direct Hit!') bombs = bombs - 1; targetsOpen = targetsOpen - 1; Board(iDrop,jDrop) = 0.75; plotBoard(iDrop,jDrop) = 0.75; if useSounds [Y,FS,Nbits]=wavread('growl.wav'); sound(Y,FS,Nbits); end elseif (Board(iDrop,jDrop) == 0) %Miss disp('Miss') bombs = bombs - 1; Board(iDrop,jDrop) = 0.25; plotBoard(iDrop,jDrop) = 0.25; if useSounds [Y,FS,Nbits]=wavread('ohman.wav'); sound(Y,FS,Nbits); end else disp('That selection has already been made or is invalid, please try again.') end end %-----------END-DISP----------------% %Draw display grid pcolor(0:SIZE,0:SIZE,Board); %Make the plot itself axis off disp('COLORS: Dark blue is clear water, light blue is a miss, dark red is open ship, and orange is a hit!') if (targetsOpen == 0) titleMsg = sprintf('You have won with %d bombs to spare.',bombs); title(titleMsg) if useSounds [Y,FS,Nbits]=wavread('enemy.wav'); sound(Y,FS,Nbits); end else titleMsg = sprintf('You lost. There were still %d open targets left.',targetsOpen); title(titleMsg) if useSounds [Y,FS,Nbits]=wavread('pathetic.wav'); sound(Y,FS,Nbits); end end