java五子棋以当前空位为中心 取9个点_急!一个Java题(在下图中的九个点上,空出中间的点...)...

展开全部

import javax.swing.JFrame ;

import javax.swing.JPanel ;

import java.awt.Graphics ;

import java.awt.BorderLayout ;

import java.awt.Color ;

import java.util.ArrayList ;

public class MobileSorter extends JFrame {

//上边距

public static final int TOP_SPA = 100 ;

//左边距

public static final int LEFT_SPA = 100 ;

public static void main(String args[]) {

JFrame.setDefaultLookAndFeelDecorated(true) ;

MobileSorter ms = new MobileSorter() ;

ms.setVisible(true) ;

}

public MobileSorter() {

//设置窗体大小

this.setSize(400 ,400) ;

//new出组件

MyPanel p = new MyPanel() ;

//布置窗体

this.setLayout(new BorderLayout()) ;

this.add(p ,BorderLayout.CENTER) ;

//启动线程

Thread t = new Thread(p) ;

t.start() ;

}

}

class MyPanel extends JPanel implements Runnable {

//存储圆数据的数组

private ArrayList ca = new ArrayList(9);

/**

* 中心圆

*/

private Circle cCenter = null ;

public MyPanel() {

init() ;

}

public void paint(Graphics g) {

//画与62616964757a686964616fe4b893e5b19e313332393039355号位的斜连接线

for (int i = 0; i<9; i++) {

if (i == 4) continue ;

Circle.drawLine(g ,(Circle)ca.get(4) ,(Circle) ca.get(i)) ;

}

//垂直和横线和竖线

g.setColor(Color.BLACK) ;

for (int i = 0; i<9; i++)

for (int j = 0; j<9; j++)

if (i != j && i < j)

if (j - i ==3 || (i + 1 == j && j%3 != 0))

Circle.drawLine(g , (Circle) ca.get(i) ,(Circle) ca.get(j)) ;

/** 画圆 */

for (int i = 0; i<9; i++)

((Circle) ca.get(i)).drawMe(g) ;

}

/**

* 初始化

*/

public void init() {

//创建圆

for (int i = 1; i<10; i++)

this.ca.add(new Circle(i)) ;

//生成圆内的数

int[] n = new int[9] ;

for (int i = 0; i

int c ;

c = (int)(Math.random() * 8) + 1 ;

boolean isRepeat = false ;

for (int j = 0; j

if (n[j] == c) {

isRepeat = true ;

break ;

}

if (isRepeat) continue ;

if (i == 4) i ++ ;

((Circle)this.ca.get(i)).setNum(c) ;

n[i] = c ;

i ++ ;

}

}

/**

* 线程

*/

public void run() {

int oPos = 0 ; //值为1的圆的标号

int sPos ; //值为1的圆的下一个圆的标号

int ePos ; //终端圆标号

int cPos = 0 ; //操作圆标号

cCenter = (Circle)this.ca.get(4) ; //中心圆

//找出圆内数字的1的圆

for (int i = 0; i

if (((Circle)this.ca.get(i)).getNum() == 1) {

oPos = i ;

break ;

}

sPos = Circle.getNextDeasil(oPos) ;

ePos = oPos ;

while (true) {

cPos = sPos ;

while (true) {

Circle c = (Circle)this.ca.get(cPos) ;

Circle n = (Circle)this.ca.get(Circle.getNextDeasil(cPos)) ;

checkSwap(c ,n) ;

cPos = Circle.getNextDeasil(cPos) ;

if(ePos == Circle.getNextDeasil(cPos)) {

ePos = cPos ;

break ;

}

}

if (ePos == Circle.getNextDeasil(sPos)) {

System.out.println ("OVER!") ;

break ;

}

}

}

/**

* 延迟

*/

private void animation() {

this.repaint() ;

try { Thread.sleep(1000) ;}catch (Exception ex) { }

}

/**

* 交换

*/

private void checkSwap(Circle c ,Circle n) {

int cNum = c.getNum() ;

int nNum = n.getNum() ;

if (cNum > nNum) {

cCenter.setNum(n.getNum()) ;

n.setNum(0) ;

this.animation() ;

n.setNum(c.getNum()) ;

c.setNum(0) ;

this.animation() ;

c.setNum(cCenter.getNum()) ;

cCenter.setNum(0) ;

this.animation() ;

}

}

}

class Circle {

/**

* 各圆之间的间距

*/

public static final int CIR_SPA_BET = 60 ;

/**

* 圆的直径

*/

public static final int CIR_WD = 30 ;

/**

* 圆的标号 ,1-9

*/

private int pos ;

/**

* 圆的左上角x坐标

*/

private int x ;

/**

* 圆的左上角y坐标

*/

private int y ;

/**

* 圆内的数

*/

private int num ;

public Circle(int pos) {

this.pos = pos ;

this.x = MobileSorter.LEFT_SPA + (pos-1) % 3 * CIR_SPA_BET ;

this.y = MobileSorter.TOP_SPA + (pos-1) / 3 * CIR_SPA_BET ;

}

/**

* 画圆与圆中的数字

*/

public void drawMe(Graphics g) {

//圆

g.setColor(Color.BLACK) ;

g.fillOval(x ,y ,CIR_WD ,CIR_WD) ;

//数字

g.setColor(Color.WHITE) ;

if (num != 0)

g.drawString(String.valueOf(num)

,x + CIR_WD / 2 - 3 ,y + CIR_WD / 2 + 5) ;

else

g.drawString("空"

,x + CIR_WD / 2 - 3 ,y + CIR_WD / 2 + 5) ;

}

/**

* 画两个圆之间的连接线

*/

public static void drawLine(Graphics g ,Circle a , Circle b) {

g.drawLine(a.getX() + Circle.CIR_WD / 2

,a.getY() + Circle.CIR_WD / 2

,b.getX() + Circle.CIR_WD / 2

,b.getY()+ Circle.CIR_WD / 2) ;

}

public void setNum(int num) {

this.num = num ;

}

public int getX() {

return x ;

}

public int getY() {

return y ;

}

public int getPos() {

return pos ;

}

public int getNum() {

return num ;

}

public static int getNextDeasil(int pos) {

if (pos >=0 && pos <=8 && pos != 4) {

if (pos == 0)

return 1 ;

else if (pos == 1)

return 2 ;

else if (pos == 2)

return 5 ;

else if (pos == 3)

return 0 ;

else if (pos == 5)

return 8 ;

else if (pos == 6)

return 3 ;

else if (pos == 7)

return 6 ;

else if (pos == 8)

return 7 ;

}

return -1 ;

}

/**

* 根据顺时针方向返回下个圆的标号

*/

public int getNextDeasil() {

if (pos >=0 && pos <=8 && pos != 4) {

if (pos == 0)

return 1 ;

else if (pos == 1)

return 2 ;

else if (pos == 2)

return 5 ;

else if (pos == 3)

return 0 ;

else if (pos == 5)

return 8 ;

else if (pos == 6)

return 3 ;

else if (pos == 7)

return 6 ;

else if (pos == 8)

return 7 ;

}

return -1 ;

}

}

本回答由提问者推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值