Java小項目——五子棋

3224 ワード

一、需要分析
1、インタフェースの設計
左:碁盤、単独で1つのGamePanel類を書きます
右:コントロールバーpanel 2
2、機能
2.1ボード
まず碁盤を描きます.再描画を考慮してpaint()書き換えに描画を書きます.
2.2碁を打つ
ユーザーは該当する位置をクリックします:駒の位置を計算して、駒を描いて、そして駒の位置を記憶します.同様にpaint()書き換えに描画を書きます.
2.3勝利
勝利判断:横、縦、斜め判断
2.4コンピューター将棋
xxxによる実装
二、実現
0、Config定数インタフェース
いくつかの数が定数であり、変える必要はありません.
必要に応じてインタフェースを直接継承します.
1、碁盤を描く
UIは2つのプレートに分かれており、左側はゲームエリアです
碁盤を描く機能コードをpaint()関数に置いて、再描画機能を解決します
  public void paint(Graphics gr){
        super.paint(gr);
        System.out.println("  ");
        //    
        for(int i=0;i

2、将棋の機能
 
三、ソースコード
0、
​
package com.java7.wuziqi0712_win;

public interface Config {
    public static final int X0=30;
    public static final int Y0=30;
    public static final int SIZE=40;//      
    public static final int LINE=19;//      

    //    ,       i,j ,      ,    0;   ,   1,   0
    public int chessArray[][] = new int[LINE][LINE];
}

​

1、GameUI
package com.java7.wuziqi0715_ai;

import javax.swing.*;
import java.awt.*;

public class GameUI implements Config {
    public static void main(String args[]){
        GameUI game = new GameUI();
        game.showUI();
    }

    public void showUI(){
        //1.    
        JFrame jf = new JFrame();
        jf.setTitle("   1.0");
        jf.setSize(X0*2+SIZE*(LINE-1)+130,X0*2+SIZE*(LINE-1)+22);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(3);

        //2.    
        GamePanel gp = new GamePanel();
        jf.add(gp);

        //3.    
        JPanel jp2 = new JPanel();

        jp2.setBackground(Color.WHITE);
        jp2.setPreferredSize(new Dimension(130,0));
        //jp2.setBorder(BorderFactory.createLineBorder(Color.red));//        
        jf.add(jp2,BorderLayout.EAST);
        JButton jb1 = new JButton("    ");
        JLabel lb1 = new JLabel("
--------
"); JButton jb2 = new JButton(" "); JButton jb3 = new JButton(" "); JLabel lb2 = new JLabel("
--------
"); JButton jb4 = new JButton(" "); jp2.add(jb1); jp2.add(lb1); jp2.add(jb2); jp2.add(jb3); jp2.add(lb2); jp2.add(jb4); JRadioButton jrb1= new JRadioButton(" ",true); JRadioButton jrb2= new JRadioButton(" "); // , , ButtonGroup bg = new ButtonGroup(); bg.add(jrb1); bg.add(jrb2); jp2.add(jrb1); jp2.add(jrb2); jf.setVisible(true); //3. : jf // Graphics gr = gp.getGraphics(); // GameMouse mouse = new GameMouse(gr,chessArray,jf); // gp.addMouseListener(mouse); jb1.addActionListener(mouse); jb2.addActionListener(mouse); jb3.addActionListener(mouse); jb4.addActionListener(mouse); jrb1.addActionListener(mouse); jrb2.addActionListener(mouse); } }

 
 
転載先:https://www.cnblogs.com/iriswang/p/11084648.html