Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android 8: Wari and Togiz Kumalak Programming

Similar presentations


Presentation on theme: "Android 8: Wari and Togiz Kumalak Programming"— Presentation transcript:

1 Android 8: Wari and Togiz Kumalak Programming

2

3

4 Introduction The goal of this set of overheads is to sketch out an implementation of Wari for an Android device In very broad terms, the implementation will involve the following: A table layout A playNextCup() method that has similarities with the playNextCup() method of One Button Recursion

5 8.1 Wari in Android 8.2 layout.xml for Wari 8.3 strings.xml for Wari 8.4 R.java for Wari 8.5 MainActivity.java for Wari

6 8.1 Wari in Android

7 The following overhead shows the appearance of the Wari app that is to be created

8

9 This is a simple description of the plan for developing Android apps:
Using widgets, develop a graphical layout (activity_main.xml) Acquire handles on these widgets through R.java Use these widgets in your Java code, MainActivity.java

10 The widgets and their layout are separately defined in their own XML file
They have a life of their own independent of the MainActivity.java source file An implementation of Wari will require that the cups on the board be logically linked, so that the dropping of seeds can proceed from cup to cup

11 In the example implementation that’s illustrated:
Each cup will be represented by a button, which is clicked to play that cup The contents of the cup, the number of seeds, will be shown next to the button And the widgets as specified in the layout file will have no connection to each other except for their relative position on the screen

12 There will be one sendMessage() method for the app
It will handle clicks from all of the buttons in the GUI It will sort out which button has been clicked and which cup is played next This is where the logical relationship between the cups is implemented

13 Implementation Approach
The fundamental beginning step in Wari is picking a cup to play If the cup contains seeds, you then move to the next cup The sendMessage() method needs to identify the cup that was clicked

14 The sendMessage() method code will contain a sequence of if/else if statements that encapsulate this logic, hardcoding the id’s of the cups: If you’re on cup x, the next cup is y; else if you’re on cup y, the next cup is z; and so on, all the way around the board sendMessage() then calls playNextCup()

15 playNextCup() also contains if/else logic
You progress around the board as far as needed and no further by doing recursion on the value of handFull that you pick up in the first cup This is where there is a similarity with One Button Recursion The stopping condition is easy and natural: When the handFull is empty (== 0) you’re done

16 Coming Attractions The remaining overheads show incomplete selections of working code for an app that implements Wari Your assignment is to implement Wari fully You may copy, modify, and complete the example code given in the overheads Complete code will not be posted

17 8.2 layout.xml for Wari Step 1: Create a layout that presents a Wari-like interface with the necessary components I used buttons for play and text views to hold the contents of cups I decided to use a table layout Android also has something called a grid layout, but the table layout is suitable for this simple version of Wari

18 I decided that it would be helpful to use the graphical tools in Android rather than trying to master the XML syntax This was not trouble-free The system will automatically number the items in the order they’re created I had to do some re-ordering/renaming because I wanted them numbered in the logical order of play

19 There were 12 buttons and 12 text views in the layout
It was desirable to have them numbered 1-12 in the order they would be played in the game

20 The following overhead shows a screenshot of my layout in the development environment
I’m showing you this in order to remind you of the graphical tools Notice the layouts and widgets on the left Also notice the tree like presentation of the layout on the right

21

22 The following overheads show the XML code that was generated up through the first row of the table layout The full file goes on at length This is where you can check to see what default names your widgets are being given They are numbered in creation order Your code doesn’t have to be the same, but it will at least be similar

23 Overall Layout and Player 1’s Captured Cup
<?xml version="1.0" encoding="utf-8"?> <!-- This is the activity_main.xml file for Wari. --> <LinearLayout xmlns:android=" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" />

24 Table Layout and the First Half of the First Row of the Table
<TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendMessage" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" />

25 Divider to Separate TextViews, Second Half of the First Row, and Closing of the Row
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendMessage" /> </TableRow>

26 8.3 strings.xml for Wari Creating a strings.xml file for an app isn’t too difficult The layout.xml file contains all of the named items that were created in the layout that are referred to in strings.xml strings.xml embeds these names in XML syntax with values strings.xml also contains a few other strings Some snippets from strings.xml are given on the following overhead

27 <resources> <string name="app_name">Wari</string> <string name="button1Contents">P1, C1</string> <string name="button2Contents">P1, C2</string> <string name="textView1Contents">4</string> <string name="textView2Contents">4</string> <string name="textView15Contents">Player 2 Winnings</string> <string name="textView16Contents">0</string> <string name="filler">" "</string> </resources>

28 8.4 R.java for Wari As you recall, R.java is auto-generated
If your layout and strings files were complete, R.java will have everything you need in it The code for MainActivity.java will refer to R.java A subset of R.java is shown on the following overhead as a reminder of what it contains

29 public static final class id {
public static final int button1=0x7f0b0057; public static final int button10=0x7f0b0067; public static final int button11=0x7f0b0061; public static final int button12=0x7f0b005b; public static final int textView1=0x7f0b0058; public static final int textView10=0x7f0b0066; public static final int textView11=0x7f0b0060; public static final int textView12=0x7f0b005a; }

30 8.5 MainActivity.java for Wari
There is nothing special about the onCreate() and onCreateOptionsMenu() methods for Wari They’re just copied as usual There’s also nothing unusual about the imports Remember Alt + Enter to easily import classes

31 The logic of the app is in the sendMessage() and playNextCup() methods
These are admittedly kind of a mess The thing to remember is that this implementation of Wari is not supposed to be an example of good code It is supposed to provide you with practice

32 You can essentially complete the assignment through inspired copying and pasting
But this will only work if you can see how things work You will need to understand how the cups in Wari are logically linked together by the executable code This will mean finding and keeping track of id’s for widgets and managing if/else code

33 And you will also need to recall how recursion works
This involves both the recursion itself, plus some syntactical details you probably haven’t considered before Because recursive methods are static they do not have an implicit parameter Therefore, all needed parameters have to be passed explicitly

34 sendMessage() for Wari
As usual, sendMessage() is the method called on a button click This is critically important to this application: Each of the 12 buttons in Wari is linked to the one sendMessage() method

35 It’s possible to have a separate listener, a sendMessage() method (with a unique name), for different buttons or other objects in an interface This was illustrated in the first half of assignment 1 It’s also possible to have many different widgets and one listener as will be shown here In theory, it would also be possible to have multiple listeners for one widget—but that isn’t addressed here

36 In this app, since each button triggers the same listener, sendMessage() has to determine which button was clicked This is accomplished by checking a parameter value in a series of if statements In each if statement, effectively the same logic is implemented The different cases differ by which button/cup/next cup are involved—and these are distinguished by the number that appears in their name

37 The widgets in the app are defined independently of each other in the layout file
The logic of the unified listener, the sendMessage() method, implements the connection between them In other words, sendMessage() functionally implements the structure of the game board After determining which which cup has been sendMessage() method calls the playNextCup() method, which implements the logic of playing the game

38 The if/else if code in sendMessage() is highly redundant
No claim is made that this is the ideal solution It is presented as a quick and dirty approach which is relatively easy to understand in the context of Android widgets The redundancy actually makes it easy to write You just copy and paste and change the numbers identifying the buttons and cups

39 The beginning of the sendMessage() method is shown on the overhead following the next one
Remember that the button which was clicked is passed as a parameter to the method And note the calls to findViewById() to get references to the components of the app These are calls on the implicit parameter, the main activity itself

40 The first two if cases, corresponding to the first two buttons and their text views, are shown
The full method just continues the pattern You will need to complete this implementation in order to do the assignment

41 public void sendMessage(View view) { Button clickedButton = (Button) view; if (clickedButton == findViewById(R.id.button1)) { TextView cup = (TextView) findViewById(R.id.textView1); int handFull = Integer.parseInt(cup.getText().toString()); if (handFull > 0) { cup.setText("0"); playNextCup(this, (TextView) findViewById(R.id.textView2), handFull); } } else if (clickedButton == findViewById(R.id.button2)) { TextView cup = (TextView) findViewById(R.id.textView2); int handFull = Integer.parseInt(cup.getText().toString()); if (handFull > 0) { cup.setText("0"); playNextCup(this, (TextView) findViewById(R.id.textView3), handFull); } }

42 playNextCup() for Wari
playNextCup() is the method that’s called if a player clicks a cup that is not empty It is a recursive method that moves from one cup to the next around the board Each successive recursive call moves to the next cup Seeds continue being dropped in each cup until handFull is empty This is the condition that ends the recursion

43 playNextCup() is redundant, like sendMessage()
Just like in sendMessage(), it’s necessary to know which cup is being played Which is the next cup is determined by which is the current cup Since the cups are found by their id, there has to be a separate case for each of the 12 cups on the board

44 Even though the code is kind of “stupid” because of its redundancy, that actually makes it relatively easy to write If you’ve got one of the cases figured out correctly, then you can copy and paste and change the numbers identifying the cups Keep in mind that when you reach the end of player 2’s side of the board, you have to go from cup 1 to cup 12

45 Warning—Static Methods and Non-Static Methods
Remember that recursive methods, as you’ve been shown how to implement them, are static methods And remember what a static method is: It can be succinctly described as a “class method”

46 Things like Math.sqrt() are examples
The methods are contained in a given class, but they’re not called on instances of a class All static methods are like mathematical functions in this sense: You have to pass in all of the needed parameters as explicit parameters, and the function returns a result, e.g.: q = f(r, s, t, …)

47 Consider the signature line for the playNextCup() method definition
The method is static And the first explicit parameter that is passed in is an instance of Activity public static void playNextCup(Activity activityIn, TextView cupIn, int handFullIn)

48 Now go back and look at the example code given for sendMessage()
The call to playNextCup() is as shown below The implicit parameter, this, is passed in to it In this case, “this” is the main activity playNextCup(this, (TextView) findViewById(R.id.textView3), handFull);

49 Briefly consider the other two explicit parameters
They are needed in order to do the recursion TextView cupIn, int handFullIn

50 What is this first explicit needed for?
Activity activityIn Passing in this parameter supports calls like these in the body of the method: activityIn.findViewById(R.id.textView1)

51 sendMessage() is a non-static method
In the code for sendMessage() you could call findViewById() on the implicit parameter This is how you acquired references to components of the app defined in layout.xml

52 playNextCup() is static
You have to pass it a reference to the main activity so that findViewById() can be called on it This is how you can acquire references to app components within playNextCup()

53 If you forget to pass in the activity and call findViewById() on it, you will get a compiler error
As compiler error messages go, it is relatively straightforward: “Cannot make a static reference to the non-static findViewById(int) from the type Activity”

54 The error message is telling you that in a static method there is no “this”, there is no object, which findViewById() can be called on Therefore, in order to make the implementation work, you have to pass in MainActivity when making the call to playNextCup()

55 The first two if cases for the playNextCup() method are shown on the following overheads
The full method just continues the pattern You will need to complete this implementation in order to do the assignment Keep in mind that when you reach the end of player 2’s side of the board you’ll need to go from cup 12 to cup 1

56 public static void playNextCup(Activity activityIn, TextView cupIn, int handFullIn) { handFullIn--; if (cupIn == activityIn.findViewById(R.id.textView1)) { TextView nextCup = (TextView) activityIn.findViewById(R.id.textView2); int seedCount = Integer.parseInt(cupIn.getText().toString()); seedCount++; cupIn.setText(seedCount + ""); if (handFullIn != 0) { playNextCup(activityIn, nextCup, handFullIn); } else if (seedCount == 2 || seedCount == 3) { TextView capturedCup = (TextView) activityIn.findViewById(R.id.textView14); int capturedCount = Integer.parseInt(capturedCup.getText().toString()); capturedCount += seedCount; capturedCup.setText(capturedCount + ""); } }

57 else if (cupIn == activityIn. findViewById(R. id
else if (cupIn == activityIn.findViewById(R.id.textView2)) { TextView nextCup = (TextView) activityIn.findViewById(R.id.textView3); int seedCount = Integer.parseInt(cupIn.getText().toString()); seedCount++; cupIn.setText(seedCount + ""); if (handFullIn != 0) { playNextCup(activityIn, nextCup, handFullIn); } else if (seedCount == 2 || seedCount == 3) { TextView capturedCup = (TextView) activityIn.findViewById(R.id.textView14); int capturedCount = Integer.parseInt(capturedCup.getText().toString()); capturedCount += seedCount; capturedCup.setText(capturedCount + ""); } }

58 Summary and Assignment
Parts of the first assignment correspond to an implementation of Wari Your app will be graded using black box testing That means that I will not look at the code, just at visual features The appearance and the functionality show whether or not it was implemented correctly

59 Your app has to show captured cups for players 1 and 2, and a set of 12 buttons and text views in a table layout, representing the Wari board This corresponds to the layout.xml file for the app

60 Correctly implementing strings
Correctly implementing strings.xml will cause the buttons to contain text, the text views for the board to contain initial values, and the explanatory text for the captured cups and their values to appear This corresponds to the strings.xml file for the app

61 The app should play the game
This corresponds to these two method implementations The sendMessage() method The playNextCup() method

62 In practice, you will be graded on the basis of black box testing of functionality only
No attempt will be made to hunt down errors, determine whether they are in sendMessage() or playNextCup(), etc.

63 The remainder of the assignment is to recycle your Wari code to become an implementation of Togiz Kumalak You will have to change the position of the captured cups relative to the game board

64 You will be given a number of cups to include on each side of the board
And you will be given a number of seeds to put in the cups initially You will be given an assignment sheet with your individual specifications marked on it

65 Assignment Hand-In You will turn in all of the parts of the assignment at the same time You will demonstrate your apps to me on your tablet or whatever Android device you’ve decided to develop on You can do it on the designated day on the syllabus

66 The End


Download ppt "Android 8: Wari and Togiz Kumalak Programming"

Similar presentations


Ads by Google