240 likes | 325 Views
Explore the implementation of advanced features such as world boundaries and random pie placement for the PieEater program. See the step-by-step coding solutions and considerations for designing and placing pies on a grid. Learn about data structures and methods used to enhance the functionality of the program.
E N D
Object Oriented ProgrammingPie Eater Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113
What we have done so far • Created PieEatersworld • grid of 8x6 • cells of 20px by 20px • Created PieEater • walks (in direction facing) • records his direction • turns left • turns right
Aims of the Presentation • To apply advanced features for PieEater • World Boundaries • Creating Pies • Placing Pies • Random Pies
What we know - Recap public class Picture { private Circle pieEater; } public void walk() { }
Let’s Create World Boundaries • How can we achieve this? • How many steps can pieEater take before he leaves his world if facing East? • How many steps can pieEater take before he leaves his world if facing South? • How many steps can pieEater take before he leaves his world if facing West? • How many steps can pieEater take before he leaves his world if facing North?
Sliding Scales • Walking East and West 1 2 3 4 5 6 7 8 intstepsx = 1; //PieEater starts in square 1 facing E We know his direction because each time we turn left or right, it is set. Each time we walk we add 1 to stepsx If stepsx < 8 and direction = ‘E’ then walk else do nothing!
Pie Eater and Boundaries Class Exercises – Write the code to solve the problem with the North and South boundary issue public class Picture { private Circle pieEater; int direction = “E”; intstepsx = 1; } public void walk() { if ((direction == "E") && (stepsx < 8)) { pieEater.moveHorizontal(40); tail.moveHorizontal(40); stepsx++; } }
My Solution public class Picture { private Circle pieEater; int direction = “E”; intstepsy = 1; } public void walk() { if ((direction == "N") && (stepsy > 1)) { pieEater.moveVertical(-40); tail.moveVertical(-40); stepsy--; } if ((direction == "S") && (stepsy < 6)) { pieEater.moveVertical(40); tail.moveVertical(40); stepsy++; } }
Let’s Create Some Pies This is a complex problem. What do we do with complicated problems? • How can we achieve this? • We will do this in a method (message) • Remember we have use of the other classes • Triangle • Square • Circle • Rectangle
One Pie First we create one pie public class Picture { private Circle pie; } public void drawPie() { pie = new Circle(); pie.changeColor("red"); pie.moveHorizontal(-30); pie.moveVertical(86); pie.changeSize(30); pie.makeVisible(); }
Creating Pies Considerations • We need to ask the user how many pies they want on the grid • We need to place the pies on the grid at random locations • Dealing with co-ordinates x and y can be confusing and difficult to work with – easier to work with rows and columns
What data structure would you use for this? Creating Pies initial solution • Create a data structure that stores the positions of the 8 columns public class Picture { int[] gridColumn = new int[8]; public void createPie() { intposx = 86; for (int column=0 ; column<8; column++) { gridColumn[column] = posx; posx=posx+40; } }
What data structure would you use for this? Creating Pies initial solution • Create a data structure that stores the positions of the 6 rows – the first row position is -30 public class Picture { int[] gridColumn = new int[8]; public void createPie() { intposx = 86; for (int column=0 ; column<8; column++) { gridColumn[column] = posx; posx=posx+40; } }
Creating Pies Initial Solution My solution public class Picture { private Circle pieEater; private Rectangle xLine; private Rectangle yLine; private Rectangle tail; } public void createPie() { int posy = -30; for (int row=0 ; row<6; row++) { gridRow[row] = posy; posy=posy+40; } }
So far • we have created a method called • drawPie () • draws a pie and places it in the first square • createPie () • creates two data structures which contains arrays called gridRow and gridColumn • How can we draw a pie using a gridRow and gridColumn value?
Two Messages public void drawPie(intcol, int row) { pie = new Circle(); pie.changeColor("red"); pie.moveHorizontal(col); pie.moveVertical(row); pie.changeSize(30); pie.makeVisible(); } public void createPie() { intposx = 86; for (int column=0 ; column<8; column++) { gridColumn[column] = posx; posx=posx+40; } int posy = -30; for (int row=0 ; row<6; row++) { gridRow[row] = posy; posy=posy+40; } drawPie (gridColumn[4],gridRow[3]); }
Placing Pies Randomly • Java comes with an inbuilt message that will generate and return a random value • We could use this to return a random gridColumn • and then again for a random gridRow row = (int)(Math.random() * 6); column = (int)(Math.random() * 8);
Two Messages public void drawPie(intcol, int row) { pie = new Circle(); pie.changeColor("red"); pie.moveHorizontal(col); pie.moveVertical(row); pie.changeSize(30); pie.makeVisible(); } • public void createPie() • { • intposx = 86; • for (int column=0 ; column<8; column++) • { • gridColumn[column] = posx; • posx=posx+40; • } • int posy = -30; • for (int row=0 ; row<6; row++) • { • gridRow[row] = posy; • posy=posy+40; • } int row = (int)(Math.random() * 6); int column = (int)(Math.random() * 8); • drawPie (gridColumn[column],gridRow[row]); • }
Nearly There! What if we want more pies? • we have created a method called • drawPie () • draws a pie and places it in a square specified • createPie () • creates two data structures which are arrays called gridRow and gridColumn • populates the array with y and x coordinates • creates random values for row and column • passes the gridRow and gridColum array elements using the random values
Two Messages public void createPie(intnumOfPies) { intposx = 86; for (int column=0 ; column<8; column++) { gridColumn[column] = posx; posx=posx+40; } int posy = -30; for (int row=0 ; row<6; row++) { gridRow[row] = posy; posy=posy+40; } for (int x=1; x < numOfPies ; x++) { int row = (int)(Math.random() * 6); int column = (int)(Math.random() * 8); drawPie (gridColumn[column],gridRow[row]); } } public void drawPie(intcol, int row) { pie = new Circle(); pie.changeColor("red"); pie.moveHorizontal(col); pie.moveVertical(row); pie.changeSize(30); pie.makeVisible(); }
So far • Pie Eater • Created Pie Eater (with a tail) • Walked • Turned Left • Turned Right • Checked Direction • Updated Direction • We have discussed boundaries • Stopped Pie Eater leaving his world • The pies • Placing them on the grid • Random location when placing them • Allowing a specific number of pies
Next • Pie Eater encounters a pie • How does pie eater know there is a pie? • How does pie eater eat the pie? • Should Pie Eater have friends in his world?
Conclusion • Pie Eater is a complicated application • Many real world factors have to be accounted for • Object Orient Programming is an excellent approach for this type of problem • You are to finish your own PieEater Application • Your project will be compiled • You will demonstrate your project next week (5 min per person) • Your project will be placed on the website so that you can impress your friends and your tutors (better work hard)
Any Questions? • Next week is a drop in support session – location to be arranged.