Click here to Skip to main content
15,888,037 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
I am designing a chess engine for my project instead of playing random: http://www.youtube.com/watch?v=8EKKTMPFY8g[^]

i am planning to use greedy MinMax algorithm, so i first started with writing the data structure

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AI
{
    class Node
    {
        #region Constructor

        public Node(object data)
        {
            this.Data = data;
        }

        #endregion

        #region Properties

        private int _order;
        private object _data;
        private Node _parent;
        private List<Node> _childern = new List<Node>();

        public object Data
        {
            get { return _data; }
            set { _data = value; }
        }

        public Node Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }

        public List<Node> Children
        {
            get { return _childern; }
            set { _childern = value; }
        }

        public int Order
        {
            get { return _order; }
            set { _order = value; }
        }

        #endregion

        #region Methods

        public void AddChild(Node next_node)
        {
            next_node._order = this.Order + 1;
            next_node.Parent = this;
            this.Children.Add(next_node);
        }

        public void AddChild(object next_node_data)
        {
            Node next_node = new Node(next_node_data);
            AddChild(next_node);
        }

        #endregion
    }

    class Tree
    {
        #region Constructor

        public Tree(object root_data)
        {
            this.Root = new Node(root_data);
            this.Root.Order = 0;
            this.length = 1;
        }

        public Tree(Node root_node)
        {
            Root = root_node;
        }

        #endregion

        #region Properties

        private Node _root;
        private int _length;

        public Node Root
        {
            get { return _root; }
            set { _root = value; }
        }

        public int length
        {
            get { return _length; }
            set { _length = value; }
        }

        #endregion
    }
}


I have a problem, once i have a evaluated the tree, and want to compare to see which is the best move, how can return to 1st order node without using a recursive method for example like array use [i] but i is no the length i is the depth if the tree

Thanks in advance,
z3ngew
Posted

1 solution

Since your nodes have a Parent property you can just move up until you reach the one with order 1. Something like this.

C#
Node primary = (currentNode.Order <= 1 ? currentNode : currentNode.Parent);
while (primary.Order > 1)
    primary = primary.Parent;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900