Пример #1
0
    public void SetPlayer(int playerNum, beings playeris, string playerName, pieces piece)
    {
        // update the relevant player's info:
        switch (playerNum)
        {
        case _PLAYER1:
            player1.PlayerIs   = playeris;
            player1.PlayerName = playerName;
            player1.PlayingAs  = piece;
            break;

        case _PLAYER2:
            player2.PlayerIs   = playeris;
            player2.PlayerName = playerName;
            player2.PlayingAs  = piece;
            break;

        // should never get here! This logs an error and tells us what "playerNum" was set to, to aid debugging.
        default:

            string errorString = String.Concat("ERROR: SetPlayer passed invalid player number: ".ToString(), playerNum.ToString());
            Debug.Log(errorString);

            break;
        }
    }
Пример #2
0
    // Called by BoardManager to signal when the game has been won.
    public void GameWon()
    {
        int  playerNum = 0;
        bool didXWin   = board.WasWinnerX();

        pieces p1Piece = gInfo.GetPlayerPiece(GameInfo._PLAYER1);

        // this is all we need to know who won.

        if (didXWin)
        {
            // find out who was playing as "X"...
            playerNum = (p1Piece == pieces.X) ? GameInfo._PLAYER1 : GameInfo._PLAYER2;
        }
        else
        {
            // looks like "O" won...
            playerNum = (p1Piece == pieces.O) ? GameInfo._PLAYER1 : GameInfo._PLAYER2;
        }


        if (playerNum == GameInfo._PLAYER1)
        {
            StartCoroutine(DoPlayer1WinsAnim());
        }
        if (playerNum == GameInfo._PLAYER2)
        {
            StartCoroutine(DoPlayer2WinsAnim());
        }
        gInfo.GameWon(playerNum);
        gInfo.state = GameInfo.GameStates.inGUI;
    }
Пример #3
0
    // Called by AIPlayer script to play its move.
    public void AIPlayMoveAt(int cell, pieces piece)
    {
        GridCellScript gcs = gridCells [cell];

        gcs.state = (piece == pieces.X) ? GridCellScript.CellStates.X : GridCellScript.CellStates.O;

        gHandler.NextTurn();                            // trigger next turn.
    }
Пример #4
0
    // It's the AI's turn to play...
    public void PlayAIMove(pieces piece)
    {
        // Because we need to use a coroutine to fake a 'thinking' period, this is just a very simple shell function...

        // Note that we only care about the piece - whether we're "X" or "O". Nothing else is relevant.
        int move = board.GetBestMove();

        StartCoroutine(DoMove(move, piece));                            // Use a coroutine to actually play the move.
    }
Пример #5
0
 public Board(pieces[,] layout, pieces player, byte step)
 {
     this.calculated = false;
     this.layout     = new pieces[8, 8];
     this.step       = step;
     this.player     = player;
     for (byte i = 0; i < 8; i++)
     {
         for (byte j = 0; j < 8; j++)
         {
             this.layout[i, j] = layout[i, j];
         }
     }
 }
Пример #6
0
        public Board()
        {
            this.player = pieces.one;
            byte i, j;

            for (i = 0; i < 8; i++)
            {
                for (j = 0; j < 8; j++)
                {
                    this.layout[i, j] = pieces.blank;
                }
            }
            this.layout[3, 4] = this.layout[4, 3] = pieces.one;
            this.layout[4, 4] = this.layout[3, 3] = pieces.two;
        }
Пример #7
0
    // Get the current player's piece and return an int containing EMPTYCELL, O_CELL or X_CELL, suitable for BoardManager.
    public int GetCurrentPlayerPieceAsInt()
    {
        // returns the current player.
        pieces piece = gInfo.GetCurrentPlayer().PlayingAs;

        int result = BoardManager.EMPTYCELL;                                                    // return this if there's an invalid value.

        if (piece == pieces.O)
        {
            result = BoardManager.O_CELL;
        }
        if (piece == pieces.X)
        {
            result = BoardManager.X_CELL;
        }

        return(result);
    }
        public ActionResult Game()
        {
            board.pieces.Clear();


            int    x1      = 1;//black pieces
            String color   = "Black";
            bool   control = false;

            for (int j = 0; j < 6; j++)
            {
                control = true;
                if (j % 2 == 0)
                {
                    control = false;
                }
                if (j == 3)
                {
                    x1    = 6;
                    color = "White";
                }

                for (int i = 1; i <= 8; i++)
                {
                    if (control)
                    {
                        pieces Pieces = new pieces();//pawn
                        Pieces.pawn.col   = i;
                        Pieces.pawn.id    = j.ToString() + i.ToString() + color + Pieces.pawn.name;
                        Pieces.pawn.row   = x1;
                        Pieces.pawn.color = color;
                        board.pieces.Add(Pieces.pawn);
                        control = false;
                    }
                    else
                    {
                        control = true;
                    }
                }
                x1++;
            }
            return(View(board));
        }
Пример #9
0
    public pieces GetPlayerPiece(int playerNum)
    {
        pieces p = pieces.X;                                    // need to set it to something or the compiler will whinge about it being used before being assigned.

        switch (playerNum)
        {
        case _PLAYER1:
            p = player1.PlayingAs;
            break;

        case _PLAYER2:
            p = player2.PlayingAs;
            break;

        default:
            // should never get here!
            Debug.Log("Invalid PlayerNum in GetPlayerPiece()");
            break;
        }

        return(p);
    }
Пример #10
0
 public PieceInfo(pieces p, int x, int y)
 {
     piece = p;
     xPos = x;
     yPos = y;
     if (piece == pieces.bBISHOP || piece == pieces.bKNIGHT || piece == pieces.wBISHOP || piece == pieces.wKNIGHT) {
         value = 3;
     } else if (piece == pieces.wPAWN || piece == pieces.bPAWN) {
         value = 1;
     } else if (piece == pieces.wROOK || piece == pieces.bROOK) {
         value = 5;
     } else if (piece == pieces.bQUEEN || piece == pieces.wQUEEN) {
         value = 9;
     }
 }
Пример #11
0
 public Move(pieces p,  int sX, int sY, int eX, int eY)
 {
     piece = p;
     startX = sX;
     startY = sY;
     endX = eX;
     endY = eY;
 }
Пример #12
0
 List<Move> getMoves(pieces piece, int x, int y)
 {
     List<Move> moves = new List<Move>();
     switch (piece)
     {
         case pieces.bKING:
             return getKingMoves(x, y);
             break;
         case pieces.bBISHOP:
             return getBishopMoves(x, y);
             break;
         case pieces.bKNIGHT:
             return getKnightMoves(x, y);
             break;
         case pieces.bROOK:
             return getRookMoves(x, y);
             break;
         case pieces.bQUEEN:
             return getQueenMoves(x, y);
             break;
         case pieces.bPAWN:
             return getPawnMoves(x, y);
             break;
     }
     Debug.Log("GetMoves should never come here");
     return moves;
 }
Пример #13
0
    // This coroutine fakes some thinking time before actually playing the move.
    private IEnumerator DoMove(int move, pieces piece)
    {
        yield return(new WaitForSeconds(1.5f));

        board.AIPlayMoveAt(move, piece);
    }
Пример #14
0
        // reset the posAble of the
        public static Boolean calculate(Board b)//写成static应该更省内存
        {
            if (!b.calculated)
            {
                pieces[,] layout = b.layout;
                pieces player = b.player;
                pieces opponent;
                if (player == pieces.one)
                {
                    opponent = pieces.two;
                }
                else
                {
                    opponent = pieces.one;
                }
                Dictionary <Position, List <Position> > posAble = new Dictionary <Position, List <Position> >();
                byte row, column;
                for (row = 0; row < 8; row++)
                {
                    for (column = 0; column < 8; column++)
                    {
                        if (layout[row, column] == pieces.blank)
                        {
                            //从空格开始依次向8个方向延伸,判段该空格是否可行棋,并将每个方向上能翻的敌方棋子记录在posFilp内
                            List <Position> posFlip = new List <Position>();
                            #region  for
                            byte            i;
                            Boolean         flag         = false;
                            List <Position> posCandidate = new List <Position>();
                            pieces          thisPos;
                            for (i = 1; i < 8 - row; i++)//向下延伸
                            {
                                thisPos = layout[row + i, column];
                                if (thisPos == opponent)//遇到敌方棋子,将敌方棋子位置记录并继续在该方向延伸
                                {
                                    posCandidate.Add(Position.board[row + i, column]);
                                    continue;
                                }
                                else if (thisPos == pieces.blank)
                                {
                                    break;       //遇到空格,该方向无效
                                }
                                else if (i != 1) //是我方棋子并且不是紧靠的棋子代表该空格可以行棋,
                                {
                                    flag = true; //该方向成立
                                    break;
                                }
                                else
                                {
                                    break; //紧靠的棋子是我方棋子,该方向无效
                                }
                            }
                            if (flag)                           //如果该方向成立
                            {
                                posFlip.AddRange(posCandidate); //将暂时记录下来的可翻棋子永久记录下来
                                flag = false;
                            }

                            //重置
                            posCandidate.Clear();
                            for (i = 1; i < row + 1; i++)//上
                            {
                                thisPos = layout[row - i, column];
                                if (thisPos == opponent)
                                {
                                    posCandidate.Add(Position.board[row - i, column]);
                                    continue;
                                }
                                else if (thisPos == pieces.blank)
                                {
                                    break;
                                }
                                else if (i != 1)
                                {
                                    flag = true;
                                    break;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            if (flag)
                            {
                                posFlip.AddRange(posCandidate);
                                flag = false;
                            }

                            posCandidate.Clear();
                            for (i = 1; i < 8 - column; i++)//右
                            {
                                thisPos = layout[row, column + i];
                                if (thisPos == opponent)
                                {
                                    posCandidate.Add(Position.board[row, column + i]);
                                    continue;
                                }
                                else if (thisPos == pieces.blank)
                                {
                                    break;
                                }
                                else if (i != 1)
                                {
                                    flag = true;
                                    break;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            if (flag)
                            {
                                posFlip.AddRange(posCandidate);
                                flag = false;
                            }

                            posCandidate.Clear();
                            for (i = 1; i < column + 1; i++)//左
                            {
                                thisPos = layout[row, column - i];
                                if (thisPos == opponent)
                                {
                                    posCandidate.Add(Position.board[row, column - i]);
                                    continue;
                                }
                                else if (thisPos == pieces.blank)
                                {
                                    break;
                                }
                                else if (i != 1)
                                {
                                    flag = true;
                                    break;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            if (flag)
                            {
                                posFlip.AddRange(posCandidate);
                                flag = false;
                            }

                            int boundLeftUp, boundLeftDown, boundRightUp, boundRightDown;
                            if (row < column)//空格更靠右上,先碰到右边界和上边界
                            {
                                boundLeftUp    = row + 1;
                                boundRightDown = 8 - column;
                            }
                            else//空格更靠左下,先碰到左边界和下边界
                            {
                                boundLeftUp    = column + 1;
                                boundRightDown = 8 - row;
                            }
                            if (row < 7 - column)//空格更靠左上
                            {
                                boundLeftDown = column + 1;
                                boundRightUp  = row + 1;
                            }
                            else//空格更靠右下
                            {
                                boundLeftDown = 8 - row;
                                boundRightUp  = 8 - column;
                            }

                            posCandidate.Clear();
                            for (i = 1; i < boundLeftUp; i++)//左上
                            {
                                thisPos = layout[row - i, column - i];
                                if (thisPos == opponent)
                                {
                                    posCandidate.Add(Position.board[row - i, column - i]);
                                    continue;
                                }
                                else if (thisPos == pieces.blank)
                                {
                                    break;
                                }
                                else if (i != 1)
                                {
                                    flag = true;
                                    break;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            if (flag)
                            {
                                posFlip.AddRange(posCandidate);
                                flag = false;
                            }

                            posCandidate.Clear();
                            for (i = 1; i < boundRightDown; i++)//右下
                            {
                                thisPos = layout[row + i, column + i];
                                if (thisPos == opponent)
                                {
                                    posCandidate.Add(Position.board[row + i, column + i]);
                                    continue;
                                }
                                else if (thisPos == pieces.blank)
                                {
                                    break;
                                }
                                else if (i != 1)
                                {
                                    flag = true;
                                    break;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            if (flag)
                            {
                                posFlip.AddRange(posCandidate);
                                flag = false;
                            }

                            posCandidate.Clear();
                            for (i = 1; i < boundLeftDown; i++)//左下
                            {
                                thisPos = layout[row + i, column - i];
                                if (thisPos == opponent)
                                {
                                    posCandidate.Add(Position.board[row + i, column - i]);
                                    continue;
                                }
                                else if (thisPos == pieces.blank)
                                {
                                    break;
                                }
                                else if (i != 1)
                                {
                                    flag = true;
                                    break;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            if (flag)
                            {
                                posFlip.AddRange(posCandidate);
                                flag = false;
                            }

                            posCandidate.Clear();
                            for (i = 1; i < boundRightUp; i++)//右上
                            {
                                thisPos = layout[row - i, column + i];
                                if (thisPos == opponent)
                                {
                                    posCandidate.Add(Position.board[row - i, column + i]);
                                    continue;
                                }
                                else if (thisPos == pieces.blank)
                                {
                                    break;
                                }
                                else if (i != 1)
                                {
                                    flag = true;
                                    break;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            if (flag)
                            {
                                posFlip.AddRange(posCandidate);
                                flag = false;
                            }
                            #endregion
                            if (posFlip.Count != 0)
                            {
                                posAble.Add(Position.board[row, column], posFlip);
                            }
                        }
                    }
                }
                b.posAble = posAble;
                return(b.calculated = true);
            }
            return(false);
        }
Пример #15
0
 bool isWhite(pieces piece)
 {
     if (piece == pieces.wKING || piece == pieces.wKNIGHT || piece == pieces.wBISHOP || piece == pieces.wPAWN || piece == pieces.wQUEEN || piece == pieces.wROOK) {
         return true;
     }
     return false;
 }
Пример #16
0
    public bool isLegal(pieces piece, int startX, int startY, int endX, int endY)
    {
        //This isn't a move. Therefore return false;
        if (startX == endX && startY == endY) {
            return false;
        }
        //not on the board
        if (endX > 7 || endY > 7 || endX < 0 || endY < 0) {
            return false;
        }
        //Can't capture own piece
        if (isWhite(board.gameBoard [startX, startY]) == isWhite (board.gameBoard [endX, endY]) && board.gameBoard [endX, endY] != pieces.EMPTY) {
            return false;
        }

        if (piece == pieces.bBISHOP || piece == pieces.wBISHOP) {
            return checkBishop (startX, startY, endX, endY);
        }
        else if (piece == pieces.bKNIGHT || piece == pieces.wKNIGHT) {
            return checkKnight(startX, startY, endX, endY);
        }
        else if (piece == pieces.bROOK || piece == pieces.wROOK) {
            return checkRook(startX, startY, endX, endY);
        }
        else if (piece == pieces.bPAWN || piece == pieces.wPAWN) {
            return checkPawn(startX, startY, endX, endY);
        }
        else if (piece == pieces.bQUEEN || piece == pieces.wQUEEN) {
            return checkRook(startX, startY, endX, endY) || checkBishop(startX, startY, endX, endY);
        }
        else if(piece ==  pieces.bKING || piece == pieces.wKING){
            return checkKing(startX, startY, endX, endY);
        }
        return false;
    }
        public ActionResult Index()
        {
            //ilk önce siyah taşları diz
            listPieces.pieces.Clear();
            int    x1 = 2, x2 = 1;//black pieces
            String color = "Black";

            for (int j = 0; j < 2; j++)
            {
                if (j == 1)    // ikinci olarak beyaz taşları diz
                {
                    x1    = 7; //7.satıra taşları diz
                    x2    = 8; //8.satra taşları diz
                    color = "White";
                }

                for (int i = 1; i <= 8; i++)
                {
                    pieces Pieces = new pieces();//pawn
                    Pieces.pawn.col   = i;
                    Pieces.pawn.id    = i.ToString() + color + Pieces.pawn.name;
                    Pieces.pawn.row   = x1;
                    Pieces.pawn.color = color;
                    listPieces.pieces.Add(Pieces.pawn);

                    switch (i)
                    {
                    case 1:    //rook
                    {
                        Pieces            = new pieces();
                        Pieces.rook.id    = i.ToString() + color + Pieces.rook.name;
                        Pieces.rook.col   = i;
                        Pieces.rook.row   = x2;
                        Pieces.rook.color = color;
                        listPieces.pieces.Add(Pieces.rook);
                        break;
                    }

                    case 2:    //knight
                    {
                        Pieces              = new pieces();
                        Pieces.knight.id    = i.ToString() + color + Pieces.knight.name;
                        Pieces.knight.col   = i;
                        Pieces.knight.row   = x2;
                        Pieces.knight.color = color;
                        listPieces.pieces.Add(Pieces.knight); break;
                    }

                    case 3:    //bishop
                    {
                        Pieces              = new pieces();
                        Pieces.bishop.id    = i.ToString() + color + Pieces.bishop.name;
                        Pieces.bishop.col   = i;
                        Pieces.bishop.row   = x2;
                        Pieces.bishop.color = color;
                        listPieces.pieces.Add(Pieces.bishop); break;
                    }

                    case 4:    //queen
                    {
                        Pieces             = new pieces();
                        Pieces.queen.id    = i.ToString() + color + Pieces.queen.name;
                        Pieces.queen.col   = i;
                        Pieces.queen.row   = x2;
                        Pieces.queen.color = color;
                        listPieces.pieces.Add(Pieces.queen); break;
                    }

                    case 5:    //king
                    {
                        Pieces            = new pieces();
                        Pieces.king.id    = i.ToString() + color + Pieces.king.name;
                        Pieces.king.col   = i;
                        Pieces.king.row   = x2;
                        Pieces.king.color = color;
                        listPieces.pieces.Add(Pieces.king); break;
                    }

                    case 6:    //bishop
                    {
                        Pieces              = new pieces();
                        Pieces.bishop.id    = i.ToString() + color + Pieces.bishop.name;
                        Pieces.bishop.col   = i;
                        Pieces.bishop.row   = x2;
                        Pieces.bishop.color = color;
                        listPieces.pieces.Add(Pieces.bishop); break;
                    }

                    case 7:    //knight
                    {
                        Pieces              = new pieces();
                        Pieces.knight.id    = i.ToString() + color + Pieces.knight.name;
                        Pieces.knight.col   = i;
                        Pieces.knight.row   = x2;
                        Pieces.knight.color = color;
                        listPieces.pieces.Add(Pieces.knight); break;
                    }

                    case 8:    //rook
                    {
                        Pieces            = new pieces();
                        Pieces.rook.id    = i.ToString() + color + Pieces.rook.name;
                        Pieces.rook.col   = i;
                        Pieces.rook.row   = x2;
                        Pieces.rook.color = color;
                        listPieces.pieces.Add(Pieces.rook); break;
                    }
                    }
                }
            }

            //foreach (var pieces in listPieces.pieces)
            //{
            //    coorhistory.Add(new ColRowHistory
            //    {
            //        id = pieces.id,
            //        col = pieces.col,
            //        row = pieces.row
            //    });
            //}

            return(View(listPieces));
        }