Esempio n. 1
0
File: AI.cs Progetto: 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();
        }
Esempio n. 2
0
        private void gameboard_MouseClick(object sender, MouseEventArgs e)
        {
            Control c = (Control)sender;
            Piece currentPiece = gameboard.getPiece(this.gameboardPanel.GetRow(c), this.gameboardPanel.GetColumn(c));

            if (!pawnChangable)
            {
                // If this is the piece we want to move
                if (currentPiece.team == currentState.getMyTeam()
                    && (previousPiece == null || (!validDestinations.Contains(new Tuple<int, int>(currentPiece.row, currentPiece.column)))))// && currentPiece != previousPiece))
                {
                    if (currentPiece == previousPiece)
                    {
                        previousPiece = null;
                        validDestinations = new List<Tuple<int, int>>();
                    }
                    else
                    {
                        validDestinations = rulebook.getValidMoves(currentPiece, gameboard);
                        if (validDestinations.Count() == 0)
                        {
                            validDestinations = new List<Tuple<int, int>>();
                            previousPiece = null;
                            return;
                        }
                        else
                        {
                            previousPiece = currentPiece;
                        }
                    }
                }
                // If we have marked a previous piece and this is the destination
                else if (validDestinations.Contains(new Tuple<int, int>(currentPiece.row, currentPiece.column)))
                {
                    gameboard.Move(previousPiece, currentPiece);
                    updateBoard();
                    validDestinations = new List<Tuple<int, int>>();

                    int resultOfMove = gameboard.checkChessMate(currentState.getMyTeam());

                    if (resultOfMove == (int)moveResult.chess)
                    {
                        MessageBox.Show("Chess - Nice!", "Chess - Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else if (resultOfMove == (int)moveResult.chessMate)
                    {
                        DialogResult result = MessageBox.Show("Chessmate! You win.\n\nPlay again?", "Chess - Message", MessageBoxButtons.YesNo);
                        promptNewGame(result);
                        return;
                    }

                    if (previousPiece.type == (int)type.pawn && (currentPiece.row == 0 || currentPiece.row == 7))
                    {
                        MessageBox.Show("You get to change your pawn to one of your dead pieces.", "Pawn has crossed the gameboard!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        pawnChangable = true;
                    }
                    else
                    {
                        previousPiece = null;
                        AIOpponent.Move(gameboard, currentState);
                        updateBoard();

                        resultOfMove = gameboard.checkChessMate(currentState.getOpponentTeam());

                        if (resultOfMove == (int)moveResult.chess)
                        {
                            MessageBox.Show("Chess - Watch out!", "Chess - Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else if (resultOfMove == (int)moveResult.chessMate)
                        {
                            DialogResult result = MessageBox.Show("Chessmate! You loose.\n\nPlay again?", "Chess - Message", MessageBoxButtons.YesNo);
                            promptNewGame(result);
                            return;
                        }
                    }
                }

                else if (previousPiece == null)
                {
                    return;
                }
                else
                {
                    validDestinations = new List<Tuple<int, int>>();
                    previousPiece = null;
                }
                updateBoard();
            }
        }