using Python chess setup and rules with AI for game playing
Budget: $10 – $30 USD
Fill the code in the code structure provided below for the ChessBoard. The main use of this code block write functions to initialize the board, draw the board, get the board state and move piece. You can add any other functions if needed.
# you can add/change the input parameters for each function
# you can change the function names and also add more functions if needed
def ChessBoardSetup():
# initialize and return a chess board - create a 2D 8x8 array that has the value for each cell
# USE the following characters for the chess pieces - lower-case for BLACK and upper-case for WHITE
# . for empty board cell
# p/P for pawn
# r/R for rook
# t/T for knight
# b/B for bishop
# q/Q for queen
# k/K for king
board = [[],[],[],[],[],[],[],[]]
board[0] = ['r','t','b','q','k','b','t','r']
board[1] = ['p','p','p','p','p','p','p','p']
board[2] = ['.','.','.','.','.','.','.','.']
board[3] = ['.','.','.','.','.','.','.','.']
board[4] = ['.','.','.','.','.','.','.','.']
board[5] = ['.','.','.','.','.','.','.','.']
board[6] = ['P','P','P','P','P','P','P','P']
board[7] = ['R','T','B','Q','K','B','T','R']
return board
def DrawBoard(board):
# write code to print the board - following is one print example
# r t b q k b t r
# p p p p p p p p
# . . . . . . . .
# . . . . . . . .
# . . . . . . . .
# . . . . . . . .
# P P P P P P P P
# R T B Q K B T R
# for row in board:
# print(*row) # asterisk useful while printing to remove comma and bracket to unpacking them
for item in board:
for j in item:
print(j, end = ' ') # inside 2d array and each array ends the output with a space
print()
# import numpy as np
def MovePiece(board, start_pos, end_pos):
# write code to move the one chess piece
# you do not have to worry about the validity of the move - this will be done before calling this function
# this function will at least take the move (from-piece and to-piece) as input and return the new board layout
# board = ChessBoardSetup()
# chessPieceName = np.array(board)
# pawn1BLACK = chessPieceName[1,0]
###########################
# pawn8BLACK = chessPieceName[1,7]
# index = chessPieceName[start_pos]
# return index
new_board = board.copy()
new_board[end_pos[0]] [end_pos[1]] = new_board[start_pos[0]] [start_pos[1]]
new_board[start_pos[0]][start_pos[1]] = '.'
return new_board
board = ChessBoardSetup()
board = MovePiece(board, (1,0), (3,0))
board = MovePiece(board, (0,0), (1,0))
DrawBoard(board)
output print
. t b q k b t r
r p p p p p p p
. . . . . . . .
p . . . . . . .
. . . . . . . .
. . . . . . . .
P P P P P P P P
R T B Q K B T R
# you can add/change the input parameters for each function
# you can change the function names and also add more functions if needed
def ChessBoardSetup():
# initialize and return a chess board - create a 2D 8x8 array that has the value for each cell
# USE the following characters for the chess pieces - lower-case for BLACK and upper-case for WHITE
# . for empty board cell
# p/P for pawn
# r/R for rook
# t/T for knight
# b/B for bishop
# q/Q for queen
# k/K for king
board = [[],[],[],[],[],[],[],[]]
board[0] = ['r','t','b','q','k','b','t','r']
board[1] = ['p','p','p','p','p','p','p','p']
board[2] = ['.','.','.','.','.','.','.','.']
board[3] = ['.','.','.','.','.','.','.','.']
board[4] = ['.','.','.','.','.','.','.','.']
board[5] = ['.','.','.','.','.','.','.','.']
board[6] = ['P','P','P','P','P','P','P','P']
board[7] = ['R','T','B','Q','K','B','T','R']
return board
def DrawBoard(board):
# write code to print the board - following is one print example
# r t b q k b t r
# p p p p p p p p
# . . . . . . . .
# . . . . . . . .
# . . . . . . . .
# . . . . . . . .
# P P P P P P P P
# R T B Q K B T R
# for row in board:
# print(*row) # asterisk useful while printing to remove comma and bracket to unpacking them
for item in board:
for j in item:
print(j, end = ' ') # inside 2d array and each array ends the output with a space
print()
# import numpy as np
def MovePiece(board, start_pos, end_pos):
# write code to move the one chess piece
# you do not have to worry about the validity of the move - this will be done before calling this function
# this function will at least take the move (from-piece and to-piece) as input and return the new board layout
# board = ChessBoardSetup()
# chessPieceName = np.array(board)
# pawn1BLACK = chessPieceName[1,0]
###########################
# pawn8BLACK = chessPieceName[1,7]
# index = chessPieceName[start_pos]
# return index
new_board = board.copy()
new_board[end_pos[0]] [end_pos[1]] = new_board[start_pos[0]] [start_pos[1]]
new_board[start_pos[0]][start_pos[1]] = '.'
return new_board
board = ChessBoardSetup()
board = MovePiece(board, (1,0), (3,0))
board = MovePiece(board, (0,0), (1,0))
DrawBoard(board)
output print
. t b q k b t r
r p p p p p p p
. . . . . . . .
p . . . . . . .
. . . . . . . .
. . . . . . . .
P P P P P P P P
R T B Q K B T R
Related categories:
Python
Software Architecture
Machine Learning (ML)
Artificial Intelligence
Pytorch