Exemple #1
0
        public void LoadFromFile(string fileName)
        {
            if (!File.Exists(fileName))
            {
                throw new System.ArgumentException(string.Format("Cannot Open file {0} for deserialization.", fileName));
            }

            try
            {
                Stream        read     = File.OpenRead(fileName);
                SoapFormatter soapRead = new SoapFormatter();

                game aGame = soapRead.Deserialize(read) as game;
                if (aGame == null)
                {
                    throw new System.Exception(string.Format("This file doesn't contain a Peg Game, {0}", fileName));
                }

                aGame.board.CopyTo(this.board, 0);
                this.pastMoves = null; // not sure if necessary
                this.pastMoves = new System.Collections.Generic.Stack <MoveTuple>(aGame.pastMoves);
            }
            catch (Exception exc)
            {
                System.Diagnostics.Trace.WriteLine(exc.Message);
                throw exc;
            }
        }
Exemple #2
0
        // Friendly, type-safe clone method
        public virtual game Clone()
        {
            // Start with a flat, memberwise copy
            game x = this.MemberwiseClone() as game;

            // Then deep-copy everything that needs the
            // special attention
            //...
            x.board     = new bool[15];
            x.pastMoves = new System.Collections.Generic.Stack <MoveTuple>(50);

            for (int idx = this.board.GetLowerBound(0); idx <= this.board.GetUpperBound(0); idx++)
            {
                x.board[idx] = this.board[idx];
            }

            if (this.pastMoves.Count > 0)
            {
                Array arr = this.pastMoves.ToArray();
                for (int idx = arr.GetUpperBound(0); idx >= arr.GetLowerBound(0); idx--)
                {
                    if (arr.GetValue(idx) != null)
                    {
                        x.pastMoves.Push((MoveTuple)arr.GetValue(idx));
                    }
                }
            }
            return(x);
        }
Exemple #3
0
        static public game LoadGameFromFile(string fileName)
        {
            if (!File.Exists(fileName))
            {
                throw new System.ArgumentException(string.Format("Cannot Open file {0} for deserialization.", fileName));
            }
            try
            {
                Stream        read     = File.OpenRead(fileName);
                SoapFormatter soapRead = new SoapFormatter();

                game aGame = soapRead.Deserialize(read) as game;
                if (aGame == null)
                {
                    throw new System.Exception(string.Format("This file doesn't contain a Peg Game, {0}", fileName));
                }

                return(aGame);
            }
            catch (Exception exc)
            {
                System.Diagnostics.Trace.WriteLine(exc.Message);
                throw exc;
            }
        }
Exemple #4
0
 private void menuNew_Click(object sender, System.EventArgs e)
 {
     theGame = null;
     theGame = new game();
     theGame.SetupBoard(EmptyHoleChoice());
     pegToMove      = -1;
     pegDestination = -1;
     setTheBoard();
 }
Exemple #5
0
 private void loadMovesMenu_Click(object sender, System.EventArgs e)
 {
     try
     {
         //theGame.LoadFromFile(@"c:\game.xml");
         theGame        = game.LoadGameFromFile(@"c:\game.xml");
         pegToMove      = -1;
         pegDestination = -1;
         setTheBoard();
     }
     catch (Exception exec)
     {
         MessageBox.Show(exec.Message, exec.Source);
     }
 }
Exemple #6
0
 static void BuildTree(GameNode root)
 {
     //build children
     foreach (MoveTuple mv in root.Game.AvailableMoves)
     {
         trianglePegs.game child = root.Game.Clone();
         child.Move(mv.original, mv.destination);
         Int64 childSig = child.Signature;
         if (!uniqueGameStates.Contains(childSig))
         {
             GameNode gnChild = new GameNode(child);
             root.Children.Add(gnChild);
             uniqueGameStates.Add(childSig, gnChild);
             BuildTree(gnChild);
         }
     }
 }
Exemple #7
0
        public board()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            gameDomain = new GameDomain();

            theGame = new game();
            theGame.SetupBoard(EmptyHoleChoice());
            holes          = new System.Collections.ArrayList(15);
            pegToMove      = -1;
            pegDestination = -1;

            layoutTheBoard();

            setTheBoard();
            pegToMove      = -1;
            pegDestination = -1;
        }
Exemple #8
0
        public static MoveTuple Diff(game BeforeMoveBoard, game AfterMoveBoard)
        {
            if (BeforeMoveBoard.PegsLeft - 1 != AfterMoveBoard.PegsLeft)
            {
                throw new ArgumentException("The BeforeMoveBoard must have only one more peg on board than the AfterMoveBoard", "BeforeMoveBoard");
            }

            Int64 b4Sig  = BeforeMoveBoard.Signature;
            Int64 aftSig = AfterMoveBoard.Signature;

            MoveTuple mt = null;

            for (int originalPosition = 0; originalPosition <= BeforeMoveBoard.board.GetUpperBound(0); originalPosition++)
            {
                if (BeforeMoveBoard.board[originalPosition] != AfterMoveBoard.board[originalPosition])
                {
                    if (!AfterMoveBoard.board[originalPosition])
                    {     // Since originalPosition is empty, it could be the original position
                        for (int destinationPosition = 0; destinationPosition <= BeforeMoveBoard.board.GetUpperBound(0); destinationPosition++)
                        { // if once a blank is found now look for possible leagl moves
                            if (BeforeMoveBoard.IsValidMove(originalPosition, destinationPosition))
                            {
                                int jumpedSpace = BeforeMoveBoard.JumpedSpace(destinationPosition, originalPosition);
                                if (aftSig == b4Sig + Convert.ToInt64(Math.Pow(2, destinationPosition) - Math.Pow(2, jumpedSpace) - Math.Pow(2, originalPosition)))
                                {
                                    mt = new MoveTuple(originalPosition, jumpedSpace, destinationPosition);
                                    break;
                                }
                            }
                        }
                    }
                    if (mt != null)
                    {
                        break;
                    }
                }
            }
            return(mt);
        }