首页 > 其他 > 详细

蚂蚁爬杆之动态演示

时间:2015-06-28 09:48:23      阅读:233      评论:0      收藏:0      [点我收藏+]

/*
 * 游戏输入说明:
 * 蚂蚁爬杆有32种方法,输入是【0,32)有效的
 * 黑色的方框表示蚂蚁,这个你懂得。
 */


///*
// * 游戏输入说明:
// * 蚂蚁爬杆有32种方法,输入是【0,32)有效的
// * 黑色的方框表示蚂蚁,这个你懂得。
// */

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;

///*
// * 有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、18厘米、23厘米这五个位置上各有一只蚂蚁。
// * 木杆很细,不能同时通过两只蚂蚁。
// * 开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。
// * 当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。
// * 假设蚂蚁们每秒钟可以走一厘米的距离。
// * 编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。
// * 要求:用类模拟出蚂蚁的行为特性,
// * 进而模拟出五只蚂蚁在木杆上的运行过程来编程求解。
// * 不能通过数学的方式直接用公式计算。
// */

public class Ants_Run extends JFrame {
	private static final long serialVersionUID = 1L;// serialVersionUID唯一的可串行化的版本
	private static boolean[][] dirs = new boolean[32][5];
	private static int[] pos = { 3, 7, 11, 18, 23 };
	private ControlJPanel controlJPanel = null;
	private PaintActiveAntsJPanel paintActiveAntsJPanel = null;
	private boolean isSuspend = false;// isSuspend是否暂停
	private int[][] ants = new int[26][29];// 最长时间24,杆长27,就有27个位置。
	private int count = 0;// 记录蚂蚁运动的位置
	private Ant[] woods = { new Ant(), new Ant(), new Ant(), new Ant(),
			new Ant() };
	private int time = 0;// 每一种情况运行的时间
	private JLabel textTimeLabel = null, textCountLabel = null;

	public Ants_Run() {
		super("动态演示蚂蚁爬杆行为");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(200, 100, 900, 200);
		this.setLayout(new GridLayout(2, 1));
		// new 对象
		controlJPanel = new ControlJPanel();
		paintActiveAntsJPanel = new PaintActiveAntsJPanel();
		situation(0);
		this.getContentPane().add(controlJPanel);
		this.getContentPane().add(paintActiveAntsJPanel);
		setVisible(true);
	}

	// 控制面板
	public class ControlJPanel extends JPanel implements ActionListener {
		private static final long serialVersionUID = 1L;
		protected JButton jButton_Pause = null, jButton_Restart = null;// 暂停/开始按钮
		protected JTextField textSituation = null;

		public ControlJPanel() {
			this.setLayout(null);// 绝对定位
			// new 对象
			jButton_Pause = new JButton("Start");// 暂停
			jButton_Restart = new JButton("Restart");// 暂停
			textSituation = new JTextField(20);
			JLabel label = new JLabel("请输入:");
			textTimeLabel = new JLabel();
			textCountLabel = new JLabel();
			// 设置位置
			jButton_Pause.setBounds(350, 50, 80, 30);
			jButton_Restart.setBounds(450, 50, 80, 30);
			label.setBounds(100, 10, 70, 30);
			textSituation.setBounds(170, 10, 100, 30);
			textTimeLabel.setBounds(450, 10, 100, 30);
			textCountLabel.setBounds(530, 10, 100, 30);
			setJLabel();
			// jButton_verify加监听
			jButton_Pause.addActionListener(this);
			jButton_Restart.addActionListener(this);
			textSituation.addActionListener(this);
			// 加组件
			this.add(jButton_Pause);
			this.add(jButton_Restart);
			this.add(label);
			this.add(textSituation);
			this.add(textTimeLabel);
			this.add(textCountLabel);
		}

		public void setJLabel() {
			textTimeLabel.setText("time: " + (time - 1));
			textCountLabel.setText("count: " + count);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals("Pause")) {
				jButton_Pause.setText("Start");
				isSuspend = true;
			}
			if (e.getActionCommand().equals("Start")) {
				jButton_Pause.setText("Pause");
				isSuspend = false;
			}
			if (e.getActionCommand().equals("Restart")) {
				paintActiveAntsJPanel.repaint();
				paintActiveAntsJPanel.start();
				jButton_Pause.setText("Pause");
				isSuspend = false;
			}
			if (e.getSource() == textSituation) {
				try {
					int i = Integer.parseInt(textSituation.getText());
					if (i < 0 || i > 26) {
						i = 0;
					}
					textSituation.setText("" + i);
					paintActiveAntsJPanel.start();
					situation(i);
					jButton_Pause.setText("Start");
				} catch (NumberFormatException e1) {
					e1.printStackTrace();
				}
			}
		}

	}

	// PaintActiveAntsJPanel 画活动的蚂蚁
	public class PaintActiveAntsJPanel extends JPanel implements ActionListener {
		private static final long serialVersionUID = 1L;
		private int width = 30;// 设置像素为1;一个像素等于10个int
		protected Timer timer = null;

		public PaintActiveAntsJPanel() {
			timer = new Timer(1000, this);
			start();
		}

		public void start() {
			count = 0;// 重新开始
			isSuspend = true;
			timer.restart();
			timer.setDelay(1000);
		}

		public void end() {
			timer.stop();
			controlJPanel.jButton_Pause.setText("Start");
		}

		@Override
		public void paint(Graphics g) {
			if (count == time) {
				end();
			}
			super.paint(g);// 擦掉原来的
			for (int j = 1; j <= 27; j++) {// 画27个位置
				g.setColor(Color.red);
				g.drawString("   " + j, 10 + j * width, 30);
				if (ants[count][j] == 0) {// 画框框
					g.setColor(Color.blue);
					g.draw3DRect(10 + j * width, 30, width, width, false);
				} else {// 实框表示蚂蚁
					g.setColor(Color.black);
					g.fill3DRect(10 + j * width, 30, width, width, true);// 35是我调节出来的
				}
			}
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			this.repaint();
			controlJPanel.setJLabel();
			if (isSuspend) {// 如果暂停,那么记录当前位置
				return;
			}
			count++;
		}
	}

	public void situation(int i) {
		// 初始化
		for (int j = 0; j < ants.length; j++) {
			for (int k = 0; k < ants[j].length; k++) {
				ants[j][k] = 0;
			}
		}
		for (int j = 0; j < woods.length; j++) {
			woods[j].setAlive(true);
			woods[j].setPosition(pos[j]);
			woods[j].setDirection(dirs[i][j]);
		}
		time = 0;
		// 蚂蚁移动
		while (true) {
			// 记录数据, 要初始化后才可以记录,不然数据有问题
			for (int j = 0; j < woods.length; j++) {
				ants[time][woods[j].position] = 1;
			}
			time++;
			// 移动
			for (int j = 0; j < woods.length; j++) {
				woods[j].move(1);
			}
			// 相撞或者相遇
			for (int j = 0; j < woods.length - 1; j++) {
				if (woods[j].isAlive && woods[j + 1].isAlive
						&& !woods[j].direction && woods[j + 1].direction) {
					if (woods[j + 1].position == woods[j].position
							|| (woods[j + 1].position - woods[j].position) == 1) {
						woods[j].direction = !woods[j].direction;
						woods[j + 1].direction = !woods[j + 1].direction;
					}
				}
			}
			// 蚂蚁是否全部掉下
			boolean isEnd = true;
			for (int j = 0; j < woods.length; j++) {
				if (woods[j].isAlive) {
					isEnd = false;
				}
			}
			// 是否结束
			if (isEnd) {
				break;
			}
		}
	}

	public static void main(String[] args) {
		for (int i = 0; i < dirs.length; i++) {
			for (int j = 0; j < dirs[i].length; j++) {
				dirs[i][j] = (i & (0x01 << j)) == 0;// true 左 false 右
			}
		}
		new Ants_Run();
	}

}

class Ant {
	protected boolean direction;
	protected int position;
	protected boolean isAlive;
	protected final int START = 1;
	protected final int END = 27;// 长度做死了

	public void setDirection(boolean direction) {
		this.direction = direction;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public void setAlive(boolean isAlive) {
		this.isAlive = isAlive;
	}

	public void move(int n) {
		if (isAlive) {
			if (direction) {
				position -= n;
			} else {
				position += n;
			}
			if (position < START) {
				isAlive = false;
			}
			if (position > END) {
				isAlive = false;
			}
		}
	}

}



蚂蚁爬杆之动态演示

原文:http://blog.csdn.net/hncu1306602liuqiang/article/details/46668645

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!