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