AIMove selectCheckBreaker(side s) { if (initial) { ArrayList moves = gimmeMoves(s); checkBreakers = new ArrayList(); foreach (AIMove m in moves) { ChessSquare[,] simulatedBoard = simulateMove(m); AttacksTable t = new AttacksTable(simulatedBoard, (s == side.Black)); if (!chess.checkCheck(simulatedBoard, s, t)) { checkBreakers.Add(m); } } } if (checkBreakers.Count == 0) { return(null); } return((AIMove)checkBreakers [Random.Range(0, checkBreakers.Count - 1)]); }
public void go(ChessSquare[,] b) { selectedMove = null; board = b; state = 1; side s = side.White; AttacksTable t = chess.attacksBlack; if (!chess.isWhiteTurn()) { s = side.Black; t = chess.attacksWhite; } if (chess.checkCheck(b, s, t)) { selectedMove = selectCheckBreaker(s); } else { selectedMove = selectAttack(s, t); if (selectedMove == null) { selectedMove = selectRandom(s); } } if (selectedMove == null) { chess.theEnd(); state = 0; } cooldown = Time.time; }
void changeTurn() { Deselect(); white = !white; attacksWhite = new AttacksTable(board, true); attacksBlack = new AttacksTable(board, false); leftCastling = false; rightCastling = false; promotion = false; /* Check detection */ side s = side.White; AttacksTable t = attacksBlack; if (!white) { s = side.Black; t = attacksWhite; } check = checkCheck(board, s, t); enemyPlaying = false; playing = true; turnCD = Time.time; }
AIMove selectAttack(side s, AttacksTable t) { if (initial) { ArrayList moves = gimmeMoves(s); attackMoves = new ArrayList(); foreach (AIMove m in moves) { if (m.eatenValue != 0) { attackMoves.Add(m); } } } AIMove move = selectMoveAttack(selectMovesOfRisk(attackMoves, 5)); if (move == null) { move = selectMoveAttack(selectAttacksDefensive(selectMovesOfRisk(attackMoves, 4))); } if (move == null) { move = selectMoveAttack(selectAttacksDefensive(selectMovesOfRisk(attackMoves, 3))); } return(move); }
public bool checkCheck(ChessSquare[,] b, side s, AttacksTable t) { for (int i = 2; i < 10; i++) { for (int j = 2; j < 10; j++) { if (b[i, j].pieceSquare == piece.King && b[i, j].sideSquare == s && t.isAttacked(i, j)) { return(true); } } } return(false); }
void Start() { //纹理初始化------ WhitePawn = Resources.Load <Texture2D>("Icons/WhitePawn"); WhiteRook = Resources.Load <Texture2D>("Icons/WhiteRook"); WhiteBishop = Resources.Load <Texture2D>("Icons/WhiteBishop"); WhiteKnight = Resources.Load <Texture2D>("Icons/WhiteKnight"); WhiteQueen = Resources.Load <Texture2D>("Icons/WhiteQueen"); WhiteKing = Resources.Load <Texture2D>("Icons/WhiteKing"); BlackKing = Resources.Load <Texture2D>("Icons/BlackKing"); BlackQueen = Resources.Load <Texture2D>("Icons/BlackQueen"); BlackPawn = Resources.Load <Texture2D>("Icons/BlackPawn"); BlackRook = Resources.Load <Texture2D>("Icons/BlackRook"); BlackBishop = Resources.Load <Texture2D>("Icons/BlackBishop"); BlackKnight = Resources.Load <Texture2D>("Icons/BlackKnight"); blackTex = Resources.Load <Texture2D>("BlackSquare"); whiteTex = Resources.Load <Texture2D>("WhiteSquare"); //------ newBoard(); instantiateBoard(); attacksWhite = new AttacksTable(board, true); attacksBlack = new AttacksTable(board, false); leftCastling = false; rightCastling = false; check = false; promotion = false; playing = true; fin = false; checkedCooldown = Time.time - 4f; history = new ArrayList(); enemy = GameObject.FindObjectOfType <ChessAI>(); white = true; enemyPlaying = false; turnCD = Time.time; starting = true; }
ArrayList selectAttacksDefensive(ArrayList list) { AttacksTable t = chess.attacksBlack; if (chess.isWhiteTurn()) { t = chess.attacksWhite; } ArrayList aux = new ArrayList(); foreach (AIMove mov in list) { if (mov.eatenValue >= mov.movedValue || t.numAttacking(mov.finalPos) > 2) { aux.Add(mov); } } return(aux); }
public void lightAttackers(int i, int j, bool light) { AttacksTable t = attacksBlack; if (!white) { t = attacksWhite; } if (light) { paintPiece(i, j, board[selectedI, selectedJ].pieceSquare, board[selectedI, selectedJ].sideSquare); transPiece(i, j); for (int x = 2; x < 10; x++) { for (int y = 2; y < 10; y++) { if (t.attacks[i, j, x, y] != piece.None) { lightPiece(x, y); } } } } else { paintPiece(i, j); for (int x = 2; x < 10; x++) { for (int y = 2; y < 10; y++) { if (t.attacks[i, j, x, y] != piece.None) { showPiece(x, y); } } } } }
ArrayList selectMovesDefensive(ArrayList list) { AttacksTable mine = chess.attacksBlack; AttacksTable their = chess.attacksWhite; if (chess.isWhiteTurn()) { mine = chess.attacksWhite; their = chess.attacksBlack; } ArrayList aux = new ArrayList(); foreach (AIMove mov in list) { if (their.numAttacking(mov.finalPos) < mine.numAttacking(mov.finalPos)) { aux.Add(mov); } } return(aux); }
//重置游戏 public void Reset() { resetBoard(); attacksWhite = new AttacksTable(board, true); attacksBlack = new AttacksTable(board, false); leftCastling = false; rightCastling = false; check = false; promotion = false; playing = true; fin = false; checkedCooldown = Time.time - 4f; history = new ArrayList(); white = true; enemyPlaying = false; turnCD = Time.time; starting = true; }
AIMove gimmeMove(int i, int j, int x, int y) { AttacksTable t = chess.attacksBlack; if (!chess.isWhiteTurn()) { t = chess.attacksWhite; } AIMove move = new AIMove(); move.initialPos = new Vector2(i, j); move.finalPos = new Vector2(x, y); move.movedValue = gimmeValue(board [i, j].pieceSquare); move.eatenValue = gimmeValue(board [x, y].pieceSquare); move.risk = 5 - t.numAttacking(x, y); if (move.risk < 0) { move.risk = 0; } return(move); }
public bool select(int i, int j) { Deselect(); if (board[i, j].pieceSquare == piece.None || board[i, j].pieceSquare == piece.OutOfBoard) { return(false); } if (board[i, j].sideSquare == side.Black && white) { return(false); } if (board[i, j].sideSquare == side.White && !white) { return(false); } selectedJ = j; selectedI = i; board[i, j].obj.GetComponent <Bouncy>().bounce(); switch (board[i, j].pieceSquare) { case piece.Bishop: lightBishop(i, j); break; case piece.King: lightKing(i, j); break; case piece.Rook: lightRook(i, j); break; case piece.Queen: lightRook(i, j); lightBishop(i, j); break; case piece.Knight: lightKnight(i, j); break; case piece.Pawn: lightPawn(i, j); break; default: break; } /* Comprobacion Castling */ AttacksTable t = attacksWhite; if (board[i, j].sideSquare == side.White) { t = attacksBlack; } if (board[i, j].pieceSquare == piece.King && board[i, j].moved == false && !t.isAttacked(i, j)) { leftCastling = true; if (board[i, 2].pieceSquare != piece.Rook || board[i, 2].moved != false) { leftCastling = false; } for (int x = 3; x < j; x++) { if (board[i, x].pieceSquare != piece.None || t.isAttacked(i, x)) { leftCastling = false; } } rightCastling = true; if (board[i, 9].pieceSquare != piece.Rook || board[i, 9].moved != false) { rightCastling = false; } for (int x = 8; x > j; x--) { if (board[i, x].pieceSquare != piece.None || t.isAttacked(i, x)) { rightCastling = false; } } } return(true); }
public bool move(int i, int j) { ChessMove move = new ChessMove(); move.castling = false; move.initialPos = new Vector2(selectedI, selectedJ); move.finalPos = new Vector2(i, j); move.eatenPiece = board[i, j].pieceSquare; move.moved = board[selectedI, selectedJ].moved; move.eatenSide = board[i, j].sideSquare; move.eatenMoved = board[i, j].moved; move.movingPiece = board[selectedI, selectedJ].pieceSquare; //把数据赋值 ChessSquare oldInitial = board[selectedI, selectedJ].Clone(); ChessSquare oldNext = board[i, j].Clone(); //复制的数据记录 board[i, j].sideSquare = board[selectedI, selectedJ].sideSquare; board[i, j].pieceSquare = board[selectedI, selectedJ].pieceSquare; board[i, j].moved = true; //旧的棋子位置需要更新状态,也就是格式化 board[selectedI, selectedJ].sideSquare = side.None; board[selectedI, selectedJ].pieceSquare = piece.None; board[selectedI, selectedJ].moved = true; //检查是否可以移动 AttacksTable t = new AttacksTable(board, !white); if (checkCheck(board, oldInitial.sideSquare, t)) { board[i, j].sideSquare = oldNext.sideSquare; board[i, j].pieceSquare = oldNext.pieceSquare; board[i, j].moved = oldNext.moved; board[selectedI, selectedJ].sideSquare = oldInitial.sideSquare; board[selectedI, selectedJ].pieceSquare = oldInitial.pieceSquare; board[selectedI, selectedJ].moved = oldInitial.moved; checkedCooldown = Time.time; return(false); } //棋子移动以及棋子移动后的状态调整 board[i, j].obj.GetComponent <Bouncy>().stopBounce(); //棋子动画停止 paintPiece(i, j); //棋子移动,具体操作将棋子的贴图赋给选择的棋盘位置上的棋子 showPiece(i, j); //让选择的棋盘为指导上的棋子显示出来 hidePiece(selectedI, selectedJ); //棋子移动后,原位置上的棋子隐藏 if (board[i, j].pieceSquare == piece.Pawn && (i == 9 || i == 2)) { selectedI = i; selectedJ = j; promotion = true; playing = false; Deselect(); board[i, j].square.GetComponent <Square>().range(); } else { changeTurn(); if (oldNext.pieceSquare == piece.King) { theEnd(); } } history.Add(move); return(true); }
public bool move(int i, int j) { ChessMove move = new ChessMove(); move.castling = false; move.initialPos = new Vector2(selectedI, selectedJ); move.finalPos = new Vector2(i, j); move.eatenPiece = board [i, j].pieceSquare; move.moved = board [selectedI, selectedJ].moved; move.eatenSide = board [i, j].sideSquare; move.eatenMoved = board [i, j].moved; move.movingPiece = board [selectedI, selectedJ].pieceSquare; /* Guardamos los datos de las 2 */ ChessSquare oldInitial = board [selectedI, selectedJ].Clone(); ChessSquare oldNext = board [i, j].Clone(); /* Se copian los datos de la ficha */ board [i, j].sideSquare = board [selectedI, selectedJ].sideSquare; board [i, j].pieceSquare = board [selectedI, selectedJ].pieceSquare; board [i, j].moved = true; /* Se formatea la antigua celda */ board [selectedI, selectedJ].sideSquare = side.None; board [selectedI, selectedJ].pieceSquare = piece.None; board [selectedI, selectedJ].moved = true; /* Comprobamos futuro jaque. Restablecemos si es invalido el movimiento. */ AttacksTable t = new AttacksTable(board, !white); if (checkCheck(board, oldInitial.sideSquare, t)) { board [i, j].sideSquare = oldNext.sideSquare; board [i, j].pieceSquare = oldNext.pieceSquare; board [i, j].moved = oldNext.moved; board [selectedI, selectedJ].sideSquare = oldInitial.sideSquare; board [selectedI, selectedJ].pieceSquare = oldInitial.pieceSquare; board [selectedI, selectedJ].moved = oldInitial.moved; checkedCooldown = Time.time; return(false); } /* Se paran los botecitos y se desplaza la ficha */ board [i, j].obj.GetComponent <Bouncy>().stopBounce(); paintPiece(i, j); showPiece(i, j); hidePiece(selectedI, selectedJ); /* Se comprueba coronamiento y se efectua */ if (board [i, j].pieceSquare == piece.Pawn && (i == 9 || i == 2)) { selectedI = i; selectedJ = j; promotion = true; playing = false; Deselect(); board [i, j].square.GetComponent <Square>().range(); } else { changeTurn(); if (oldNext.pieceSquare == piece.King) { theEnd(); } } history.Add(move); return(true); }