java 板


import java.awt.*; import java.awt.event.*; import java.util.Random; class riavnc public static void main(String[] args){ private byte[] map; // 以一维阵列来取代二维,以减少物件的使用 private int[] x, y; private Frame f; private int[] directions; // 前四个bits用来记录此格的资料, 後面四个bits用来记录旁边的地雷数 private static final byte BOMB = 0x10; // 用以纪录本格有地雷 private static final byte VISIT = 0x20; // 用以纪录已按左键 private static final byte MARK = 0x40; // 用以纪录已按右键 private static final byte NEB = 0x0f; // 用以取出旁边的地雷数 void checkWin() { int width = x + 2; for (int i = 1; i <= y; i++) { // 由上而下 int tmp = i * width; for (int j = 1; j <= x; j++) { // 由左而右 int tt = tmp + j; if ((((map[tt] & MARK) > 0) && ((map[tt] & BOMB) == 0)) || (((map[tt] & MARK) == 0) && ((map[tt] & BOMB) > 0))) { return; } } } showAll(); showWin(); } void showAll() { int width = x + 2; for (int i = 1; i <= y; i++) { // 由上而下 int tmp = i*width; for (int j=1; j<=y; j++) { // 由左而右 int tt = tmp + j; if ((map[tt] & BOMB) != 0) { // 画上标示 map[tt] |= MARK; } else { map[tt] = (byte)((map[tt] | VISIT) & ~MARK); // 设为按过, 再把标示拿掉 } } } repaint(); } void showWin() { Dialog d; (d = new Dialog(f, true)).addWindowListener(new CloseWindow(d)); ((Button)d.add(new Button("恭喜过关"))).addActionListener(new CloseWindow(d)); d.pack(); d.show(); } public Dimension getPreferredSize() { return new Dimension(22*x,22*y); } void reset() { map = new byte[(x+2)*(y+2)]; int width = x + 2; for (int i = 0; i <= y + 1; i++) { // 由上而下 int tmp = i * width; for (int j = 0; j <= x + 1; j++) { // 由左而右 int tt = tmp + j; if (i==0 || i == (x + 1) || j == 0 || j == (y + 1)) { map[tt] = VISIT; // boundary } else { map[tt] = 0; // normal position } } } // 15%为地雷 for (int i = 0, bombNumber = (int)(x*y*0.15); i < bombNumber;) { // setup bombs int row = (int)(Math.random()*y)+1; int col = (int)(Math.random()*x)+1; int tt = row * width + col; if ((map[tt] & BOMB) == 0) { // has no bomb yet i++; map[tt] |= BOMB; } } // 计算旁边的地雷数 for (int i = 1; i <= y; i++) { // 由上而下 int tmp = i * width; for (int j = 1; j <= x; j++) { // 由左而右 int tt = tmp + j; for (int k = 0; k < 8; k++) { if ((map[tt + directions[k]] & BOMB) != 0) { // 旁有地雷 map[tt]++; // 後面四个bit用来记录旁边的地雷数 } } } } repaint(); } // 画出盘面 public void paint(Graphics g) { g.setColor(Color.red); for (int i = 1; i <= x; i++) { // 画纵线 g.drawLine(i*20, 0, i*20, 200); } for (int j = 1; j <= y; j++) { // 画横线 g.drawLine(0, j*20, 200, j*20); } int width = x + 2; // 画出每一格目前的状态 for (int i = 1; i <= y; i++) { // 由上而下 int tmp = i * width; for (int j = 1; j <= x; j++) { // 由左而右 int tt = tmp + j; if ((map[tt] & VISIT) != 0) { // 已经翻过来了 g.setColor(Color.blue); if ((map[tt] & NEB) != 0) { // 旁有地雷, show出地雷数 g.drawString(Integer.toString(map[tt] & NEB), j * 20 - 15, i * 20 - 5); } else { // 没有地雷, 填空白 g.fillRect((j - 1) * 20, (i - 1) * 20, 20, 20); } } else if ((map[tt] & MARK) != 0) { // 已用右键做记号 g.setColor(Color.green); g.fillOval((j - 1) * 20, (i - 1) * 20, 20, 20); } } } } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if (command.equals("Exit")) { System.exit(0); } else if (command.equals("New Game")) { reset(); } else { System.out.println("Unknown Command"); } } public Mine2(int x, int y) { MenuBar mb; Menu m; this.x = x; this.y = y; int width = y + 2; directions = new int[8]; directions[0] = 1; directions[1] = -1; directions[2] = width; directions[3] = -width; directions[4] = -width - 1; directions[5] = -width + 1; directions[6] = width - 1; directions[7] = width + 1; (f = new Frame("踩地雷")).add(this).addMouseListener(this); f.addWindowListener(new CloseWindow(f,true)); f.setMenuBar(mb=new MenuBar()); mb.add(m = new Menu("File")); m.add(new MenuItem("New Game")).addActionListener(this); m.add(new MenuItem("Exit")).addActionListener(this); reset(); f.pack(); f.show(); } void setTouched(int tt) { map[tt] |= VISIT; if ((map[tt] & NEB) == 0) { // no bomb around, auto flip for (int i = 0; i < 8; i++) { if ((map[tt + directions[i]] & VISIT) == 0) { // 已翻开就不用再做了 setTouched(tt + directions[i]); } } } } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) { int row = e.getY()/20 + 1; int col = e.getX()/20 + 1; int tt = row*(y + 2) + col; if (row > x || col > y || (map[tt] & VISIT) != 0) { // 超出边界或已经翻过就不必处理 return; } if (e.isMetaDown()) { // 按右键, 作个标记就好了 map[tt] ^= MARK; } else if ((map[tt] & BOMB) != 0) { // 按到地雷了 showAll(); return; } else { setTouched(tt); } checkWin(); repaint(); } public static void main(String[] args) { new Mine2(10,10); } } 不管我怎麽改、除错。 GEL显示的错误都是第五行的: public static void main(String args[]) { 及 最後一个 } 问遍了同学们 也没人可以解答QQ 第一次挑战这麽长的程式(踩地雷) 请问有那位朋友 可以教教我那里写错了? -- * Origin: 中山大学-美丽之岛BBS * From: 59.113.143.144







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草
伺服器连线错误,造成您的不便还请多多包涵!
「赞助商连结」






like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Soft_Job站内搜寻

TOP