18 2DArraysBattleship

18 2DArraysBattleship - 2 dimensional arrays 2 dimensional...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 2 dimensional arrays 2 dimensional arrays Can be thought of as a “grid” with rows and columns 1 2 dimensional arrays 2 dimensional arrays int nums = { {-3, -1, 5}, {7, 12, 13} }; {7, //Note: rows first, then columns println(nums[1][2]); //displays 13 println(nums[2][1]); //Crash! 2 The Battleship program The Battleship program In Battleship, we have a two dimensional array of buttons IFButton buttons; 3 The Battleship program The Battleship program Don't worry too much about the arithmetic in the nested loops that is arranging them into rows and columns for(int r = 0; r < rows; r++) { for(int c = 0; c < cols; c++) for(int { int x = c * buttonSize + 40; int int y = r * buttonSize + 60; int buttons[r][c] = new IFButton ("", x, y, buttonSize, buttonSize); buttons[r][c] buttons[r][c].addActionListener(this); buttons[r][c].addActionListener(this); control.add (buttons[r][c]); control.add } } 4 The Battleship program The Battleship program In OOP we think about our program as a collection of interacting objects In the Battleship program, the objects include Buttons and a Ship The Button class has already been written, but we will need to write a class that models a Ship Before writing code for the Ship class, we should ask ourselves what a ship HAS, and also what it DOES. 5 The Battleship program The Battleship program We use functions to represent what the object does and variables for what it has. class Ship { //some variables //some Ship() //constructor Ship() { } //other functions //other } 6 The Battleship program The Battleship program Our Ship is hiding behind four Buttons, so our class will need four IFButton variables class Ship { IFButton b1,b2,b3,b4; IFButton Ship() //constructor Ship() { } //other functions //other } 7 The Battleship program The Battleship program The constructor's job is to initialize the variables class Ship { IFButton b1,b2,b3,b4; IFButton Ship() //constructor Ship() { b1 = ?? b2 = ?? b2 b3 = ?? b3 b4 = ?? b4 } //other functions //other } 8 The Battleship program The Battleship program Eventually we'll hide the ship randomly, but to get started, we can just choose any four buttons class Ship { IFButton b1,b2,b3,b4; IFButton Ship() //constructor Ship() { b1 = buttons[0][2]; b2 = buttons[1][2]; b2 b3 = buttons[2][2]; b3 b4 = buttons[3][2]; b4 } //other functions //other } 9 Creating an instance of the ship class Creating an instance of the ship class Once we have the beginnings of a ship class, we can make an instance with code like Ship bob = new Ship(); 10 Checking for a hit Checking for a hit void actionPerformed (GUIEvent e) void { IFButton shot = (IFButton)e.getSource(); IFButton if(shot.getX() >= 145) //replace this with //code that checks for a hit //code { shot.setLookAndFeel(hit); shot.setLookAndFeel(hit); } else else { shot.setLookAndFeel(miss); shot.setLookAndFeel(miss); } } 11 How can we find out if bob was hit? How can we find out if void actionPerformed (GUIEvent e) void { IFButton shot = (IFButton)e.getSource(); (IFButton)e.getSource(); if( bob ?? ) bob { shot.setLookAndFeel(hit); shot.setLookAndFeel(hit); } else else { shot.setLookAndFeel(miss); shot.setLookAndFeel(miss); } } 12 The isHit function The We'll check to see if shot is the same button as b1, b2, b3, or b4 class Ship { IFButton b1,b2,b3,b4; IFButton Ship() //constructor Ship() { b1 = buttons[0][2]; b2 = buttons[1][2]; b2 b3 = buttons[2][2]; b3 b4 = buttons[3][2]; b4 } boolean isHit(IFButton shot) boolean { return false; //replace this with code that checks to //see if the ship was hit //see } } 13 Now we can call isHit() Now we can call isHit() void actionPerformed (GUIEvent e) void { IFButton shot = (IFButton)e.getSource(); (IFButton)e.getSource(); if( bob.isHit(shot) == true ) bob.isHit(shot) { shot.setLookAndFeel(hit); shot.setLookAndFeel(hit); } else else { shot.setLookAndFeel(miss); shot.setLookAndFeel(miss); } } 14 A trick question! A trick question! How many Buttons does the following code create? IFButton b1; 15 None! None! Buttons are Objects What word do we need in Java to build an instance of a class? IFButton b1; 16 new new Now one Button, called an instance has been created IFButton b1; b1 = new IFButton b1 ("one",20,40,50,17); (" 17 So, if this isn't a Button, what is So, if this isn't a Button, what is it? IFButton b1; 18 It's just a name for a Button It's just a IFButton b1; It's kind of like saying "If I ever get a dog, I'll name him Bob" We have a name for Button that hasn't been created yet In programming we this kind of name a pointer 19 ...
View Full Document

This note was uploaded on 01/13/2012 for the course CS 12 taught by Professor Ma during the Spring '08 term at UC Davis.

Ask a homework question - tutors are online