jaedirectory.blogg.se

C program for chess board
C program for chess board







C PROGRAM FOR CHESS BOARD CODE

That’s why let’s code an interface, clean up our code, and then test our AI again. The above testing seems to be a lot of code. Let's summarize our evaluation function right now, with the help of the flowchart given below: (As, favorable conditions for White = unfavorable conditions for Black) eval = material + pawnsq + knightsq + bishopsq + rooksq + queensq + kingsq if board.turn: return eval else: return -eval material = 100 * (wp - bp) + 320 * (wn - bn) + 330 * (wb - bb) + 500 * (wr - br) + 900 * (wq - bq) pawnsq = sum( for i in board.pieces(chess.PAWN, chess.WHITE)]) pawnsq = pawnsq + sum( for i in board.pieces(chess.PAWN, chess.BLACK)]) knightsq = sum( for i in board.pieces(chess.KNIGHT, chess.WHITE)]) knightsq = knightsq + sum( for i in board.pieces(chess.KNIGHT, chess.BLACK)]) bishopsq = sum( for i in board.pieces(chess.BISHOP, chess.WHITE)]) bishopsq = bishopsq + sum( for i in board.pieces(chess.BISHOP, chess.BLACK)]) rooksq = sum( for i in board.pieces(chess.ROOK, chess.WHITE)]) rooksq = rooksq + sum( for i in board.pieces(chess.ROOK, chess.BLACK)]) queensq = sum( for i in board.pieces(chess.QUEEN, chess.WHITE)]) queensq = queensq + sum( for i in board.pieces(chess.QUEEN, chess.BLACK)]) kingsq = sum( for i in board.pieces(chess.KING, chess.WHITE)]) kingsq = kingsq + sum( for i in board.pieces(chess.KING, chess.BLACK)])Īt last, let’s calculate the evaluation function which will return the summation of the material scores and the individual scores for white and when it comes for black, let’s negate it. The individual pieces score is the sum of piece-square values of positions where the respective piece is present at that instance of the game. The material score is calculated by the summation of all respective piece’s weights multiplied by the difference between the number of that respective piece between white and black. wp = len(board.pieces(chess.PAWN, chess.WHITE)) bp = len(board.pieces(chess.PAWN, chess.BLACK)) wn = len(board.pieces(chess.KNIGHT, chess.WHITE)) bn = len(board.pieces(chess.KNIGHT, chess.BLACK)) wb = len(board.pieces(chess.BISHOP, chess.WHITE)) bb = len(board.pieces(chess.BISHOP, chess.BLACK)) wr = len(board.pieces(chess.ROOK, chess.WHITE)) br = len(board.pieces(chess.ROOK, chess.BLACK)) wq = len(board.pieces(chess.QUEEN, chess.WHITE)) bq = len(board.pieces(chess.QUEEN, chess.BLACK)) Secondly, we must calculate the total number of pieces so that we can pass it into our material function. So, let’s code: if board.is_checkmate(): if board.turn: return -9999 else: return 9999 if board.is_stalemate(): return 0 if board.is_insufficient_material(): return 0 For stalemate or any insufficient material, let’s return 0 as its draw. Now the logic behind coding this stage is that If it returns true at checkmate then it will check whose turn to make a move, if the current turn is of WHITE it must return -9999 i.e previous turn must be of BLACK, and BLACK wins or else it must return +9999 and then White wins. Now let’s get our evaluation function by following these four methods:įirstly, let’s check if the game is still going on. Let’s take another example, suppose the Queen, she would like her to be placed at the center position as she can dominate more number of positions from the center and therefore we will set higher values at the center and the same for the other pieces as chess is all about defending the king and dominating the center.Įnough of the theory lets initialize the piece square tables: pawntable = knightstable = bishopstable = rookstable = queenstable = kingstable = We will use piece square tables to evaluate our board pieces and the values will be set in an 8x8 matrix such as in chess such that it must have a higher value at favorable positions and a lower value at a non-favorable place.įor example, the probability of the white’s king crossing the centerline will be less than 20% and therefore we will place negative values in that matrix.







C program for chess board