Esempio n. 1
0
    private static void DoCycle(ConwayGrid3D grid)
    {
        var comparer  = new ArrayByValueComparer();
        int nbActives = grid.PointsAndValues().Where(x => x.Value.before).Count();

        Console.WriteLine($"Actives : {nbActives}, Size: [{string.Join(",", grid.Size)}]");

        var minIndex = grid.MinKeys.Select(x => x - 1).ToArray();
        var maxIndex = grid.MaxKeys.Select(x => x + 1).ToArray();

        foreach (var point in grid.PointsAndValues(minIndex, maxIndex))
        {
            var nbActiveNeihbors = 0;
            foreach (var neighbor in grid.AreaSquareAround(point.Key, 1))
            {
                if (neighbor.Value.before && !comparer.Equals(point.Key, neighbor.Key))
                {
                    nbActiveNeihbors++;
                }
            }

            if (point.Value.before)
            {
                grid[point.Key] = (point.Value.before, nbActiveNeihbors.Between(2, 3));
            }
            else
            {
                grid[point.Key] = (point.Value.before, nbActiveNeihbors == 3);
            }
        }
    }
Esempio n. 2
0
    private static int PlayGame(List <int> p1Deck, List <int> p2Deck, bool print)
    {
        var p1DeckHistory = new List <int[]>();
        var p2DeckHistory = new List <int[]>();

        var comparer = new ArrayByValueComparer();

        var round = 1;

        while (p1Deck.Count != 0 && p2Deck.Count != 0)
        {
            if (p1DeckHistory.Contains(p1Deck.ToArray(), comparer) && p2DeckHistory.Contains(p2Deck.ToArray(), comparer))
            {
                if (print)
                {
                    Console.WriteLine($"Player 1 WIN game {GameIndex}because of history!");
                }
                return(1);
            }

            p1DeckHistory.Add(p1Deck.ToArray());
            p2DeckHistory.Add(p2Deck.ToArray());

            var topP1 = p1Deck.First();
            var topP2 = p2Deck.First();
            p1Deck.RemoveAt(0);
            p2Deck.RemoveAt(0);

            p1DeckHistory.Add(p1Deck.ToArray());
            p2DeckHistory.Add(p2Deck.ToArray());

            var playerWinner = 0;

            if (p1Deck.Count >= topP1 && p2Deck.Count >= topP2)
            {
                if (print)
                {
                    Console.WriteLine($"Lets start a sub game {GameIndex++} with\nP1: {p1Deck.GetPrintStr<int>()}\nP2: {p2Deck.GetPrintStr<int>()}");
                }
                playerWinner = PlayGame(p1Deck.Take(topP1).ToArray().ToList(), p2Deck.Take(topP2).ToArray().ToList(), print);
            }
            else
            {
                playerWinner = (topP1 > topP2) ? 1 : 2;
            }

            if (playerWinner == 1)
            {
                p1Deck.Add(topP1);
                p1Deck.Add(topP2);
            }
            else
            {
                p2Deck.Add(topP2);
                p2Deck.Add(topP1);
            }

            var outputStr = $"--Round {round++} -\n"
                            + $"Player 1's deck: {p1Deck.GetPrintStr()}\n"
                            + $"Player 2's deck: {p2Deck.GetPrintStr()}\n"
                            + $"Player 1 plays: {topP1}\n"
                            + $"Player 2 plays: {topP2}\n"
                            + $"Player {((topP1 > topP2) ? "1" : "2")} wins the round!\n\n";
            //Console.WriteLine(outputStr);
        }
        if (print)
        {
            Console.WriteLine($"Player {((p1Deck.Count == 0) ? "2" : "1")} WIN game {GameIndex}!");
        }
        return((p1Deck.Count == 0) ? 2 : 1);
    }