Moved CuckooChess project to trunk/

This commit is contained in:
Peter Osterlund
2011-11-12 19:43:59 +00:00
parent f2946fe6c7
commit 0461be164f
16 changed files with 2187 additions and 0 deletions

View File

@@ -0,0 +1,309 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.7" type="org.netbeans.modules.form.forminfo.JAppletFormInfo">
<NonVisualComponents>
<Component class="javax.swing.ButtonGroup" name="PlayerColor">
</Component>
</NonVisualComponents>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="MainPanel" alignment="1" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="MainPanel" alignment="1" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Container class="javax.swing.JPanel" name="MainPanel">
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="ChessBoardPanel" pref="502" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="jPanel1" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="1" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0">
<Component id="jPanel1" alignment="0" max="32767" attributes="0"/>
<Component id="ChessBoardPanel" alignment="0" pref="502" max="32767" attributes="1"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Container class="javax.swing.JPanel" name="ChessBoardPanel">
<Properties>
<Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
<Border info="org.netbeans.modules.form.compat2.border.LineBorderInfo">
<LineBorder/>
</Border>
</Property>
<Property name="preferredSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor">
<Dimension value="[500, 500]"/>
</Property>
</Properties>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="ChessBoard" alignment="0" pref="500" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="ChessBoard" alignment="0" pref="500" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JLabel" name="ChessBoard">
<Events>
<EventHandler event="mousePressed" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="ChessBoardMousePressed"/>
<EventHandler event="mouseReleased" listener="java.awt.event.MouseListener" parameters="java.awt.event.MouseEvent" handler="ChessBoardMouseReleased"/>
<EventHandler event="mouseDragged" listener="java.awt.event.MouseMotionListener" parameters="java.awt.event.MouseEvent" handler="ChessBoardMouseDragged"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_CreateCodeCustom" type="java.lang.String" value="new ChessBoardPainter()"/>
</AuxValues>
</Component>
</SubComponents>
</Container>
<Container class="javax.swing.JPanel" name="jPanel1">
<Properties>
<Property name="focusable" type="boolean" value="false"/>
</Properties>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Group type="103" groupAlignment="1" attributes="0">
<Component id="jScrollPane1" alignment="0" pref="319" max="32767" attributes="1"/>
<Component id="StatusLine" alignment="0" pref="319" max="32767" attributes="1"/>
<Group type="103" alignment="0" groupAlignment="0" max="-2" attributes="0">
<Group type="102" alignment="0" attributes="1">
<Component id="NewGame" min="-2" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
<Component id="Backward" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="Forward" min="-2" max="-2" attributes="0"/>
</Group>
<Component id="SettingsPanel" alignment="0" max="32767" attributes="1"/>
</Group>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Component id="SettingsPanel" min="-2" max="-2" attributes="1"/>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="NewGame" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="Forward" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="Backward" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Component id="jScrollPane1" pref="283" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="StatusLine" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JButton" name="NewGame">
<Properties>
<Property name="text" type="java.lang.String" value="New Game"/>
<Property name="focusable" type="boolean" value="false"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="NewGameActionPerformed"/>
</Events>
</Component>
<Container class="javax.swing.JPanel" name="SettingsPanel">
<Properties>
<Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
<Border info="org.netbeans.modules.form.compat2.border.TitledBorderInfo">
<TitledBorder title="Settings"/>
</Border>
</Property>
<Property name="focusable" type="boolean" value="false"/>
</Properties>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="ShowThinking" alignment="0" min="-2" pref="157" max="-2" attributes="1"/>
<Group type="102" alignment="0" attributes="0">
<Component id="PlayerWhite" min="-2" max="-2" attributes="1"/>
<EmptySpace pref="104" max="32767" attributes="0"/>
<Component id="FlipBoard" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Component id="TimeLabel" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="TimeSlider" min="-2" max="-2" attributes="0"/>
</Group>
<Component id="PlayerBlack" alignment="0" min="-2" max="-2" attributes="1"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Group type="103" groupAlignment="3" attributes="0">
<Component id="PlayerWhite" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="FlipBoard" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Component id="PlayerBlack" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="18" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="TimeLabel" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="TimeSlider" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Component id="ShowThinking" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JRadioButton" name="PlayerWhite">
<Properties>
<Property name="buttonGroup" type="javax.swing.ButtonGroup" editor="org.netbeans.modules.form.RADComponent$ButtonGroupPropertyEditor">
<ComponentRef name="PlayerColor"/>
</Property>
<Property name="selected" type="boolean" value="true"/>
<Property name="text" type="java.lang.String" value="Play White"/>
<Property name="focusable" type="boolean" value="false"/>
</Properties>
</Component>
<Component class="javax.swing.JRadioButton" name="PlayerBlack">
<Properties>
<Property name="buttonGroup" type="javax.swing.ButtonGroup" editor="org.netbeans.modules.form.RADComponent$ButtonGroupPropertyEditor">
<ComponentRef name="PlayerColor"/>
</Property>
<Property name="text" type="java.lang.String" value="Play Black"/>
<Property name="focusable" type="boolean" value="false"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="TimeLabel">
<Properties>
<Property name="text" type="java.lang.String" value="Thinking Time"/>
</Properties>
</Component>
<Component class="javax.swing.JSlider" name="TimeSlider">
<Properties>
<Property name="majorTickSpacing" type="int" value="15"/>
<Property name="maximum" type="int" value="60"/>
<Property name="minorTickSpacing" type="int" value="5"/>
<Property name="paintLabels" type="boolean" value="true"/>
<Property name="paintTicks" type="boolean" value="true"/>
<Property name="value" type="int" value="10"/>
<Property name="focusable" type="boolean" value="false"/>
</Properties>
<Events>
<EventHandler event="stateChanged" listener="javax.swing.event.ChangeListener" parameters="javax.swing.event.ChangeEvent" handler="TimeSliderStateChanged"/>
</Events>
</Component>
<Component class="javax.swing.JCheckBox" name="ShowThinking">
<Properties>
<Property name="text" type="java.lang.String" value="Show Thinking"/>
<Property name="focusable" type="boolean" value="false"/>
</Properties>
<Events>
<EventHandler event="stateChanged" listener="javax.swing.event.ChangeListener" parameters="javax.swing.event.ChangeEvent" handler="ShowThinkingStateChanged"/>
</Events>
</Component>
<Component class="javax.swing.JCheckBox" name="FlipBoard">
<Properties>
<Property name="text" type="java.lang.String" value="Flip Board"/>
<Property name="focusable" type="boolean" value="false"/>
</Properties>
<Events>
<EventHandler event="stateChanged" listener="javax.swing.event.ChangeListener" parameters="javax.swing.event.ChangeEvent" handler="FlipBoardStateChanged"/>
</Events>
</Component>
</SubComponents>
</Container>
<Container class="javax.swing.JScrollPane" name="jScrollPane1">
<AuxValues>
<AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
</AuxValues>
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JTextPane" name="LogTextArea">
<Properties>
<Property name="editable" type="boolean" value="false"/>
<Property name="verifyInputWhenFocusTarget" type="boolean" value="false"/>
</Properties>
</Component>
</SubComponents>
</Container>
<Component class="javax.swing.JTextField" name="StatusLine">
<Properties>
<Property name="editable" type="boolean" value="false"/>
<Property name="focusable" type="boolean" value="false"/>
</Properties>
</Component>
<Component class="javax.swing.JButton" name="Forward">
<Properties>
<Property name="text" type="java.lang.String" value="-&gt;"/>
<Property name="defaultCapable" type="boolean" value="false"/>
<Property name="focusPainted" type="boolean" value="false"/>
<Property name="focusable" type="boolean" value="false"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="ForwardActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="Backward">
<Properties>
<Property name="text" type="java.lang.String" value="&lt;-"/>
<Property name="defaultCapable" type="boolean" value="false"/>
<Property name="focusPainted" type="boolean" value="false"/>
<Property name="focusable" type="boolean" value="false"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="BackwardActionPerformed"/>
</Events>
</Component>
</SubComponents>
</Container>
</SubComponents>
</Container>
</SubComponents>
</Form>

View File

@@ -0,0 +1,440 @@
/*
CuckooChess - A java chess program.
Copyright (C) 2011 Peter Österlund, peterosterlund2@gmail.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 gui;
import guibase.ChessController;
import guibase.GUIInterface;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import chess.ComputerPlayer;
import chess.Move;
import chess.Position;
/**
* The main class for the chess GUI.
* @author petero
*/
public class AppletGUI extends javax.swing.JApplet implements GUIInterface {
private static final long serialVersionUID = 7357610346389734323L;
ChessBoardPainter cbp;
ChessController ctrl;
final static int ttLogSize = 19; // Use 2^19 hash entries.
String moveListStr = "";
String thinkingStr = "";
/** Initializes the applet AppletGUI */
@Override
public void init() {
ctrl = new ChessController(this);
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
cbp = (ChessBoardPainter)ChessBoard;
ctrl.newGame(PlayerWhite.isSelected(), ttLogSize, true);
ctrl.startGame();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Entry point for the GUI version of the chess program.
*/
public static void main(String[] args) {
javax.swing.JApplet theApplet = new AppletGUI();
theApplet.init();
javax.swing.JFrame window = new javax.swing.JFrame(ComputerPlayer.engineName);
window.setContentPane(theApplet);
window.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);
}
/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
PlayerColor = new javax.swing.ButtonGroup();
MainPanel = new javax.swing.JPanel();
ChessBoardPanel = new javax.swing.JPanel();
ChessBoard = new ChessBoardPainter();
jPanel1 = new javax.swing.JPanel();
NewGame = new javax.swing.JButton();
SettingsPanel = new javax.swing.JPanel();
PlayerWhite = new javax.swing.JRadioButton();
PlayerBlack = new javax.swing.JRadioButton();
TimeLabel = new javax.swing.JLabel();
TimeSlider = new javax.swing.JSlider();
ShowThinking = new javax.swing.JCheckBox();
FlipBoard = new javax.swing.JCheckBox();
jScrollPane1 = new javax.swing.JScrollPane();
LogTextArea = new javax.swing.JTextPane();
StatusLine = new javax.swing.JTextField();
Forward = new javax.swing.JButton();
Backward = new javax.swing.JButton();
ChessBoardPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
ChessBoardPanel.setPreferredSize(new java.awt.Dimension(500, 500));
ChessBoard.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
ChessBoardMousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
ChessBoardMouseReleased(evt);
}
});
ChessBoard.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
ChessBoardMouseDragged(evt);
}
});
javax.swing.GroupLayout ChessBoardPanelLayout = new javax.swing.GroupLayout(ChessBoardPanel);
ChessBoardPanel.setLayout(ChessBoardPanelLayout);
ChessBoardPanelLayout.setHorizontalGroup(
ChessBoardPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ChessBoard, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
);
ChessBoardPanelLayout.setVerticalGroup(
ChessBoardPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ChessBoard, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
);
jPanel1.setFocusable(false);
NewGame.setText("New Game");
NewGame.setFocusable(false);
NewGame.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NewGameActionPerformed(evt);
}
});
SettingsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Settings"));
SettingsPanel.setFocusable(false);
PlayerColor.add(PlayerWhite);
PlayerWhite.setSelected(true);
PlayerWhite.setText("Play White");
PlayerWhite.setFocusable(false);
PlayerColor.add(PlayerBlack);
PlayerBlack.setText("Play Black");
PlayerBlack.setFocusable(false);
TimeLabel.setText("Thinking Time");
TimeSlider.setMajorTickSpacing(15);
TimeSlider.setMaximum(60);
TimeSlider.setMinorTickSpacing(5);
TimeSlider.setPaintLabels(true);
TimeSlider.setPaintTicks(true);
TimeSlider.setValue(5);
TimeSlider.setFocusable(false);
TimeSlider.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
TimeSliderStateChanged(evt);
}
});
ShowThinking.setText("Show Thinking");
ShowThinking.setFocusable(false);
ShowThinking.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
ShowThinkingStateChanged(evt);
}
});
FlipBoard.setText("Flip Board");
FlipBoard.setFocusable(false);
FlipBoard.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
FlipBoardStateChanged(evt);
}
});
javax.swing.GroupLayout SettingsPanelLayout = new javax.swing.GroupLayout(SettingsPanel);
SettingsPanel.setLayout(SettingsPanelLayout);
SettingsPanelLayout.setHorizontalGroup(
SettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ShowThinking, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(SettingsPanelLayout.createSequentialGroup()
.addComponent(PlayerWhite)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 104, Short.MAX_VALUE)
.addComponent(FlipBoard)
.addContainerGap())
.addGroup(SettingsPanelLayout.createSequentialGroup()
.addComponent(TimeLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(TimeSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(PlayerBlack)
);
SettingsPanelLayout.setVerticalGroup(
SettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(SettingsPanelLayout.createSequentialGroup()
.addGroup(SettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(PlayerWhite)
.addComponent(FlipBoard))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(PlayerBlack)
.addGap(18, 18, 18)
.addGroup(SettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(TimeLabel)
.addComponent(TimeSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(ShowThinking)
.addContainerGap())
);
LogTextArea.setEditable(false);
LogTextArea.setVerifyInputWhenFocusTarget(false);
jScrollPane1.setViewportView(LogTextArea);
StatusLine.setEditable(false);
StatusLine.setFocusable(false);
Forward.setText("->");
Forward.setDefaultCapable(false);
Forward.setFocusPainted(false);
Forward.setFocusable(false);
Forward.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ForwardActionPerformed(evt);
}
});
Backward.setText("<-");
Backward.setDefaultCapable(false);
Backward.setFocusPainted(false);
Backward.setFocusable(false);
Backward.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BackwardActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
.addComponent(StatusLine, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(NewGame)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Backward)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Forward))
.addComponent(SettingsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(SettingsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(NewGame)
.addComponent(Forward)
.addComponent(Backward))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(StatusLine, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
javax.swing.GroupLayout MainPanelLayout = new javax.swing.GroupLayout(MainPanel);
MainPanel.setLayout(MainPanelLayout);
MainPanelLayout.setHorizontalGroup(
MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(MainPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(ChessBoardPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
MainPanelLayout.setVerticalGroup(
MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, MainPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(ChessBoardPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(MainPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(MainPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
private void ChessBoardMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ChessBoardMousePressed
if (ctrl.humansTurn()) {
int sq = cbp.eventToSquare(evt);
Move m = cbp.mousePressed(sq);
if (m != null) {
ctrl.humanMove(m);
}
}
}//GEN-LAST:event_ChessBoardMousePressed
private void FlipBoardStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_FlipBoardStateChanged
cbp.setFlipped(FlipBoard.isSelected());
}//GEN-LAST:event_FlipBoardStateChanged
private void NewGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NewGameActionPerformed
ctrl.newGame(PlayerWhite.isSelected(), ttLogSize, true);
ctrl.startGame();
}//GEN-LAST:event_NewGameActionPerformed
private void ShowThinkingStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_ShowThinkingStateChanged
ctrl.setMoveList();
}//GEN-LAST:event_ShowThinkingStateChanged
private void BackwardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackwardActionPerformed
ctrl.takeBackMove();
}//GEN-LAST:event_BackwardActionPerformed
private void ForwardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ForwardActionPerformed
ctrl.redoMove();
}//GEN-LAST:event_ForwardActionPerformed
private void TimeSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_TimeSliderStateChanged
ctrl.setTimeLimit();
}//GEN-LAST:event_TimeSliderStateChanged
private void ChessBoardMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ChessBoardMouseDragged
if (ctrl.humansTurn()) {
cbp.mouseDragged(evt);
}
}//GEN-LAST:event_ChessBoardMouseDragged
private void ChessBoardMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ChessBoardMouseReleased
if (ctrl.humansTurn()) {
int sq = cbp.eventToSquare(evt);
Move m = cbp.mouseReleased(sq);
if (m != null) {
ctrl.humanMove(m);
}
}
}//GEN-LAST:event_ChessBoardMouseReleased
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton Backward;
private javax.swing.JLabel ChessBoard;
private javax.swing.JPanel ChessBoardPanel;
private javax.swing.JCheckBox FlipBoard;
private javax.swing.JButton Forward;
private javax.swing.JTextPane LogTextArea;
private javax.swing.JPanel MainPanel;
private javax.swing.JButton NewGame;
private javax.swing.JRadioButton PlayerBlack;
private javax.swing.ButtonGroup PlayerColor;
private javax.swing.JRadioButton PlayerWhite;
private javax.swing.JPanel SettingsPanel;
private javax.swing.JCheckBox ShowThinking;
private javax.swing.JTextField StatusLine;
private javax.swing.JLabel TimeLabel;
private javax.swing.JSlider TimeSlider;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables
public void setPosition(Position pos) {
cbp.setPosition(pos);
}
public void setSelection(int sq) {
cbp.setSelection(sq);
}
public void setStatusString(String str) {
StatusLine.setText(str);
}
public void setMoveListString(String str) {
moveListStr = str;
str = moveListStr + "\n" + thinkingStr;
if (!str.equals(LogTextArea.getText())) {
LogTextArea.setText(str);
}
}
public void setThinkingString(String str) {
thinkingStr = str;
str = moveListStr + "\n" + thinkingStr;
if (!str.equals(LogTextArea.getText())) {
LogTextArea.setText(str);
}
}
public final int timeLimit() {
return Math.max(25, TimeSlider.getValue() * 1000);
}
public final boolean showThinking() {
return ShowThinking.isSelected();
}
public void requestPromotePiece() {
runOnUIThread(new Runnable() {
public void run() {
Object[] options = { "Queen", "Rook", "Bishop", "Knight" };
int choice = JOptionPane.showOptionDialog(
cbp, "Promote pawn to?", "Pawn Promotion",
0, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
ctrl.reportPromotePiece(choice);
}
});
}
public void runOnUIThread(Runnable runnable) {
SwingUtilities.invokeLater(runnable);
}
@Override
public boolean randomMode() {
return false;
}
@Override
public void reportInvalidMove(Move m) {
}
}

View File

@@ -0,0 +1,292 @@
/*
CuckooChess - A java chess program.
Copyright (C) 2011 Peter Österlund, peterosterlund2@gmail.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 gui;
import chess.Move;
import chess.Piece;
import chess.Position;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JLabel;
/**
* Draws a graphical chess board. Also handles user interaction.
* @author petero
*/
public class ChessBoardPainter extends JLabel {
private static final long serialVersionUID = -1319250011487017825L;
private Position pos;
private int selectedSquare;
private int x0, y0, sqSize;
private boolean flipped;
private Font chessFont;
// For piece animation during dragging
private int activeSquare;
private boolean dragging;
private int dragX;
private int dragY;
private boolean cancelSelection;
ChessBoardPainter() {
pos = new Position();
selectedSquare = -1;
x0 = y0 = sqSize = 0;
flipped = false;
activeSquare = -1;
}
/**
* Set the board to a given state.
* @param pos
*/
final public void setPosition(Position pos) {
this.pos = pos;
repaint();
}
/**
* Set/clear the board flipped status.
* @param flipped
*/
final public void setFlipped(boolean flipped) {
this.flipped = flipped;
repaint();
}
/**
* Set/clear the selected square.
* @param square The square to select, or -1 to clear selection.
*/
final public void setSelection(int square) {
if (square != this.selectedSquare) {
this.selectedSquare = square;
repaint();
}
}
@Override
public void paint(Graphics g0) {
Graphics2D g = (Graphics2D)g0;
Dimension size = getSize();
sqSize = (Math.min(size.width, size.height) - 4) / 8;
x0 = (size.width - sqSize * 8) / 2;
y0 = (size.height - sqSize * 8) / 2;
boolean doDrag = (activeSquare >= 0) && dragging;
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
final int xCrd = getXCrd(x);
final int yCrd = getYCrd(y);
g.setColor(Position.darkSquare(x, y) ? Color.GRAY : new Color(190, 190, 90));
g.fillRect(xCrd, yCrd, sqSize, sqSize);
int sq = Position.getSquare(x, y);
int p = pos.getPiece(sq);
if (doDrag && (sq == activeSquare)) {
// Skip this piece. It will be drawn later at (dragX,dragY)
} else {
drawPiece(g, xCrd + sqSize / 2, yCrd + sqSize / 2, p);
}
}
}
if (selectedSquare >= 0) {
int selX = Position.getX(selectedSquare);
int selY = Position.getY(selectedSquare);
g.setColor(Color.RED);
g.setStroke(new BasicStroke(3));
g.drawRect(getXCrd(selX), getYCrd(selY), sqSize, sqSize);
}
if (doDrag) {
int p = pos.getPiece(activeSquare);
drawPiece(g, dragX, dragY, p);
}
}
private final void drawPiece(Graphics2D g, int xCrd, int yCrd, int p) {
g.setColor(Piece.isWhite(p) ? Color.WHITE : Color.BLACK);
String ps;
switch (p) {
case Piece.EMPTY:
ps = "";
break;
case Piece.WKING:
ps = "k";
break;
case Piece.WQUEEN:
ps = "q";
break;
case Piece.WROOK:
ps = "r";
break;
case Piece.WBISHOP:
ps = "b";
break;
case Piece.WKNIGHT:
ps = "n";
break;
case Piece.WPAWN:
ps = "p";
break;
case Piece.BKING:
ps = "l";
break;
case Piece.BQUEEN:
ps = "w";
break;
case Piece.BROOK:
ps = "t";
break;
case Piece.BBISHOP:
ps = "v";
break;
case Piece.BKNIGHT:
ps = "m";
break;
case Piece.BPAWN:
ps = "o";
break;
default:
ps = "?";
break;
}
if (ps.length() > 0) {
FontRenderContext frc = g.getFontRenderContext();
if ((chessFont == null) || (chessFont.getSize() != sqSize)) {
InputStream inStream = getClass().getResourceAsStream("/gui/casefont.ttf");
try {
Font font = Font.createFont(Font.TRUETYPE_FONT, inStream);
chessFont = font.deriveFont((float)sqSize);
} catch (FontFormatException ex) {
throw new RuntimeException();
} catch (IOException ex) {
throw new RuntimeException();
}
}
g.setFont(chessFont);
Rectangle2D textRect = g.getFont().getStringBounds(ps, frc);
int xCent = (int)textRect.getCenterX();
int yCent = (int)textRect.getCenterY();
g.drawString(ps, xCrd - xCent, yCrd - yCent);
}
}
private final int getXCrd(int x) {
return x0 + sqSize * (flipped ? 7 - x : x);
}
private final int getYCrd(int y) {
return y0 + sqSize * (flipped ? y : (7 - y));
}
/**
* Compute the square corresponding to the coordinates of a mouse event.
* @param evt Details about the mouse event.
* @return The square corresponding to the mouse event, or -1 if outside board.
*/
final int eventToSquare(MouseEvent evt) {
int xCrd = evt.getX();
int yCrd = evt.getY();
int sq = -1;
if ((xCrd >= x0) && (yCrd >= y0) && (sqSize > 0)) {
int x = (xCrd - x0) / sqSize;
int y = 7 - (yCrd - y0) / sqSize;
if ((x >= 0) && (x < 8) && (y >= 0) && (y < 8)) {
if (flipped) {
x = 7 - x;
y = 7 - y;
}
sq = Position.getSquare(x, y);
}
}
return sq;
}
final Move mousePressed(int sq) {
Move m = null;
cancelSelection = false;
int p = pos.getPiece(sq);
if ((selectedSquare >= 0) && (sq == selectedSquare)) {
int fromPiece = pos.getPiece(selectedSquare);
if ((fromPiece == Piece.EMPTY) || (Piece.isWhite(fromPiece) != pos.whiteMove)) {
return m; // Can't move the piece the oppenent just moved.
}
}
if ((selectedSquare < 0) &&
((p == Piece.EMPTY) || (Piece.isWhite(p) != pos.whiteMove))) {
return m; // You must click on one of your own pieces.
}
activeSquare = sq;
dragging = false;
dragX = dragY = -1;
if (selectedSquare >= 0) {
if (sq == selectedSquare) {
cancelSelection = true;
} else {
if ((p == Piece.EMPTY) || (Piece.isWhite(p) != pos.whiteMove)) {
m = new Move(selectedSquare, sq, Piece.EMPTY);
activeSquare = -1;
setSelection(sq);
}
}
}
if (m == null) {
setSelection(-1);
}
return m;
}
final void mouseDragged(MouseEvent evt) {
final int xCrd = evt.getX();
final int yCrd = evt.getY();
if (!dragging || (dragX != xCrd) || (dragY != yCrd)) {
dragging = true;
dragX = xCrd;
dragY = yCrd;
repaint();
}
}
final Move mouseReleased(int sq) {
Move m = null;
if (activeSquare >= 0) {
if (sq != activeSquare) {
m = new Move(activeSquare, sq, Piece.EMPTY);
setSelection(sq);
} else if (!cancelSelection) {
setSelection(sq);
}
activeSquare = -1;
repaint();
}
return m;
}
}

Binary file not shown.