Package chesstrainer

Source Code of chesstrainer.Main

/*
* Copyright (C) 2014 Tim Declercq <caveman1917@hotmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package chesstrainer;

import chesstrainer.endgame.CSoapEndgameTablebase;
import chesstrainer.endgame.IEndgameTablebase;
import chesstrainer.ui.CAnalysisBoard;
import chesstrainer.ui.CBoard;
import chesstrainer.util.CLanguage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.security.CodeSource;
import java.util.Map;
import java.util.Properties;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

/**
*
* @author Tim Declercq <caveman1917@hotmail.com>
*/
public class Main {
   
    public static final Path DIRECTORY;
    public static final Properties PROGRAM_PROPERTIES;
    public static final CLanguage LANGUAGE;
    public static final Logger LOG = LogManager.getLogManager().getLogger(Logger.GLOBAL_LOGGER_NAME);

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        LOG.log(Level.INFO, "Application started");
        testEGTB();
        showBoard();
    }
   
    private static void showBoard() {
        JFrame window = new JFrame("Board test");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        CAnalysisBoard board = new CAnalysisBoard();
        window.add(board);
        window.pack();
        window.setVisible(true);
    }
   
    private static void testEGTB() {
        IEndgameTablebase egtb = new CSoapEndgameTablebase();
        String fen = "8/8/8/8/6p1/7p/4kB2/6K1 b - -";
        System.out.println(egtb.getScore(fen));
        Map<CMove, Integer> moves = egtb.getBestMoves(fen);
        for (CMove move : moves.keySet()) {
            System.out.println(move.toString() + " " + moves.get(move));
        }
    }
   
    static {
       
        // DIRECTORY
        File jarFile = null;
        try {
            CodeSource codeSource = Main.class.getProtectionDomain().getCodeSource();
            jarFile = new File(codeSource.getLocation().toURI().getPath());
        } catch (Exception e) {
            JFileChooser fc = new JFileChooser();
            int val = fc.showOpenDialog(null);
            if (val != JFileChooser.APPROVE_OPTION) {
                System.exit(-1);
            } else {
                jarFile = fc.getSelectedFile();
            }
        }
        if (jarFile == null) {
            DIRECTORY = null;
            System.exit(-1);
        } else {
            DIRECTORY = jarFile.getParentFile().toPath();
        }
       
        // LOG
        LOG.setUseParentHandlers(false);
        for (Handler handler : LOG.getHandlers()) {
            LOG.removeHandler(handler);
        }
        LOG.addHandler(new ConsoleHandler());
        Path logFile = DIRECTORY.resolve("log.log").toAbsolutePath();
        try {
            LOG.addHandler(new FileHandler(logFile.toString()));    
        } catch (IOException | SecurityException e) {
            LOG.log(Level.WARNING, "Cannot open log file", e);
        }
        for (Handler handler : LOG.getHandlers()) {
            handler.setFormatter(new SimpleFormatter());
        }
       
        // PROPERTIES
        PROGRAM_PROPERTIES = new Properties();
        Path propFile = DIRECTORY.resolve("config").resolve("program.properties");
        try (FileInputStream is = new FileInputStream(propFile.toFile())) {
            PROGRAM_PROPERTIES.load(is);
        } catch (Exception e) {
            LOG.log(Level.SEVERE, "Cannot load program properties", e);
            System.exit(-1);
        }
       
        // LANGUAGE
        LANGUAGE = new CLanguage();
        Path langFile = DIRECTORY.resolve("lang")
                .resolve(PROGRAM_PROPERTIES.getProperty("Language") + ".lang");
        try (FileInputStream is = new FileInputStream(langFile.toFile())) {
            LANGUAGE.load(is);
        } catch (Exception e) {
            LOG.log(Level.WARNING, "Cannot load language", e);
        }
       
    }
}
TOP

Related Classes of chesstrainer.Main

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.