Fourteen Peg Puzzle by James Pate Williams, Jr., BA, BS, MSwE, PhD

I created this project five years ago.

Technologies

C#, Visual Studio 2008, Windows Forms, Visual C#

Topics

C#, Puzzles, 14 Peg Puzzle

Platforms

Desktop

Requirements

Primary language

en-US

Updated

10/16/2015

License

MIT

View this sample online

Introduction

The fourteen peg puzzle is a form of amusement which I have seen at my local Cracker Barrel. The puzzle involves fourteen pegs in fifteen holes in a triangular configuration. The top hole is empty. The object of the puzzle is to jump pegs over one another until only one peg is left. Jumped pegs are removed from the board.

Building the Sample

This project should build as is using Visual Studio 2008.

Description

This application was translated from a Turbo Pascal program found in Data Structure Using Turbo Pascal by Thomas M. Boger. Depth-first search with backtracking is utilized to find the solution in the allowed thirteen moves. Two stacks are used to store the moves a move stack and a reverse move stack. The translation process had to take into account a relatively strange Turbo Pascal table and an array whose base index was -1. Boger creates an array for the representation of the fourteen peg puzzle that is a right triangle for internal application operations.

C#

using System;

using System.Collections.Generic;

using System.Windows.Forms;

namespace FourteenPegPuzzle

{

    class PegPuzzle

    {

        private const int OverRow = 0;

        private const int OverCol = 1;

        private const int ToRow = 2;

        private const int ToCol = 3;

        private const int Addend = 0;

        private int[,] moveTable =

        {{1, 0, 2, 0}, {-1, 0, -2, 0},

            {0, 1, 0, 2}, {0, -1, 0, -2},

            {1, 1, 2, 2}, {-1, -1, -2, -2}};

        private char[,] board;

        public struct StatusRec

        {

            public int pegRow, pegCol, moveNumber;

        }

        private Stack<StatusRec> moveStack;

        private Stack<StatusRec> reverseStack;

        private bool blocked;

        private int moveCount, totalMovePush;

        private StatusRec currentMove, newMove;

        private TextBox tb;

        public PegPuzzle(TextBox tb)

        {

            this.tb = tb;

            totalMovePush = 0;

            board = new char[9, 9];

        }

        private void AdvancePeg()

        {

            currentMove.pegCol++;

            if (currentMove.pegCol > currentMove.pegRow)

            {

                currentMove.pegCol = 2;

                currentMove.pegRow++;

            }

        }

        private void AdvanceMove()

        {

            currentMove.moveNumber++;

            if (currentMove.moveNumber > 5)

            {

                currentMove.moveNumber = 0;

                AdvancePeg();

            }

        }

        private void InitializeBoard()

        {

            for (int row = 0; row < 9; row++)

                for (int col = 0; col < 9; col++)

                    board[row, col] = ‘ ‘;

            for (int row = 3; row <= 6; row++)

                for (int col = 2; col <= row; col++)

                    board[row, col] = ‘X’;

            board[2, 2] = ‘O’;

        }

        private bool LegalMove()

        {

            int overRow = moveTable[currentMove.moveNumber, OverRow] + Addend;

            int overCol = moveTable[currentMove.moveNumber, OverCol] + Addend;

            int toRow = moveTable[currentMove.moveNumber, ToRow] + Addend;

            int toCol = moveTable[currentMove.moveNumber, ToCol] + Addend;

            return board[currentMove.pegRow + overRow, currentMove.pegCol + overCol] == ‘X’ &&

                board[currentMove.pegRow + toRow, currentMove.pegCol + toCol] == ‘O’;

        }

        private void PrintBoard()

        {

            for (int row = 2; row <= 6; row++)

            {

                for (int i = 1; i <= 6 – row; i++)

                    tb.Text += ‘ ‘;

                for (int col = 2; col <= row; col++)

                    tb.Text += board[row, col] + ” “;

                tb.Text += “\r\n”;

            }

            tb.Text += “\r\n”;

        }

        public void Solve()

        {

            moveStack = new Stack<StatusRec>();

            InitializeBoard();

            blocked = false;

            moveCount = 0;

            currentMove.pegRow = 2;

            currentMove.pegCol = 2;

            currentMove.moveNumber = 0;

            while (moveCount < 13 && !blocked)

            {

                if (board[currentMove.pegRow, currentMove.pegCol] != ‘X’)

                    AdvancePeg();

                else if (!LegalMove())

                    AdvanceMove();

                else

                {

                    newMove = new StatusRec();

                    newMove.moveNumber = currentMove.moveNumber;

                    newMove.pegCol = currentMove.pegCol;

                    newMove.pegRow = currentMove.pegRow;

                    moveStack.Push(newMove);

                    moveCount++;

                    totalMovePush++;

                    board[currentMove.pegRow, currentMove.pegCol] = ‘O’;

                    int overRow = moveTable[currentMove.moveNumber, OverRow] + Addend;

                    int overCol = moveTable[currentMove.moveNumber, OverCol] + Addend;

                    int toRow = moveTable[currentMove.moveNumber, ToRow] + Addend;

                    int toCol = moveTable[currentMove.moveNumber, ToCol] + Addend;

                    board[currentMove.pegRow + overRow, currentMove.pegCol + overCol] = ‘O’;

                    board[currentMove.pegRow + toRow, currentMove.pegCol + toCol] = ‘X’;

                    currentMove.pegRow = 2;

                    currentMove.pegCol = 2;

                    currentMove.moveNumber = 0;

                }

                while (currentMove.pegRow > 6 && !blocked)

                {

                    if (moveStack.Count == 0)

                        blocked = true;

                    else

                    {

                        currentMove = moveStack.Pop();

                        board[currentMove.pegRow, currentMove.pegCol] = ‘X’;

                        int overRow = moveTable[currentMove.moveNumber, OverRow] + Addend;

                        int overCol = moveTable[currentMove.moveNumber, OverCol] + Addend;

                        int toRow = moveTable[currentMove.moveNumber, ToRow] + Addend;

                        int toCol = moveTable[currentMove.moveNumber, ToCol] + Addend;

                        board[currentMove.pegRow + overRow, currentMove.pegCol + overCol] = ‘X’;

                        board[currentMove.pegRow + toRow, currentMove.pegCol + toCol] = ‘O’;

                        moveCount–;

                        AdvanceMove();

                    }

                }

            }

            if (!blocked)

            {

                InitializeBoard();

                reverseStack = new Stack<StatusRec>();

                while (moveStack.Count != 0)

                {

                    currentMove = moveStack.Pop();

                    newMove = new StatusRec();

                    newMove.moveNumber = currentMove.moveNumber;

                    newMove.pegCol = currentMove.pegCol;

                    newMove.pegRow = currentMove.pegRow;

                    reverseStack.Push(newMove);

                }

                PrintBoard();

                while (reverseStack.Count != 0)

                {

                    currentMove = reverseStack.Pop();

                    board[currentMove.pegRow, currentMove.pegCol] = ‘O’;

                    int overRow = moveTable[currentMove.moveNumber, OverRow] + Addend;

                    int overCol = moveTable[currentMove.moveNumber, OverCol] + Addend;

                    int toRow = moveTable[currentMove.moveNumber, ToRow] + Addend;

                    int toCol = moveTable[currentMove.moveNumber, ToCol] + Addend;

                    board[currentMove.pegRow + overRow, currentMove.pegCol + overCol] = ‘O’;

                    board[currentMove.pegRow + toRow, currentMove.pegCol + toCol] = ‘X’;

                    PrintBoard();

                }

            }

        }

    }

}

Unknown's avatar

Author: jamespatewilliamsjr

My whole legal name is James Pate Williams, Jr. I was born in LaGrange, Georgia approximately 70 years ago. I barely graduated from LaGrange High School with low marks in June 1971. Later in June 1979, I graduated from LaGrange College with a Bachelor of Arts in Chemistry with a little over a 3 out 4 Grade Point Average (GPA). In the Spring Quarter of 1978, I taught myself how to program a Texas Instruments desktop programmable calculator and in the Summer Quarter of 1978 I taught myself Dayton BASIC (Beginner's All-purpose Symbolic Instruction Code) on LaGrange College's Data General Eclipse minicomputer. I took courses in BASIC in the Fall Quarter of 1978 and FORTRAN IV (Formula Translator IV) in the Winter Quarter of 1979. Professor Kenneth Cooper, a genius poly-scientist taught me a course in the Intel 8085 microprocessor architecture and assembly and machine language. We would hand assemble our programs and insert the resulting machine code into our crude wooden box computer which was designed and built by Professor Cooper. From 1990 to 1994 I earned a Bachelor of Science in Computer Science from LaGrange College. I had a 4 out of 4 GPA in the period 1990 to 1994. I took courses in C, COBOL, and Pascal during my BS work. After graduating from LaGrange College a second time in May 1994, I taught myself C++. In December 1995, I started using the Internet and taught myself client-server programming. I created a website in 1997 which had C and C# implementations of algorithms from the "Handbook of Applied Cryptography" by Alfred J. Menezes, et. al., and some other cryptography and number theory textbooks and treatises.

Leave a comment