MobiCom on Android: MinMax Games, Content Providers as Game Engines, Asynchronous Tasks, Game Engine Clients
Upcoming SlideShare
Loading in...5
×
 

MobiCom on Android: MinMax Games, Content Providers as Game Engines, Asynchronous Tasks, Game Engine Clients

on

  • 941 views

 

Statistics

Views

Total Views
941
Slideshare-icon Views on SlideShare
594
Embed Views
347

Actions

Likes
0
Downloads
8
Comments
0

35 Embeds 347

http://vkedco.blogspot.com 175
http://www.vkedco.blogspot.com 64
http://vkedco.blogspot.in 16
http://reader.aol.com 15
http://vkedco.blogspot.de 6
http://www.vkedco.blogspot.co.uk 5
http://vkedco.blogspot.fr 5
http://www.vkedco.blogspot.in 5
http://vkedco.blogspot.sg 4
http://vkedco.blogspot.ca 4
http://vkedco.blogspot.sk 4
http://vkedco.blogspot.ru 3
http://translate.googleusercontent.com 3
http://vkedco.blogspot.co.il 3
http://www.vkedco.blogspot.fr 3
http://vkedco.blogspot.com.br 3
http://vkedco.blogspot.kr 2
http://vkedco.blogspot.mx 2
http://www.vkedco.blogspot.tw 2
http://vkedco.blogspot.pt 2
http://vkedco.blogspot.co.uk 2
http://vkedco.blogspot.it 2
http://vkedco.blogspot.com.ar 2
http://vkedco.blogspot.nl 2
http://www.vkedco.blogspot.ca 2
http://vkedco.blogspot.com.es 2
http://www.vkedco.blogspot.nl 1
http://vkedco.blogspot.be 1
http://vkedco.blogspot.gr 1
http://vkedco.blogspot.com.au 1
http://vkedco.blogspot.co.at 1
http://www.vkedco.blogspot.be 1
http://vkedco.blogspot.cz 1
http://vkedco.blogspot.co.nz 1
http://vkedco.blogspot.ie 1
More...

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    MobiCom on Android: MinMax Games, Content Providers as Game Engines, Asynchronous Tasks, Game Engine Clients MobiCom on Android: MinMax Games, Content Providers as Game Engines, Asynchronous Tasks, Game Engine Clients Presentation Transcript

    • MobiCom on Android Min/Max Games, Content Providers as Game Engines, Asynchronous Tasks, & Game Engine Clients Vladimir Kulyukinhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • Outline ● Some Background on Tic Tac Toe, ever so simple and ever so popular ● Min/Max Games ● Auto Generation & Persistence of Min/Max Game Trees ● Min/Max Game Engines as Content Providers ● Asynchronous Tasks ● Min/Max Game Engine Clientshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Tic Tac Toe Backgroundwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Why Tic Tac Toe? ● This game was known in Roman Empire and Ancient Egypt ● Useful pedagogical tool that ties many concepts together ● Useful AI testbed with for testing various algorithms beginning from machine learning to game state search optimization: if your AI cannot handle Tic Tac Toe, it may not be not worth muchwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Tic Tac Toe Combinatorics ● 19,683 possible board layouts ● 19,683 = 39 because each of the nine board cells can be X, O, or None ● 362,880 = 9! possible games, where each game is a sequence of Xs and Os of length 9 ● This is relatively simple (not as complex as chess, for example) but non-trivial combinatoricswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Tic Tac Toe Combinatorics ● In 2001 and 2002, Henry Bottomley and Steve Schaeffer proved that, assuming that X is the first to move: – 131,184 finished games are won by (X) – 77,904 finished games are won by (O) – 46,080 finished games are drawnwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Min/Max Gameshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Min/Max Games ● Min/Max games are played by two players: MAX & MIN ● MAX & MIN are just names that can be replaced with WHITE & BLACK in the context of names ● MAX typically moves first and then turns are taken until the game is overwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Formal Definition of Min/Max Games ● A Min/Max game is defined by the following components: – Initial state – some formal representation of the initial state of the game where MAX must move – A finite set of operators available to both players (e.g., legal chess moves) – A game end test to determine if the current state is terminal – A state utility function that evaluates the current state for both players (e.g., utility function may return 1, 0, and -1, where 1 is a win for MAX, 0 is a draw, and -1 is a win for MIN)www.youtube.com/vkedco www.vkedco.blogspot.com
    • Generic Min/Max Algorithm ● Given a game state, a player, and a utility function – Use the operators available to the player to generate the set of next states – For each of the generated states: ● if a state is terminal, use the utility function to compute its utility for the player ● if a state is not terminal, switch the player and recursively compute its utility – Sort the states by utility – If the player is MAX, choose a maximum utility state – if the player is MIN, choose a minimum utility statewww.youtube.com/vkedco www.vkedco.blogspot.com
    • Example Mk,m is k-th move from state m MAX chooses state 4 from state 1, MAX 1 u=2 because it gives a maximum utility of 2 u=n, is utility value for a specificstate M1,1 M1,3 MIN chooses state 9 M1,2 from state 4, because it gives a minimum utility of 2 MIN 2 u=1 3 u=0 4 u=2 M2,1 M2,2 M3,1 M3,2 M4,1 M4,2 5 6 7 8 9 10 u=1 u=3 u=0 u=5 u=2 u=10http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Tic Tac Toe Game Engine as an Android Content Providerhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Problem Develop a Tic Tac Toe game engine as an Anroid content provider. Tic Tac Toe clients can query the content provider for appropriate moves in specific states.www.youtube.com/vkedco www.vkedco.blogspot.com
    • Plan of Attack ● Develop a Tic Tac Toe Min/Max Tree Generator (since this is a MobiCom class, we will skip this step and use an existing generator) ● Persist the generated tree (disk or database) ● Place the generated tree in a content provider ● Implement a Tic Tac Toe client that queries the content provider for optimal moveswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Auto Generation & Persistence of Tic Tac Toe Min/Max Game State Treeshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Board State Encodings Board State 1 Board State 2 Coding Conventions: row 0 x – encodes x in a cell row 0 o – encodes o in a cell ? - encodes a blank row 1 row 1 row 2 row 2 To encode Board State 1: To encode Board State 2: 1) row 0 is encoded as x??; 1) row 0 is encoded as x??; 2) row 1 is encoded as ???; 2) row 1 is encoded as ?o?; 3) row 2 is encoded as ???; 3) row 2 is encoded as ???; 4) concatenation of tree row 4) concatenation of tree row encodings gives us the encoding encodings gives us the encoding of the entire board state as of the entire board state as x???????? x???o????http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Persisting Min/Max Game Trees in TXT Files ● While the Tic Tac Toe combinatorics are non-trivial, they are still simple enough for us the generate the entire Tic Tac Toe min/max tree and save it in a file ● The question is, what should the file format be? ● We will adopt a format where each line will have the following syntax: Line ::= Board_State;Player; MoveUtilities ● Example 01: ?????????;X;0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 ● In Example 01, the board state is ?????????; the player is X; and the move utilities are 0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0, which means that if X plays 0, the utility of that move is 0; if X plays 1, the utility of that move is 1, etc. This means that Tic Tac Toe is a draw game. ● Example 02: x????????;O;4:0 1:1 2:1 3:1 5:1 6:1 7:1 8:1 ● In Example 02, the board state is x????????; the player is O, and the move utilities are 4:0 1:1 2:1 3:1 5:1 6:1 7:1 8:1www.youtube.com/vkedco www.vkedco.blogspot.com
    • TicTacToe Game State Tree TXT File Here is an excerpt from a Tic Tac Toe Game State Tree file: ?????????;X;0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 x????????;O;4:0 1:1 2:1 3:1 5:1 6:1 7:1 8:1 x???o????;X;1:0 2:0 3:0 5:0 6:0 7:0 8:0 xx??o????;O;2:0 3:1 5:1 6:1 7:1 8:1 xxo?o????;X;3:-1 5:-1 7:-1 8:-1 6:0 xxoxo????;O;6:-1 5:1 7:1 8:1 xxoxo?o??;X;-1 xxoxoo???;X;7:-1 8:-1 6:1 xxoxoo?x?;O;6:-1 8:-1 xxoxooox?;X;-1 Click here to download a Eclipse project with a Java program that generates a Tic Tac Toe Min/Max game state tree and saves it in a file with the above formathttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Getting Off the UI Thread ● OK, we have figured out how to generate and persist Tic Tac Toe game state trees ● Next question, when does a content provider access those trees? ● One possible answer is to place a TXT file into /assets folder, read it line by line when the content provider app is installed, and persist each line as a record in an SQLite database ● Note that once the SQLite database is created, the TXT file can be discarded, because the content provider will retrieve all info from the database ● Since the creation of a large SQLite database is time consuming, it should be done off the main thread – enter AsyncTaskwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Asynchronous Taskshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • When to Use AsyncTask ● Asynchronous tasks (AsyncTask) allow developers to get expensive computations off the main UI thread ● AsyncTasks are best suited for long one-time computations (downloads, initializaitons, etc) ● If repeated computations are required, threads, background services and other tools in java.util.concurrent should be consideredhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • AsyncTask<Param, Progress, Result> ● AsyncTask is defined by three generic parameters: Param, Progress, and Result ● Param, Progress, and Result must be legal Java data types ● Param is the input type ● Progress is the progress type ● Result is the output typehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • AsyncTask Execution ● An AsyncTask is executed in four stages, each of which is done in the corresponding overriden method: – 1) onPreExecute() – 2) doInBackground(Param... input_params) – 3) onProgressUpdate(Progress... progress_params) – 4) onPostExecute(Result rslt)http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • AsyncTask.onPreExecute() ● onPreExecute() is executed on the main UI thread ● This method is used to do the setup and can complement the constructor ● If you application, for example, needs a progress bar, this method is the place to do ithttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • AsyncTask.doInBackground() ● This method is invoked off the main UI thread ● This method is used to perform all background computation ● This method returns the Result type ● This step can use publishProgress() to publish progress on the main threadhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • AsyncTask.onProgressUpdate() ● This method is invoked on the main UI thread ● The timing of execution is undefined in the documentation ● This method is executed while the background computation is still in progresshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • AsyncTask.onPostExecute() ● This method is invoked on the main UI thread ● The method is executed after the background computation is done ● The result of the background computation is passed to this methodhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Implementation Details ● An implementation class must extend AsyncTask ● An implementation must, at the very least, implement doInBackground() ● Many implementations also implement onPostExecute()http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Example // Param is String, i.e., doInBackground takes a variable array of Strings // Program is Void, which means that we will not track progress // Result is String, i.e., doInBackground returns a String class TicTacToeBoardStateDbPopulator extends AsyncTask<String, Void, String>{ protected void onPreExecute() { // do initialization on the main UI thread } protected String doInBackground(String... params) { // do processing off the main UI thread } protected void onPostExecute(String rslt) { // do post excecution things on the main UI thread } }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • AsyncTask Cancelation ● An AsyncTask can be canceled at any time by invoking cancel() ● You can check if an AsyncTask has been canceled via isCanceled() ● It is a good best practice to check if the task is canceled in doInBackground()http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Threading & AsyncTasks ● An AsyncTask must be created on the main UI thread ● An AsyncTask must do execute(Param...) on the main UI thread ● An AsyncTask guarantees that all callback calls are synchronizedhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • TicTacToeGameCPCreator Application source code is herehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • What This App Does ● The apps sole purpose is to install a content provider that other applications can query for possible moves in a given Tic Tac Toe boards ● The app places the create of an SQLite database from a game state txt file in an subclass of AsyncTask called TicTacToeBoardStateDbPopulatorTask ● This class implements onPreExecute(), doInBackground(), and onPostExecute()http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • AsyncTask Constructor // Take a content and a reference to a writetable SQLite database public TicTacToeBoardStateDbPopulatorTask(Context cntxt, SQLiteDatabase writeDb) { mContext = cntxt; mWriteDb = writeDb; }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Where AsyncTask is Launched // The AsyncTask is launced into execution in onCreate() of the main activity // TicTacToeCPCreateAct // large_game_state_tree is a tic tac toe game state tree saved in /assets final static String ASSETS_GAME_STATE_TREE_FILE = "large_game_state_tree.txt"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tic_tac_toe_cp_creator_activity_layout); TicTacToeBoardStateDbAdptr.createTicTacToeBoardStateDbOpenHelper(this); new TicTacToeBoardStateDbPopulatorTask(this, TicTacToeBoardStateDbAdptr.getWriteDb()) .execute(ASSETS_GAME_STATE_TREE_FILE); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • The Workhorse Method class TicTacToeBoardStateDbPopulator extends AsyncTask<String, Void, String>{ // this is the worhorse method that populates the board state sqlite database through // a call to a static method of the TicTacToeBoardStateDbPopulator class @Override protected String doInBackground(String... params) { TicTacToeBoardStateDbPopulator .populateTTTBoardStateDb(mContext, params[0], mWriteDb); // see next slide return DB_CREATED; // this is a string constant } }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Population of DB static final String SEMICOLON = ";"; // each line in TXT file is ;-separated final static void populateTTTBoardStateDb(Context cntxt, String file_path, SQLiteDatabase writeDb) { InputStream is = null; BufferedReader br = null; try { is = cntxt.getResources().getAssets().open(file_path, Context.MODE_WORLD_READABLE); br = new BufferedReader(new InputStreamReader(is)); String line = null; while ((line = br.readLine()) != null) { processGameStateLine(line, writeDb); } } catch (IOException e) { e.printStackTrace(); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Content Provider Registration ● Remember to register the content provider in AndroidManifest.xml ● We define an authority for the clients to interact with the content provider ● We also request the content provider to run in a separate process <provider android:name=".TicTacToeGameContentProvider" android:authorities="org.vkedco.mobappdev.content_providers.tic_tac_toe_boards" android:process=":org.vkedco.mobappdev.content_providers.tic_tac_toe_boards" > </provider>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Responding to Clients Queries ● Clients will query the Tic Tac Toe game engine content provider with the following URIs: content://org.vkedco.mobappdev.content_providers.tic_tac_toe_boards/board_state/query?board=x??0????? ● The value of the board parameter is used as the key to the SQLite Tic Tac Toe game tree database ● The db lookup is done in the query method of TicTacToeGameContentProvider.java ● The query method returns a Cursor object to the current Contexts ContentResolver, which then passes it on to the client ● Note the synchronized db access in query()http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • TicTacToe Game Client source code is herewww.youtube.com/vkedco www.vkedco.blogspot.com
    • Tic Tac Toe Game Client ● The client displays a Tic Tac Toe board and lets the player choose to play X or O with a menuhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Tic Tac Toe Game Client ● The client captures touch screen clicks, draws appropriate symbols (X or O) for the user on the board, encodes the board states, queries the content provider for the optimal moves of the opponent, and draws the opponents symbols (X or O) on the boardhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Tic Tac Toe Clients CP Queries ● The clients strings.xml (see white line below) contains the content providers URI ● The client encodes the board states (e.g., x????????) and uses those strings as the values of the board parameter to query the content provider ● The actual quering is done in TicTacToeContentProviderProxy.java in the getBoardMoveUtilsByBoard() method, which uses the current Contexts Content Resolver to get a Cursor object from the content provider <string name="ttt_board_query_uri">content://org.vkedco.mobappdev.content_providers.tic_tac_toe_boards/board_state/query?board=</string>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Tic Tac Toe Game Client: End Game ● The client detects end games and signals them with appropriate toastshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • References ● http://en.wikipedia.org/wiki/Tic-tac-toe ● http://en.wikipedia.org/wiki/Minimax ● en.wikipedia.org/wiki/Touchscreen ● http://developer.android.com/reference/android/os/AsyncTask.html ● http://developer.android.com/reference/android/view/View.OnTouchListener.htmlhttp://www.youtube.com/vkedco www.vkedco.blogspot.com