Beispiel #1
0
 public PieceSet(PieceSet pieceSet)
 {
     for (int i = 0; i < 6; i++)
     {
         this.isAvailable[i] = pieceSet.IsAvailable(i);
     }
 }
Beispiel #2
0
        private PieceSet FindPieces()
        {
            PieceSet pieces = new PieceSet();


            PointSet unsorted = new PointSet();

            for (Int32 x = 0; x < width; x++)
            {
                for (Int32 y = 0; y < height; y++)
                {
                    if (matrix[x, y])
                    {
                        unsorted.Add(new Point(x, y));
                    }
                }
            }

            while (unsorted.Count > 0)
            {
                pieces.Add(CreatePiece(unsorted));
            }

            return(pieces);
        }
Beispiel #3
0
        private PixelMap ReduceOtherPieces()
        {
            if (bestPiece != null)
            {
                return(this);
            }

            PieceSet pieces   = FindPieces();
            Int32    maxCost  = 0;
            Int32    maxIndex = 0;

            for (Int32 i = 0; i < pieces.Count; i++)
            {
                if (pieces[i].Cost() > maxCost)
                {
                    maxCost  = pieces[i].Cost();
                    maxIndex = i;
                }
            }


            for (Int32 i = 0; i < pieces.Count; i++)
            {
                if (i != maxIndex)
                {
                    pieces[i].BleachPiece(ref matrix);
                }
            }
            if (pieces.Count != 0)
            {
                bestPiece = pieces[maxIndex];
            }
            return(this);
        }
Beispiel #4
0
        public PixelMap ReduceOtherPieces()
        {
            if (bestPiece != null)
            {
                return(this);
            }
            PieceSet pieces   = FindPieces();
            int      maxCost  = 0;
            int      maxIndex = 0;

            for (int i = 0; i < pieces.Count; i++)
            {
                if (pieces[i].Cost() > maxCost)
                {
                    maxCost  = pieces[i].Cost();
                    maxIndex = i;
                }
            }

            for (int i = 0; i < pieces.Count; i++)
            {
                if (i != maxIndex)
                {
                    pieces[i].BleachPiece();
                }
            }
            if (pieces.Count != 0)
            {
                bestPiece = pieces[maxIndex];
            }
            return(this);
        }
Beispiel #5
0
    public static Piece GetPiece(int index)
    {
        if (piece[0] == null)
        {
            PieceSet.CreatePieces();
        }

        return(piece[index]);
    }
Beispiel #6
0
    void Start()
    {
        //TODO: Fix this when we get to multi-piece puzzles
        puzzleSections = GetComponents <PolygonCollider2D>().ToList();
        pieceSet       = Instantiate(pieceSetPrefab).GetComponent <PieceSet>();
        pieceSet.transform.SetParent(this.transform);

        TangramsSupervisor.GetInstance().DragController.PieceSet = pieceSet;
    }
Beispiel #7
0
        public GameRules.MoveOutcomes applyMove(Move m, GameRules rules) // move must be legal
        {
            // apply the movement
            m.movingPiece.pos = m.ToCoord;
            this.PiecesLayout[m.FromCoord.X, m.FromCoord.Y] = null;
            this.PiecesLayout[m.ToCoord.X, m.ToCoord.Y]     = null;

            // using a single battle function allows for easier customization

            // if a battle took place at all, reveal the pieces
            if (m.movingPiece != null && m.opponentPiece != null)
            {
                m.movingPiece.turnRevealed = TurnNumber;
            }
            if (m.opponentPiece != null)
            {
                m.opponentPiece.turnRevealed = TurnNumber;
            }

            m.outcome = rules.BattleFunction(m.movingPiece, m.opponentPiece);

            switch (m.outcome)
            {
            case GameRules.MoveOutcomes.Move:
                this.PiecesLayout[m.ToCoord.X, m.ToCoord.Y] = m.movingPiece;
                break;

            case GameRules.MoveOutcomes.Win:
                this.PiecesLayout[m.ToCoord.X, m.ToCoord.Y] = m.movingPiece;

                // remove the opponent
                m.opponentPiece.pos = Piece.removedPos;
                PieceSet.Remove(m.opponentPiece);
                break;

            case GameRules.MoveOutcomes.Lose:
                this.PiecesLayout[m.ToCoord.X, m.ToCoord.Y] = m.opponentPiece;

                // remove our piece
                m.movingPiece.pos = Piece.removedPos;
                PieceSet.Remove(m.movingPiece);
                break;

            case GameRules.MoveOutcomes.Tie:
                // remove the opponent
                m.opponentPiece.pos = Piece.removedPos;
                PieceSet.Remove(m.opponentPiece);
                // remove our piece
                m.movingPiece.pos = Piece.removedPos;
                PieceSet.Remove(m.movingPiece);
                break;
            }

            return(m.outcome);
        }
Beispiel #8
0
 public bool IsInPieces(PieceSet pieces, int x, int y)
 {
     foreach (Piece piece in pieces)
     {
         foreach (Point point in piece)
         {
             if (point.Equals(x, y))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Beispiel #9
0
        public bool undoMove(Move m)
        {
            // undo the movement
            m.movingPiece.pos = m.FromCoord;
            this.PiecesLayout[m.ToCoord.X, m.ToCoord.Y]     = m.opponentPiece; // remember, can be null
            this.PiecesLayout[m.FromCoord.X, m.FromCoord.Y] = m.movingPiece;

            // re-hide the pieces if they were just revealed
            if (m.movingPiece.turnRevealed == this.TurnNumber)
            {
                m.movingPiece.turnRevealed = null;
            }

            if (m.opponentPiece.turnRevealed == this.TurnNumber)
            {
                m.opponentPiece.turnRevealed = null;
            }



            // only add the moving piece back into the piece set IF it was removed previously
            if (m.outcome == GameRules.MoveOutcomes.Lose || m.outcome == GameRules.MoveOutcomes.Tie)
            {
                PieceSet.Add(m.movingPiece);
            }

            m.movingPiece.pos = m.FromCoord;
            if (m.opponentPiece != null)
            {
                m.opponentPiece.pos = m.ToCoord;

                // only add the opponent piece back into the piece set IF it was removed previously
                if (m.outcome == GameRules.MoveOutcomes.Win || m.outcome == GameRules.MoveOutcomes.Tie)
                {
                    PieceSet.Add(m.opponentPiece);
                }
            }

            return(true);
        }
Beispiel #10
0
        public void GetDiscoveredStraightAttackMoves_RookE1KnightE2TargetE7_AllKnightMoves()
        {
            // arrange
            var s = new Side("A1",
                new PieceSet<Queen>(),
                new PieceSet<Bishop>(),
                new PieceSet<Knight>(new Square("E2")),
                new PieceSet<Rook>(new Square("E1")),
                new WhitePawns()
                );

            var expected = new PieceSet<Knight>(new Square("E2")).GetMoves(Bitboard.Empty, Bitboard.Empty);

            // act
            var result = s.GetDiscoverdStraightAttackMoves(new Square("E7"), Bitboard.Empty);

            // assert
            result.Should().Have.SameSequenceAs(expected);
        }
Beispiel #11
0
        public void GetDiscoveredStraightAttackMoves_RookE1KingE3TargetE7()
        {
            // arrange
            var s = new Side("E3",
                             new PieceSet<Queen>(),
                             new PieceSet<Bishop>(),
                             new PieceSet<Knight>(),
                             new PieceSet<Rook>(new Square("E1")),
                             new WhitePawns()
                );

            IEnumerable<Move> expected = new PieceSet<King>(new Square("E3")).GetMoves(Bitboard.Empty, Bitboard.Empty,
                                                                                       Bitboard.Full,
                                                                                       ~Bitboard.With.FileE.Build());

            // act
            IEnumerable<Move> result = s.GetDiscoverdStraightAttackMoves(new Square("E7"), Bitboard.Empty);

            // assert
            result.Should().Have.SameSequenceAs(expected);
        }
Beispiel #12
0
    private void FixedUpdate()
    {
        if (solutionList.Count == 0 || done)
        {
            return;
        }

        bool skip = false;

        do
        {
            skip = false;
            if (sulotionIndex < solutionList.Count)
            {
                PieceInPosition[] solution = solutionList[sulotionIndex];


                if (jammerPiece != null && enableSkip)
                {
                    PieceInPosition possibleJammerPiece = solution[jammerPiece.slotIndex];
                    if (possibleJammerPiece.pieceIndex == jammerPiece.pieceIndex && possibleJammerPiece.rotationY == jammerPiece.rotationY && possibleJammerPiece.rotationX == jammerPiece.rotationX)
                    {
                        skip = true;
                    }
                    else
                    {
                        jammerPiece = null;
                    }
                }

                if (!skip)
                {
                    world.Clear();
                    bool wasted = false;
                    for (int slotIndex = 0; slotIndex < 6; slotIndex++)
                    {
                        bool couldPlace = slots[slotIndex].CanPlaceInWorld(PieceSet.GetPiece(solution[slotIndex].pieceIndex), solution[slotIndex].rotationY, solution[slotIndex].rotationX, world);
                        slots[slotIndex].ForcePlaceInWorld(PieceSet.GetPiece(solution[slotIndex].pieceIndex), solution[slotIndex].rotationY, solution[slotIndex].rotationX, world, wasted || !couldPlace);
                        if (!couldPlace || wasted)
                        {
                            if (!wasted)
                            {
                                jammerPiece           = solution[slotIndex];
                                jammerPiece.slotIndex = slotIndex;
                            }
                            wasted = true;
                            if (enableSkip)
                            {
                                break;
                            }
                        }
                        else
                        {
                            if (slotIndex + 1 > piecePlacedRecord)
                            {
                                piecePlacedRecord = slotIndex + 1;
                                recordWorld       = new World(world);

                                if (piecePlacedRecord == solutionLimit)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }

                sulotionIndex++;
                if (sulotionIndex % 100 == 0)
                {
                    float timeElapsed = Time.realtimeSinceStartup - startTime;
                    float timeLeft    = (((float)solutionList.Count / (float)sulotionIndex) * timeElapsed - timeElapsed) / 60f;
                    Debug.Log("Checking combination: " + sulotionIndex + " of " + solutionList.Count + ", Checked: " + 100f * ((float)sulotionIndex / (float)solutionList.Count) + " %, Record: " + piecePlacedRecord + " pieces, Estimated time left: < " + (timeLeft < 60 ? timeLeft + " minutes" : timeLeft / 60f + " hours"));
                }

                if (piecePlacedRecord == solutionLimit)
                {
                    Debug.Log("Yey!! :) Solution found!!");
                    done = true;
                }

                visualWorld.Refresh(world);
            }
            else
            {
                Debug.Log("This puzzle is Shit! Every possible combination tried (" + solutionList.Count + "). Could only place " + piecePlacedRecord + " pieces. One of the best solutions is shown.");
                visualWorld.Refresh(recordWorld);
                done = true;
            }
        } while (skip);
    }
 public ChessPiece(PieceType type, PieceSet set)
 {
     _type = type;
     _set  = set;
 }
Beispiel #14
0
    IEnumerator RepairInternal(PieceSet pieces)
    {
        var renderer = GetComponent <SpriteRenderer>();

        renderer.enabled = false;
        var sprite = renderer.sprite;

        var piecesMap = new Dictionary <Piece, List <Piece> >();

        foreach (var p in pieces.Pieces)
        {
            piecesMap[p] = new List <Piece>();
        }

        for (int y = (int)sprite.rect.y; y < sprite.rect.height; y++)
        {
            for (int x = (int)sprite.rect.x; x < sprite.rect.width; x++)
            {
                var color = sprite.texture.GetPixel(x, y);
                if (color.a <= 0)
                {
                    continue;
                }
                var obj = Instantiate(GameSystem.Curernt.PiecePrefab);
                obj.transform.parent = pieces.transform;
                var piece = obj.GetComponent <Piece>();
                if (!sprite || !sprite.texture)
                {
                    Debug.Log(renderer);
                }
                piece.Color = color;
                var pos = new Vector2(x, y);
                piece.TargetPosition = transform.position + ((pos - sprite.pivot) / sprite.pixelsPerUnit).ToVector3();
                piece.GetComponent <Rigidbody>().isKinematic = true;

                var hostPiece = pieces.Pieces[Random.Range(0, pieces.Pieces.Count)];
                if (!piecesMap.ContainsKey(hostPiece))
                {
                    piecesMap[hostPiece] = new List <Piece>();
                }
                piecesMap[hostPiece].Add(piece);
                piece.transform.parent        = hostPiece.transform;
                piece.transform.localPosition = Vector3.zero;
                piece.transform.localRotation = Quaternion.identity;
            }
            yield return(null);
        }

        while (piecesMap.Count > 0)
        {
            var count = Mathf.CeilToInt(piecesMap.Count * Random.value * .9f);
            for (var i = 0; i < count; i++)
            {
                var host = piecesMap.Keys.RandomTake(1).First();
                if (piecesMap[host].Count > 0)
                {
                    var p = piecesMap[host][0];
                    piecesMap[host].RemoveAt(0);
                    p.transform.parent = pieces.transform;
                    p.StartRepair();
                }
                if (piecesMap[host].Count <= 0)
                {
                    piecesMap.Remove(host);
                    Destroy(host.gameObject);
                }
            }
            yield return(new WaitForSeconds(.1f));
        }

        yield return(new WaitForSeconds(1));

        Destroy(pieces.gameObject);
        renderer.enabled = true;
    }
Beispiel #15
0
 public void RepairFrom(PieceSet pieces)
 {
     CurrentState = State.Repairing;
     StartCoroutine(RepairInternal(pieces));
 }