Question
Prompt: Based on your initial findings, provide five screenshots of pieces of the code and describe what is being done (for example, where there is
Prompt: Based on your initial findings, provide five screenshots of pieces of the code and describe what is being done (for example, where there is and there is not programming related to AI). If you were to modify the code in order to make any improvements, what are possible proposals and why? Please substantiate your answer.
#### #handles the setup of the board (i.e. piece placement) #### def SetupBoard(s): while s.state == 'CustomSetup': s.Click() if s.state == 'Play': s.Play() #### #handles the general play of the game #### def Play(s): while s.state == 'Play': #+-added if statement if s.is1P and s.compIsColour == s.pTurn: s.CompTurn() else: s.Click() if s.state == 'CustomSetup': s.SetupBoard() #### #+-added to be able to control the computer's turn #### def CompTurn(s): s.moves = s.movesAvailable() s.badMoves = [] #######consider making s.goodMoves which replaces s.badMoves for many uses (changes conditions a bit as well)
#To prevent leaving the back row (currently top priority) for move in s.moves: if s.movesFromBack(move): s.badMoves.append(move) s.removeBadMoves()
#This may not always work, it is not expected/designed to (hopefully it usually works) #It is meant to promote the use of moves that allow capture of another piece afterwards for move in s.moves: if not s.PieceCanCapture(s.moveEndsAt(move)[0],s.moveEndsAt(move)[1]): s.badMoves.append(move) s.removeBadMoves()
#Promotes move which make kings for move in s.moves: if not (((s.moveEndsAt(move)[1]==7 and s.tiles[move[0]][move[1]].isWhite) or \ (s.moveEndsAt(move)[1]==0 and s.tiles[move[0]][move[1]].isBlack)) and \ s.tiles[move[0]][move[1]].isPawn): s.badMoves.append(move) s.removeBadMoves()
#Promotes moves which take kings for free for move in s.moves: if not ((s.tiles[move[2]][move[3]].isKing) and s.isMoveSafe(move)): s.badMoves.append(move) s.removeBadMoves()
#This may not always work, it is not expected/designed to (hopefully it usually works) #Promotes moves which trade your pawn for opponent king for move in s.moves: if not ((s.tiles[move[0]][move[1]].isPawn) and (s.tiles[move[2]][move[3]].isKing) and not s.isMoveSafe(move)): s.badMoves.append(move) s.removeBadMoves()
#This may not always work, it is not expected/designed to (hopefully it usually works) #Promotes moves which trade kings when ahead in ***pieces <--might want to change classification of being ahead for move in s.moves: if not ((s.tiles[move[0]][move[1]].isKing) and (s.tiles[move[2]][move[3]].isKing) and not s.isMoveSafe(move) and s.hasMorePieces()): s.badMoves.append(move) s.removeBadMoves()
#This may not always work, it is not expected/designed to (hopefully it usually works) #Promotes moves which trade pawns when ahead in ***pieces <--might want to change classification of being ahead for move in s.moves: if not ((s.tiles[move[0]][move[1]].isPawn) and (s.tiles[move[2]][move[3]].isPawn) and not s.isMoveSafe(move) and s.hasMorePieces()): s.badMoves.append(move) s.removeBadMoves()
#Promotes moves which does not endanger itself needlessly for move in s.moves: if not s.isMoveSafe(move): s.badMoves.append(move) s.removeBadMoves() #should implement the next part to pick at random from the remaining moves (does not do this) #performs the select and move action for the computer m = randrange(0,len(s.moves)) if s.selectedTileAt == []: s.Action(s.moves[m][0],s.moves[m][1]) s.Action(s.moves[m][2],s.moves[m][3])
#### #+-added to determine whether the player whose turn it is has more pieces than opponent #### def hasMorePieces(s): return s.numColour(s.pTurn) > s.numColour(s.opposite(s.pTurn)) #+-added new method #Returns true if a GIVEN piece cannot be taken immediately after ITS proposed move #########Might want another method to know if a move exposes another piece #Likely to not work (depends on how PieceCanCapturePiece method works) def isMoveSafe(s,move): X1,Y1 = [s.moveEndsAt(move)[0]-1,s.moveEndsAt(move)[0]+1],[s.moveEndsAt(move)[1]-1,s.moveEndsAt(move)[1]+1] for i in range(2): for j in range(2): if s.SpecialPCCP(s.tiles[move[0]][move[1]].pieceColour,X1[i],Y1[j],s.moveEndsAt(move)[0],s.moveEndsAt(move)[1],move[0],move[1]): return False return True
#+-added new method #modification to PieceCanCapturePiece such that it works with the isMoveSafe method def SpecialPCCP(s,piece2Colour,x,y,X,Y,initX,initY):
X1,X2,Y1,Y2 = [x-1,x+1],[x-2,x+2],[y-1,y+1],[y-2,y+2] if ((0<=X<8) and (0<=Y<8)) and ((0<=x<8) and (0<=y<8)): if (piece2Colour == s.opposite(s.tiles[x][y].pieceColour)): if s.CanDoWalk(x,y,X,Y,exception=False): for i in range(2): for j in range(2): if X1[i]==X and Y1[j]==Y: if (0<=X2[i]<8) and (0<=Y2[j]<8): if not (s.tiles[X2[i]][Y2[j]].isPiece) or (X2[i]==initX and Y2[j]==initY): return True return False
#+-added new method #Removes badMoves from the list of moves that can be made #if all of the moves that can be made are badMoves, we still need to do something so it leaves all of them #Note: at any time this is run, the moves that are considered bad should be considered bad for the same reason (i.e. they are equivalently bad) def removeBadMoves(s): #assumes moves were added to badMoves in the same order as they appear in moves if s.moves != s.badMoves: for move in s.badMoves: s.moves.remove(move) s.badMoves = []
#+-added new method def movesFromBack(s,move): if (move[1]==0 and s.compIsColour=='White') or \ (move[1]==7 and s.compIsColour=='Black'): return True else: return False
#+-added new method def moveEndsAt(s,move): #takes 4 element array representing a move if s.tiles[move[2]][move[3]].isPiece: return [move[0]+(move[2]-move[0])*2,move[1]+(move[3]-move[1])*2] else: return [move[2],move[3]] #+-added to calculate all the available valid moves def movesAvailable(s): moves=[] for j in range(8): for i in range(8): X1,Y1 = [i-1,i+1],[j-1,j+1] for a in range(2): for b in range(2): if 0<=X1[a]<8 and 0<=Y1[b]<8: if s.moveIsValid(i,j,X1[a],Y1[b]): moves.append([i,j,X1[a],Y1[b]]) return moves
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started