예제 #1
0
파일: Queen.cs 프로젝트: mapplet/Chess
        public List<Tuple<int, int>> Rules(Piece currentPiece, Gameboard gameboard)
        {
            this.moves_dia = get_diagonal.Rules(currentPiece, gameboard);
            this.moves_str = get_straight.Rules(currentPiece, gameboard);

            moves_str = moves_str.Concat(moves_dia).ToList();

            return moves_str;
        }
예제 #2
0
파일: Knight.cs 프로젝트: mapplet/Chess
        public List<Tuple<int, int>> Rules(Piece current_piece, Gameboard gameboard)
        {
            this.gameboard = gameboard;
            this.current_piece = current_piece;

            if (current_piece.column > min && current_piece.row > min + 1)
            {
                next_piece = gameboard.getPiece(current_piece.row - 2, current_piece.column - 1);
                if (next_piece.empty == true || next_piece.team != current_piece.team)
                    valid_destinations.Add(new Tuple<int, int>(current_piece.row - 2, current_piece.column - 1));   // up left
            }
            if (current_piece.column < max && current_piece.row > min + 1)
            {
                Piece next_piece = gameboard.getPiece(current_piece.row - 2, current_piece.column + 1);
                if (next_piece.empty == true || next_piece.team != current_piece.team)
                    valid_destinations.Add(new Tuple<int, int>(current_piece.row -2, current_piece.column + 1)); // up right
            }
            if (current_piece.column < max - 1 && current_piece.row > min)
            {
                Piece next_piece = gameboard.getPiece(current_piece.row - 1, current_piece.column + 2);
                if (next_piece.empty == true || next_piece.team != current_piece.team)
                    valid_destinations.Add(new Tuple<int, int>(current_piece.row - 1, current_piece.column + 2)); // right up
            }
            if (current_piece.column < max - 1 && current_piece.row < max)
            {
                Piece next_piece = gameboard.getPiece(current_piece.row + 1, current_piece.column + 2);
                if (next_piece.empty == true || next_piece.team != current_piece.team)
                    valid_destinations.Add(new Tuple<int, int>(current_piece.row + 1, current_piece.column + 2));  // right down
            }
            if (current_piece.column < max && current_piece.row < max - 1)
            {
                Piece next_piece = gameboard.getPiece(current_piece.row + 2, current_piece.column + 1);
                if (next_piece.empty == true || next_piece.team != current_piece.team)
                    valid_destinations.Add(new Tuple<int, int>(current_piece.row + 2, current_piece.column + 1));  // down right
            }
            if (current_piece.column > min && current_piece.row < max - 1)
            {
                Piece next_piece = gameboard.getPiece(current_piece.row + 2, current_piece.column - 1);
                if (next_piece.empty == true || next_piece.team != current_piece.team)
                    valid_destinations.Add(new Tuple<int, int>(current_piece.row + 2, current_piece.column - 1));  // down left
            }
            if (current_piece.column > min + 1 && current_piece.row < max)
            {
                Piece next_piece = gameboard.getPiece(current_piece.row + 1, current_piece.column - 2);
                if (next_piece.empty == true || next_piece.team != current_piece.team)
                    valid_destinations.Add(new Tuple<int, int>(current_piece.row + 1, current_piece.column - 2));  // left down
            }
            if (current_piece.column > min + 1 && current_piece.row > min)
            {
                Piece next_piece = gameboard.getPiece(current_piece.row - 1, current_piece.column - 2);
                if (next_piece.empty == true || next_piece.team != current_piece.team)
                    valid_destinations.Add(new Tuple<int, int>(current_piece.row - 1, current_piece.column - 2)); // left up
            }
            return valid_destinations;
        }
예제 #3
0
파일: State.cs 프로젝트: mapplet/Chess
 public void Print(Gameboard gameboard)
 {
     Console.WriteLine("############## CURRENT GAMEBOARD ##############");
     for (int row = 0; row < 8; ++row)
     {
         for (int column = 0; column < 8; ++column)
         {
             Console.Write("[{0},{1}] ", gameboard.getPiece(row, column).team, gameboard.getPiece(row, column).type);
         }
         Console.WriteLine("");
     }
     Console.WriteLine("###############################################");
 }
예제 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PlayerChess"/> class.
        /// </summary>
        /// <param name="color">The piece color.</param>
        /// <param name="gameboard">The gameboard.</param>
        public PlayerChess(IPieceColor color, Gameboard gameboard)
            : base(color)
        {
            IPiece newPiece;
            int offset = 0;

            this.gameboard = gameboard;
            this.selectedPiece = null;

            if (color == IPieceColor.White)
            {
                offset = 7;
            }
            else if (color == IPieceColor.Black)
            {
                offset = 0;
            }

            for (int i = 0; i < 8; i++)
            {
                newPiece = new Pawn(color, gameboard, new Position(i, Math.Abs(offset - 1)));
                newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
                this.PieceList.Add(newPiece);
            }

            newPiece = new Rook(color, gameboard, new Position(0, offset));
            newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
            this.PieceList.Add(newPiece);
            newPiece = new Rook(color, gameboard, new Position(7, offset));
            newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
            this.PieceList.Add(newPiece);
            newPiece = new Knight(color, gameboard, new Position(1, offset));
            newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
            this.PieceList.Add(newPiece);
            newPiece = new Knight(color, gameboard, new Position(6, offset));
            newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
            this.PieceList.Add(newPiece);
            newPiece = new Bishop(color, gameboard, new Position(2, offset));
            newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
            this.PieceList.Add(newPiece);
            newPiece = new Bishop(color, gameboard, new Position(5, offset));
            newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
            this.PieceList.Add(newPiece);
            newPiece = new Queen(color, gameboard, new Position(3, offset));
            newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
            this.PieceList.Add(newPiece);
            newPiece = new King(color, gameboard, new Position(4, offset));
            newPiece.GameEvent += new PieceEvent(this.Piece_GameEvent);
            this.PieceList.Add(newPiece);
            this.King = newPiece;
        }
예제 #5
0
파일: Bishop.cs 프로젝트: mapplet/Chess
        public List<Tuple<int, int>> Rules(Piece current_piece, Gameboard gameboard)
        {
            this.gameboard = gameboard;
            this.current_piece = current_piece;

            if (current_piece.column != min && current_piece.row != min) // NOT max up or left
                up_left_function();
            if (current_piece.column != max && current_piece.row != min) // NOT max or right
                up_right_function();
            if (current_piece.column != min && current_piece.row != max) // NOT max down or left
                down_left_function();
            if (current_piece.column != max && current_piece.row != max) // NOT max down or right
                down_right_function();

            return valid_destinations;
        }
예제 #6
0
파일: Rock.cs 프로젝트: mapplet/Chess
        public List<Tuple<int, int>> Rules(Piece current_piece, Gameboard gameboard)
        {
            this.current_piece = current_piece;
            this.gameboard = gameboard;

            if (current_piece.column != min)
                check_left_function();
            if (current_piece.column != max)
                check_right_function();
            if (current_piece.row != min)
                check_up_function();
            if (current_piece.row != max)
                check_down_function();

            return valid_destinations;
        }
예제 #7
0
파일: Rulebook.cs 프로젝트: mapplet/Chess
        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;
        }
예제 #8
0
파일: Gameboard.cs 프로젝트: mapplet/Chess
 public Gameboard(Gameboard originalGameboard)
 {
     for (int row = 0; row<8; ++row)
     {
         List<Piece> copyOfRow = new List<Piece>();
         foreach (Piece piece in originalGameboard.getRow(row))
         {
             Piece copyOfPiece = new Piece(piece);
             copyOfRow.Add(copyOfPiece);
             if (copyOfPiece.team == (int)team.black)
                 this.blackTeam.Add(copyOfPiece);
             else if (copyOfPiece.team == (int)team.white)
                 this.whiteTeam.Add(copyOfPiece);
         }
         this.gameboard.Add(copyOfRow);
     }
 }
예제 #9
0
파일: Rulebook.cs 프로젝트: mapplet/Chess
        public List<Tuple<int, int>> getValidMoves(Piece currentPiece, Gameboard gameboard, bool doRecurse = true)
        {
            this.copyOfCurrentPiece = new Piece(currentPiece);
            this.gameboard = new Gameboard(gameboard);

            switch (currentPiece.type)
            {
                case (int)type.rock: // tower
                    Rock rock = new Rock();
                    this.validDestinations = rock.Rules(currentPiece, gameboard);
                    if (doRecurse)
                        cleanUp();
                    return validDestinations;
                case (int)type.knight: // horsie
                    Knight knight = new Knight();
                    this.validDestinations = knight.Rules(currentPiece, gameboard);
                    if (doRecurse)
                        cleanUp();
                    return validDestinations;
                case (int)type.bishop: // springare
                    Bishop bishop = new Bishop();
                    this.validDestinations = bishop.Rules(currentPiece, gameboard);
                    if (doRecurse)
                        cleanUp();
                    return validDestinations;
                case (int)type.queen: //los quuenos
                    Queen queen = new Queen();
                    this.validDestinations = queen.Rules(currentPiece, gameboard);
                    if (doRecurse)
                        cleanUp();
                    return validDestinations;
                case (int)type.king: // los kingos
                    King king = new King();
                    this.validDestinations = king.Rules(currentPiece, gameboard);
                    if (doRecurse)
                        cleanUp();
                    return validDestinations;
                case (int)type.pawn: // los farmeros
                    Pawn pawn = new Pawn();
                    this.validDestinations = pawn.Rules(currentPiece, gameboard);
                    if (doRecurse)
                        cleanUp();
                    return validDestinations;
            }
            return new List<Tuple<int,int>>();
        }
예제 #10
0
파일: Pawn.cs 프로젝트: mapplet/Chess
        public List<Tuple<int, int>> Rules(Piece piece, Gameboard gameboard)
        {
            if (piece.row == 0 || piece.row == 7)
                return new List<Tuple<int, int>>();

            int max_row;
            int moving_factor;
            int step = 1;
            List<Tuple<int, int>> valid_destinations = new List<Tuple<int, int>>();
            if (piece.team == (int)team.black)
            {
                max_row = 7;
                moving_factor = 1;
                if (piece.row == 1)
                    ++step;
            }
            else
            {
                max_row = 0;
                moving_factor = -1;
                if (piece.row == 6)
                    ++step;
            }

            for (int i = 1; i < step + 1; i++)
            {
                if (gameboard.getPiece(piece.row + (i * moving_factor), piece.column).empty && piece.row != max_row)
                {
                    valid_destinations.Add(new Tuple<int, int>(piece.row + (i * moving_factor), piece.column));
                }
                if (i == 1 && piece.column != 0 && gameboard.getPiece(piece.row + (i * moving_factor), piece.column - 1).team != piece.team && !gameboard.getPiece(piece.row + (i * moving_factor), piece.column - 1).empty)
                {
                    valid_destinations.Add(new Tuple<int, int>(piece.row + (i * moving_factor), piece.column-1));
                }
                if (i == 1 && piece.column != 7 && gameboard.getPiece(piece.row + (i * moving_factor), piece.column + 1).team != piece.team && !gameboard.getPiece(piece.row + (i * moving_factor), piece.column + 1).empty)
                {
                    valid_destinations.Add(new Tuple<int, int>(piece.row + (i * moving_factor), piece.column+1));
                }
            }

            return valid_destinations;
        }
예제 #11
0
파일: AI.cs 프로젝트: mapplet/Chess
        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();
        }
예제 #12
0
파일: King.cs 프로젝트: mapplet/Chess
        public List<Tuple<int, int>> Rules(Piece current_piece, Gameboard gameboard)
        {
            this.gameboard = gameboard;
            this.current_piece = current_piece;

            if (current_piece.row != min && current_piece.column != min)
                up_left();
            if (current_piece.row != min && current_piece.column != max)
                up_right();
            if (current_piece.row != max && current_piece.column != min)
                down_left();
            if (current_piece.row != max && current_piece.column != max)
                down_right();
            if (current_piece.row != min)
                up();
            if (current_piece.row != max)
                down();
            if (current_piece.column != min)
                left();
            if (current_piece.column != max)
                right();

            return valid_destinations;
        }
예제 #13
0
파일: game.cs 프로젝트: mapplet/Chess
        private void initializeGame(int myTeam)
        {
            Console.WriteLine("# Init: Thread ID: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
            // Resume game?
            if (myTeam == (int)team.none)
            {
                Console.WriteLine("# Resuming Game.");
                Tuple<Gameboard, State> loadedGame = storage.LoadGame();
                gameboard = loadedGame.Item1;
                currentState = loadedGame.Item2;
                gameboard.updateBoard();
                currentState.updateState(currentState.getMyTeam());
            }

            // Else initialize default
            else
            {
                Console.WriteLine("# Initializes new Game.");
                gameboard = new Gameboard();
                gameboard.initialize();
                currentState = new State(myTeam);
                storage.manuallyInitialize(gameboard, currentState);
            }

            AIOpponent = new AI(currentState.getOpponentTeam());

            storage.getWatcher().SynchronizingObject = this;

            // Update GUI when database is changed
            storage.propertyChanged += new PropertyChangedEventHandler(OnDatabaseChanged);

            if (currentState.getOpponentTeam() == (int)team.white)
            {
                Console.WriteLine("# Computer will make first move.");
                AIOpponent.Move(gameboard, currentState);
            }

            updateBoard();
        }