import textwrap class ChessMaster: def __init__(self): userInput = self.fromString() global boardString boardString = userInput.split(',') self.boardString = boardString for i in range(0,len(boardString)): for j in range(0,len(boardString[i]),2): print "|", boardString[i][j:j+2], print "|\n" def fromString(self): return raw_input("Here is the Chess Board: \n") def returnWhiteKing(self): for i in range(0,len(boardString)): for j in range(0,len(boardString[i]),2): if boardString[i][j:j+2] == 'kl': return i,j def IsWhiteKingChecked(self, i,j): #print i,j r = 0 p = 1 q = 2 if boardString[i+p][j+q*2:j+q*2+2] == 'nd' or boardString[i+q][j+p*2:j+p*2+2] == 'nd' or \ boardString[i-p][j+q*2:j+q*2+2] == 'nd' or boardString[i+p][j-q*2:j-q*2+2] == 'nd' or \ boardString[i+q][j-p*2:j-p*2+2] == 'nd' or boardString[i-q][j+p*2:j+p*2+2] == 'nd' or \ boardString[i-p][j-q*2:j-q*2+2] == 'nd' or boardString[i-q][j-p*2:j-p*2+2] == 'nd' : checked = True elif boardString[i-p][j-r*2:j-r*2+2] == 'kd' or boardString[i+p][j-r*2:j-r*2+2] == 'kd' or \ boardString[i-p][j-p*2:j-p*2+2] == 'kd' or boardString[i+p][j-p*2:j-p*2+2] == 'kd' or \ boardString[i-p][j+p*2:j+p*2+2] == 'kd' or boardString[i+p][j+p*2:j+p*2+2] == 'kd' or \ boardString[i+r][j+p*2:j+p*2+2] == 'kd' or boardString[i+r][j-p*2:j-p*2+2] == 'kd' : checked = True else: checked = False return checked def isWhiteInCheck(self): i,j = self.returnWhiteKing() #print i,j result = self.IsWhiteKingChecked(i,j) if result : print "isWhiteInCheck: Yes" else: print "isWhiteInCheck: No" return result def assistCheckMate(self,i,j): r = 0; p = 1; temp = 0 if self.IsWhiteKingChecked(i, j) : temp +=1 if self.IsWhiteKingChecked(i-p, j-r*2) : temp +=1 else : i1 = i-p; j1 = j-r*2 if self.IsWhiteKingChecked(i+p, j-r*2) : temp +=1 else : i1 = i+p; j1 = j-r*2 if self.IsWhiteKingChecked(i-p, j-p*2) : temp +=1 else : i1 = i-p; j1 = j-p*2 if self.IsWhiteKingChecked(i+p, j-p*2) : temp +=1 if self.IsWhiteKingChecked(i-p, j+p*2) : temp +=1 if self.IsWhiteKingChecked(i+p, j+p*2) : temp +=1 if self.IsWhiteKingChecked(i+r, j+p*2) : temp +=1 if self.IsWhiteKingChecked(i+r, j-p*2) : temp +=1 return temp def isWhiteInCheckMate(self): i,j = self.returnWhiteKing() temp = self.assistCheckMate(i,j) print temp if temp == 9: print "isWhiteInCheckMate: Yes" return True else: print "isWhiteInCheckMate: No" return False def canBlackCheckMateInOneMove(self): i,j = self.returnWhiteKing() if self.assistCheckMate(i,j) == 8: print "canBlackCheckMateInOneMove: Yes" return True else: print "canBlackCheckMateInOneMove: No" return False obj = ChessMaster() obj.isWhiteInCheck() obj.isWhiteInCheckMate() obj.canBlackCheckMateInOneMove()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question