Climbing的专栏

Life is limited, but art is long

huang ClimbingID:hpdlzu80100
3194次访问,排名2万外好友4人,关注者7
热爱生活,积极向上,流自己的汗,吃自己的饭!
hpdlzu80100的文章
原创 23 篇
翻译 0 篇
转载 5 篇
评论 1 篇
最近评论
diqizhan:很喜欢这个
文章分类
收藏
    相册
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 武士之旅小游戏(Chess Board game: Tour of knight)收藏

    新一篇: 自定义40位大整数类及测试类(Define a huge integer and implement its arithmetic operation) | 旧一篇: 简单的海龟绘图语言(Design a Turtle Graphics language which can be used to draw "L" graphics)

    //Chess Board game: Tour of knight
    //Java how to program, 5/e, Exercise 7.22
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class TourOfKnight extends JApplet implements ActionListener {
     
    int currentRow, currentColumn,number,loopCounter=0,count=1;
     
    int[] moveNumber={0,1,2,3,4,5,6,7};
     
    int[] vertical={-1,-2,-2,-1,1,2,2,1};
     
    int[] horizontal={2,1,-1,-2,-2,-1,1,2};
     
    int[][] chessBoard;
     
    int sentinel=0;
     JLabel TourLabel;
     JTextArea output;
     JButton playButton;
     
     
    public void init()
    {
         
         Container  container
    =getContentPane();
         container.setLayout(
    new FlowLayout());
         
         TourLabel
    = new JLabel("Game of Tour of Knight:");
         container.add(TourLabel);
         
         playButton
    = new JButton("Play");
         container.add(playButton);
         playButton.addActionListener(
    this);
         
         output
    =new JTextArea();
         output.setFont(
    new Font("Monospaced",Font.PLAIN,12));
         container.add(output);
         
         chessBoard
    =new int[8][8];
         
    for (int i=0;i<=7;i++)
             
    for (int j=0;j<=7;j++)
                 chessBoard[i][j]
    =0;
         
         

     }
     
     
    public void actionPerformed (ActionEvent event)
     {
         output.setText(
    "");
         output.append(
    "The first location is: "+"["+Integer.toString(currentRow)+","
                 
    +Integer.toString(currentColumn)+"] ");
         chessBoard[
    0][0]=1;
         sentinelCalculate();
        
    while (sentinel>0)
         {
             knightMove(currentRow,currentColumn,number);
             
    //output.append("Next location is: "+"["+Integer.toString(currentRow)+","
                     
    //+Integer.toString(currentColumn)+"] ");
             sentinelCalculate();
             loopCounter
    ++;
         }
        output.append(
    "The last location is: "+"["+Integer.toString(currentRow)+","
                 
    +Integer.toString(currentColumn)+"] ");
        output.append(
    "The loop counter is: "+Integer.toString(loopCounter)+" ");
       
       
        
    for (int i=0;i<=7;i++)
            {
            
    for (int j=0;j<=7;j++)
                {
    if(chessBoard[i][j]>=10)
                        output.append(Integer.toString(chessBoard[i][j])
    +" ");
                
    else
                    output.append(Integer.toString(chessBoard[i][j])
    +"  ");}
            output.append(
    " ");
            }
                
        
    //output.append("The second location is: "+"["+Integer.toString(currentRow)+","
                
    // +Integer.toString(currentColumn)+"] ");
        output.append("Total pace is: "+Integer.toString(count)+" ");
     }
     
     
    public void knightMove(int row, int column,int number)
     {
         count
    ++;
         chessBoard[currentRow][currentColumn]
    =count;
         }
     
     
    public int knightMoveCheck(int row, int column, int argument)
    {
         currentRow
    =row+=vertical[argument];
         currentColumn
    =column+=horizontal[argument];
        
    if (0<=row&&row<=7&&0<=column&&column<=7)
            {
            
    if (chessBoard[currentRow][currentColumn]==0)
                {number
    =argument;
                
    return 1;}
            
    else
            {
                currentRow
    =row-=vertical[argument];
                currentColumn
    =column-=horizontal[argument];
                
    return 0;
            }
            }
            
    else
                {
                currentRow
    =row-=vertical[argument];
                currentColumn
    =column-=horizontal[argument];
                }
        
    return 0;
        
     }
     
     
    public int sentinelCalculate()
     {
         
    for (int i=0;i<=7;i++)
           {
                sentinel
    =knightMoveCheck(currentRow,currentColumn,i);
                
    if (sentinel>0)
                    
    break;
            }
         
    return sentinel;
     }
     
     }
     

    发表于 @ 2008年04月21日 17:50:00|评论(loading...)|编辑

    新一篇: 自定义40位大整数类及测试类(Define a huge integer and implement its arithmetic operation) | 旧一篇: 简单的海龟绘图语言(Design a Turtle Graphics language which can be used to draw "L" graphics)

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © Climbing