private BgMove() { From = 0; To = 0; FirstMove = null; NextMove = null; }
public BgMove CommitMove() { if (IsAgentMoveing() && m_MoveRepresentationList.Count() > 0) { int numBestMoves = 1; double bestMoveScore = m_MoveRepresentationList.GetMoveRepresentation(0).GetScore(); for (int i = 1; i < m_MoveRepresentationList.Count(); i++) { double currentCompareMoveScore = m_MoveRepresentationList.GetMoveRepresentation(i).GetScore(); if (bestMoveScore == currentCompareMoveScore) { numBestMoves++; } else { break; } } int selectedMove = m_Rand.Next(numBestMoves); MoveRepresentation bestMove = m_MoveRepresentationList.GetMoveRepresentation(selectedMove); BgMove m = bestMove.GetMoves().FirstMove; while (m != null) { m_BgGame.MakeMove(m.From, m.To); m = m.NextMove; } return(bestMove.GetMoves()); } else { return(null); } }
public static BgMove CloneFromFirst( BgMove move ) { if ( !BgMove.ReferenceEquals( move, move.FirstMove ) ) return null; BgMove result = new BgMove(); result.From = move.From; result.To = move.To; result.FirstMove = result; result.NextMove = CloneSubMove( result, move.NextMove ); return result; }
private static BgMove CloneSubMove( BgMove precedingMove, BgMove cloneFrom ) { if ( cloneFrom == null ) return null; BgMove result = new BgMove(); result.From = cloneFrom.From; result.To = cloneFrom.To; result.FirstMove = precedingMove.FirstMove; result.NextMove = CloneSubMove( result, cloneFrom.NextMove ); return result; }
public MoveRepresentation GetClone() { MoveRepresentation result = new MoveRepresentation(null); result.m_BoardPattern = new int[m_BoardPattern.Length]; for (int i = 0; i < m_BoardPattern.Length; i++) { result.m_BoardPattern[i] = m_BoardPattern[i]; } result.m_Move = BgMove.CloneFromFirst(m_Move.FirstMove); result.m_Score = m_Score; return(result); }
private static BgMove CloneSubMove(BgMove precedingMove, BgMove cloneFrom) { if (cloneFrom == null) { return(null); } BgMove result = new BgMove(); result.From = cloneFrom.From; result.To = cloneFrom.To; result.FirstMove = precedingMove.FirstMove; result.NextMove = CloneSubMove(result, cloneFrom.NextMove); return(result); }
public BgMove(int from, int to, BgMove precedingMove) { From = from; To = to; NextMove = null; if (precedingMove == null) { FirstMove = this; } else { precedingMove.NextMove = this; FirstMove = precedingMove.FirstMove; } }
public BgMove( int from, int to, BgMove precedingMove ) { From = from; To = to; NextMove = null; if ( precedingMove == null ) { FirstMove = this; } else { precedingMove.NextMove = this; FirstMove = precedingMove.FirstMove; } }
public static BgMove CloneFromFirst(BgMove move) { if (!BgMove.ReferenceEquals(move, move.FirstMove)) { return(null); } BgMove result = new BgMove(); result.From = move.From; result.To = move.To; result.FirstMove = result; result.NextMove = CloneSubMove(result, move.NextMove); return(result); }
private void FindSingleMoves(BgMoveList moveList, int val) { for (int i = m_BgGame.BoardSquareCount - 1; i > 0; i--) { if (m_BgGame.SquareOwner(i) == m_BgGame.CurrentPlayer) { if (m_BgGame.ValidateMove(i, i - val)) { BgMove move = new BgMove(i, i - val, null); moveList.AddBgMove(move); } } if (i <= 6 && m_BgGame.ValidateMove(i, 26)) { BgMove move = new BgMove(i, 26, null); moveList.AddBgMove(move); } } }
public void UpdatePossibleMovesList(MoveRepresentationList knowledgeList) { m_PossibilitiesList.Items.Clear(); for (int i = 0; i < knowledgeList.Count(); i++) { string stringToAdd = ""; BgMove m = knowledgeList.GetMoveRepresentation(i).GetMoves(); while (m != null) { stringToAdd += ((m.From == 25)? "bar" : m.From.ToString()) + "/" + ((m.To == 26)? "off" : m.To.ToString()) + "\t"; m = m.NextMove; } stringToAdd += Math.Round(knowledgeList.GetMoveRepresentation(i).GetScore(), 5); m_PossibilitiesList.Items.Add(stringToAdd); } m_NumberPossibilitiesLabel.Text = "Possibilities: " + m_PossibilitiesList.Items.Count; }
public MoveRepresentation( GammonInterface game, BgMove move ) : base(game) { m_Score = 0.0; m_Move = move; BgMove m = m_Move.FirstMove; while ( m != null ) { if ( m.To != 26 && m_BoardPattern[m.To] == -1 ) { m_BoardPattern[m.To] = 0; m_BoardPattern[0]--; } m_BoardPattern[m.From]--; if ( m.To != 26 ) m_BoardPattern[m.To]++; m = m.NextMove; } }
private void GenerateMoves(BgMoveList moveList, BgMove prevMove) { BgMoveList singleMoves = FindAllSingleMoves(); if (singleMoves.Count() > 0) { for (int i = 0; i < singleMoves.Count(); i++) { BgMove currentMove = new BgMove(singleMoves.GetBgMove(i).From, singleMoves.GetBgMove(i).To, prevMove); m_BgGame.MakeMove(currentMove.From, currentMove.To); GenerateMoves(moveList, currentMove); m_BgGame.Undo(); } } else { if (prevMove != null) { moveList.AddBgMove(BgMove.CloneFromFirst(prevMove.FirstMove)); } } }
public MoveRepresentation(GammonInterface game, BgMove move) : base(game) { m_Score = 0.0; m_Move = move; BgMove m = m_Move.FirstMove; while (m != null) { if (m.To != 26 && m_BoardPattern[m.To] == -1) { m_BoardPattern[m.To] = 0; m_BoardPattern[0]--; } m_BoardPattern[m.From]--; if (m.To != 26) { m_BoardPattern[m.To]++; } m = m.NextMove; } }
private void GenerateMoves( BgMoveList moveList, BgMove prevMove ) { BgMoveList singleMoves = FindAllSingleMoves(); if ( singleMoves.Count() > 0 ) { for ( int i = 0; i < singleMoves.Count(); i++ ) { BgMove currentMove = new BgMove( singleMoves.GetBgMove( i ).From, singleMoves.GetBgMove( i ).To, prevMove ); m_BgGame.MakeMove( currentMove.From, currentMove.To ); GenerateMoves( moveList, currentMove ); m_BgGame.Undo(); } } else { if ( prevMove != null ) moveList.AddBgMove( BgMove.CloneFromFirst( prevMove.FirstMove ) ); } }
private void m_BoardEditMenuItem_Click( object sender, EventArgs e ) { m_BoardEditMenuItem.Checked = !m_BoardEditMenuItem.Checked; m_GameOver = false; if ( m_BoardEditMenuItem.Checked ) { m_NewGameMenuItem.Enabled = false; m_GammonGame.EditBoard(); DrawBoard(); UpdatePanel(); } else { m_NewGameMenuItem.Enabled = true; m_GammonGame.StopEditBoard(); m_Agent1.ViewBoard(); m_Agent2.ViewBoard(); m_LastMoveA1 = m_Agent1.CommitMove(); m_LastMoveA2 = m_Agent2.CommitMove(); DrawBoard(); UpdatePanel(); } }
public void m_BoardPictureBox_MouseDown(object sender, MouseEventArgs e) { int squareClick = SquareClicked( e.X, e.Y ); if ( m_BoardEditMenuItem.Checked ) { if ( squareClick == 26 ) m_GammonGame.EditBoard(); if ( DiceClicked( e.X, e.Y ) && e.Button == MouseButtons.Left ) DiceEditor.ShowDiceEditor( this, false ); else if ( DiceClicked( e.X, e.Y ) && e.Button == MouseButtons.Right ) m_GammonGame.SwitchPlayer(); //TODO: enable cube editing if ( e.Button == MouseButtons.Left ) m_GammonGame.PutPiece( squareClick, BgPlayer.Dark ); else if ( e.Button == MouseButtons.Right ) m_GammonGame.PutPiece( squareClick, BgPlayer.Light ); } else if ( !m_GameOver ) { if ( DiceClicked( e.X, e.Y ) && m_GammonGame.IsTurnOver() ) { if ( m_UsingCube ) { if ( m_Agent1.Double() || m_Agent2.Double() ) { if ( !( m_Agent1.IsAgentPlaying( m_GammonGame.CurrentPlayer ) || m_Agent2.IsAgentPlaying( m_GammonGame.CurrentPlayer ) ) ) { DialogResult r = MessageBox.Show( "Opponent doubles, do you accept?", "Opponent doubles", MessageBoxButtons.YesNo, MessageBoxIcon.Question ); if ( r == DialogResult.Yes ) m_GammonGame.DoubleCube( m_GammonGame.CurrentOpponentPlayer ); else if ( r == DialogResult.No ) m_GammonGame.SetWinner( m_GammonGame.CurrentOpponentPlayer ); } else { if ( m_Agent1.IsAgentPlaying( m_GammonGame.CurrentPlayer ) ) { if ( m_Agent1.AcceptDouble() ) m_GammonGame.DoubleCube( m_GammonGame.CurrentOpponentPlayer ); else m_GammonGame.SetWinner( m_GammonGame.CurrentOpponentPlayer ); } if ( m_Agent2.IsAgentPlaying( m_GammonGame.CurrentPlayer ) ) { if ( m_Agent2.AcceptDouble() ) m_GammonGame.DoubleCube( m_GammonGame.CurrentOpponentPlayer ); else m_GammonGame.SetWinner( m_GammonGame.CurrentOpponentPlayer ); } } } } if ( m_GammonGame.EndTurn() ) { if ( m_ManualDiceMenuItem.Checked ) DiceEditor.ShowDiceEditor( this, false ); m_Agent1.ViewBoard(); m_Agent2.ViewBoard(); m_LastMoveA1 = m_Agent1.CommitMove(); m_LastMoveA2 = m_Agent2.CommitMove(); if ( !m_GameOver ) UpdateDiceStats(); } } if ( CubeClicked( e.X, e.Y ) && m_GammonGame.IsTurnOver() ) { if ( m_UsingCube ) { if ( ( m_GammonGame.GetCubeOwner() == m_GammonGame.CurrentOpponentPlayer || m_GammonGame.GetCubeOwner() == BgPlayer.None ) && !( m_Agent1.IsAgentPlaying( m_GammonGame.CurrentOpponentPlayer ) || m_Agent2.IsAgentPlaying( m_GammonGame.CurrentOpponentPlayer ) ) ) { if ( m_Agent1.IsAgentPlaying( m_GammonGame.CurrentPlayer ) ) { if ( m_Agent1.AcceptDouble() ) m_GammonGame.DoubleCube( m_GammonGame.CurrentOpponentPlayer ); else m_GammonGame.SetWinner( m_GammonGame.CurrentOpponentPlayer ); } else if ( m_Agent2.IsAgentPlaying( m_GammonGame.CurrentPlayer ) ) { if ( m_Agent2.AcceptDouble() ) m_GammonGame.DoubleCube( m_GammonGame.CurrentOpponentPlayer ); else m_GammonGame.SetWinner( m_GammonGame.CurrentOpponentPlayer ); } else if ( !( m_Agent1.IsAgentPlaying( m_GammonGame.CurrentPlayer ) || m_Agent2.IsAgentPlaying( m_GammonGame.CurrentPlayer ) ) ) { DialogResult r = MessageBox.Show( "Opponent doubles, do you accept?", "Opponent doubles", MessageBoxButtons.YesNo, MessageBoxIcon.Question ); if ( r == DialogResult.Yes ) m_GammonGame.DoubleCube( m_GammonGame.CurrentOpponentPlayer ); else if ( r == DialogResult.No ) m_GammonGame.SetWinner( m_GammonGame.CurrentOpponentPlayer ); } } } } if ( m_GammonGame.Winner() != BgPlayer.None && !m_GameOver ) { m_Agent1.Learn(); m_Agent2.Learn(); string winner = ""; if ( m_GammonGame.Winner() == BgPlayer.Dark ) { winner = "Dark"; m_DarkWins[m_GammonGame.VictoryType() - 1]++; m_DarkWins[3] += m_GammonGame.VictoryType() * m_GammonGame.GetCubeValue(); } if ( m_GammonGame.Winner() == BgPlayer.Light ) { winner = "Light"; m_LightWins[m_GammonGame.VictoryType() - 1]++; m_LightWins[3] += m_GammonGame.VictoryType() * m_GammonGame.GetCubeValue(); } string winType = ""; if ( m_GammonGame.VictoryType() == 3 ) winType = "backgammon"; if ( m_GammonGame.VictoryType() == 2 ) winType = "gammon"; if ( m_GammonGame.VictoryType() == 1 ) winType = "normal"; MessageBox.Show( this, winner + " wins a " + winType + " victory\n\nDark sum: " + m_DarkDiceSum + ", Doubles: " + m_DarkDoubles + "\nLight sum: " + m_LightDiceSum + ", Doubles: " + m_LightDoubles, "Game over" ); m_GameOver = true; } if ( m_HoldingPieceFrom == -1 && squareClick >= 0 && squareClick <= 25 && m_GammonGame.SquareOwner( squareClick ) == m_GammonGame.CurrentPlayer ) { m_HoldingPieceFrom = squareClick; } else if ( m_HoldingPieceFrom >= 0 && squareClick >= 0 && squareClick <= 26 ) { m_GammonGame.MakeMove( m_HoldingPieceFrom, squareClick ); m_HoldingPieceFrom = -1; } } DrawBoard(); UpdatePanel(); }
public void NewGame() { if ( m_ManualDiceMenuItem.Checked ) DiceEditor.ShowDiceEditor( this, true ); if ( m_SeedSelectorForm.RandomSeed ) m_GammonGame.NewGame( m_NackgammonMenuItem.Checked, m_MaxFivePiecesOnPointMenuItem.Checked ); else m_GammonGame.NewGame( m_NackgammonMenuItem.Checked, m_MaxFivePiecesOnPointMenuItem.Checked, m_SeedSelectorForm.Seed ); m_HoldingPieceFrom = -1; m_GameOver = false; m_LightDiceSum = 0; m_DarkDiceSum = 0; m_LightDoubles = 0; m_DarkDoubles = 0; m_UsingCube = m_UseCubeMenuItem.Checked; UpdateDiceStats(); m_Agent1.ResetGeneretedBoards(); m_Agent2.ResetGeneretedBoards(); m_Agent1.ViewBoard(); m_Agent2.ViewBoard(); m_LastMoveA1 = m_Agent1.CommitMove(); m_LastMoveA2 = m_Agent2.CommitMove(); DrawBoard(); UpdatePanel(); }
private void AutoPlay( bool animate, bool fiveHundredLimit ) { bool lightDefined = false; bool darkDefined = false; if ( m_Agent1.IsAgentPlaying( BgPlayer.Light ) || m_Agent2.IsAgentPlaying( BgPlayer.Light ) ) lightDefined = true; if ( m_Agent1.IsAgentPlaying( BgPlayer.Dark ) || m_Agent2.IsAgentPlaying( BgPlayer.Dark ) ) darkDefined = true; if ( !lightDefined || !darkDefined ) { MessageBox.Show( "One or more of the playing sides is not assigned to an agent", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error ); return; } if ( !m_SeedSelectorForm.RandomSeed ) { DialogResult r = MessageBox.Show( "Are you sure you want to autoplay with a defined dice seed?", "Caution", MessageBoxButtons.YesNo, MessageBoxIcon.Question ); if ( r == DialogResult.No ) return; } m_Agent1.EnableGUI( false ); m_Agent2.EnableGUI( false ); m_AutoPlayStop.Enabled = true; m_AutoPlay.Enabled = false; m_AutoPlayFast.Enabled = false; m_AutoPlayThousand.Enabled = false; m_EditMenu.Enabled = false; m_NewGameMenuItem.Enabled = false; m_BoardPictureBox.Enabled = false; // AutoPlayCancelForm cancelForm = new AutoPlayCancelForm( this ); // cancelForm.Show(); m_HoldingPieceFrom = -1; m_GameOver = false; m_UsingCube = m_UseCubeMenuItem.Checked; if ( !animate ) { m_Agent1.OutputMovesAndScore = false; m_Agent2.OutputMovesAndScore = false; } if ( m_SeedSelectorForm.RandomSeed ) m_GammonGame.NewGame( m_NackgammonMenuItem.Checked, m_MaxFivePiecesOnPointMenuItem.Checked ); else m_GammonGame.NewGame( m_NackgammonMenuItem.Checked, m_MaxFivePiecesOnPointMenuItem.Checked, m_SeedSelectorForm.Seed ); m_LightDiceSum = 0; m_DarkDiceSum = 0; m_LightDoubles = 0; m_DarkDoubles = 0; UpdateDiceStats(); DrawBoard(); UpdatePanel(); int count = 500; m_AutoPlayMode = true; while ( m_AutoPlayMode && count > 0 ) { m_Agent1.ViewBoard(); m_Agent2.ViewBoard(); m_LastMoveA1 = m_Agent1.CommitMove(); m_LastMoveA2 = m_Agent2.CommitMove(); if ( m_UsingCube ) { if ( m_Agent1.Double() || m_Agent2.Double() ) { if ( m_Agent1.IsAgentPlaying( m_GammonGame.CurrentPlayer ) ) { if ( m_Agent1.AcceptDouble() ) m_GammonGame.DoubleCube( m_GammonGame.CurrentOpponentPlayer ); else m_GammonGame.SetWinner( m_GammonGame.CurrentOpponentPlayer ); } if ( m_Agent2.IsAgentPlaying( m_GammonGame.CurrentPlayer ) ) { if ( m_Agent2.AcceptDouble() ) m_GammonGame.DoubleCube( m_GammonGame.CurrentOpponentPlayer ); else m_GammonGame.SetWinner( m_GammonGame.CurrentOpponentPlayer ); } } } if ( animate ) { DrawBoard(); UpdatePanel(); System.Threading.Thread.Sleep( 500 ); } m_GammonGame.EndTurn(); UpdateDiceStats(); if ( m_GammonGame.Winner() != BgPlayer.None ) { if ( fiveHundredLimit ) count--; m_Agent1.Learn(); m_Agent2.Learn(); if ( m_GammonGame.Winner() == BgPlayer.Dark ) { m_DarkWins[m_GammonGame.VictoryType() - 1]++; m_DarkWins[3] += m_GammonGame.VictoryType() * m_GammonGame.GetCubeValue(); } if ( m_GammonGame.Winner() == BgPlayer.Light ) { m_LightWins[m_GammonGame.VictoryType() - 1]++; m_LightWins[3] += m_GammonGame.VictoryType() * m_GammonGame.GetCubeValue(); } if ( !animate ) UpdatePanel(); if ( m_SeedSelectorForm.RandomSeed ) m_GammonGame.NewGame( m_NackgammonMenuItem.Checked, m_MaxFivePiecesOnPointMenuItem.Checked ); else m_GammonGame.NewGame( m_NackgammonMenuItem.Checked, m_MaxFivePiecesOnPointMenuItem.Checked, m_SeedSelectorForm.Seed ); m_LightDiceSum = 0; m_DarkDiceSum = 0; m_LightDoubles = 0; m_DarkDoubles = 0; UpdateDiceStats(); if ( animate ) { DrawBoard(); UpdatePanel(); System.Threading.Thread.Sleep( 500 ); } } Application.DoEvents(); } m_Agent1.OutputMovesAndScore = true; m_Agent2.OutputMovesAndScore = true; m_Agent1.ViewBoard(); m_Agent2.ViewBoard(); m_LastMoveA1 = m_Agent1.CommitMove(); m_LastMoveA2 = m_Agent2.CommitMove(); DrawBoard(); UpdatePanel(); m_BoardPictureBox.Enabled = true; m_EditMenu.Enabled = true; m_NewGameMenuItem.Enabled = true; m_AutoPlayStop.Enabled = false; m_AutoPlay.Enabled = true; m_AutoPlayFast.Enabled = true; m_AutoPlayThousand.Enabled = true; m_Agent1.EnableGUI( true ); m_Agent2.EnableGUI( true ); }
private void FindSingleMoves( BgMoveList moveList, int val ) { for ( int i = m_BgGame.BoardSquareCount - 1; i > 0; i-- ) { if ( m_BgGame.SquareOwner( i ) == m_BgGame.CurrentPlayer ) { if ( m_BgGame.ValidateMove( i, i - val ) ) { BgMove move = new BgMove( i, i - val, null ); moveList.AddBgMove( move ); } } if ( i <= 6 && m_BgGame.ValidateMove( i, 26 ) ) { BgMove move = new BgMove( i, 26, null ); moveList.AddBgMove( move ); } } }
public void AddBgMove(BgMove move) { m_List.Add(move); }
public void AddBgMove( BgMove move ) { m_List.Add( move ); }