def legalMovesKing( pos, piece ): legal = [] # A bit modified, no breaks for i in range( -1, 2 ): # range is REALLY F****D UP for j in range( -1, 2 ): newPos = [ pos[0]+i, pos[1]+j ] if not posLegal( newPos ): continue if boardIndex( newPos ) == " ": legal.append( newPos ) continue # This means there is a piece on the new pos if boardIndex( newPos )[1] != piece[1]: # Different color => enemy legal.append( newPos ) else: # Friendly, not allowed continue # More processing needed, king is not allowed to check himself. #F**k this shit # for iter, move in enumerate( legal ): # for i in range( 0, 8 ): # for j in range( 0, 8 ): # enemyPos = [ i, j ] # if boardIndex( enemyPos ) != " " and boardIndex( enemyPos )[1] != piece[1]: # Enemy # for enemyMove in legalMoves( enemyPos ): # if boardIndex( enemyPos )[0] == pieces["pawn"]: # if enemyPos[1] == enemyMove[1]: # continue # Pawns are only a threat diagonally # if move == enemyMove: # legal.pop( iter ) return legal
def isKingCheck( color ): kingPos = board.findKing( color ) for i in range( 0, 8 ): for j in range( 0, 8 ): enemyPos = [ i, j ] enemy = boardIndex( enemyPos ) if enemy == " " or enemy[1] == color: # Empty or friend continue for enemyMove in legalMoves( enemyPos ): if enemy[0] == pieces["pawn"]: if enemyPos[1] == enemyMove[1]: continue # Pawns are only a threat diagonally if enemyMove == kingPos: return True return False
def movePiece( pos, to ): if not posLegal( to ): raise exceptions.IllegalPositionException allowed = False for move in legalMoves( pos ): if move == to: allowed = True break if not allowed: raise exceptions.IllegalMoveException boardCopy = board.getBoardCopy() board.movePiece( pos, to ) if isKingCheck( boardIndex( to )[1] ): board.setBoard( boardCopy ) # Revert move raise exceptions.SelfCheckException # Cannot check self
def legalMoves( pos ): piece = boardIndex( pos ) legal = [] if piece[0] == pieces["pawn"]: # Pawn is a s***e to code legal = legalMovesPawn( pos, piece ) elif piece[0] == pieces["rook"]: # Ez legal = legalMovesRook( pos, piece ) elif piece[0] == pieces["bishop"]: # Copypaste legal = legalMovesBishop( pos, piece ) elif piece[0] == pieces["queen"]: # Hax hax # Queen is equal to bishop and rook, so DRY! legal = legalMovesQueen( pos, piece ) elif piece[0] == pieces["king"]: legal = legalMovesKing( pos, piece ) elif piece[0] == pieces["knight"]: legal = legalMovesKnight( pos, piece ) return legal
def legalMovesRook( pos, piece ): legal = [] # Just brute force it, once for each direction for i in range( 1, 8 ): newPos = [ pos[0]-i, pos[1] ] if not posLegal( newPos ): break if boardIndex( newPos ) == " ": legal.append( newPos ) continue # This means there is a piece on the new pos if boardIndex( newPos )[1] != piece[1]: # Different color => enemy legal.append( newPos ) break # Since we can't move through pieces else: # Friendly, not allowed break for i in range( 1, 8 ): newPos = [ pos[0]+i, pos[1] ] if not posLegal( newPos ): break if boardIndex( newPos ) == " ": legal.append( newPos ) continue # This means there is a piece on the new pos if boardIndex( newPos )[1] != piece[1]: # Different color => enemy legal.append( newPos ) break # Since we can't move through pieces else: # Friendly, not allowed break for i in range( 1, 8 ): newPos = [ pos[0], pos[1]-i ] if not posLegal( newPos ): break if boardIndex( newPos ) == " ": legal.append( newPos ) continue # This means there is a piece on the new pos if boardIndex( newPos )[1] != piece[1]: # Different color => enemy legal.append( newPos ) break # Since we can't move through pieces else: # Friendly, not allowed break for i in range( 1, 8 ): newPos = [ pos[0], pos[1]+i ] if not posLegal( newPos ): break if boardIndex( newPos ) == " ": legal.append( newPos ) continue # This means there is a piece on the new pos if boardIndex( newPos )[1] != piece[1]: # Different color => enemy legal.append( newPos ) break # Since we can't move through pieces else: # Friendly, not allowed break return legal
def legalMovesPawn( pos, piece ): legal = [] if piece[1] == colors["white"]: # Can move two spaces first move if( pos[0] == 6 ): newPos = [ pos[0]-2, pos[1] ] if posLegal( newPos ): if boardIndex( newPos ) == " ": legal.append( newPos ) newPos = [ pos[0]-1, pos[1] ] if posLegal( newPos ): if boardIndex( newPos ) == " ": legal.append( newPos ) newPos = [ pos[0]-1, pos[1]-1 ] if posLegal( newPos ): if boardIndex( newPos ) != " " and boardIndex( newPos )[1] != piece[1]: legal.append( newPos ) newPos = [ pos[0]-1, pos[1]+1 ] if posLegal( newPos ): if boardIndex( newPos ) != " " and boardIndex( newPos )[1] != piece[1]: legal.append( newPos ) else: #Black # Can move two spaces first move if( pos[0] == 1 ): newPos = [ pos[0]+2, pos[1] ] if posLegal( newPos ): if boardIndex( newPos ) == " ": legal.append( newPos ) newPos = [ pos[0]+1, pos[1] ] if posLegal( newPos ): if boardIndex( newPos ) == " ": legal.append( newPos ) newPos = [ pos[0]+1, pos[1]-1 ] if posLegal( newPos ): if boardIndex( newPos ) != " " and boardIndex( newPos )[1] != piece[1]: legal.append( newPos ) newPos = [ pos[0]+1, pos[1]+1 ] if posLegal( newPos ): if boardIndex( newPos ) != " " and boardIndex( newPos )[1] != piece[1]: legal.append( newPos ) return legal
def legalMovesKnight( pos, piece ): legal = [] # Funny little hopper newPos = [ pos[0]-2, pos[1]-1 ] if posLegal( newPos ): if boardIndex( newPos ) == " ": legal.append( newPos ) else: # This means there is a piece on the new pos if boardIndex( newPos )[1] != piece[1]: # Different color => enemy legal.append( newPos ) else: # Friendly, not allowed pass newPos = [ pos[0]-2, pos[1]+1 ] if posLegal( newPos ): if boardIndex( newPos ) == " ": legal.append( newPos ) else: # This means there is a piece on the new pos if boardIndex( newPos )[1] != piece[1]: # Different color => enemy legal.append( newPos ) else: # Friendly, not allowed pass newPos = [ pos[0]+2, pos[1]-1 ] if posLegal( newPos ): if boardIndex( newPos ) == " ": legal.append( newPos ) else: # This means there is a piece on the new pos if boardIndex( newPos )[1] != piece[1]: # Different color => enemy legal.append( newPos ) else: # Friendly, not allowed pass newPos = [ pos[0]+2, pos[1]+1 ] if posLegal( newPos ): if boardIndex( newPos ) == " ": legal.append( newPos ) else: # This means there is a piece on the new pos if boardIndex( newPos )[1] != piece[1]: # Different color => enemy legal.append( newPos ) else: # Friendly, not allowed pass newPos = [ pos[0]-1, pos[1]-2 ] if posLegal( newPos ): if boardIndex( newPos ) == " ": legal.append( newPos ) else: # This means there is a piece on the new pos if boardIndex( newPos )[1] != piece[1]: # Different color => enemy legal.append( newPos ) else: # Friendly, not allowed pass newPos = [ pos[0]-1, pos[1]+2 ] if posLegal( newPos ): if boardIndex( newPos ) == " ": legal.append( newPos ) else: # This means there is a piece on the new pos if boardIndex( newPos )[1] != piece[1]: # Different color => enemy legal.append( newPos ) else: # Friendly, not allowed pass newPos = [ pos[0]+1, pos[1]-2 ] if posLegal( newPos ): if boardIndex( newPos ) == " ": legal.append( newPos ) else: # This means there is a piece on the new pos if boardIndex( newPos )[1] != piece[1]: # Different color => enemy legal.append( newPos ) else: # Friendly, not allowed pass newPos = [ pos[0]+1, pos[1]+2 ] if posLegal( newPos ): if boardIndex( newPos ) == " ": legal.append( newPos ) else: # This means there is a piece on the new pos if boardIndex( newPos )[1] != piece[1]: # Different color => enemy legal.append( newPos ) else: # Friendly, not allowed pass return legal
def main(): while( True ): printing.clearScreen() print( "Velkommen til Knuts sjakk-simulator 3000!" ) print( "Vil du spille et parti sjakk?" ) a = input() if a == "": continue if a[0].lower() != "j" and a[0].lower() != "y": print( "Du kommer i alle fall ikke å bli verdensmester hvis du ikke en gang vil prøve!" ) exit() else: break printing.clearScreen() print( "Hvit og svart spiller annenhver tur. Hvit begynner." ) print( "Du flytter ved å skrive brikkeposisjon og den du vil flytte til." ) print( "Ex. \"b7b5\" flytter bonden på b7 til b5." ) print( "Trykk enter for å starte!" ) input() # YEEEAH #printing.printBoard() whoseTurn = colors["white"] while( True ): printing.printBoard() if whoseTurn == colors["white"]: if isKingMate( colors["white"] ): printing.print_red( "HVIT ER SJAKK MATT! GRATULERER, SVART!" ) break elif isKingCheck( colors["white"] ): printing.print_red( "Hvits konge er satt i sjakk." ) print( "Hvits tur!" ) if whoseTurn == colors["black"]: if isKingMate( colors["black"] ): printing.print_red( "SVART ER SJAKK MATT! GRATULERER, HVIT!" ) break elif isKingCheck( colors["black"] ): printing.print_red( "Svartss konge er satt i sjakk." ) print( "Svarts tur!" ) print( "Hva vil du flytte? (fratil)" ) print( "Skriv q for å avslutte" ) while( True ): a = input() if a.lower() == "q": print( "Ha det bra!" ) exit() if len( a ) != 4: print( "Skriv posisjon du vil flytte fra og til (ex. a4b7)" ) continue frompos, topos = str2pos( a ) if not posLegal( frompos ) or not posLegal( topos ): print( "Du kan ikke flytte utenfor brettet!" ) print( "Prøv noe annet!" ) continue if board.boardIndex( frompos ) == " ": print( "Det står ingen brikke der du vil flytte fra!" ) print( "Flytt en brikke!" ) continue if board.boardIndex( frompos )[1] != whoseTurn: print( "Fy fy! Du kan bare flytte dine egne brikker." ) print( "Prøv en annen!" ) continue try: movePiece( frompos, topos ) except SelfCheckException: print( "Du kan ikke sette deg selv i sjakk." ) print( "Prøv noe annet." ) continue except IllegalPositionException: print( "Du kan ikke flytte til et sted utenfor brettet." ) print( "Prøv igjen." ) continue except IllegalMoveException: print( "Ugyldig trekk!" ) print( "Prøv et annet." ) continue except: print( "WTF" ) input() piece = board.boardIndex( topos ) if piece[0] == pieces["pawn"]: if( ( piece[1] == colors["white"] and topos[0] == 0 ) or ( piece[1] == colors["black"] and topos[0] == 7 ) ): printing.printBoard() print( "Gratulerer, du får forfremmet bonden din!" ) print( "Hva vil du ha? (q, k, r, b)" ) while( True ): lel = input() lel = lel[:1].lower() if( lel != "q" and lel != "k" and lel != "r" and lel != "b" ): print( "Skriv q, k r eller b" ) continue else: break board.promote( topos, lel ) if whoseTurn == colors["white"]: whoseTurn = colors["black"] else: whoseTurn = colors["white"] break input()