예제 #1
0
        // Menu click method that undoes the previous move.
        private void undoMoveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Finds the index of the last disk moved.
            int ind = board.movements.Count - 2;
            // Finds the ID of the last disk moved.
            int aLabelID = Convert.ToInt32(board.movements[ind]);
            // Finds the label of the disk moved.
            Label aLabel = FindLabel(aLabelID);
            // Gets the peg the label was moved to.
            int aPegID = Convert.ToInt32(board.movements[ind + 1]);
            // Removes the last move from the movements arraylist and returns the previous position of the Disk.
            int lastPeg = board.unDo(ind);
            // Deducts 1 from the move count.
            int Count = Convert.ToInt32(txt_Count.Text) - 1;

            txt_Count.Text = Convert.ToString(Count);
            // Resets the Moves text box to it's blank state and re-populates the moves made thus far.
            txt_Moves.Text = "";
            for (int i = 0; i < Count; i++)
            {
                DiskMove reDoMoves = new DiskMove(Convert.ToInt32(board.movements[i]), Convert.ToInt32(board.movements[i + 1]));
                txt_Moves.Text += reDoMoves.AsText();
            }
            // Finds the panel belonging to the last peg and moves the disk there.
            Panel aPanel = FindPanel(lastPeg);

            board.move(aLabel, lastPeg);
            aLabel.Location = board.Display(aLabel, aPanel);
        }
예제 #2
0
        public void move(Disk aDisk, int newLevel)
        {
            Label current = aDisk.getLabel();  // This methode require the pole to be set before getting here

            aDisk.setLevel(newLevel + 1);

            int disksID = -1; // default value to reduce errors

            if ((aDisk.getLabel()).Name == "lblDisk1")
            {
                disksID = 0;
            }
            else if ((aDisk.getLabel()).Name == "lblDisk2")
            {
                disksID = 1;
            }
            else if ((aDisk.getLabel()).Name == "lblDisk3")
            {
                disksID = 2;
            }
            else if ((aDisk.getLabel()).Name == "lblDisk4")
            {
                disksID = 3;
            }

            DiskMove newMove = new DiskMove(disksID, aDisk.getPegNum() - 1);

            movements.Add(newMove); // adding the just made move to the movement list
            Display();
        }
예제 #3
0
        // Finds the selected Disk in the 2 dimensional board array, sets that position to null and adds selected disk to it's
        // new position in the 2 dimensional board array.
        public void move(Label aDisk, int newLevel)
        {
            Disk selectedDisk    = FindDisk(aDisk);
            int  currentPegIndex = getPegIndex(selectedDisk);
            int  toPegNumIndex   = getPegIndex(newLevel);

            for (int i = 0; i < NUM_DISKS; i++)
            {
                if (board[currentPegIndex, i] == selectedDisk)
                {
                    board[currentPegIndex, i] = null;
                    for (int j = NUM_DISKS - 1; j >= 0; j--)
                    {
                        if (board[toPegNumIndex, j] == null)
                        {
                            board[toPegNumIndex, j] = selectedDisk;
                            selectedDisk.setPegNum(newLevel);
                            // Creates a DiskMove object representing the latest move to the movements ArrayList.
                            DiskMove diskMove = new DiskMove(Convert.ToInt32(aDisk.Text), newLevel);
                            movements.Add(diskMove.SaveDiskAsText());
                            movements.Add(diskMove.savePegAsText());
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
                else
                {
                    continue;
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Get old coordinates of disk, set new coordinates, stores moves
        /// @param active disk
        /// @param target level
        /// </summary>
        /// <param name="aDisk"></param>
        /// <param name="newLevel"></param>
        public void move(Disk aDisk, int newLevel)
        {
            //Find moved disk index
            int ind = -1;

            for (int i = 0; i < NUM_DISKS; i++)
            {
                if (disks[i] == aDisk)
                {
                    ind = i;
                    break;
                }
            }

            int oldPeg   = getPegInd(ind);
            int oldLevel = getLevel(ind);
            int newPeg   = aDisk.getPegNum();

            disks[ind].setLevel(newLevel);
            disks[ind].setPegNum(newPeg);
            board[newPeg - 1, newLevel - 1] = disks[ind];
            board[oldPeg - 1, oldLevel - 1] = null;

            //Create DiskMove object with latest turn
            DiskMove diskMove = new DiskMove(ind, newPeg - 1);

            movements.Add(diskMove);

            Display(ind, newPeg, newLevel);
        }
예제 #5
0
        //public String getText(int cnt)
        //{
        //    return "1";    // Dummy return to avoid syntax error - must be changed
        //}


        //public void backToSelected(int ind)
        //{

        //}


        //public int getPegInd(int ind)
        //{
        //     return disks[ind].getPegNum();
        //}


        //public int getLevel(int ind)
        //{
        //    return disks[ind].getLevel();
        //}


        /// <summary>
        /// Undo the last move
        /// </summary>
        public void unDo()
        {
            DiskMove diskMove = (DiskMove)movements[movements.Count - 1];
            Disk     disk     = disks[diskMove.getDiskInd()];

            board[disk.getPegNum(), disk.getLevel()] = null;
            disk.setLevel(lastLevel);
            disk.setPegNum(lastPegNum);
            board[lastPegNum, lastLevel] = disk;
            movements.Remove(diskMove);
        }
예제 #6
0
        public void loadData(ArrayList dm) // Used to load the data from the text file
        {
            movements.Clear();
            foreach (string s in dm)
            {
                DiskMove move = new DiskMove(s);
                movements.Add(move);
            }

            for (int m = 0; m < movements.Count; m++)
            {
                animate(m);
            }
        }
예제 #7
0
        /// <summary>method: moveOutput
        /// Retrieves move count and moves made data from Board and
        /// updates output textboxes after each move completed.
        /// </summary>
        public void MoveOutput(Label aLabel, Panel toPeg)
        {
            try
            {
                int newCount = Convert.ToInt32(txt_Count.Text) + 1;
                txt_Count.Text = Convert.ToString(newCount);
            }
            catch (FormatException)
            {
                int newCount = 0 + 1;
                txt_Count.Text = Convert.ToString(newCount);
            }
            int      aLabelID = Convert.ToInt32(aLabel.Text);
            DiskMove newMove  = new DiskMove(aLabelID, board.getPegID(toPeg));

            txt_Moves.Text += newMove.AsText();
        }
예제 #8
0
        private void btnLoadGame_Click(object sender, EventArgs e)
        {
            Board.reset();
            targetPole   = 0;
            MoveCount    = 0;
            txtMove.Text = "";
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    disks[i, j] = null;
                }
            }
            disks[0, 3] = new Disk(lblDisk1, 1, 4);
            disks[0, 2] = new Disk(lblDisk2, 1, 3);
            disks[0, 1] = new Disk(lblDisk3, 1, 2);
            disks[0, 0] = new Disk(lblDisk4, 1, 1);
            string filePath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), @"SavedGame.txt");
            string getFile  = System.IO.File.ReadAllText(filePath);

            getFile       = getFile.TrimEnd('\r', '\n');
            txtMoves.Text = getFile + "\r\n";
            string[] fileParts = txtMoves.Text.Split('\r');

            try
            {
                foreach (string Parts in fileParts)
                {
                    string[] partSplit = Parts.Split(',');
                    int      pegID     = Convert.ToInt32(partSplit[1]) - 1;
                    string   textMove  = returnDiskID(partSplit[0]).ToString() + ',' + pegID;
                    DiskMove = new DiskMove(textMove);
                    txtMove.AppendText(DiskMove.AsText() + "\r\n");
                }
            }
            catch (Exception ex)
            {
            }

            MoveCount     = fileParts.Length;
            lblMoves.Text = (--MoveCount).ToString();
            animateLine   = 0;

            targetPole           = 0;
            animateTimer.Enabled = true;
        }
예제 #9
0
        public MainForm()
        {
            /// <summary>
            /// Initialising MainForm, Board, and Diskmove classes
            /// </summary>
            InitializeComponent();

            board = new Board(

                new Disk(lblDisk1, 0, 3, 0),
                new Disk(lblDisk2, 0, 2, 0),
                new Disk(lblDisk3, 0, 1, 0),
                new Disk(lblDisk4, 0, 0, 0)

                );

            diskMove = new DiskMove();
        }
예제 #10
0
        public void move(Disk aDisk, int newLevel, int newPole)
        {
            //Change the disk place in the board
            board[aDisk.getPegNum(), aDisk.getLevel()] = null;
            board[newPole, newLevel] = aDisk;

            //Set the new pegNum and new level of the disk
            aDisk.setPegNum(newPole);
            aDisk.setLevel(newLevel);

            Display(aDisk);

            //Store the move to the movement array
            diskMove = new DiskMove(getDiskInd(aDisk), newPole);
            movements.Add(diskMove.AsText());

            //Increase move count
            count += 1;
        }
예제 #11
0
        /// <summary>
        /// Load saved moves data
        /// </summary>
        public void loadData()
        {
            try
            {
                using (StreamReader sr = new StreamReader(new FileStream(Application.UserAppDataPath + "\\appdata.txt", FileMode.OpenOrCreate)))
                {
                    while (sr.Peek() >= 0)
                    {
                        DiskMove diskMove = new DiskMove(sr.ReadLine());
                        movements.Add(diskMove);
                    }
                }
            }
            catch (IOException e)
            {
                MessageBox.Show("Error: " + e.ToString());
            }

            if (movements.Count > 0)
            {
                foreach (DiskMove diskMove in movements)
                {
                    int  diskInd = diskMove.getDiskInd();
                    int  pegNum  = diskMove.getPegInd();
                    int  level   = newLevInPeg(pegNum);
                    Disk disk    = disks[diskInd];
                    board[disk.getPegNum(), disk.getLevel()] = null;
                    disk.setPegNum(pegNum);
                    disk.setLevel(level);
                    board[pegNum, level] = disk;

                    if ((pegNum == 2) && (level == 3))
                    {
                        completed = true;
                    }
                }
                Display();
            }
        }
예제 #12
0
        /// <summary>
        /// Draw graphics
        /// </summary>
        public bool drawAnimation()
        {
            if (drawCount < movements.Count)
            {
                DiskMove diskMove = (DiskMove)movements[drawCount++];
                int      diskInd  = diskMove.getDiskInd();
                int      pegNum   = diskMove.getPegInd();
                int      level    = newLevInPeg(pegNum);
                Disk     disk     = disks[diskInd];

                board[disk.getPegNum(), disk.getLevel()] = null;
                disk.setPegNum(pegNum);
                disk.setLevel(level);
                board[pegNum, level] = disk;
                Display();

                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #13
0
        public int animate(int step) // This methode will animate one step at a time. Works in a similar way to move methode
        {
            DiskMove dm = movements[step] as DiskMove;

            int diskIndex = dm.getDiskInd();
            int pegIndex  = dm.getPegInd() + 1;

            string Disk = "";

            if (diskIndex == 0)
            {
                Disk = "lblDisk1";
            }                                               // Find the disk label name
            else if (diskIndex == 1)
            {
                Disk = "lblDisk2";
            }
            else if (diskIndex == 2)
            {
                Disk = "lblDisk3";
            }
            else if (diskIndex == 3)
            {
                Disk = "lblDisk4";
            }

            Disk animateDisk = new Disk();

            foreach (Disk d in disks)
            {
                if ((d.getLabel()).Name == Disk)
                {
                    animateDisk = d;
                }
            }

            if (animateDisk == null)
            {
                return(-1);
            }


            int oldLevel, oldPeg;

            oldLevel = animateDisk.getLevel();
            oldPeg   = animateDisk.getPegNum();

            animateDisk.setPegNum(pegIndex);
            this.board[oldPeg - 1, oldLevel - 1] = null;

            int newLevel = newLevInPeg(pegIndex);

            this.board[pegIndex - 1, newLevel] = animateDisk;
            this.animatemove(animateDisk, newLevel);

            if (step + 1 == movements.Count)
            {
                return(0);    // Return 0 if there are no more moves left to be animated
            }
            return(1);
        }
예제 #14
0
        private bool DropOnPole(int oldPole, int oldLevel, int newPole)
        //Move a disk from its current position to the next available position
        //on the new pole and check that the move is valid
        {
            int    newLevel;
            string theMove = "", textMove = "";

            oldPole--;
            oldLevel--;
            newPole--;

            if (disks[newPole, 3] != null)
            {
                MessageBox.Show("Invalid Move - pole is full");
                return(false);
            }
            else if (disks[oldPole, oldLevel] == null)
            {
                MessageBox.Show("Invalid Move - no disk at start point");
                return(false);
            }

            if (oldLevel < 3)
            {
                if (disks[oldPole, oldLevel + 1] != null)
                {
                    MessageBox.Show("Invalid Move - can only move top disk");
                    return(false);
                }
            }

            newLevel = 3;
            for (int i = 0; i < 4; i++)
            {
                if (disks[newPole, i] == null)
                {
                    newLevel = i;
                    break;
                }
            }

            if (newLevel > 0)
            {
                if (disks[oldPole, oldLevel].thisDisk.Width > disks[newPole, newLevel - 1].thisDisk.Width)
                {
                    MessageBox.Show("Invalid Move - cannot drop bigger disk on smaller");
                    return(false);
                }
            }

            if (newLevel == oldLevel + 1 && newPole == oldPole)
            {
                MessageBox.Show("Invalid Move - cannot move on the same peg");
                return(false);
            }

            disks[newPole, newLevel] = disks[oldPole, oldLevel];
            disks[oldPole, oldLevel] = null;
            disks[newPole, newLevel].Move(newPole + 1, newLevel + 1);
            if (!isStepping)
            {
                {
                    theMove = disks[newPole, newLevel].thisDisk.Name + ','
                              + (newPole + 1).ToString() + ',' + (newLevel + 1).ToString() + "\r\n";
                    txtMoves.AppendText(theMove);
                    textMove = returnDiskID(disks[newPole, newLevel].thisDisk.Name).ToString() + ','
                               + (newPole).ToString() + "\r\n";
                    DiskMove = new DiskMove(textMove);
                    txtMove.AppendText(DiskMove.AsText() + "\r\n");
                    MoveCount++;
                    lblMoves.Text = MoveCount.ToString();
                }


                if (theMove == "lblDisk1,3,4\r\n" && lastMove == "lblDisk2,3,3\r\n")
                {
                    if (lblMoves.Text == "15")
                    {
                        MessageBox.Show("Congrats! You have successfully completed the game with minimum number of moves");
                    }
                    else
                    {
                        MessageBox.Show("Game Over: You have successfully completed the game but not with the minimum number of moves");
                    }
                }

                lastMove = theMove;
            }
            return(true);
        }