org/homelinux/largo/schach/Schach.java
author Markus Bröker <mbroeker@largo.homelinux.org>
Fri, 17 Dec 2010 22:30:33 +0100
changeset 13 f83884cc7d2f
parent 7 93fe1f21e0d8
child 14 f12f77aa13b2
permissions -rw-r--r--
Source Code re-formatted
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     1
/**
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     2
 *   $Id: Schach.java 143 2008-04-25 12:19:23Z mbroeker $
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     3
 *  $URL: http://localhost/svn/eclipse/Schachspiel/trunk/org/homelinux/largo/schach/Schach.java $
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     4
 */
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     5
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     6
package org.homelinux.largo.schach;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     7
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     8
import java.awt.BorderLayout;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     9
import java.awt.Color;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    10
import java.awt.event.ActionEvent;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    11
import java.awt.event.ActionListener;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    12
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    13
import javax.swing.JButton;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    14
import javax.swing.JFrame;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    15
import javax.swing.JLabel;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    16
import javax.swing.JPanel;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    17
import javax.swing.JSpinner;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    18
import javax.swing.SpinnerNumberModel;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    19
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    20
import org.homelinux.largo.games.board.MouseListener;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    21
import org.homelinux.largo.games.board.Point;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    22
import org.homelinux.largo.utils.BrowserLaunch;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    23
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    24
public class Schach extends JFrame implements Runnable {
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    25
    static final long serialVersionUID = 140208;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    26
    static final String helpURL = "http://largo.homelinux.org/cgi-bin/gitweb.cgi?p=games/Schach.git;a=summary";
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    27
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    28
    MouseListener listener;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    29
    KIBoard board;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    30
    Point p;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    31
    JLabel human = new JLabel("<html><font size='5' color='#FFFFFF'>Human</font></html>");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    32
    JLabel computer = new JLabel("<html><font size='5' color='#FFFFFF'>Computer</font></html>");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    33
    JButton neu = new JButton("Play");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    34
    JButton pconly = new JButton("PC-PC");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    35
    JButton back = new JButton("Back");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    36
    JButton forward = new JButton("For");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    37
    JButton help = new JButton("Source");
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    38
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    39
    SpinnerNumberModel model = new SpinnerNumberModel(4, 1, 8, 1);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    40
    JSpinner combo = new JSpinner(model);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    41
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    42
    JPanel panel;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    43
    JPanel fpanel;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    44
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    45
    int search_depth;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    46
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    47
    boolean pc_only;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    48
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    49
    public Schach(int w, int h) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    50
        super("Chess by Largo Enterprises");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    51
        setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    52
        board = new KIBoard(w, h);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    53
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    54
        fpanel = new JPanel();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    55
        fpanel.add(back);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    56
        fpanel.add(neu);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    57
        fpanel.add(combo);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    58
        fpanel.add(pconly);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    59
        fpanel.add(help);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    60
        fpanel.add(forward);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    61
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    62
        panel = new JPanel();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    63
        panel.setLayout(new BorderLayout());
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    64
        panel.add(human, "West");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    65
        panel.add(fpanel, "Center");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    66
        panel.add(computer, "East");
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    67
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    68
        fpanel.setBackground(new Color(50, 100, 200));
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    69
        panel.setBackground(new Color(50, 100, 200));
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    70
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    71
        getContentPane().setLayout(new BorderLayout());
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    72
        getContentPane().add(panel, "North");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    73
        getContentPane().add(board, "Center");
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    74
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    75
        neu.addActionListener(new ActionListener() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    76
            public void actionPerformed(ActionEvent actionevent) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    77
                human.setText("<html><font size='5' color='#FFFFFF'>Human</font></html>");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    78
                computer.setText("<html><font size='5' color='#FFFFFF'>Computer</font></html>");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    79
                pc_only = false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    80
                board.init();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    81
                board.negateEstimation(false);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    82
            }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    83
        });
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    84
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    85
        pconly.addActionListener(new ActionListener() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    86
            public void actionPerformed(ActionEvent actionevent) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    87
                human.setText("<html><font size='5' color='#FFFFFF'>Computer</font></html>");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    88
                computer.setText("<html><font size='5' color='#FFFFFF'>Computer</font></html>");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    89
                pc_only = true;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    90
            }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    91
        });
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    92
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    93
        back.addActionListener(new ActionListener() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    94
            public void actionPerformed(ActionEvent actionevent) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    95
                board.backwards();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    96
            }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    97
        });
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    98
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    99
        forward.addActionListener(new ActionListener() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   100
            public void actionPerformed(ActionEvent actionevent) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   101
                board.forward();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   102
            }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   103
        });
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   104
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   105
        help.addActionListener(new ActionListener() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   106
            public void actionPerformed(ActionEvent actionevent) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   107
                BrowserLaunch browser = new BrowserLaunch();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   108
                browser.openURL(helpURL);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   109
            }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   110
        });
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   111
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   112
        listener = board.getMouseListener();
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   113
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   114
        pc_only = false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   115
        search_depth = 3;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   116
        pack();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   117
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   118
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   119
    public void computer_play() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   120
        while (pc_only) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   121
            search_depth = model.getNumber().intValue() - 1;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   122
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   123
            if (!board.isBlacksTurn()) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   124
                human.setText("<html><font size='5' color='#FF3333'>Computer</font></html>");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   125
                board.negateEstimation(true);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   126
                if (!board.doBestMove(search_depth)) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   127
                    pc_only = false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   128
                    return;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   129
                }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   130
                board.negateEstimation(false);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   131
                human.setText("<html><font size='5' color='#FFFFFF'>Computer</font></html>");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   132
                board.setBlacksTurn(true);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   133
            } else {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   134
                computer.setText("<html><font size='5' color='#FF3333'>Computer</font></html>");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   135
                if (!board.doBestMove(search_depth)) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   136
                    pc_only = false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   137
                    return;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   138
                }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   139
                computer.setText("<html><font size='5' color='#FFFFFF'>Computer</font></html>");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   140
                board.setBlacksTurn(false);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   141
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   142
                if (board.simu_debug()) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   143
                    for (int i = 0; i < 64; i++) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   144
                        if ((i + 1) % 8 == 0)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   145
                            System.err.printf("%2d=%d%n", i, board.getValue(i));
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   146
                        else
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   147
                            System.err.printf("%2d=%d ", i, board.getValue(i));
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   148
                    }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   149
                }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   150
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   151
                try {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   152
                    Thread.sleep(10);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   153
                } catch (InterruptedException e) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   154
                    System.err.println("computer_play: " + e.getLocalizedMessage());
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   155
                    System.err.println("========================================================================");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   156
                    e.printStackTrace();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   157
                    System.err.println("========================================================================");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   158
                    return;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   159
                }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   160
            }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   161
        }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   162
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   163
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   164
    public void run() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   165
        for (;;) {
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   166
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   167
            /* is this thread safe */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   168
            search_depth = model.getNumber().intValue() - 1;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   169
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   170
            if (pc_only) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   171
                computer_play();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   172
            }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   173
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   174
            if (!board.isBlacksTurn()) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   175
                if (listener.isSelected()) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   176
                    p = listener.getSelection();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   177
                    if (board.move(p))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   178
                        board.setBlacksTurn(true);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   179
                }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   180
                try {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   181
                    Thread.sleep(50);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   182
                } catch (InterruptedException e) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   183
                    System.err.println("Schach::run: " + e.getLocalizedMessage());
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   184
                    System.err.println("========================================================================");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   185
                    e.printStackTrace();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   186
                    System.err.println("========================================================================");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   187
                    return;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   188
                }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   189
            } else {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   190
                human.setText("<html><font size='5' color='#FFFFFF'>Human</font></html>");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   191
                computer.setText("<html><font size='5' color='#FF3333'>Computer</font></html>");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   192
                board.doBestMove(search_depth);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   193
                human.setText("<html><font size='5' color='#FF3333'>Human</font></html>");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   194
                computer.setText("<html><font size='5' color='#FFFFFF'>Computer</font></html>");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   195
                board.setBlacksTurn(false);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   196
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   197
                if (board.simu_debug()) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   198
                    for (int i = 0; i < 64; i++) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   199
                        if ((i + 1) % 8 == 0)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   200
                            System.out.printf("%2d=%d%n", i, board.getValue(i));
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   201
                        else
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   202
                            System.out.printf("%2d=%d ", i, board.getValue(i));
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   203
                    }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   204
                }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   205
            }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   206
        }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   207
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   208
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   209
    public static void main(String args[]) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   210
        Schach s = new Schach(74, 74);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   211
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   212
        s.setResizable(false);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   213
        s.setVisible(true);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   214
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   215
        if (args.length == 1) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   216
            if (args[0].equals("full"))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   217
                s.board.setDebug(true, true);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   218
            else if (args[0].equals("simu"))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   219
                s.board.setDebug(true, false);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   220
            else if (args[0].equals("undo"))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   221
                s.board.setDebug(false, true);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   222
            else if (args[0].equals("none"))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   223
                s.board.setDebug(false, false);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   224
            else {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   225
                System.out.println("   Chess: java -jar games.jar [full|simu|undo|none]");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   226
                System.out
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   227
                        .println("Checkers: java -cp games.jar org.homelinux.largo.checkers.Checkers [full|simu|undo|none]");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   228
            }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   229
        }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   230
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   231
        Thread t = new Thread(s);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   232
        t.setPriority(Thread.NORM_PRIORITY - 1);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   233
        t.start();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
   234
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   235
}