예제 #1
0
 private void Instance_OnOver(PossibleMove sender)
 {
     if (OnOverEvent != null)
     {
         OnOverEvent(this);
     }
 }
예제 #2
0
    public void PlaySmartMove()
    {
        //Debug.Log("Play Random");
        //Definir l'ensemble des coups possibles
        DefineAllPossibleMoves();
        //On choisit le best move (TODO)



        int moveToPlayIndex = rand.Next(possibleMoves.Count);

// fin du TODO


        // Debug.Log("Move choisit : "+moveToPlayIndex);
        PossibleMove moveToPlay = possibleMoves[moveToPlayIndex];

        moveToPlay.cellWherePieceCanMove.SetIsPlayable(true);


        //on le joue
        MovePiece(moveToPlay.piece, moveToPlay.cellWherePieceCanMove, true, moveToPlay);

        CheckEndOfGame(moveToPlay.piece);
    }
    // Update is called once per frame
    private void Update()
    {
        //Player left clicks
        if (Input.GetMouseButtonDown(0) && !gameOver)
        {
            //If it is the players turn
            if ((aiDifficulty == 0 && (playerTurn || !playerTurn)) || (aiDifficulty != 0 && playerTurn))
            {
                //Find which square they clicked in
                Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                mousePosition = new Vector3(Mathf.Round(mousePosition.x), Mathf.Round(mousePosition.y), 0);

                //If they clicked within the board
                if (mousePosition.x > -1 && mousePosition.x < boardWidth && mousePosition.y > -1 && mousePosition.y < boardHeight)
                {
                    PossibleMove moveToMake = new PossibleMove();

                    //Check if the position is within the ruleset -- Is it next to a piece and does it flip other pieces
                    if (WithinPossibleMoves((int)mousePosition.x, (int)mousePosition.y, ref moveToMake))
                    {
                        //Now there is confirmation, make the change
                        PlacePiece((int)mousePosition.x, (int)mousePosition.y, (playerTurn) ? 1 : 2, moveToMake);

                        //Change Turn
                        MoveCompleted();
                    }
                }
            }
        }
    }
예제 #4
0
 public PossibleMoveWrapper(PossibleMove instance)
 {
     this.instance    = instance;
     instance.OnOver += Instance_OnOver;
     instance.tag     = this;
     InitShape();
 }
예제 #5
0
        private void OnPossibleMoveCreate(PossibleMove possibleMove)
        {
            PossibleMoveWrapper pmw = new PossibleMoveWrapper(possibleMove);

            DrawCanvas.Children.Add(pmw.WinShape);
            pmw.OnOverEvent += Pmw_OnOverEvent;
        }
예제 #6
0
        private void OnPossibleMoveCreate(PossibleMove possibleMove)
        {
            PossibleMoveWrapper posmove = new PossibleMoveWrapper(possibleMove);

            possibleMove.tag     = posmove;
            possibleMove.OnOver += OnPossibleMoveOver;
            possibleMoves.Add(posmove);
        }
예제 #7
0
    private void Execute(PossibleMove move)
    {
        ship.MoveBySailing(ship.CoordInFront(), move.facing);

        Hide();

        Finish();
    }
    private void PlacePiece(int x, int y, int playerState, PossibleMove pm)
    {
        SetPositionToPieceColour(x, y, playerState);

        for (int i = 0; i < pm.positionsFlipped.Length; i++)
        {
            SetPositionToPieceColour((int)pm.positionsFlipped[i].x, (int)pm.positionsFlipped[i].y, playerState);
        }
    }
예제 #9
0
    // tymczasowe zasymulowanie wykonania ruchu
    public Bazowa_Figura tymczasowyRuch(PossibleMove possibleMove)
    {
        Bazowa_Figura figura = AllCells[possibleMove.toX, possibleMove.toY].Aktualna_Figura;

        AllCells[possibleMove.toX, possibleMove.toY].Aktualna_Figura = AllCells[possibleMove.fromX, possibleMove.fromY].Aktualna_Figura;
        AllCells[possibleMove.toX, possibleMove.toY].Aktualna_Figura._CurrentCell = AllCells[possibleMove.toX, possibleMove.toY];
        AllCells[possibleMove.fromX, possibleMove.fromY].Aktualna_Figura          = null;
        return(figura);
    }
예제 #10
0
        public static BoardNode GetChild(Board b, PossibleMove pm, int trn)
        {
            var nb = new Board(b._size);

            Board.CopyBoard(nb, b);
            var item = new BoardNode(nb, pm);

            item._bData.AddPiece(pm.x, pm.y, trn);
            AI.TakeTurn(pm.x, pm.y, trn, item._bData);
            return(item);
        }
예제 #11
0
    // Cofniecie symulacji wykonania ruchu
    public void cofnijTymczasowyRuch(PossibleMove possibleMove, Bazowa_Figura figura)
    {
        AllCells[possibleMove.fromX, possibleMove.fromY].Aktualna_Figura = AllCells[possibleMove.toX, possibleMove.toY].Aktualna_Figura;
        AllCells[possibleMove.fromX, possibleMove.fromY].Aktualna_Figura._CurrentCell = AllCells[possibleMove.fromX, possibleMove.fromY];

        AllCells[possibleMove.toX, possibleMove.toY].Aktualna_Figura = figura;
        if (figura != null)
        {
            AllCells[possibleMove.toX, possibleMove.toY].Aktualna_Figura._CurrentCell = AllCells[possibleMove.toX, possibleMove.toY];
        }
    }
    private bool WithinPossibleMoves(int x, int y, ref PossibleMove pm)
    {
        for (int i = 0; i < possibleMoves.Count; i++)
        {
            if (possibleMoves[i].x == x && possibleMoves[i].y == y)
            {
                pm = possibleMoves[i];
                return(true);
            }
        }

        return(false);
    }
예제 #13
0
 private void PawnPromotion(Piece _piece, bool isAIplaying, PossibleMove moveToDo)
 {
     Debug.Log("le pion va être promu");
     if (isAIplaying)
     {
         //Debug.LogError("le moveToDo est null :"+(moveToDo == null));
         Debug.Log("le pion va être promu" + moveToDo.nameOfPieceToBecome);
         SetNameAndSpriteOnPiece(moveToDo.nameOfPieceToBecome, _piece);
         moves[moves.Count - 1].hasBeenPromoted = true;
     }
     else
     {
         pawnPromotion.SetActive(true);
     }
 }
예제 #14
0
    public static void PlayRandomMove()
    {
        //Debug.Log("Play Random");
        //Definir l'ensemble des coups possibles
        GameManager.instance.DefineAllPossibleMoves();
        //On choisit au hazard un move
        int moveToPlayIndex = GameManager.instance.rand.Next(GameManager.instance.possibleMoves.Count);
        // Debug.Log("Move choisit : "+moveToPlayIndex);
        PossibleMove moveToPlay = GameManager.instance.possibleMoves[moveToPlayIndex];

        moveToPlay.cellWherePieceCanMove.SetIsPlayable(true);


        //on le joue
        GameManager.instance.MovePiece(moveToPlay.piece, moveToPlay.cellWherePieceCanMove, true, moveToPlay);

        GameManager.instance.CheckEndOfGame(moveToPlay.piece);
    }
예제 #15
0
    // private void Update() {

    //      for (int i = 0; i < GameManager.instance.board.pieces.Count; i++)
    //      {
    //          if(GameManager.instance.board.pieces[i].isDead){
    //              continue;
    //          }
    //          GameManager.instance.PutAPieceToItsCell(GameManager.instance.board.pieces[i]);


    //      //UpdateCellUI();
    //  }
    // }

    public IEnumerator PlaySlownLyAllPossibleMoves()
    {
        for (int i = 0; i < possibleMoves1.Count; i++)
        {
            PossibleMove move = possibleMoves1[i];

            int tempcol  = move.piece.column;
            int templine = move.piece.line;
            GameManager.instance.MoveAPieceOnASpecificCell(move.piece, move.cellWherePieceCanMove);
            //GameManager.instance.MovePiece(move.piece,move.cellWherePieceCanMove,true,move);
            GameManager.instance.UpdatePiecePositionUI();
            UnityEngine.Debug.Log("une seconde :");
            yield return(new WaitForSeconds(0.2f));

            GameManager.instance.MoveAPieceOnASpecificCell(move.piece, GameManager.instance.GetCell(tempcol, templine));
            yield return(new WaitForSeconds(0.5f));
        }
    }
예제 #16
0
    public void Show()
    {
        Coord pos = ship.CoordInFront();

        if (MapController.instance.IsWithinMap(pos))
        {
            if (MapController.instance.GetTile(pos).ship != null)
            {
                // ram
                posHighlight = TileHighlighter.instance.CreateHighlight(pos, 5);

                ramMove = new RammingMove(this, pos, posHighlight);

                Tile tile = MapController.instance.GetTile(pos);
                tile.OnMouseClick += ramMove.OnMouseClick;
            }
            else
            {
                // move
                posHighlight = TileHighlighter.instance.CreateHighlight(pos, 2);

                for (int d = -1; d <= 1; d++)
                {
                    int   facing      = (int)Mathf.Repeat(ship.facing + d, 8);
                    Coord destination = pos + Coord.neighbours[facing];

                    if (MapController.instance.IsWithinMap(destination))
                    {
                        Highlight hl = TileHighlighter.instance.CreateHighlight(destination, 3, facing);

                        PossibleMove move = new PossibleMove(this, destination, hl, facing);

                        Tile destTile = MapController.instance.GetTile(destination);
                        destTile.OnMouseClick += move.OnMouseClick;

                        possibleMoves.Add(move);
                    }
                }
            }
        }
    }
예제 #17
0
 public PossibleMoveWrapper(PossibleMove instance)
 {
     this.instance = instance;
     this.gemPos   = new Vector2(instance.Key.Position.x, instance.Key.Position.y);
     this.movePos  = new Vector2(instance.MatchablePosition.x, instance.MatchablePosition.y);
 }
예제 #18
0
    public void MovePiece(Piece piece, Cell cellToMove, bool isAIplaying, PossibleMove moveToDo = null, bool isCheckingKingChecked = false)
    {
        //Cell cellToMove = GetNearestCellTransform(piece.transform).GetComponent<Cell>();
        //Cell cellToMove = GetCell(col,line);

        if (piece.column != cellToMove.columnNumber || piece.line != cellToMove.lineNumber)     // Si la piece a bougé

        {
            if (cellToMove.GetIsPlayable())
            {
                Move currentMove = new Move();
                currentMove.turn                = turn;
                currentMove.pieceMoved          = piece;
                currentMove.cellWherePieceMoved = cellToMove;
                currentMove.originalCell        = GetCell(piece.column, piece.line);



                //Manger une piece sur la même case

                if (cellToMove.GetPieceOnIt())
                {
                    Piece deadPiece = GetPieceOnCell(cellToMove);
                    PieceDie(deadPiece, currentMove);
                }

                //Prise en passant(TODO)
                if (piece.name == "Pawn")
                {
                    if (cellToMove.columnNumber != piece.column)
                    {
                        //Get  et tue la piece à gauche si elle existe
                        KillPawnEnPassant(piece.column - 1, piece.line, currentMove);

                        //Get  et tue la piece à droite si elle existe
                        KillPawnEnPassant(piece.column + 1, piece.line, currentMove);
                    }
                }

                //Roque (
                if (piece.name == "King")
                {
                    if (piece.column - cellToMove.columnNumber == 2)                         // grand roque
                    {
                        Piece tower = GetPieceOnCell(GetCell(piece.column - 4, piece.line)); //Can only be a tower
                        //Debug.LogError(tower);

                        if (tower.name == "Tower")
                        {
                            currentMove.towerRocked = tower;
                            currentMove.cellFromWhereTowerCastled = GetCell(tower.column, tower.line);
                            MoveAPieceOnASpecificCell(tower, GetCell(tower.column + 3, tower.line));
                        }
                    }
                    else
                    if (piece.column - cellToMove.columnNumber == -2)                        // petit roque
                    {
                        Piece tower = GetPieceOnCell(GetCell(piece.column + 3, piece.line)); //Can only be a tower
                        //Debug.LogError(tower);

                        if (tower.name == "Tower")
                        {
                            currentMove.towerRocked = tower;
                            currentMove.cellFromWhereTowerCastled = GetCell(tower.column, tower.line);
                            MoveAPieceOnASpecificCell(tower, GetCell(tower.column - 2, tower.line));
                        }
                    }
                }


                turn++;
                piece.move++;
                if (!piece.hasAlreadyMoved)
                {
                    piece.hasAlreadyMoved = true;
                }
                if (!isCheckingKingChecked)
                {
                    lastMovedPiece = piece;
                }

                //      if(!isCheckingKingChecked){
                //     Debug.Log(" bef piece line: "+piece.line);
                //     Debug.Log("bef piece col "+piece.column);
                //     Debug.Log("bef cell line "+cellToMove.lineNumber);
                //     Debug.Log("bef cell col "+cellToMove.columnNumber);

                // }



                //Realiser le déplacement
                MoveAPieceOnASpecificCell(piece, cellToMove);



                //      if(!isCheckingKingChecked){
                //     Debug.Log("aft piece line: "+piece.line);
                //     Debug.Log("aft piece col "+piece.column);
                //     Debug.Log("aft cell line "+cellToMove.lineNumber);
                //     Debug.Log("aft cell col "+cellToMove.columnNumber);

                // }



                //Ajout du move
                moves.Add(currentMove);

                if (PawnPromotionCondition(piece))
                {
                    if (!isCheckingKingChecked)
                    {
                        PawnPromotion(piece, isAIplaying, moveToDo);
                    }
                }

                if (!isCheckingKingChecked)
                {
                    positions.Add(GetPosition());
                }
            }
            else
            {
                // TO PUT PlaceInOriginalCell(piece);
            }
        }
        else
        {
            // TO PUT PlaceInOriginalCell(piece);
        }
    }
    private void CheckFlipsPieces(int x, int y)
    {
        //Set which piece we're checking for depending on whose turn it is
        int pieceState = (playerTurn) ? 1 : 2;

        //All the positions that would be flipped if this piece was placed
        List <Vector2> totalPositions   = new List <Vector2>();
        List <Vector2> checkedPositions = new List <Vector2>();

        //Loop through all the offsets to check all 8 directions
        for (int i = 0; i < offsets.Length; i++)
        {
            //Reset the not at end of search bool for all directions
            bool notAtEnd = true;

            //Reset the positions being checked list
            checkedPositions.Clear();

            //Reset the position to check
            int checkY = y;
            int checkX = x;

            //Loop through all positions of the direction
            while (notAtEnd)
            {
                //Modify the position by the offset
                checkY += (int)offsets[i].y;
                checkX += (int)offsets[i].x;

                //Check if the position has reached the end of the board
                if (checkY < 0 || checkY > boardHeight - 1 || checkX < 0 || checkX > boardWidth - 1)
                {
                    notAtEnd = false;
                }
                else
                {
                    //Check if the position has reached a piece of the same colour
                    if (board[checkY, checkX].state == pieceState)
                    {
                        //If it is not directly next to the piece placed
                        if (checkedPositions.Count > 0)
                        {
                            //Add all the positions that have been checked along the way to the total list to be flipped
                            for (int j = 0; j < checkedPositions.Count; j++)
                            {
                                totalPositions.Add(checkedPositions[j]);
                            }
                        }
                        notAtEnd = false;
                    }
                    //Check if the position has reached an empty space
                    else if (board[checkY, checkX].state == 0)
                    {
                        notAtEnd = false;
                    }
                }

                //Add the current position that has been determined to still be on the board and not the same piece and not a blank to the list
                checkedPositions.Add(new Vector2(checkX, checkY));
            }
        }

        if (totalPositions.Count > 0)
        {
            PossibleMove move = new PossibleMove(x, y, totalPositions.Count, totalPositions.ToArray());
            possibleMoves.Add(move);
        }
    }
예제 #20
0
    public void DefineAllPossibleMoves()
    {
        possibleMoves.Clear();

        for (int i = 0; i < board.pieces.Count; i++)
        {
            Piece piece = board.pieces[i];
            if (piece.isDead)
            {
                continue;
            }
            // Debug.Log("Piece team : "+piece.team);
            // Debug.Log("GameManager team : "+teamToPlay);
            if (piece.team == teamToPlay)
            {
                SetCellsNonPlayable();
                CalculatePlayableOrAttackedCells(piece, true, false);
                for (int col = 0; col < 8; col++)
                {
                    for (int line = 0; line < 8; line++)
                    {
                        if (GetCell(col, line).GetIsPlayable())
                        {
                            if (piece.name == "Pawn" && ((line == 0) || (line == 7)))    //Si promotion du pion
                            {
                                PossibleMove possibleMoveQueen = new PossibleMove();
                                possibleMoveQueen.cellWherePieceCanMove = GetCell(col, line);
                                possibleMoveQueen.piece = piece;
                                possibleMoveQueen.nameOfPieceToBecome = "Queen";
                                possibleMoves.Add(possibleMoveQueen);

                                PossibleMove possibleMoveTower = new PossibleMove();
                                possibleMoveTower.cellWherePieceCanMove = GetCell(col, line);
                                possibleMoveTower.piece = piece;
                                possibleMoveTower.nameOfPieceToBecome = "Tower";
                                possibleMoves.Add(possibleMoveTower);

                                PossibleMove possibleMoveBishop = new PossibleMove();
                                possibleMoveBishop.cellWherePieceCanMove = GetCell(col, line);
                                possibleMoveBishop.piece = piece;
                                possibleMoveBishop.nameOfPieceToBecome = "Bishop";
                                possibleMoves.Add(possibleMoveBishop);

                                PossibleMove possibleMoveKnight = new PossibleMove();
                                possibleMoveKnight.cellWherePieceCanMove = GetCell(col, line);
                                possibleMoveKnight.piece = piece;
                                possibleMoveKnight.nameOfPieceToBecome = "Knight";
                                possibleMoves.Add(possibleMoveKnight);
                            }
                            else
                            {
                                PossibleMove possibleMove = new PossibleMove();
                                possibleMove.cellWherePieceCanMove = GetCell(col, line);
                                possibleMove.piece = piece;
                                possibleMoves.Add(possibleMove);
                            }
                        }
                    }
                }
            }
        }
    }
예제 #21
0
        private void OnPossibleMoveOver(PossibleMove sender)
        {
            PossibleMoveWrapper posmove = (PossibleMoveWrapper)sender.tag;

            possibleMoves.Remove(posmove);
        }
예제 #22
0
 public BoardNode(Board b, PossibleMove move)
 {
     _bData    = b;
     _move     = move;
     _children = new List <BoardNode>();
 }
    public List <PossibleMove> GetAllPossibleMoves()
    {
        List <PossibleMove> allPossibleMovesList = new List <PossibleMove>();

        // Test the Horizontal Axis first, prioritize nodes lower on the grid
        for (int y = 0; y < gridHeight; y++)
        {
            for (int x = 0; x < gridWidth; x++)
            {
                // Test Swap: Left, Right, Up, Down
                List <PossibleMove> testPossibleMoveList = new List <PossibleMove>();
                testPossibleMoveList.Add(new PossibleMove(x, y, x - 1, y + 0));
                testPossibleMoveList.Add(new PossibleMove(x, y, x + 1, y + 0));
                testPossibleMoveList.Add(new PossibleMove(x, y, x + 0, y + 1));
                testPossibleMoveList.Add(new PossibleMove(x, y, x + 0, y - 1));

                for (int i = 0; i < testPossibleMoveList.Count; i++)
                {
                    PossibleMove possibleMove = testPossibleMoveList[i];

                    bool skipPossibleMove = false;

                    for (int j = 0; j < allPossibleMovesList.Count; j++)
                    {
                        PossibleMove tmpPossibleMove = allPossibleMovesList[j];
                        if (tmpPossibleMove.startX == possibleMove.startX &&
                            tmpPossibleMove.startY == possibleMove.startY &&
                            tmpPossibleMove.endX == possibleMove.endX &&
                            tmpPossibleMove.endY == possibleMove.endY)
                        {
                            // Already tested this combo
                            skipPossibleMove = true;
                            break;
                        }
                        if (tmpPossibleMove.startX == possibleMove.endX &&
                            tmpPossibleMove.startY == possibleMove.endY &&
                            tmpPossibleMove.endX == possibleMove.startX &&
                            tmpPossibleMove.endY == possibleMove.startY)
                        {
                            // Already tested this combo
                            skipPossibleMove = true;
                            break;
                        }
                    }

                    if (skipPossibleMove)
                    {
                        continue;
                    }

                    SwapGridPositions(possibleMove.startX, possibleMove.startY, possibleMove.endX, possibleMove.endY); // Swap

                    List <List <GemGridPosition> > allLinkedGemGridPositionList = GetAllMatch3Links();

                    if (allLinkedGemGridPositionList.Count > 0)
                    {
                        // Making this Move results in a Match
                        possibleMove.allLinkedGemGridPositionList = allLinkedGemGridPositionList;
                        allPossibleMovesList.Add(possibleMove);
                    }

                    SwapGridPositions(possibleMove.startX, possibleMove.startY, possibleMove.endX, possibleMove.endY); // Swap Back
                }
            }
        }

        return(allPossibleMovesList);
    }
예제 #24
0
    // tworzenie jakby drzewa gdzie w każdej gałęzi liczone jest co się najbardziej opłaca
    protected int minimax(int glebokosc, bool czy_Max, Board newBoard, bool czarne)
    {
        if (glebokosc == 0)
        {
            return(wartosc_planszy(true, newBoard)); //wartość pionków na planszy po ruchach
        }
        if (czy_Max)
        {
            int najlepsza_wartosc             = -9999999;
            List <PossibleMove> possibleMoves = newBoard.GetPossibleMoves(czarne); //tworzy listę możliwych ruchów

            possibleMoves.Sort((a, b) => 1 - 2 * UnityEngine.Random.Range(0, 1));
            for (int i = 0; i < possibleMoves.Count; i++) //losowe ułożenie listy
            {
                PossibleMove temp        = possibleMoves[i];
                int          randomIndex = UnityEngine.Random.Range(i, possibleMoves.Count);
                possibleMoves[i]           = possibleMoves[randomIndex];
                possibleMoves[randomIndex] = temp;
            }

            foreach (PossibleMove move in possibleMoves)
            {
                Bazowa_Figura figura  = newBoard.tymczasowyRuch(move);
                int           wartosc = minimax(glebokosc - 1, !czy_Max, newBoard, !czarne);
                if (wartosc > najlepsza_wartosc) //jak znajdzie lepszą wartość to ją wstawia
                {
                    najlepsza_wartosc = wartosc;
                    if (glebokosc == 2)
                    {
                        najlepszy_ruch = move;
                    }
                }
                newBoard.cofnijTymczasowyRuch(move, figura);
                //Debug.Log(" Best Move: " + najlepsza_wartosc + " Figura: " + move.fromX + "." + move.fromY);
            }
            return(najlepsza_wartosc);
        }
        else
        {
            int najlepsza_wartosc             = 9999999;
            List <PossibleMove> possibleMoves = newBoard.GetPossibleMoves(czarne); //tworzy listę możliwych ruchów

            possibleMoves.Sort((a, b) => 1 - 2 * UnityEngine.Random.Range(0, 1));
            for (int i = 0; i < possibleMoves.Count; i++) //losowe ułożenie listy
            {
                PossibleMove temp        = possibleMoves[i];
                int          randomIndex = UnityEngine.Random.Range(i, possibleMoves.Count);
                possibleMoves[i]           = possibleMoves[randomIndex];
                possibleMoves[randomIndex] = temp;
            }

            foreach (PossibleMove move in possibleMoves)
            {
                Bazowa_Figura figura  = newBoard.tymczasowyRuch(move);
                int           wartosc = minimax(glebokosc - 1, !czy_Max, newBoard, !czarne);
                if (wartosc < najlepsza_wartosc) //jak znajdzie lepszą wartość to ją wstawia
                {
                    najlepsza_wartosc = wartosc;
                    if (glebokosc == 2)
                    {
                        najlepszy_ruch = move;
                    }
                }
                newBoard.cofnijTymczasowyRuch(move, figura);
                //Debug.Log(" Best Move: " + najlepsza_wartosc + " Figura: " + move.fromX + "." + move.fromY);
            }
            return(najlepsza_wartosc);
        }
    }