"Battleship Game" Computer Program

July 22, 2016 | Author: Max Kondrath | Category: Topics, Art & Design
Share Embed Donate


Short Description

Matlab program that was created to simulate the board game "Battleships"....

Description

function battleShapes() %max kondrath %donnie fenrich %4/10/10 %This is the main program of our battleship game %it encompasses a subprogram with most of the code (battleShip(x,y)) %This program inputs a turn counter to increase or decrease the difficulty %of the game

%Parameters---------------------------------------

%Initializes sounds---------------------------------%game introduction music airRaid1 = wavread('airRaid.wav'); airRaid = airRaid1(1:1200000); %boat sink sound boatSink = wavread('boatSink.wav'); %boat hit sound boom = wavread('boom.wav'); %sea gulls and water sounds seaGulls = wavread('seaGulls.wav'); %sub ping sound subPing = wavread('subPing.wav'); %Game exit sound trumpet = wavread('trumpet.wav'); %boat miss sound whoosh = wavread('whoosh.wav'); %max and min axis values maximum = 1000; minimum = 1; %Turn value for different difficulties noviceTurn = 20; mediumTurn = 15; masterTurn = 10; %variable for new color of boat once they have been sunk colormap winter; %final display when game ends

final = imread('final.jpeg'); %variables boat1Hit = boat2Hit = boat3Hit = boat4Hit = boat5Hit =

to count number of hits on each boat 0; 0; 0; 0; 0;

%number of hits each boat can stand strength1 = 1; strength2 = 1; strength3 = 1; strength4 = 1; strength5 = 1;

%empty ocean and color values redSea = zeros(1000,1000); boatColor = 255; %declares size of each boat in a matrix boat1 = zeros(100,100); boat1(1:100,1:100) = boatColor; boat2 = zeros(100,100); boat2(1:100,1:100) = boatColor; boat3 = zeros(200,200); boat3(1:200,1:200) = boatColor; boat4 = zeros(200,200); boat4(1:200,1:200) = boatColor; boat5 = zeros(200,200); boat5(1:200,1:400) = boatColor; %starting template/position for boats on grid boat1X = (20:119); boat1Y = (20:119); redSea(boat1Y,boat1X) = boat1; boat2X = (401:500); boat2Y = (101:200); redSea(boat2Y,boat2X) = boat2; boat3X = (701:900); boat3Y = (201:400); redSea(boat3Y, boat3X) = boat3; boat4X = (201:400); boat4Y = (401:600);

redSea(boat4Y,boat4X) = boat4; boat5X = (501:900); boat5Y = (701:900); redSea(boat5Y,boat5X) = boat5; %GAME INTRODUCTION DISPLAYS---------------------------------------------------disp(['Many fought for their lives, the sea..',... 'red with blood, blue with water,']); disp('

');

disp(['Stained from betrayal,ravished by conquest,',... 'cascading the solemn colors of greed']); disp('

');

disp(['Your brothers fight, you have been injured,',... 'your body is weak; but your will is strong']); disp('

');

disp(['As the success of your escape draws near,',... 'enemy fire swallows the landscape.']) disp('

');

disp(['You are our last hope, fight with bravery,',... 'die with honor..']);

%plays game introductory music soundsc(airRaid,70000); quest2 = questdlg(['rageeee']); %image matrix that is intial ocean with battleships on it imagesc(redSea);

%Algorithm-------------------------------------------------------------------------------------------------------------------------------

%game setup and instructions----------------dialog boxes with error checking-----------------------------quest1 = questdlg(['Read carefully. First, the',... 'key to the game is to destroy your enemys ships. In this game your enemy is the turn counter.',... 'You will be picking coordinates to launch your missiles on a 1000 by 1000 grid.',...

strengths.

'Your enemy fleet will have different sized boats with different ',... 'Are you ready to contintue?','BATTLESHAPES']);

if length(quest1) == 2 || length(quest1) ==6 warndlg('You must click yes to continue retry game!'); return end

if length(quest2) == 2 || length(quest2) ==6 warndlg('You must click yes to continue retry game!'); return end quest3 = questdlg(['Has the game playing field (figure1) been place over the right',... 'half of the screen?','BATTLESHAPES']); if length(quest3) == 2 || length(quest3) ==6 warndlg('You must click yes to continue retry game!'); return end

quest4 = questdlg(['Are you ready to return to the commanders window and ',... 'start the game?','BATTLESHAPES']); if length(quest4) == 2 || length(quest4) ==6 warndlg('You must click yes to continue retry game!'); return end

%input for amount of turns (difficulty) difficulty = input('Please enter difficulty>>> novice, medium, or master in SINGLE QUOTES please: '); %using user input sets number of turns for user disp('number of shots: '); if difficulty == ('novice') numberTurns = noviceTurn; disp(numberTurns);

elseif difficulty == ('medium') numberTurns = mediumTurn; disp(numberTurns); elseif difficulty == ('master') numberTurns = masterTurn; disp(numberTurns); else warndlg('wrong value was entered read carefully and try again. Sorry, program exiting'); return end

%MAIN GAME %ALGORITHM----------------------------------------------------------------%--------------------------------------------------------------------------

%while loop including random number subprogram and hit miss checker... %loops through "infinitely"(1000)times while number of terms is checked near bottom %you lose once you go over your amount of turns infiniteTurn = 10000; count = 1; while (count < infiniteTurn)

%calls random number subprogram----------------------randomX = randomMex1; randomY = randomMex2;

%sets users input shot to coordinates and checks for %errors-------------------------------------------------------xCoord = input('input x-Coordinate: disp('X shot error: '); disp(randomX);

');

%Checks if users input coordinates are out of bounds %if so then they will be corrected to closest boundary value if xCoord > maximum xCoord = maximum;

elseif xCoord < minimum xCoord = minimum; end %displays x and y shot error (adds difficulty to game) yCoord = input('input y-Coordinate: disp('Y shot error: '); disp(randomY);

');

%Checks if users input coordinates are out of bounds %if so then they will be corrected to closest boundary value if yCoord > maximum yCoord = maximum; elseif yCoord < minimum yCoord = minimum; end %gets new skewed coordinates and checks if they are out of bounds %if coordinates are out of bounds they are set to boundary values newXCoord = randomX + xCoord; newYCoord = randomY + yCoord; if newXCoord > maximum newXCoord = maximum; elseif newXCoord < minimum newXCoord = minimum; end

if newYCoord > maximum newYCoord = maximum; elseif newYCoord < minimum newYCoord = minimum; end %end of coordinate skew code %--------------------------------------------------------------

%displays users final shot before hit/miss analysis executes disp('Your shot coordinates using (x,y) coordinate system: disp(newXCoord); disp(newYCoord); %plays sound (also used as a pause) soundsc(subPing,50000);

');

%Master Subprogram %determines if shot is a hit or miss %-----------------------------------------------------------------%-----------------------------------------------------------------hitMiss = coordinateInputs((newYCoord),(newXCoord));

%-----------------------------------------------------------------%-----------------------------------------------------------------%--------------------HITMISS CONDITIONALS-------------------------%------------------------------------------------------------------

%Conditional branching that takes value from hitMiss subprogram and %evalutes correct operations %------------------------------------------------------------%variables to count hits on boats are also incremented when %necessary

if (hitMiss == 0) %message box and sound for a missed shot msgbox('Miss, try again.','BATTLESHAPES'); soundsc(whoosh,50000);

elseif (hitMiss == 1) boat1Hit = boat1Hit +1; %checks for sunken boat 1-------------if (boat1Hit == strength1) soundsc(boatSink,50000); soundsc(seaGulls(1:200000),50000); msgbox('You sunk my Destroyer!','BATTLESHAPES') % Fade color while loop--------------------colorIndex = 255; stopIndex = 0; while (colorIndex >= stopIndex) redSea(boat1Y,boat1X) = colorIndex; imagesc(redSea); pause(.001); colorIndex = colorIndex - 1; end

elseif boat1Hit > strength1 %if boat is already sunk miss operations will evaluate soundsc(whoosh,50000); msgbox('You have already sunk this ship!','BATTLESHAPES') else soundsc(boom,50000); msgbox('>>>>>>>>> Hit Hit Hit Hit Hit
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF