Beispiel #1
0
 public TrainTree(int r, int c, TrainTree prev)
 {
     _row      = r;
     _col      = c;
     _previous = prev;
 }
Beispiel #2
0
        public List <TrainTree> getWinningPath(Player.Color color)
        {
            int[,] disks = new int[6, 6];
            // bread crumbs are laid on disks mark up board
            for (int r = 0; r < 6; r++)
            {
                for (int c = 0; c < 6; c++)
                {
                    disks[r, c] = 0;
                    if (r < 5 && board[r + 1, c] != null && board[r + 1, c].color == color) // S
                    {
                        disks[r, c] += 15;
                    }
                    if (r > 0 && board[r - 1, c] != null && board[r - 1, c].color == color) // N
                    {
                        disks[r, c] += 1;
                    }
                    if (c < 5 && board[r, c + 1] != null && board[r, c + 1].color == color) // E
                    {
                        disks[r, c] += 7;
                    }
                    if (c > 0 && board[r, c - 1] != null && board[r, c - 1].color == color) // W
                    {
                        disks[r, c] += 3;
                    }
                }
            }


            List <TrainTree> winningPath = new List <TrainTree>();
            Pair             start, end;

            if (color == Player.Color.White)
            {
                start = new Pair(0, 0);
                end   = new Pair(5, 5);
            }
            else
            {
                start = new Pair(0, 5);
                end   = new Pair(5, 0);
            }


            TrainTree root = new TrainTree(start.row, start.col, null);

            disks[start.row, start.col] = 0;
            TrainTree         leaf     = null;
            Queue <TrainTree> findPath = new Queue <TrainTree>();
            TrainTree         current;

            current = root;
            findPath.Enqueue(root);


            while (leaf == null)
            {
                current = findPath.Dequeue();
                if (current.Row < 5 &&
                    board[current.Row + 1, current.Col] != null &&
                    board[current.Row + 1, current.Col].color == color && // S
                    disks[current.Row + 1, current.Col] > 0)
                {
                    findPath.Enqueue(new TrainTree(current.Row + 1, current.Col, current));
                    disks[current.Row + 1, current.Col] = 0;
                }

                if (current.Row > 0 &&
                    board[current.Row - 1, current.Col] != null &&
                    board[current.Row - 1, current.Col].color == color && // N
                    disks[current.Row - 1, current.Col] > 0)
                {
                    findPath.Enqueue(new TrainTree(current.Row - 1, current.Col, current));
                    disks[current.Row - 1, current.Col] = 0;
                }

                if (current.Col < 5 &&
                    board[current.Row, current.Col + 1] != null &&
                    board[current.Row, current.Col + 1].color == color && // E
                    disks[current.Row, current.Col + 1] > 0)
                {
                    findPath.Enqueue(new TrainTree(current.Row, current.Col + 1, current));
                    disks[current.Row, current.Col + 1] = 0;
                }

                if (current.Col > 0 &&
                    board[current.Row, current.Col - 1] != null &&
                    board[current.Row, current.Col - 1].color == color && // W
                    disks[current.Row, current.Col - 1] > 0)
                {
                    findPath.Enqueue(new TrainTree(current.Row, current.Col - 1, current));
                    disks[current.Row, current.Col - 1] = 0;
                }


                if (current.Row == end.row && current.Col == end.col)
                {
                    leaf = current;
                }
            }

            // identify singular rout
            current = leaf;
            while (current != null)
            {
                winningPath.Add(current);
                current = current.Previous;
            }

            // reinitialize disks markup board
            for (int r = 0; r < 6; r++)
            {
                for (int c = 0; c < 6; c++)
                {
                    disks[r, c] = 0;
                }
            }

            // mark winning disks
            foreach (TrainTree tt in winningPath)
            {
                disks[tt.Row, tt.Col] = 1;
            }

            // identify the track index;
            foreach (TrainTree tt in winningPath)
            {
                tt.DirectionalIndex = 0;
                if (tt.Row < 5 && disks[tt.Row + 1, tt.Col] != 0) // S
                {
                    tt.DirectionalIndex += 15;
                }
                if (tt.Row > 0 && disks[tt.Row - 1, tt.Col] != 0) // N
                {
                    tt.DirectionalIndex += 1;
                }
                if (tt.Col < 5 && disks[tt.Row, tt.Col + 1] != 0) // E
                {
                    tt.DirectionalIndex += 7;
                }
                if (tt.Col > 0 && disks[tt.Row, tt.Col - 1] != 0) // W
                {
                    tt.DirectionalIndex += 3;
                }
            }

            return(winningPath);
        }