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