简体   繁体   English

高尔夫纸牌游戏 State 树

[英]Game State Tree for Golf Solitaire

I am working on a homework project in which we need to generate a state tree for the game of Golf Solitaire.我正在做一个家庭作业项目,我们需要为高尔夫纸牌游戏生成一棵 state 树。 To do this I decided to create a Move class which represents a move from one stack of cards to another - simply put, it holds a reference to the two stacks involved in a single move.为此,我决定创建一个Move class,它表示从一叠纸牌到另一叠纸牌的移动 - 简而言之,它包含对单个移动中涉及的两叠纸牌的引用。 Each node in my tree knows what it's move is.我树中的每个节点都知道它要移动的是什么。

The problem I am currently having is that my trees are much too large - like OutOfMemoryError too large.我目前遇到的问题是我的树太大了——比如OutOfMemoryError太大了。 Edit - I should note that when I make a tree for a very small game I do not get an error, but my tree is still much larger than it should be!编辑- 我应该注意,当我为一个非常小的游戏制作一棵树时,我没有收到错误,但我的树仍然比它应该的大得多!

For those unfamiliar with the game: Golf Solitaire Rules .对于那些不熟悉游戏的人: 高尔夫纸牌规则 Note - We did not need to implement the restriction on playing a Queen after a King.注意- 我们不需要实施在国王之后扮演女王的限制。

public void makeTree()
{
  _root = makeNode( null, null, 0 );
}

private Node makeNode( Node parent, Move m, int depth )
{
  Node node = new Node( parent, m, depth );
  ArrayList<Move> moves = findAllMoves();
  if( moves.size() == 0 )
    node.setScore( getScore() );
  else {
    for( Move mv : moves ) {
      mv.makeMove();
      Node child = makeNode( node, mv, depth++ );
      node.addChild( child );
      mv.undoMove();
    }
  }
  return node;
}

private ArrayList<Move> findAllMoves()
{
  ArrayList<Move> moves = new ArrayList<Move>();

  for( int i = 0; i < numPlayPiles; i++ ) {
    if( _play[i].size() != 0 ) {
       if( Math.abs( _play[i].top().getRank().ordinal() - 
          discard.getRank().ordinal() ) == 1 ) {
          moves.add( new Move( _play[i], _discard ) );
       }
    }
  }

  if( _draw.size() != 0 )
    moves.add( new Move( _draw, _discard ) );

  return moves;
}

_play[i] is simply referencing a pile of cards in the game. _play[i]只是引用游戏中的一堆纸牌。

I realize this is a bit much of code to be posting in a question, but my question is simply about my logic.我意识到要在问题中发布的代码有点多,但我的问题只是关于我的逻辑。 I have a suspicion that my error is in how I am returning makeNode yet I am totally stuck here.我怀疑我的错误在于我返回makeNode的方式,但我完全被困在这里。 Could anybody confirm my suspicion and/or possibly give me some guidance?有人可以证实我的怀疑和/或可能给我一些指导吗?

For anybody who is curious, my issue was one of not reading the assignment properly.对于任何好奇的人,我的问题之一是没有正确阅读作业。 Specifically, my findAllMoves method was the issue.具体来说,我的findAllMoves方法是问题所在。 A node should have either a move from the draw pile or all possible moves from play piles, but not both.一个节点应该抽牌堆中移动或从游戏堆中移动所有可能的移动,但不能同时移动。 My code above allows each child, even at the same depth, to each create a Move from the draw pile to discard pile - resulting in a huge amount of duplicate nodes.我上面的代码允许每个孩子,即使在相同的深度,每个孩子都创建一个从抽奖堆到丢弃堆的Move - 导致大量重复节点。

It also never checks the end-game conditions.它也从不检查游戏结束条件。 As a result, each leaf node was either a losing score, or 0 (the method would recurse until the draw pile was empty even if the player already won).结果,每个叶节点要么是失败的分数,要么是 0(即使玩家已经赢了,该方法也会递归直到抽奖堆为空)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM