private Color PickUnusedColor(Game game, ColorableObject f1, ColorableObject f2) { Random r = new Random(); List<Color> unusedColors = new List<Color>(); foreach (Color c in game.colors) if (!game.usedColors.Contains(c)) unusedColors.Add(c); ListExtension.Shuffle<Color>(unusedColors); foreach (Color c in unusedColors) if (game.CheckIfValidMove(f1, c) && f1.color != f2.color) return c; foreach (Color c in unusedColors) if (game.CheckIfValidMove(f1, c)) return c; Color color; do color = game.colors[r.Next(game.colors.Length)]; while (!game.CheckIfValidMove(f1, color)); return color; }
/// <summary> /// Funkcja sprawdzajaca nacisk myszy na element okna Kreatora grafu /// </summary> /// <param name="mousePos"></param> /// <param name="pi"></param> /// <param name="content"></param> public void CheckGraphCreator(Point mousePos, PlayerInterface pi, ContentManager content) { int index = GetIndex(GCButtons, mousePos); List<Fence> outFences; if (index > -1) { switch (GCButtons[index].name) { case "anuluj": pi.state = InterfaceState.MainMenu; break; case "zapisz-graf": DateTime now = DateTime.Now; PrepareGraphForSerialization(); SerializationManager.SerializeObject(graph, SerializationManager.CreateFileName(now)); pi.state = InterfaceState.MainMenu; break; case "usun": if(GCButtons[index].color == Color.White) { if (lastClicked is Fence) { graph.fences.Remove((Fence)lastClicked); graph.fencesNumber--; } else if(lastClicked is Flower) { outFences = graph.GetOutFences((Flower)lastClicked); if (outFences.Count > 0) Game1.MessageBox(new IntPtr(), "Najpierw usuń wychodzące krawędzie", "", 0); else { graph.flowers.Remove((Flower)lastClicked); graph.flowersNumber--; } } GCButtons[index].color = Color.Gray; lastClicked.color = Color.White; lastClicked = null; GCButtons[3].color = Color.White; return; } break; case "dodaj-kwiatek": if (GCButtons[index].color == Color.White) { List<int> indices = new List<int>(); foreach(Flower f in graph.flowers) { indices.Add(f.index); } indices.Sort(); int previous_index = -1; int flower_index = graph.flowersNumber; foreach(int ind in indices) { if(ind - previous_index > 1) { flower_index = previous_index + 1; break; } previous_index = ind; } Flower flower = new Flower(Game1.GetRatioDimensions(new Vector2(0, 390)), "Kwiatek", flower_index); graph.flowers.Add(flower); graph.flowersNumber++; return; } break; } } if (!(lastClicked is Fence)) { for (int i = 0; i < graph.flowersNumber; i++) { if (graph.flowers[i].ContainsPoint(mousePos)) { if (lastClicked == null) { lastClicked = graph.flowers[i]; lastClicked.color = Color.LightBlue; movingFlower = true; GCButtons[2].color = Color.White; GCButtons[3].color = Color.Gray; break; } else if (!movingFlower) { if (graph.flowers[i] != lastClicked) { Fence f = new Fence((Flower)lastClicked, graph.flowers[i], "Plotek"); outFences = graph.GetOutFences((Flower)lastClicked); if (!outFences.Exists(x => (x.f1.Equals(lastClicked) && x.f2.Equals(graph.flowers[i])) || (x.f1.Equals(graph.flowers[i]) && x.f2.Equals(lastClicked)))) { graph.fences.Add(f); graph.fencesNumber += 1; } } lastClicked.color = Color.White; lastClicked = null; GCButtons[2].color = Color.Gray; GCButtons[3].color = Color.White; return; } } } } if (movingFlower) { if (previousMousePosition != Vector2.Zero) { ((Flower)lastClicked).position.X -= previousMousePosition.X - mousePos.X; ((Flower)lastClicked).position.Y -= previousMousePosition.Y - mousePos.Y; ((Flower)lastClicked).center.X -= previousMousePosition.X - mousePos.X; ((Flower)lastClicked).center.Y -= previousMousePosition.Y - mousePos.Y; } previousMousePosition = new Vector2(mousePos.X, mousePos.Y); } else { if (lastClicked == null || lastClicked is Fence) { for (int i = 0; i < graph.fencesNumber; i++) { if (graph.fences[i].ContainsPoint(mousePos)) { if (lastClicked == null) { lastClicked = graph.fences[i]; lastClicked.color = Color.LightBlue; GCButtons[2].color = Color.White; GCButtons[3].color = Color.Gray; } else { if (graph.fences[i] == lastClicked) { lastClicked.color = Color.White; lastClicked = null; GCButtons[2].color = Color.Gray; GCButtons[3].color = Color.White; break; } } } } } } }
/// <summary> /// Funkcja robiaca ruch /// </summary> /// <param name="obj">wybrany element</param> /// <param name="c">kolor</param> /// <param name="game">gra</param> public void MakeMove(ColorableObject obj, Color c, Game game) { obj.color = c; if (obj is Flower) coloredFlowersNumber++; else coloredFencesNumber++; if (!game.usedColors.Contains(c)) game.usedColors.Add(c); }
/// <summary> /// Funkcja sprawdzajaca prawidlowosc ruchu /// </summary> /// <param name="cb">wybrany obiekt</param> /// <param name="c">kolor</param> /// <returns></returns> public bool CheckIfValidMove(ColorableObject cb, Color c) { List<Fence> outFences; if (cb is Flower) { Flower flower = cb as Flower; outFences = graph.GetOutFences(flower); foreach (Fence f in outFences) { if (f.f1 != flower) { if (f.f1.color == c) return false; } else { if (f.f2.color == c) return false; } } } else { Fence fence = cb as Fence; outFences = graph.GetOutFences(fence.f1); foreach (Fence f in outFences) { if (f != fence) { if (f.color == c) return false; } } outFences = graph.GetOutFences(fence.f2); foreach (Fence f in outFences) { if (f != fence) { if (f.color == c) return false; } } } return true; }