Beispiel #1
0
        private void cleanUp()
        {
            Gameboard copyOfGameboard = new Gameboard(gameboard);
            Rulebook rulebook = new Rulebook();
            List<Tuple<int, int>> copyOfValids = new List<Tuple<int, int>>();
            bool foundIllegalMove = false;

            Piece originalCurrentPiece;

            int currentTeam = copyOfCurrentPiece.team;
            int opponentTeam = info.getOpponent(currentTeam);

            foreach (Tuple<int, int> validMove in validDestinations) // För varje möjligt drag för markerad Piece
            {
                copyOfGameboard = new Gameboard(gameboard);
                originalCurrentPiece = copyOfGameboard.getPiece(copyOfCurrentPiece.row, copyOfCurrentPiece.column);
                Piece destination = copyOfGameboard.getPiece(validMove.Item1, validMove.Item2);

                copyOfGameboard.Move(originalCurrentPiece, destination); // Gör temporärt move
                foundIllegalMove = false;

                    foreach (Piece opponent in copyOfGameboard.getTeam(opponentTeam)) // För varje Piece i team OPPONENT
                    {
                        List<Tuple<int,int>> validDest = rulebook.getValidMoves(opponent, copyOfGameboard, false); // Hämta valid moves för Piece i OPPONENT (Utan hänsyn till möjliga drag som sätter kungen i schack..)
                        foreach (Tuple<int,int> coordinate in validDest) // För varje möjligt drag för Piece i OPPONENT
                        {
                            if (copyOfGameboard.getPiece(coordinate.Item1, coordinate.Item2).type == (int)type.king) // Om Piece i OPPONENT kan ta kungen i CURRENT
                            {
                                foundIllegalMove = true;
                                break;
                            }
                            if (foundIllegalMove)
                                break;
                        }
                        if (foundIllegalMove)
                            break;
                    }

                if (!foundIllegalMove)
                    copyOfValids.Add(new Tuple<int, int>(validMove.Item1, validMove.Item2));

                copyOfGameboard.setPiece(originalCurrentPiece);
                copyOfGameboard.setPiece(destination);
            }

            validDestinations = copyOfValids;
        }
Beispiel #2
0
        public void Move(Gameboard gameboard, State currentState)
        {
            Rulebook rulebook = new Rulebook();
            List<Tuple<Piece, Piece>> topValidMoves = new List<Tuple<Piece, Piece>>();
            foreach(Piece piece in gameboard.getTeam(thisTeam))
            {
                List<Tuple<int, int>> validDestinations = rulebook.getValidMoves(piece, gameboard);
                foreach (Tuple<int,int> coordinate in validDestinations)
                {
                    if (validDestinations.Count() == 0)
                        break;
                    else if (topValidMoves.Count() == 0 || rewards[topValidMoves[0].Item2.type] == rewards[gameboard.getPiece(coordinate.Item1, coordinate.Item2).type])
                        topValidMoves.Add(new Tuple<Piece, Piece>(piece, gameboard.getPiece(coordinate.Item1, coordinate.Item2)));
                    else if (rewards[topValidMoves[0].Item2.type] < rewards[gameboard.getPiece(coordinate.Item1, coordinate.Item2).type])
                    {
                        topValidMoves.Clear();
                        topValidMoves.Add(new Tuple<Piece, Piece>(piece, gameboard.getPiece(coordinate.Item1, coordinate.Item2)));
                    }
                }

            }
            if (topValidMoves.Count() != 0)
            {
                Random rand = new Random();
                int index = rand.Next(topValidMoves.Count());
                gameboard.Move(topValidMoves[index].Item1, topValidMoves[index].Item2);

                if (topValidMoves[index].Item1.type == (int)type.pawn && (topValidMoves[index].Item1.row == 0 || topValidMoves[index].Item1.row == 7))
                {
                    Piece bestPieceFromDead = new Piece();
                    foreach (Piece deadPiece in gameboard.getDead(thisTeam))
                    {
                        if (rewards[deadPiece.type] > rewards[bestPieceFromDead.type])
                            bestPieceFromDead = deadPiece;
                    }
                    gameboard.tradePawn(topValidMoves[index].Item1, bestPieceFromDead);
                }

                //gameboard.checkChessMate(currentState.getWhosTurn());
            }
            //currentState.swapTurn();
        }