def AttackMatrix(sFEN): # This I made
    print("FEN string is: " + sFEN)
    
    sFENnp = sFEN # setting up for sFEN without pawns
    sFENnp = sFENnp.replace('p','1')
    sFENnp = sFENnp.replace('P','1')

    
    ###############
    # DIAGNOSTIC
    #im = draw_board(fen=sFEN)
    #im.save(os.path.join(os.getcwd(),'diagnostics','withpawns.png'))
    #im = draw_board(fen=sFENnp)
    #im.save(os.path.join(os.getcwd(),'diagnostics','withoutpawns.png'))
    ###############\
    chessgame = Game(sFEN) # this and next ...
    possiblem = chessgame.get_moves() # ... get valid moves with all pieces
    chessgamenp = Game(sFENnp) # this and next ...
    possiblemnp = chessgamenp.get_moves() # ... get valid moves without pawns
    vm = rp(possiblem, possiblemnp) # all moves on board with pawns w/o pawn-fwd moves

    #TallyMatrix[row][column]
    TallyMatrix = [[0 for x in range(8)] for x in range(8)]

    for move in vm:
        vr = move[2]
        vc = move[3]
        vr = ''.join([(str(ord(x)-96) if x.isalpha() else x) for x in list(vr)])
        vr = int(vr)-1
        vc = int(vc)-1
        TallyMatrix[vc][vr] = TallyMatrix[vc][vr] + 1
    print('h1 ...... h8')
    for r in range(len(TallyMatrix)):
        print(TallyMatrix[7-r])
    print('a1 ...... a8')
    return TallyMatrix #need this line, d'oh!
示例#2
0
    def _validate(self, curr_fen: str) -> bool:
        with open(self.__game_path, 'r')as f:
            lines = f.read().splitlines()

        last_line = lines[-2]
        try:
            prev_ginfo = GameInformation(json.loads(last_line.split('$')[1]))
        except IndexError:
            if last_line.startswith('-'):
                print('Must be a new file, passing through')
                return True

        curr = Game()
        curr.set_fen(curr_fen)

        if str(self.__ginfo) == str(prev_ginfo):
            self.__game_is_updated = False
            return True
        prev = Game()
        prev.set_fen(prev_ginfo.get_fen())
        print(prev)
        print(curr)
        if str(prev) == str(curr):
            self.__game_is_updated = True
            return True

        for move in prev.get_moves():
            tmp: Game = copy.deepcopy(prev)
            tmp.apply_move(move)
            if str(tmp) == str(curr):
                self.__game_is_updated = True
                print('Valid move made: %s' % move)
                return True
        print('Your opponent seems to be cheating... Game aborted. You are the winner.')
        self.__ginfo.set_status(State.CHEATED)
        self.__ginfo.set_winner(self.get_who_am_i())
        self.get_ginfo().set_loser('p1' if self.get_who_am_i() == 'p2' else 'p2')
        self.get_ginfo().inc_seq()
        self._update()
        return False
示例#3
0
from Chessnut import Game

chessgame = Game()
print(chessgame)  # 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'

print(chessgame.get_moves())
"""
['a2a3', 'a2a4', 'b2b3', 'b2b4', 'c2c3', 'c2c4', 'd2d3', 'd2d4', 'e2e3', 
 'e2e4', 'f2f3', 'f2f4', 'g2g3', 'g2g4', 'h2h3', 'h2h4', 'b1c3', 'b1a3', 
 'g1h3', 'g1f3']
"""

chessgame.apply_move('e2e4')  # succeeds!
print(chessgame)  # 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1'

#chessgame.apply_move('e2e4')  # fails! (raises InvalidMove exception)
示例#4
0
文件: chess.py 项目: idandrei94/SWA
import subprocess
import cgitb
import cgi
cgitb.enable()

print("Content-Type: text/html")
print()
board = Game()
moves = ""
arg_moves = []
#try:
# arg = cgi.FieldStorage()['history']
# arg_moves = arg.value.split(' ')
#except:
# pass
#for move in arg_moves:
# try:
#  board.apply_move(move.strip())
#  moves = moves + " " + move
# except:
#  pass
arg = cgi.FieldStorage()['history']
arg_moves = arg.value.split(' ')
for move in arg_moves:
    board.apply_move(move.strip())

available_moves = board.get_moves()
chosen_move = available_moves[randint(0, len(available_moves) - 1)]
moves = moves + " " + chosen_move
print(chosen_move)
示例#5
0
originalBoard = originalGame.board._position

def ind(a,b):
    return a * 8 + b

def tryMoveAndEval(move):
    tempGame = Game(fen, True)
    tempGame.apply_move(move)
    tempBoard = tempGame.board._position
    score = 0
    for i in range(8):
        for u in range(8):
            if originalBoard[ind(i,u)] != tempBoard[ind(i,u)]:
                score += alignedMap[i][u]
    return score

bestMove = None
bestScore = -1.0

for move in originalGame.get_moves():
    key = tryMoveAndEval(move)
    if key > bestScore:
        bestMove = move
        bestScore = key

if bestMove:
    print(bestMove)
else:
    print('ERROR ERROR ERROR')
    sys.exit(1)
示例#6
0
from Chessnut import Game

chessgame = Game()
print(chessgame)  # 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'

print(chessgame.get_moves())
"""
['a2a3', 'a2a4', 'b2b3', 'b2b4', 'c2c3', 'c2c4', 'd2d3', 'd2d4', 'e2e3', 
 'e2e4', 'f2f3', 'f2f4', 'g2g3', 'g2g4', 'h2h3', 'h2h4', 'b1c3', 'b1a3', 
 'g1h3', 'g1f3']
"""

chessgame.apply_move('e2e4')  # succeeds!
print(
    chessgame)  # 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1'

#chessgame.apply_move('e2e4')  # fails! (raises InvalidMove exception)