SlideShare a Scribd company logo
Building a State Tree

• Node associated with a
 state of the game
 • has 1 child node for each
   valid move from the node
 • score of the node is the best
   score of its children
• Leafno valid moves
       node
 • has
 • leaf score determined by end
   state




                                   1
Building a State Tree
                              void buildTree():
                              root = makeNode( startState, null )

• Node associated with a            Node makeNode( GameState s, Node parent ):
    state of the game               node = new Node( s, parent )
    • has 1 child node for each     moves = findAllMoves( s )
                                    if moves.size == 0 // base case: leaf node
      valid move from the node
    • score of the node is the best   node.score = getScore( s )
                                    else
      score of its children           foreach move in moves
•   Leaf node                            save state s
    • has no valid moves                 do the move
    • leaf score determined by end       child = makeNode( s, node )
                                         node.children.add( child )
      state
                                         if child.score is best so far
                                           bestChild = child
                                         restore state s
                                      node.score = bestChild.score




                                            1
Using a Move object




         2
Using a Move object
void doMove( Move mv )
   {
      CardStack startPile = mv.getStartPile();
      CardStack endPile = mv.getEndPile();
      Card c = startPile.pop();
      endPile.push( c );
   }

void undoMove( Move mv )
   {
      CardStack toPile = mv.getStartPile();
      CardStack fromPile = mv.getEndPile();
      if ( fromPile.size() == 0 )
         print error msg
      else
         toPile.push( fromPile.pop() );
       update();
   }




                        2
Build State tree with Move


Alternatively, save
differences between states
(the move!)
  •   the “inverse” move
      restores the previous
      state
  •   much more efficient




                              3
Build State tree with Move
                              void buildTree():

                              root = makeNode( null,
                                       new Move( _drawPile, _discards ), 0 );

                              Node makeNode( Node parent, Move m, int depth):
Alternatively, save           node = new Node( parent, m, depth )
differences between states    moves = findAllMoves( )
(the move!)                   if moves.size == 0 // base case:
                                                    // leaf node game ends
  •   the “inverse” move        node.score = getScore()
      restores the previous   else
      state                     foreach move in moves
  •   much more efficient          doMove( move )
                                   do the move
                                   child = makeNode( node, move, depth+1 )
                                   node.children.add( child )
                                   if child.score is best so far
                                     bestChild = child
                                   undoMove( move )
                                node.score = bestChild.score




                                            3

More Related Content

DOC
Describe what makes Utilize Consulting and Management Service -Det CEO MAG
PDF
Pdf open parameters
PDF
212 t5dlv gabriela soto olea francisco cuamea lizárraga_serie ohuira completa
DOCX
Soal uts pkn kelas ix
PPSX
Video
PDF
By Eng. Ashraf Ayoub المشروع النهائى
PPTX
Presentation with all my experiences
PDF
Arrecifes de Coral
Describe what makes Utilize Consulting and Management Service -Det CEO MAG
Pdf open parameters
212 t5dlv gabriela soto olea francisco cuamea lizárraga_serie ohuira completa
Soal uts pkn kelas ix
Video
By Eng. Ashraf Ayoub المشروع النهائى
Presentation with all my experiences
Arrecifes de Coral

Viewers also liked (7)

PPTX
The European Economy
PDF
[Vnmath.com] sophuc tu a toi z
PDF
Agricultural Drainage
PPT
Alat peraga matematika
DOCX
Soal uts pkn kelas ix
PDF
Ryder Systems, Inc. Case Study: Comprehensive Business Analysis and Review
DOCX
B_C_Shelton Resume_updated-2015
The European Economy
[Vnmath.com] sophuc tu a toi z
Agricultural Drainage
Alat peraga matematika
Soal uts pkn kelas ix
Ryder Systems, Inc. Case Study: Comprehensive Business Analysis and Review
B_C_Shelton Resume_updated-2015
Ad

Recently uploaded (20)

DOCX
Elisabeth de Pot, the Witch of Flanders .
PPTX
Squares64 Quiz, A chessboard of questions, crafted with care by @mahi_anmol_ ...
PPTX
Picture Perception - a constructive narrative
PPTX
GILGIT BALTISTAN HISTORY ,ADMINISTRATIVE , CONSTITUTUINAL STATUS , GEOGRAPMY ...
DOC
UNG毕业证学历认证,阿莱恩特国际大学毕业证文凭证书
PDF
Module-3-Week005-to-Week006-PPT.pdf hahahgs
PDF
Best IPTV Service Providers in the UK (2025) – Honest Reviews & Top Picks
PPTX
701301-Happy Birthday Slideshow Template.pptx
PDF
WKA #29: "FALLING FOR CUPID" TRANSCRIPT.pdf
PPTX
Introduction to NGO’s098765789709876.pptx
PDF
Download FL Studio Crack Latest version 2025
PPTX
Health_System_in_India_Oasrganization_BSc_Nursing.pptx
PDF
Download GTA 5 Free Full PC Game+Latest Version 2025
PPTX
Social Awareness on Municipal Solid Waste.pptx
PDF
CityofHorror_v1.1.pdf manual en español i
PDF
WKA #29: "FALLING FOR CUPID" TRANSCRIPT.pdf
PDF
mnbnyuynhncf ytdnbvdfghdfhghdhdfhdghdghdghghgfhfh
PPTX
Safety_Pharmacology_Tier2_Edibbbbbbbbbbbbbbbable.pptx
PPTX
SWweredddddaregqrgWWEQEwqdewf final.pptx
PDF
Hip Hop Culture – More Than Just Music & Style
Elisabeth de Pot, the Witch of Flanders .
Squares64 Quiz, A chessboard of questions, crafted with care by @mahi_anmol_ ...
Picture Perception - a constructive narrative
GILGIT BALTISTAN HISTORY ,ADMINISTRATIVE , CONSTITUTUINAL STATUS , GEOGRAPMY ...
UNG毕业证学历认证,阿莱恩特国际大学毕业证文凭证书
Module-3-Week005-to-Week006-PPT.pdf hahahgs
Best IPTV Service Providers in the UK (2025) – Honest Reviews & Top Picks
701301-Happy Birthday Slideshow Template.pptx
WKA #29: "FALLING FOR CUPID" TRANSCRIPT.pdf
Introduction to NGO’s098765789709876.pptx
Download FL Studio Crack Latest version 2025
Health_System_in_India_Oasrganization_BSc_Nursing.pptx
Download GTA 5 Free Full PC Game+Latest Version 2025
Social Awareness on Municipal Solid Waste.pptx
CityofHorror_v1.1.pdf manual en español i
WKA #29: "FALLING FOR CUPID" TRANSCRIPT.pdf
mnbnyuynhncf ytdnbvdfghdfhghdhdfhdghdghdghghgfhfh
Safety_Pharmacology_Tier2_Edibbbbbbbbbbbbbbbable.pptx
SWweredddddaregqrgWWEQEwqdewf final.pptx
Hip Hop Culture – More Than Just Music & Style
Ad

7 p help

  • 1. Building a State Tree • Node associated with a state of the game • has 1 child node for each valid move from the node • score of the node is the best score of its children • Leafno valid moves node • has • leaf score determined by end state 1
  • 2. Building a State Tree void buildTree(): root = makeNode( startState, null ) • Node associated with a Node makeNode( GameState s, Node parent ): state of the game node = new Node( s, parent ) • has 1 child node for each moves = findAllMoves( s ) if moves.size == 0 // base case: leaf node valid move from the node • score of the node is the best node.score = getScore( s ) else score of its children foreach move in moves • Leaf node save state s • has no valid moves do the move • leaf score determined by end child = makeNode( s, node ) node.children.add( child ) state if child.score is best so far bestChild = child restore state s node.score = bestChild.score 1
  • 3. Using a Move object 2
  • 4. Using a Move object void doMove( Move mv ) { CardStack startPile = mv.getStartPile(); CardStack endPile = mv.getEndPile(); Card c = startPile.pop(); endPile.push( c ); } void undoMove( Move mv ) { CardStack toPile = mv.getStartPile(); CardStack fromPile = mv.getEndPile(); if ( fromPile.size() == 0 ) print error msg else toPile.push( fromPile.pop() ); update(); } 2
  • 5. Build State tree with Move Alternatively, save differences between states (the move!) • the “inverse” move restores the previous state • much more efficient 3
  • 6. Build State tree with Move void buildTree(): root = makeNode( null, new Move( _drawPile, _discards ), 0 ); Node makeNode( Node parent, Move m, int depth): Alternatively, save node = new Node( parent, m, depth ) differences between states moves = findAllMoves( ) (the move!) if moves.size == 0 // base case: // leaf node game ends • the “inverse” move node.score = getScore() restores the previous else state foreach move in moves • much more efficient doMove( move ) do the move child = makeNode( node, move, depth+1 ) node.children.add( child ) if child.score is best so far bestChild = child undoMove( move ) node.score = bestChild.score 3

Editor's Notes