<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="et">
	<id>http://www.lambda.ee/w/index.php?action=history&amp;feed=atom&amp;title=Loengus_tehtud_kabe_toorik</id>
	<title>Loengus tehtud kabe toorik - Redigeerimiste ajalugu</title>
	<link rel="self" type="application/atom+xml" href="http://www.lambda.ee/w/index.php?action=history&amp;feed=atom&amp;title=Loengus_tehtud_kabe_toorik"/>
	<link rel="alternate" type="text/html" href="http://www.lambda.ee/w/index.php?title=Loengus_tehtud_kabe_toorik&amp;action=history"/>
	<updated>2026-11-07T00:39:40Z</updated>
	<subtitle>Selle lehekülje redigeerimiste ajalugu</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>http://www.lambda.ee/w/index.php?title=Loengus_tehtud_kabe_toorik&amp;diff=7815&amp;oldid=prev</id>
		<title>Tammet – 29. november 2006, kell 16:13</title>
		<link rel="alternate" type="text/html" href="http://www.lambda.ee/w/index.php?title=Loengus_tehtud_kabe_toorik&amp;diff=7815&amp;oldid=prev"/>
		<updated>2006-11-29T16:13:04Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Uus lehekülg&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
   This applet lets two uses play checkers against each other.&lt;br /&gt;
   Red always starts the game.  If a player can jump an opponent&amp;#039;s&lt;br /&gt;
   piece, then the player must jump.  When a plyer can make no more&lt;br /&gt;
   moves, the game ends.&lt;br /&gt;
   &lt;br /&gt;
   The applet size should be 350-by-250 (or very close to that).&lt;br /&gt;
   &lt;br /&gt;
   This file defines four class files, all of which are necessary&lt;br /&gt;
   to use the applet.&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
import java.awt.*;&lt;br /&gt;
import java.awt.event.*;&lt;br /&gt;
import javax.swing.*;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Checkers extends JFrame {&lt;br /&gt;
&lt;br /&gt;
   /* The main applet class only lays out the applet.  The work of&lt;br /&gt;
      the game is all done in the Board object.  A null layout&lt;br /&gt;
      is used, and all setup of sizes and positions is done here.*/&lt;br /&gt;
      &lt;br /&gt;
   JButton newGameButton;  // Button for starting a new game.&lt;br /&gt;
   JButton resignButton;   // Button that a player can use to end the &lt;br /&gt;
                           //   game by resigning.&lt;br /&gt;
   &lt;br /&gt;
   JLabel message;  // Label for displaying messages to the user.&lt;br /&gt;
&lt;br /&gt;
   public static void main(String[] args) {&lt;br /&gt;
        // create a new window, call init and&lt;br /&gt;
        // set the size, location and visibility of a window&lt;br /&gt;
        Checkers frame = new Checkers();&lt;br /&gt;
        frame.init(); &lt;br /&gt;
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&lt;br /&gt;
        frame.setSize(500,500);&lt;br /&gt;
        frame.setLocation(200,200);        &lt;br /&gt;
        frame.setVisible(true);          &lt;br /&gt;
    }    &lt;br /&gt;
    &lt;br /&gt;
   public void init() {&lt;br /&gt;
   &lt;br /&gt;
      Container content = getContentPane();  // Content pane of applet.&lt;br /&gt;
   &lt;br /&gt;
      content.setLayout(null);  // I will do the layout myself.&lt;br /&gt;
   &lt;br /&gt;
      content.setBackground(new Color(0,150,0));  // Dark green background.&lt;br /&gt;
      &lt;br /&gt;
      /* Create the components and add them to the applet. */&lt;br /&gt;
      &lt;br /&gt;
      Board board = new Board();  // Note: The constructor for the&lt;br /&gt;
                                  //   board also creates the buttons&lt;br /&gt;
                                  //   and label.&lt;br /&gt;
      content.add(board);&lt;br /&gt;
      content.add(newGameButton);&lt;br /&gt;
      content.add(resignButton);&lt;br /&gt;
      content.add(message);&lt;br /&gt;
      &lt;br /&gt;
      /* Set the position and size of each component by calling&lt;br /&gt;
         its setBounds() method. */&lt;br /&gt;
&lt;br /&gt;
      board.setBounds(20,20,164,164); // Note:  size MUST be 164-by-164 !&lt;br /&gt;
      newGameButton.setBounds(210, 60, 120, 30);&lt;br /&gt;
      resignButton.setBounds(210, 120, 120, 30);&lt;br /&gt;
      message.setBounds(0, 200, 350, 30);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   // --------------------  Nested Classes -------------------------------&lt;br /&gt;
   &lt;br /&gt;
   static class CheckersMove {&lt;br /&gt;
        // A CheckersMove object represents a move in the game of Checkers.&lt;br /&gt;
        // It holds the row and column of the piece that is to be moved&lt;br /&gt;
        // and the row and column of the square to which it is to be moved.&lt;br /&gt;
        // (This class makes no guarantee that the move is legal.)&lt;br /&gt;
      int fromRow, fromCol;  // Position of piece to be moved.&lt;br /&gt;
      int toRow, toCol;      // Square it is to move to.&lt;br /&gt;
      CheckersMove(int r1, int c1, int r2, int c2) {&lt;br /&gt;
           // Constructor.  Just set the values of the instance variables.&lt;br /&gt;
         fromRow = r1;&lt;br /&gt;
         fromCol = c1;&lt;br /&gt;
         toRow = r2;&lt;br /&gt;
         toCol = c2;&lt;br /&gt;
      }&lt;br /&gt;
      boolean isJump() {&lt;br /&gt;
           // Test whether this move is a jump.  It is assumed that&lt;br /&gt;
           // the move is legal.  In a jump, the piece moves two&lt;br /&gt;
           // rows.  (In a regular move, it only moves one row.)&lt;br /&gt;
         return (fromRow - toRow == 2 || fromRow - toRow == -2);&lt;br /&gt;
      }&lt;br /&gt;
   }  // end class CheckersMove.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   class Board extends JPanel implements ActionListener, MouseListener {&lt;br /&gt;
&lt;br /&gt;
        // This canvas displays a 160-by-160 checkerboard pattern with&lt;br /&gt;
        // a 2-pixel black border.  It is assumed that the size of the&lt;br /&gt;
        // canvas is set to exactly 164-by-164 pixels.  This class does&lt;br /&gt;
        // the work of letting the users play checkers, and it displays&lt;br /&gt;
        // the checkerboard.&lt;br /&gt;
&lt;br /&gt;
      CheckersData board;  // The data for the checkers board is kept here.&lt;br /&gt;
                           //    This board is also responsible for generating&lt;br /&gt;
                           //    lists of legal moves.&lt;br /&gt;
&lt;br /&gt;
      boolean gameInProgress; // Is a game currently in progress?&lt;br /&gt;
&lt;br /&gt;
      /* The next three variables are valid only when the game is in progress. */&lt;br /&gt;
&lt;br /&gt;
      int currentPlayer;      // Whose turn is it now?  The possible values&lt;br /&gt;
                              //    are CheckersData.RED and CheckersData.BLACK.&lt;br /&gt;
      int selectedRow, selectedCol;  // If the current player has selected a piece to&lt;br /&gt;
                                     //     move, these give the row and column&lt;br /&gt;
                                     //     containing that piece.  If no piece is&lt;br /&gt;
                                     //     yet selected, then selectedRow is -1.&lt;br /&gt;
      CheckersMove[] legalMoves;  // An array containing the legal moves for the&lt;br /&gt;
                                  //   current player.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      public Board() {&lt;br /&gt;
             // Constructor.  Create the buttons and lable.  Listen for mouse&lt;br /&gt;
             // clicks and for clicks on the buttons.  Create the board and&lt;br /&gt;
             // start the first game.&lt;br /&gt;
         setBackground(Color.black);&lt;br /&gt;
         addMouseListener(this);&lt;br /&gt;
         resignButton = new JButton(&amp;quot;Resign&amp;quot;);&lt;br /&gt;
         resignButton.addActionListener(this);&lt;br /&gt;
         newGameButton = new JButton(&amp;quot;New Game&amp;quot;);&lt;br /&gt;
         newGameButton.addActionListener(this);&lt;br /&gt;
         message = new JLabel(&amp;quot;&amp;quot;,JLabel.CENTER);&lt;br /&gt;
         message.setFont(new  Font(&amp;quot;Serif&amp;quot;, Font.BOLD, 14));&lt;br /&gt;
         message.setForeground(Color.green);&lt;br /&gt;
         board = new CheckersData();&lt;br /&gt;
         doNewGame();&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      public void actionPerformed(ActionEvent evt) {&lt;br /&gt;
            // Respond to user&amp;#039;s click on one of the two buttons.&lt;br /&gt;
         Object src = evt.getSource();&lt;br /&gt;
         if (src == newGameButton)&lt;br /&gt;
            doNewGame();&lt;br /&gt;
         else if (src == resignButton)&lt;br /&gt;
            doResign();&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      void doNewGame() {&lt;br /&gt;
            // Begin a new game.&lt;br /&gt;
         if (gameInProgress == true) {&lt;br /&gt;
                // This should not be possible, but it doens&amp;#039;t &lt;br /&gt;
                // hurt to check.&lt;br /&gt;
            message.setText(&amp;quot;Finish the current game first!&amp;quot;);&lt;br /&gt;
            return;&lt;br /&gt;
         }&lt;br /&gt;
         board.setUpGame();   // Set up the pieces.&lt;br /&gt;
         currentPlayer = CheckersData.RED;   // RED moves first.&lt;br /&gt;
         legalMoves = board.getLegalMoves(CheckersData.RED);  // Get RED&amp;#039;s legal moves.&lt;br /&gt;
         selectedRow = -1;   // RED has not yet selected a piece to move.&lt;br /&gt;
         message.setText(&amp;quot;Red:  Make your move.&amp;quot;);&lt;br /&gt;
         gameInProgress = true;&lt;br /&gt;
         newGameButton.setEnabled(false);&lt;br /&gt;
         resignButton.setEnabled(true);&lt;br /&gt;
         System.out.println(&amp;quot;repainti call&amp;quot;);&lt;br /&gt;
         repaint();&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      void doResign() {&lt;br /&gt;
             // Current player resigns.  Game ends.  Opponent wins.&lt;br /&gt;
          if (gameInProgress == false) {&lt;br /&gt;
             message.setText(&amp;quot;There is no game in progress!&amp;quot;);&lt;br /&gt;
             return;&lt;br /&gt;
          }&lt;br /&gt;
          if (currentPlayer == CheckersData.RED)&lt;br /&gt;
             gameOver(&amp;quot;RED resigns.  BLACK wins.&amp;quot;);&lt;br /&gt;
          else&lt;br /&gt;
             gameOver(&amp;quot;BLACK resigns.  RED wins.&amp;quot;);&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      void gameOver(String str) {&lt;br /&gt;
             // The game ends.  The parameter, str, is displayed as a message&lt;br /&gt;
             // to the user.  The states of the buttons are adjusted so playes&lt;br /&gt;
             // can start a new game.&lt;br /&gt;
         message.setText(str);&lt;br /&gt;
         newGameButton.setEnabled(true);&lt;br /&gt;
         resignButton.setEnabled(false);&lt;br /&gt;
         gameInProgress = false;&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      void doClickSquare(int row, int col) {&lt;br /&gt;
            // This is called by mousePressed() when a player clicks on the&lt;br /&gt;
            // square in the specified row and col.  It has already been checked&lt;br /&gt;
            // that a game is, in fact, in progress.&lt;br /&gt;
&lt;br /&gt;
         /* If the player clicked on one of the pieces that the player&lt;br /&gt;
            can move, mark this row and col as selected and return.  (This&lt;br /&gt;
            might change a previous selection.)  Reset the message, in&lt;br /&gt;
            case it was previously displaying an error message. */&lt;br /&gt;
&lt;br /&gt;
         for (int i = 0; i &amp;lt; legalMoves.length; i++)&lt;br /&gt;
            if (legalMoves[i].fromRow == row &amp;amp;&amp;amp; legalMoves[i].fromCol == col) {&lt;br /&gt;
               selectedRow = row;&lt;br /&gt;
               selectedCol = col;&lt;br /&gt;
               if (currentPlayer == CheckersData.RED)&lt;br /&gt;
                  message.setText(&amp;quot;RED:  Make your move.&amp;quot;);&lt;br /&gt;
               else&lt;br /&gt;
                  message.setText(&amp;quot;BLACK:  Make your move.&amp;quot;);&lt;br /&gt;
               repaint();&lt;br /&gt;
               return;&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
         /* If no piece has been selected to be moved, the user must first&lt;br /&gt;
            select a piece.  Show an error message and return. */&lt;br /&gt;
&lt;br /&gt;
         if (selectedRow &amp;lt; 0) {&lt;br /&gt;
             message.setText(&amp;quot;Click the piece you want to move.&amp;quot;);&lt;br /&gt;
             return;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         /* If the user clicked on a squre where the selected piece can be&lt;br /&gt;
            legally moved, then make the move and return. */&lt;br /&gt;
&lt;br /&gt;
         for (int i = 0; i &amp;lt; legalMoves.length; i++)&lt;br /&gt;
            if (legalMoves[i].fromRow == selectedRow &amp;amp;&amp;amp; legalMoves[i].fromCol == selectedCol&lt;br /&gt;
                    &amp;amp;&amp;amp; legalMoves[i].toRow == row &amp;amp;&amp;amp; legalMoves[i].toCol == col) {&lt;br /&gt;
               doMakeMove(legalMoves[i]);&lt;br /&gt;
               System.out.println(&amp;quot;doMakeMove sai labi&amp;quot;);         &lt;br /&gt;
               return;&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
         /* If we get to this point, there is a piece selected, and the square where&lt;br /&gt;
            the user just clicked is not one where that piece can be legally moved.&lt;br /&gt;
            Show an error message. */&lt;br /&gt;
&lt;br /&gt;
         message.setText(&amp;quot;Click the square you want to move to.&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
      }  // end doClickSquare()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      void doMakeMove(CheckersMove move) {&lt;br /&gt;
             // This is called when the current player has chosen the specified&lt;br /&gt;
             // move.  Make the move, and then either end or continue the game&lt;br /&gt;
             // appropriately.&lt;br /&gt;
&lt;br /&gt;
         board.makeMove(move);&lt;br /&gt;
&lt;br /&gt;
         /* If the move was a jump, it&amp;#039;s possible that the player has another&lt;br /&gt;
            jump.  Check for legal jumps starting from the square that the player&lt;br /&gt;
            just moved to.  If there are any, the player must jump.  The same&lt;br /&gt;
            player continues moving.&lt;br /&gt;
         */&lt;br /&gt;
&lt;br /&gt;
         if (move.isJump()) {&lt;br /&gt;
            legalMoves = board.getLegalJumpsFrom(currentPlayer,move.toRow,move.toCol);&lt;br /&gt;
            if (legalMoves != null) {&lt;br /&gt;
               if (currentPlayer == CheckersData.RED)&lt;br /&gt;
                  message.setText(&amp;quot;RED:  You must continue jumping.&amp;quot;);&lt;br /&gt;
               else&lt;br /&gt;
                  message.setText(&amp;quot;BLACK:  You must continue jumping.&amp;quot;);&lt;br /&gt;
               selectedRow = move.toRow;  // Since only one piece can be moved, select it.&lt;br /&gt;
               selectedCol = move.toCol;&lt;br /&gt;
               repaint();&lt;br /&gt;
               return;&lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         /* The current player&amp;#039;s turn is ended, so change to the other player.&lt;br /&gt;
            Get that player&amp;#039;s legal moves.  If the player has no legal moves,&lt;br /&gt;
            then the game ends. */&lt;br /&gt;
&lt;br /&gt;
         if (currentPlayer == CheckersData.RED) {&lt;br /&gt;
            currentPlayer = CheckersData.BLACK;&lt;br /&gt;
            legalMoves = board.getLegalMoves(currentPlayer);&lt;br /&gt;
            if (legalMoves == null)&lt;br /&gt;
               gameOver(&amp;quot;BLACK has no moves.  RED wins.&amp;quot;);&lt;br /&gt;
            else if (legalMoves[0].isJump())&lt;br /&gt;
               message.setText(&amp;quot;BLACK:  Make your move.  You must jump.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
               message.setText(&amp;quot;BLACK:  Make your move.&amp;quot;);&lt;br /&gt;
         }&lt;br /&gt;
         else {&lt;br /&gt;
            currentPlayer = CheckersData.RED;&lt;br /&gt;
            legalMoves = board.getLegalMoves(currentPlayer);&lt;br /&gt;
            if (legalMoves == null)&lt;br /&gt;
               gameOver(&amp;quot;RED has no moves.  BLACK wins.&amp;quot;);&lt;br /&gt;
            else if (legalMoves[0].isJump())&lt;br /&gt;
               message.setText(&amp;quot;RED:  Make your move.  You must jump.&amp;quot;);&lt;br /&gt;
            else&lt;br /&gt;
               message.setText(&amp;quot;RED:  Make your move.&amp;quot;);&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         /* Set selectedRow = -1 to record that the player has not yet selected&lt;br /&gt;
             a piece to move. */&lt;br /&gt;
&lt;br /&gt;
         selectedRow = -1;&lt;br /&gt;
&lt;br /&gt;
         /* As a courtesy to the user, if all legal moves use the same piece, then&lt;br /&gt;
            select that piece automatically so the use won&amp;#039;t have to click on it&lt;br /&gt;
            to select it. */&lt;br /&gt;
&lt;br /&gt;
         if (legalMoves != null) {&lt;br /&gt;
            boolean sameStartSquare = true;&lt;br /&gt;
            for (int i = 1; i &amp;lt; legalMoves.length; i++)&lt;br /&gt;
               if (legalMoves[i].fromRow != legalMoves[0].fromRow&lt;br /&gt;
                                    || legalMoves[i].fromCol != legalMoves[0].fromCol) {&lt;br /&gt;
                   sameStartSquare = false;&lt;br /&gt;
                   break;&lt;br /&gt;
               }&lt;br /&gt;
            if (sameStartSquare) {&lt;br /&gt;
               selectedRow = legalMoves[0].fromRow;&lt;br /&gt;
               selectedCol = legalMoves[0].fromCol;&lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         /* Make sure the board is redrawn in its new state. */&lt;br /&gt;
&lt;br /&gt;
         repaint();&lt;br /&gt;
         System.out.println(&amp;quot;Current player: &amp;quot;);&lt;br /&gt;
         if (currentPlayer == CheckersData.RED)&lt;br /&gt;
             System.out.println(&amp;quot;red&amp;quot;);&lt;br /&gt;
         else {&lt;br /&gt;
             // Think now!&lt;br /&gt;
             System.out.println(&amp;quot;black&amp;quot;);&lt;br /&gt;
             legalMoves = board.getLegalMoves(currentPlayer); &lt;br /&gt;
             double val=staticAnalyze(board);&lt;br /&gt;
             System.out.println(&amp;quot;analyzer gives &amp;quot;+val);&lt;br /&gt;
             int i;&lt;br /&gt;
             for (i=0; i&amp;lt;legalMoves.length; i++) {&lt;br /&gt;
                System.out.println(&amp;quot;move nr &amp;quot;+i+&amp;quot; is &amp;quot;+&lt;br /&gt;
                   legalMoves[i].fromRow+&amp;quot; &amp;quot;+ &lt;br /&gt;
                   legalMoves[i].fromCol+&amp;quot; &amp;quot;+&lt;br /&gt;
                   legalMoves[i].toRow+&amp;quot; &amp;quot;+ &lt;br /&gt;
                   legalMoves[i].toCol) ;                &lt;br /&gt;
             }&lt;br /&gt;
             doMakeMove(legalMoves[0]);&lt;br /&gt;
             repaint();             &lt;br /&gt;
         }    &lt;br /&gt;
&lt;br /&gt;
      }  // end doMakeMove();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      public void paintComponent(Graphics g) {&lt;br /&gt;
           // Draw  checkerboard pattern in gray and lightGray.  Draw the&lt;br /&gt;
           // checkers.  If a game is in progress, hilite the legal moves.&lt;br /&gt;
&lt;br /&gt;
         /* Draw a two-pixel black border around the edges of the canvas. */&lt;br /&gt;
&lt;br /&gt;
         g.setColor(Color.black);&lt;br /&gt;
         g.drawRect(0,0,getSize().width-1,getSize().height-1);&lt;br /&gt;
         g.drawRect(1,1,getSize().width-3,getSize().height-3);&lt;br /&gt;
&lt;br /&gt;
         /* Draw the squares of the checkerboard and the checkers. */&lt;br /&gt;
&lt;br /&gt;
         for (int row = 0; row &amp;lt; 8; row++) {&lt;br /&gt;
            for (int col = 0; col &amp;lt; 8; col++) {&lt;br /&gt;
                if ( row % 2 == col % 2 )&lt;br /&gt;
                   g.setColor(Color.lightGray);&lt;br /&gt;
                else&lt;br /&gt;
                   g.setColor(Color.gray);&lt;br /&gt;
                g.fillRect(2 + col*20, 2 + row*20, 20, 20);&lt;br /&gt;
                switch (board.pieceAt(row,col)) {&lt;br /&gt;
                   case CheckersData.RED:&lt;br /&gt;
                      g.setColor(Color.red);&lt;br /&gt;
                      g.fillOval(4 + col*20, 4 + row*20, 16, 16);&lt;br /&gt;
                      break;&lt;br /&gt;
                   case CheckersData.BLACK:&lt;br /&gt;
                      g.setColor(Color.black);&lt;br /&gt;
                      g.fillOval(4 + col*20, 4 + row*20, 16, 16);&lt;br /&gt;
                      break;&lt;br /&gt;
                   case CheckersData.RED_KING:&lt;br /&gt;
                      g.setColor(Color.red);&lt;br /&gt;
                      g.fillOval(4 + col*20, 4 + row*20, 16, 16);&lt;br /&gt;
                      g.setColor(Color.white);&lt;br /&gt;
                      g.drawString(&amp;quot;K&amp;quot;, 7 + col*20, 16 + row*20);&lt;br /&gt;
                      break;&lt;br /&gt;
                   case CheckersData.BLACK_KING:&lt;br /&gt;
                      g.setColor(Color.black);&lt;br /&gt;
                      g.fillOval(4 + col*20, 4 + row*20, 16, 16);&lt;br /&gt;
                      g.setColor(Color.white);&lt;br /&gt;
                      g.drawString(&amp;quot;K&amp;quot;, 7 + col*20, 16 + row*20);&lt;br /&gt;
                      break;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         /* If a game is in progress, hilite the legal moves.   Note that legalMoves&lt;br /&gt;
            is never null while a game is in progress. */      &lt;br /&gt;
&lt;br /&gt;
         if (gameInProgress) {&lt;br /&gt;
               // First, draw a cyan border around the pieces that can be moved.&lt;br /&gt;
            g.setColor(Color.cyan);&lt;br /&gt;
            for (int i = 0; i &amp;lt; legalMoves.length; i++) {&lt;br /&gt;
               g.drawRect(2 + legalMoves[i].fromCol*20, 2 + legalMoves[i].fromRow*20, 19, 19);&lt;br /&gt;
            }&lt;br /&gt;
               // If a piece is selected for moving (i.e. if selectedRow &amp;gt;= 0), then&lt;br /&gt;
               // draw a 2-pixel white border around that piece and draw green borders &lt;br /&gt;
               // around eacj square that that piece can be moved to.&lt;br /&gt;
            if (selectedRow &amp;gt;= 0) {&lt;br /&gt;
               g.setColor(Color.white);&lt;br /&gt;
               g.drawRect(2 + selectedCol*20, 2 + selectedRow*20, 19, 19);&lt;br /&gt;
               g.drawRect(3 + selectedCol*20, 3 + selectedRow*20, 17, 17);&lt;br /&gt;
               g.setColor(Color.green);&lt;br /&gt;
               for (int i = 0; i &amp;lt; legalMoves.length; i++) {&lt;br /&gt;
                  if (legalMoves[i].fromCol == selectedCol &amp;amp;&amp;amp; legalMoves[i].fromRow == selectedRow)&lt;br /&gt;
                     g.drawRect(2 + legalMoves[i].toCol*20, 2 + legalMoves[i].toRow*20, 19, 19);&lt;br /&gt;
               }&lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
      }  // end paintComponent()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      public Dimension getPreferredSize() {&lt;br /&gt;
            // Specify desired size for this component.  Note:&lt;br /&gt;
            // the size MUST be 164 by 164.&lt;br /&gt;
         return new Dimension(164, 164);&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      public Dimension getMinimumSize() {&lt;br /&gt;
         return new Dimension(164, 164);&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      public Dimension getMaximumSize() {&lt;br /&gt;
         return new Dimension(164, 164);&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      public void mousePressed(MouseEvent evt) {&lt;br /&gt;
            // Respond to a user click on the board.  If no game is&lt;br /&gt;
            // in progress, show an error message.  Otherwise, find&lt;br /&gt;
            // the row and column that the user clicked and call&lt;br /&gt;
            // doClickSquare() to handle it.&lt;br /&gt;
         if (gameInProgress == false)&lt;br /&gt;
            message.setText(&amp;quot;Click \&amp;quot;New Game\&amp;quot; to start a new game.&amp;quot;);&lt;br /&gt;
         else {&lt;br /&gt;
            int col = (evt.getX() - 2) / 20;&lt;br /&gt;
            int row = (evt.getY() - 2) / 20;&lt;br /&gt;
            if (col &amp;gt;= 0 &amp;amp;&amp;amp; col &amp;lt; 8 &amp;amp;&amp;amp; row &amp;gt;= 0 &amp;amp;&amp;amp; row &amp;lt; 8)&lt;br /&gt;
               doClickSquare(row,col);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      public void mouseReleased(MouseEvent evt) { }&lt;br /&gt;
      public void mouseClicked(MouseEvent evt) { }&lt;br /&gt;
      public void mouseEntered(MouseEvent evt) { }&lt;br /&gt;
      public void mouseExited(MouseEvent evt) { }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   }  // end class Board&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   static class CheckersData {&lt;br /&gt;
&lt;br /&gt;
         // An object of this class holds data about a game of checkers.&lt;br /&gt;
         // It knows what kind of piece is on each sqaure of the checkerboard.&lt;br /&gt;
         // Note that RED moves &amp;quot;up&amp;quot; the board (i.e. row number decreases)&lt;br /&gt;
         // while BLACK moves &amp;quot;down&amp;quot; the board (i.e. row number increases).&lt;br /&gt;
         // Methods are provided to return lists of available legal moves.&lt;br /&gt;
&lt;br /&gt;
      /*  The following constants represent the possible contents of a square&lt;br /&gt;
          on the board.  The constants RED and BLACK also represent players&lt;br /&gt;
          in the game.&lt;br /&gt;
      */&lt;br /&gt;
&lt;br /&gt;
      public static final int&lt;br /&gt;
                EMPTY = 0,&lt;br /&gt;
                RED = 1,&lt;br /&gt;
                RED_KING = 2,&lt;br /&gt;
                BLACK = 3,&lt;br /&gt;
                BLACK_KING = 4;&lt;br /&gt;
&lt;br /&gt;
      private int[][] board;  // board[r][c] is the contents of row r, column c.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      public CheckersData() {&lt;br /&gt;
            // Constructor.  Create the board and set it up for a new game.&lt;br /&gt;
         board = new int[8][8];&lt;br /&gt;
         setUpGame();&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
      &lt;br /&gt;
      &lt;br /&gt;
      public void setUpGame() {&lt;br /&gt;
             // Set up the board with checkers in position for the beginning&lt;br /&gt;
             // of a game.  Note that checkers can only be found in squares&lt;br /&gt;
             // that satisfy  row % 2 == col % 2.  At the start of the game,&lt;br /&gt;
             // all such squares in the first three rows contain black squares&lt;br /&gt;
             // and all such squares in the last three rows contain red squares.&lt;br /&gt;
         for (int row = 0; row &amp;lt; 8; row++) {&lt;br /&gt;
            for (int col = 0; col &amp;lt; 8; col++) {&lt;br /&gt;
               if ( row % 2 == col % 2 ) {&lt;br /&gt;
                  if (row &amp;lt; 3)&lt;br /&gt;
                     board[row][col] = BLACK;&lt;br /&gt;
                  else if (row &amp;gt; 4)&lt;br /&gt;
                     board[row][col] = RED;&lt;br /&gt;
                  else&lt;br /&gt;
                     board[row][col] = EMPTY;&lt;br /&gt;
               }&lt;br /&gt;
               else {&lt;br /&gt;
                  board[row][col] = EMPTY;&lt;br /&gt;
               }&lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
      }  // end setUpGame()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      public int pieceAt(int row, int col) {&lt;br /&gt;
             // Return the contents of the square in the specified row and column.&lt;br /&gt;
          return board[row][col];&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      public void setPieceAt(int row, int col, int piece) {&lt;br /&gt;
             // Set the contents of the square in the specified row and column.&lt;br /&gt;
             // piece must be one of the constants EMPTY, RED, BLACK, RED_KING,&lt;br /&gt;
             // BLACK_KING.&lt;br /&gt;
          board[row][col] = piece;&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      public void makeMove(CheckersMove move) {&lt;br /&gt;
            // Make the specified move.  It is assumed that move&lt;br /&gt;
            // is non-null and that the move it represents is legal.&lt;br /&gt;
         makeMove(move.fromRow, move.fromCol, move.toRow, move.toCol);&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      public void makeMove(int fromRow, int fromCol, int toRow, int toCol) {&lt;br /&gt;
            // Make the move from (fromRow,fromCol) to (toRow,toCol).  It is&lt;br /&gt;
            // assumed that this move is legal.  If the move is a jump, the&lt;br /&gt;
            // jumped piece is removed from the board.  If a piece moves&lt;br /&gt;
            // the last row on the opponent&amp;#039;s side of the board, the &lt;br /&gt;
            // piece becomes a king.&lt;br /&gt;
         board[toRow][toCol] = board[fromRow][fromCol];&lt;br /&gt;
         board[fromRow][fromCol] = EMPTY;&lt;br /&gt;
         if (fromRow - toRow == 2 || fromRow - toRow == -2) {&lt;br /&gt;
               // The move is a jump.  Remove the jumped piece from the board.&lt;br /&gt;
            int jumpRow = (fromRow + toRow) / 2;  // Row of the jumped piece.&lt;br /&gt;
            int jumpCol = (fromCol + toCol) / 2;  // Column of the jumped piece.&lt;br /&gt;
            board[jumpRow][jumpCol] = EMPTY;&lt;br /&gt;
         }&lt;br /&gt;
         if (toRow == 0 &amp;amp;&amp;amp; board[toRow][toCol] == RED)&lt;br /&gt;
            board[toRow][toCol] = RED_KING;&lt;br /&gt;
         if (toRow == 7 &amp;amp;&amp;amp; board[toRow][toCol] == BLACK)&lt;br /&gt;
            board[toRow][toCol] = BLACK_KING;&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      public CheckersMove[] getLegalMoves(int player) {&lt;br /&gt;
             // Return an array containing all the legal CheckersMoves&lt;br /&gt;
             // for the specfied player on the current board.  If the player&lt;br /&gt;
             // has no legal moves, null is returned.  The value of player&lt;br /&gt;
             // should be one of the constants RED or BLACK; if not, null&lt;br /&gt;
             // is returned.  If the returned value is non-null, it consists&lt;br /&gt;
             // entirely of jump moves or entirely of regular moves, since&lt;br /&gt;
             // if the player can jump, only jumps are legal moves.&lt;br /&gt;
&lt;br /&gt;
         if (player != RED &amp;amp;&amp;amp; player != BLACK)&lt;br /&gt;
            return null;&lt;br /&gt;
&lt;br /&gt;
         int playerKing;  // The constant representing a King belonging to player.&lt;br /&gt;
         if (player == RED)&lt;br /&gt;
            playerKing = RED_KING;&lt;br /&gt;
         else&lt;br /&gt;
            playerKing = BLACK_KING;&lt;br /&gt;
&lt;br /&gt;
         ArrayList moves = new ArrayList();  // Moves will be stored in this list.&lt;br /&gt;
&lt;br /&gt;
         /*  First, check for any possible jumps.  Look at each square on the board.&lt;br /&gt;
             If that square contains one of the player&amp;#039;s pieces, look at a possible&lt;br /&gt;
             jump in each of the four directions from that square.  If there is &lt;br /&gt;
             a legal jump in that direction, put it in the moves ArrayList.&lt;br /&gt;
         */&lt;br /&gt;
&lt;br /&gt;
         for (int row = 0; row &amp;lt; 8; row++) {&lt;br /&gt;
            for (int col = 0; col &amp;lt; 8; col++) {&lt;br /&gt;
               if (board[row][col] == player || board[row][col] == playerKing) {&lt;br /&gt;
                  if (canJump(player, row, col, row+1, col+1, row+2, col+2))&lt;br /&gt;
                     moves.add(new CheckersMove(row, col, row+2, col+2));&lt;br /&gt;
                  if (canJump(player, row, col, row-1, col+1, row-2, col+2))&lt;br /&gt;
                     moves.add(new CheckersMove(row, col, row-2, col+2));&lt;br /&gt;
                  if (canJump(player, row, col, row+1, col-1, row+2, col-2))&lt;br /&gt;
                     moves.add(new CheckersMove(row, col, row+2, col-2));&lt;br /&gt;
                  if (canJump(player, row, col, row-1, col-1, row-2, col-2))&lt;br /&gt;
                     moves.add(new CheckersMove(row, col, row-2, col-2));&lt;br /&gt;
               }&lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         /*  If any jump moves were found, then the user must jump, so we don&amp;#039;t &lt;br /&gt;
             add any regular moves.  However, if no jumps were found, check for&lt;br /&gt;
             any legal regualar moves.  Look at each square on the board.&lt;br /&gt;
             If that square contains one of the player&amp;#039;s pieces, look at a possible&lt;br /&gt;
             move in each of the four directions from that square.  If there is &lt;br /&gt;
             a legal move in that direction, put it in the moves ArrayList.&lt;br /&gt;
         */&lt;br /&gt;
&lt;br /&gt;
         if (moves.size() == 0) {&lt;br /&gt;
            for (int row = 0; row &amp;lt; 8; row++) {&lt;br /&gt;
               for (int col = 0; col &amp;lt; 8; col++) {&lt;br /&gt;
                  if (board[row][col] == player || board[row][col] == playerKing) {&lt;br /&gt;
                     if (canMove(player,row,col,row+1,col+1))&lt;br /&gt;
                        moves.add(new CheckersMove(row,col,row+1,col+1));&lt;br /&gt;
                     if (canMove(player,row,col,row-1,col+1))&lt;br /&gt;
                        moves.add(new CheckersMove(row,col,row-1,col+1));&lt;br /&gt;
                     if (canMove(player,row,col,row+1,col-1))&lt;br /&gt;
                        moves.add(new CheckersMove(row,col,row+1,col-1));&lt;br /&gt;
                     if (canMove(player,row,col,row-1,col-1))&lt;br /&gt;
                        moves.add(new CheckersMove(row,col,row-1,col-1));&lt;br /&gt;
                  }&lt;br /&gt;
               }&lt;br /&gt;
            }&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
         /* If no legal moves have been found, return null.  Otherwise, create&lt;br /&gt;
            an array just big enough to hold all the legal moves, copy the&lt;br /&gt;
            legal moves from the ArrayList into the array, and return the array. */&lt;br /&gt;
&lt;br /&gt;
         if (moves.size() == 0)&lt;br /&gt;
            return null;&lt;br /&gt;
         else {&lt;br /&gt;
            CheckersMove[] moveArray = new CheckersMove[moves.size()];&lt;br /&gt;
            for (int i = 0; i &amp;lt; moves.size(); i++)&lt;br /&gt;
               moveArray[i] = (CheckersMove)moves.get(i);&lt;br /&gt;
            return moveArray;&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
      }  // end getLegalMoves&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      public CheckersMove[] getLegalJumpsFrom(int player, int row, int col) {&lt;br /&gt;
            // Return a list of the legal jumps that the specified player can&lt;br /&gt;
            // make starting from the specified row and column.  If no such&lt;br /&gt;
            // jumps are possible, null is returned.  The logic is similar&lt;br /&gt;
            // to the logic of the getLegalMoves() method.&lt;br /&gt;
         if (player != RED &amp;amp;&amp;amp; player != BLACK)&lt;br /&gt;
            return null;&lt;br /&gt;
         int playerKing;  // The constant representing a King belonging to player.&lt;br /&gt;
         if (player == RED)&lt;br /&gt;
            playerKing = RED_KING;&lt;br /&gt;
         else&lt;br /&gt;
            playerKing = BLACK_KING;&lt;br /&gt;
         ArrayList moves = new ArrayList();  // The legal jumps will be stored in this list.&lt;br /&gt;
         if (board[row][col] == player || board[row][col] == playerKing) {&lt;br /&gt;
            if (canJump(player, row, col, row+1, col+1, row+2, col+2))&lt;br /&gt;
               moves.add(new CheckersMove(row, col, row+2, col+2));&lt;br /&gt;
            if (canJump(player, row, col, row-1, col+1, row-2, col+2))&lt;br /&gt;
               moves.add(new CheckersMove(row, col, row-2, col+2));&lt;br /&gt;
            if (canJump(player, row, col, row+1, col-1, row+2, col-2))&lt;br /&gt;
               moves.add(new CheckersMove(row, col, row+2, col-2));&lt;br /&gt;
            if (canJump(player, row, col, row-1, col-1, row-2, col-2))&lt;br /&gt;
               moves.add(new CheckersMove(row, col, row-2, col-2));&lt;br /&gt;
         }&lt;br /&gt;
         if (moves.size() == 0)&lt;br /&gt;
            return null;&lt;br /&gt;
         else {&lt;br /&gt;
            CheckersMove[] moveArray = new CheckersMove[moves.size()];&lt;br /&gt;
            for (int i = 0; i &amp;lt; moves.size(); i++)&lt;br /&gt;
               moveArray[i] = (CheckersMove)moves.get(i);&lt;br /&gt;
            return moveArray;&lt;br /&gt;
         }&lt;br /&gt;
      }  // end getLegalMovesFrom()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      private boolean canJump(int player, int r1, int c1, int r2, int c2, int r3, int c3) {&lt;br /&gt;
              // This is called by the two previous methods to check whether the&lt;br /&gt;
              // player can legally jump from (r1,c1) to (r3,c3).  It is assumed&lt;br /&gt;
              // that the player has a piece at (r1,c1), that (r3,c3) is a position&lt;br /&gt;
              // that is 2 rows and 2 columns distant from (r1,c1) and that &lt;br /&gt;
              // (r2,c2) is the square between (r1,c1) and (r3,c3).&lt;br /&gt;
&lt;br /&gt;
         if (r3 &amp;lt; 0 || r3 &amp;gt;= 8 || c3 &amp;lt; 0 || c3 &amp;gt;= 8)&lt;br /&gt;
            return false;  // (r3,c3) is off the board.&lt;br /&gt;
&lt;br /&gt;
         if (board[r3][c3] != EMPTY)&lt;br /&gt;
            return false;  // (r3,c3) already contains a piece.&lt;br /&gt;
&lt;br /&gt;
         if (player == RED) {&lt;br /&gt;
            if (board[r1][c1] == RED &amp;amp;&amp;amp; r3 &amp;gt; r1)&lt;br /&gt;
               return false;  // Regular red piece can only move  up.&lt;br /&gt;
            if (board[r2][c2] != BLACK &amp;amp;&amp;amp; board[r2][c2] != BLACK_KING)&lt;br /&gt;
               return false;  // There is no black piece to jump.&lt;br /&gt;
            return true;  // The jump is legal.&lt;br /&gt;
         }&lt;br /&gt;
         else {&lt;br /&gt;
            if (board[r1][c1] == BLACK &amp;amp;&amp;amp; r3 &amp;lt; r1)&lt;br /&gt;
               return false;  // Regular black piece can only move downn.&lt;br /&gt;
            if (board[r2][c2] != RED &amp;amp;&amp;amp; board[r2][c2] != RED_KING)&lt;br /&gt;
               return false;  // There is no red piece to jump.&lt;br /&gt;
            return true;  // The jump is legal.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
      }  // end canJump()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
      private boolean canMove(int player, int r1, int c1, int r2, int c2) {&lt;br /&gt;
            // This is called by the getLegalMoves() method to determine whether&lt;br /&gt;
            // the player can legally move from (r1,c1) to (r2,c2).  It is&lt;br /&gt;
            // assumed that (r1,r2) contains one of the player&amp;#039;s pieces and&lt;br /&gt;
            // that (r2,c2) is a neighboring square.&lt;br /&gt;
&lt;br /&gt;
         if (r2 &amp;lt; 0 || r2 &amp;gt;= 8 || c2 &amp;lt; 0 || c2 &amp;gt;= 8)&lt;br /&gt;
            return false;  // (r2,c2) is off the board.&lt;br /&gt;
&lt;br /&gt;
         if (board[r2][c2] != EMPTY)&lt;br /&gt;
            return false;  // (r2,c2) already contains a piece.&lt;br /&gt;
&lt;br /&gt;
         if (player == RED) {&lt;br /&gt;
            if (board[r1][c1] == RED &amp;amp;&amp;amp; r2 &amp;gt; r1)&lt;br /&gt;
                return false;  // Regualr red piece can only move down.&lt;br /&gt;
             return true;  // The move is legal.&lt;br /&gt;
         }&lt;br /&gt;
         else {&lt;br /&gt;
            if (board[r1][c1] == BLACK &amp;amp;&amp;amp; r2 &amp;lt; r1)&lt;br /&gt;
                return false;  // Regular black piece can only move up.&lt;br /&gt;
             return true;  // The move is legal.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
      }  // end canMove()&lt;br /&gt;
     &lt;br /&gt;
      static double staticAnalyze(int[][] board) {&lt;br /&gt;
         int i=0;&lt;br /&gt;
         int j=0;&lt;br /&gt;
         int p;&lt;br /&gt;
         double res=0.0;&lt;br /&gt;
         double sq=0.0;  &lt;br /&gt;
          &lt;br /&gt;
         for(i=0; i&amp;lt;8; i++) {&lt;br /&gt;
           for(j=0; j&amp;lt;8; j++) {&lt;br /&gt;
             p=board[i][j];      &lt;br /&gt;
             if (p==EMPTY) {&lt;br /&gt;
             } else if (p==RED) { &lt;br /&gt;
                 sq=-10-(8-i);               &lt;br /&gt;
             } else if (p==BLACK) { &lt;br /&gt;
                 sq=10+(8+i);               &lt;br /&gt;
             } else if (p==RED_KING) { &lt;br /&gt;
                 sq=-30;               &lt;br /&gt;
             } else {&lt;br /&gt;
                 sq=30;&lt;br /&gt;
             }&lt;br /&gt;
             res=res+sq; &lt;br /&gt;
           }             &lt;br /&gt;
         }&lt;br /&gt;
         return res;          &lt;br /&gt;
      }    &lt;br /&gt;
&lt;br /&gt;
   } // end class CheckersData&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
} // end class Checkers&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Tammet</name></author>
	</entry>
</feed>