Exemplo n.º 1
0
        //private void SetHighestFlipValue(NodeOtelloPiece[] placeableNodes) //skica
        //{
        //    for (int i = 0; i < placeableNodes.Length; i++) //går igenom alla pacerbara platser
        //    {

        //        for (int j = 0; j < placeableNodes[i].Neighbors.Count; j++) //går igenom den placerbara nodens grannar kollar viability
        //        {
        //            if (!placeableNodes[i].Neighbors.ToArray()[i].EmptyTile) //om grannarna till den placerbara noden inte är tomma (för de är fortfarande grannar även om de inte håller en tile)
        //            {
        //                if (placeableNodes[i].Neighbors.ToArray()[j].NodeColor != myColor) //om grannen inte har samma färg
        //                {
        //                    //behöver kolla den direction som hittades och adda poängen
        //                    Vector2 dir = new Vector2(placeableNodes[i].Position.X - placeableNodes[i].Neighbors.ToArray()[j].Position.X, )
        //                    placeableNodes[i].FlipScore++;
        //                }
        //            }
        //            else if (placeableNodes[i].Neighbors.ToArray()[j].NodeColor == myColor && placeableNodes[i].FlipScore > 0) // kanske inte behöver kolla flipscoren här då
        //            {
        //                //if a node of my color is found and if the score is higher than 0 then this is a valid move the val will be positiv but if the color of the node is
        //                //opposite then the flipvalue will be "negative" in the min max. This is because the smaller values will be "prioretised" by the agent as the players
        //                //best moves
        //            }
        //        }
        //    }

        //}

        private NodeOtelloPiece MiniMax(NodeOtelloPiece node, int depth, bool isAgent)
        {
            if (depth == 0)
            {
                return(node);
            }

            if (isAgent)
            {
                int best = minEval;
                foreach (var neighbor in node.Neighbors)
                {
                    if (neighbor != null)
                    {
                        evalNode = MiniMax(neighbor, depth - 1, false);
                        maxEval  = Max(maxEval, evalNode.FlipScore);

                        for (int i = 0; i < node.Neighbors.Count; i++)
                        {
                            if (node.Neighbors.ToArray()[i].FlipScore == maxEval)
                            {
                                //behöver reurnera värdet som fick högst poäng men är helt s**t i huvet och det blir bara mer och mer grötig kod #helpMe ;_;
                            }
                        }
                    }
                }
            }

            else if (!isAgent)
            {
                foreach (var neigbor in node.Neighbors)
                {
                    if (neigbor != null)
                    {
                        evalNode = MiniMax(neigbor, depth - 1, true);
                        minEval  = Min(minEval, -evalNode.FlipScore);
                    }
                }
            }


            else
            {
                foreach (var neighbor in node.Neighbors)
                {
                    int best = maxEval;
                    if (neighbor != null)
                    {
                        evalNode = MiniMax(neighbor, depth - 1, true);
                    }
                }
            }

            return(null);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Creates a two-dimensional graph.
 /// </summary>
 void CreateGraph()
 {
     for (int y = 0; y < gridLength; y++)
     {
         for (int x = 0; x < gridLength; x++)
         {
             graph[x, y]          = new NodeOtelloPiece(new Vector2(x, y), otelloTileTex);
             graph[x, y].MyHitbox = new Rectangle(x * otelloTileTex.Width, y * otelloTileTex.Height, otelloTileTex.Width, otelloTileTex.Height);
         }
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Gets called by the players (agent/ human) and then places the tile with the correct color
 /// where the human pressed or where the agent chose.
 /// </summary>
 /// <param name="mouseX">Coordinate for the mouse x Position</param>
 /// <param name="posY">Coordinate for the mouse y Position</param>
 /// <param name="playerColor">Which player placed it</param>
 /// <returns></returns>
 public void PlaceTile(int posX, int posY, Color playerColor)
 {
     for (int y = 0; y < gridLength; y++)
     {
         for (int x = 0; x < gridLength; x++)
         {
             if (graph[x, y].MyHitbox.Contains(posX, posY) && graph[x, y].EmptyTile) //where i have clicked is empty, is k to place
             {
                 graph[x, y].EmptyTile = false;
                 graph[x, y].NodeColor = playerColor;
                 startNode             = graph[x, y]; // is the node that
                 Flip(x, y);                          //I dont need the color since the one I'm checking will have the right one
             }
         }
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// /irst takes in the placeable we're checking and dir then by using the positon of both i can iterate in the right direction using
        /// the information from the previous method
        /// </summary>
        /// <param name="node"></param>
        /// <param name="directon"></param>
        private void FindNodeValue(NodeOtelloPiece node, Vector2 directon)
        {
            if ((node.Position.X + directon.X) < gridLength && (node.Position.X + directon.X) <= 0 && (node.Position.Y + directon.Y) < gridLength && (node.Position.Y + directon.Y) <= 0) // så länge de är innanför planen go ahead
            {
                if (graph[(int)(node.Position.X + directon.X), (int)(node.Position.Y + directon.Y)].EmptyTile)                                                                            //if neigbor of certain dir is not empty
                {
                    if (graph[(int)(node.Position.X + directon.X), (int)(node.Position.Y + directon.Y)].NodeColor != node.NodeColor)                                                      //if the neighbor we're checking is not of the same color
                    {
                        countup++;                                                                                                                                                        //bara så att vi kan kolla att det faktiskt går att få dessa poängen
                        FindNodeValue(graph[(int)(node.Position.X + directon.X), (int)(node.Position.Y + directon.Y)], directon);                                                         //skicka in nästa nod och steg
                        //kan behöva en brake här då jag inte är säker på att rekursionen är enough
                    }

                    if (graph[(int)(node.Position.X + directon.X), (int)(node.Position.Y + directon.Y)].NodeColor == node.NodeColor && countup > 0)
                    {
                        node.FlipScore = countup;
                    }
                }
            }
            countup = 0; // reset för next nod
        }
Exemplo n.º 5
0
 public void AddNehigbors(NodeOtelloPiece node) //ta in min pos hitta grannar
 {
     Neighbors.Enqueue(node);
 }