Presentation is loading. Please wait.

Presentation is loading. Please wait.

EGR 115 Introduction to Computing for Engineers Designing The Battleship Game in MATLAB Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers.

Similar presentations


Presentation on theme: "EGR 115 Introduction to Computing for Engineers Designing The Battleship Game in MATLAB Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers."— Presentation transcript:

1 EGR 115 Introduction to Computing for Engineers Designing The Battleship Game in MATLAB Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers

2 Lecture Outline Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers Designing The Battleship Game in MATLAB Grid Occupancy Legend: 0 = unoccupied 1 = occupied 9 = unknown Slide 2 of 14

3 Designing The Battleship Game in MATLAB Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers Design the game of battleships using MATLAB  Two players on separate computers o Each player will display two 10  10 “grids”  One grid corresponding to your own ship locations (completely known = 0 or 1)  The other corresponding to your view of your opponents grid (initially all unknown = 9)  Own Grid: Each player will first deploy 5 ships (each a row or col vector) o One length five, o One length four, o Two length three, and o One length two Slide 3 of 14

4 Designing The Battleship Game in MATLAB Flowchart - Initialization Phase Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers Start Initialize My grid My view of my opponents grid Save grids Player Opponent Initialization Phase Start Initialize My grid My view of my opponents grid Save grids Slide 4 of 14

5 Designing The Battleship Game in MATLAB Flowchart – Playing Phase Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers Fire a salvo (request coordinates) Update actual opponents grid with a ‘0’ Update your copy of opponents grid with a ‘1’ Your Turn! Load your opponents grid Hit? yes Update your copy of opponents grid with a ‘0’ no Load your own gridSave your opponents grid Opponent’s Turn! Save Your Grids Slide 5 of 14

6 Designing The Battleship Game in MATLAB Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers Task 0: Create an empty script file named “YourLastName_initialize_game.m”  Document the header of the file,  Add code to clear any variables in the workspace, and  Clear text from the command window % COURSE: EGR 115 Introduction to Computing for Engineers % DESCRIPTION: Initialize Battleship Game % AUTHOR: LastName, FirstName % DATE: 9/??/2014 % COMMENTS: Any comments which you may have. clear all; % Clear all of the variables in the workspace clc; % Clear text from the Command Window Slide 6 of 14

7 Designing The Battleship Game in MATLAB Initialization Phase Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers TASK #1: Build the initial grids 1.Your grid: o Build a 10x10 matrix containing either a ‘1’ (occupied by a ship) or ‘0’ (unoccupied) in each cell. (5 ships)  Assign this matrix to the variable “my_grid” 2.Your opponents grid: o Build a 10x10 matrix containing a ‘9’ in every cell. The 9 corresponds to ‘unknown’  Assign this matrix to the variable “opponents_grid” 3.Assign your Name o my_name = 'YourLastName'; Slide 7 of 14

8 Designing The Battleship Game in MATLAB Initialization Phase Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers An example of starting grids: What is the sum of the elements in my_grid? Slide 8 of 14

9 Designing The Battleship Game in MATLAB Initialization Phase Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers TASK #2: Save the grids & name to a file  Save your three variables to a *.mat file named 'YourLastName'.mat o Note: Save this file to the course drive => T:\EGR115\Bruder o E.g., if your last name is “TBD” >> save T:\EGR115\Bruder\TBD.mat my_grid opponents_grid my_name o Saves all three variables from your workspace!! Check to see if you have created a YourLastName.mat file on the “T” drive. Slide 9 of 14

10 Designing The Battleship Game in MATLAB Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers Task 3: Create the logic & Play the Game  Create a second script file named “YourLastName_battleship_game.m”  Look on the T-drive to select an opponent o Preferably the person sitting next to you o He or she should select you as their opponent  “Hard code” the name of your opponent >> my_opponent = 'TBD';  Load your opponents *.mat file >> opponents_filename = ['T:\EGR115\Bru… >>load(opponents_filename) Slide 10 of 14

11 Designing The Battleship Game in MATLAB Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers  Store the sum of your opponent’s grid >> prior_sum = ????;  Select the row, col coordinates for firing a salvo >> row= input('Enter your Row Target Coordinate: '); >> col= input('Enter your Col Target Coordinate: ');  Write a zero to those coordinates in your opponents grid. >> my_grid(row, col) = 0;  Record the new sum. Did you hit a ship? >> new_sum = ???  Resave your opponents mat file. >> save(opponents_filename, ' my_grid ', ' opponents_grid ' ); Slide 11 of 14

12 Designing The Battleship Game in MATLAB Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers  Load your own grid file >> load T:\EGR115\Bruder\???.mat  Update your view of your opponents grid with a ‘0’ at location (row, col): >> if (new_sum < prior_sum) % Hit!!! >> opponents_grid(row, col) = 1; >> disp('You hit a ship - Go again!!!'); >> else % Missed >> opponents_grid(row, col) = 0; >> disp('You missed – Opponent goes next!'); >> end Slide 12 of 14

13 Designing The Battleship Game in MATLAB Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers  Save your grids >> save T:\EGR115\Bruder\???.mat my_grid opponents_grid  View your own grids >> my_grid >> opponents_grid Task 4:  Next move!! Slide 13 of 14

14 Next Lecture Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers Debugging and Playing The Battleship Game in MATLAB Slide 14 of 14


Download ppt "EGR 115 Introduction to Computing for Engineers Designing The Battleship Game in MATLAB Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers."

Similar presentations


Ads by Google