1 / 24

Object Oriented Programming Pie Eater

Object Oriented Programming Pie Eater. Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113. What we have done so far. Created PieEaters world grid of 8x6 cells of 20px by 20px Created PieEater walks (in direction facing) records his direction turns left turns right.

raleigh
Download Presentation

Object Oriented Programming Pie Eater

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented ProgrammingPie Eater Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113

  2. 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

  3. Aims of the Presentation • To apply advanced features for PieEater • World Boundaries • Creating Pies • Placing Pies • Random Pies

  4. What we know - Recap public class Picture { private Circle pieEater; } public void walk() { }

  5. 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?

  6. 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!

  7. 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++; } }

  8. 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++; } }

  9. 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

  10. 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(); }

  11. 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

  12. 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; } }

  13. 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; } }

  14. 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; } }

  15. 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?

  16. 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]); }

  17. 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);

  18. 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]); • }

  19. 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

  20. 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(); }

  21. 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

  22. 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?

  23. 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)

  24. Any Questions? • Next week is a drop in support session – location to be arranged.

More Related