Esempio n. 1
0
def train(screenshot):
    # for filename in os.listdir(DATA_FOLDER):
    #   if filename.endswith('data'):
    #     os.remove(os.path.join(DATA_FOLDER, filename))

    if (os.path.exists(SAMPLES_FILE)):
        samples = np.loadtxt(SAMPLES_FILE)
        responses = [int(i) for i in np.loadtxt(RESPONSES_FILE)]
    else:
        samples = np.empty((0, imgUtils.FEATURE_VECTOR_SIZE))
        responses = []

    grid = imgUtils.cropBoard2Player(screenshot)

    index = 0
    for square, rowIndex, colIndex in imgUtils.parseSquaresFromBoard(grid):
        # cv2.imshow('%d, %d' % (colIndex, rowIndex), square)
        # # Enter the key corresponding to the board enums
        # key = cv2.waitKey(0)
        # if key == 27: break
        # val = int(chr(key))
        # printVal(val)
        # cv2.destroyAllWindows()
        val = TRAIN1_DATA[index]
        index += 1
        if val == board.CLEAR or val == board.EMPTY or val == board.DEAD:
            continue

        # Add to training data
        feature_vec = imgUtils.squareFeatureVector(square)
        responses.append(val)
        samples = np.append(samples, feature_vec, 0)

    np.savetxt(SAMPLES_FILE, samples)
    np.savetxt(RESPONSES_FILE, responses)
Esempio n. 2
0
def train(screenshot):
  # for filename in os.listdir(DATA_FOLDER):
  #   if filename.endswith('data'):
  #     os.remove(os.path.join(DATA_FOLDER, filename))

  if (os.path.exists(SAMPLES_FILE)):
    samples = np.loadtxt(SAMPLES_FILE)
    responses = [int(i) for i in np.loadtxt(RESPONSES_FILE)]
  else:
    samples = np.empty((0, imgUtils.FEATURE_VECTOR_SIZE))
    responses = []

  grid = imgUtils.cropBoard2Player(screenshot)

  index = 0
  for square, rowIndex, colIndex in imgUtils.parseSquaresFromBoard(grid):
    # cv2.imshow('%d, %d' % (colIndex, rowIndex), square)
    # # Enter the key corresponding to the board enums
    # key = cv2.waitKey(0)
    # if key == 27: break
    # val = int(chr(key))
    # printVal(val)
    # cv2.destroyAllWindows()
    val = TRAIN1_DATA[index]
    index += 1
    if val == board.CLEAR or val == board.EMPTY or val == board.DEAD: continue

    # Add to training data
    feature_vec = imgUtils.squareFeatureVector(square)
    responses.append(val)
    samples = np.append(samples, feature_vec, 0)

  np.savetxt(SAMPLES_FILE, samples)
  np.savetxt(RESPONSES_FILE, responses)
Esempio n. 3
0
def run(brain):
    print '[emulator.py] Running game...'

    for i in xrange(100):
        for _ in range(12):
            controller.D_UP()
        for _ in range(6):
            controller.D_LEFT()

        screenshot = screen.takeScreenshot()
        if screenshot == None:
            time.sleep(MOVE_WAIT)
            continue
        if imgUtils.gameHasEnded(screenshot):
            break
        # grid = imgUtils.cropBoard2Player(screenshot)
        # grid = imgUtils.filterBackground(grid)
        grid = imgUtils.cropBoard1Player(screenshot)
        grid = imgUtils.filterBackground1Player(grid)

        # Generate board model.
        inputRows = [[-1] * board.WIDTH for _ in range(board.HEIGHT)]
        for square, rowIndex, colIndex in imgUtils.parseSquaresFromBoard(grid):
            val, score = imgUtils.getSquareClassification(
                square, rowIndex, colIndex)
            inputRows[rowIndex][colIndex] = Piece(val)

        # Get next action and perform.
        b = board.Board(inputRows=inputRows)
        print '---BOARD---'
        b.printBoard()
        moves = brain.getNextMoves(b)
        print '---MOVES---'
        print moves
        print ''
        cursor_pos = performMoves(moves, (0, 0))

        # End loop.
    print '[emulator.py] Done running game.'
Esempio n. 4
0
def run(brain):
  print '[emulator.py] Running game...'

  for i in xrange(100):
    for _ in range(12): controller.D_UP()
    for _ in range(6): controller.D_LEFT()

    screenshot = screen.takeScreenshot()
    if screenshot == None:
      time.sleep(MOVE_WAIT)
      continue
    if imgUtils.gameHasEnded(screenshot):
      break
    # grid = imgUtils.cropBoard2Player(screenshot)
    # grid = imgUtils.filterBackground(grid)
    grid = imgUtils.cropBoard1Player(screenshot)
    grid = imgUtils.filterBackground1Player(grid)

    # Generate board model.
    inputRows = [[-1] * board.WIDTH for _ in range(board.HEIGHT)]
    for square, rowIndex, colIndex in imgUtils.parseSquaresFromBoard(grid):
      val, score = imgUtils.getSquareClassification(square, rowIndex, colIndex)
      inputRows[rowIndex][colIndex] = Piece(val)

    # Get next action and perform.
    b = board.Board(inputRows=inputRows)
    print '---BOARD---'
    b.printBoard()
    moves = brain.getNextMoves(b)
    print '---MOVES---'
    print moves
    print ''
    cursor_pos = performMoves(moves, (0, 0))

    # End loop.
  print '[emulator.py] Done running game.'