| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1461 人关注过本帖
标题:[分享][开源]井字游戏JAVA applet版隆重推出!
取消只看楼主 加入收藏
冰的热度
Rank: 2
等 级:禁止访问
威 望:5
帖 子:404
专家分:0
注 册:2006-12-2
收藏
 问题点数:0 回复次数:3 
[分享][开源]井字游戏JAVA applet版隆重推出!

在推出井字游戏C语言版和C++版之后,作者再次推出JAVA applet版,功能更加强大,

可以用鼠标控制,点击时还有伴音!

选择菜单项可以重新玩......

C和C++版有幸加了精,希望这一款再次加精!

pdb7zY6K.rar (29.77 KB) [分享][开源]井字游戏JAVA applet版隆重推出!


搜索更多相关主题的帖子: JAVA applet版 鼠标 游戏 
2007-05-09 20:51
冰的热度
Rank: 2
等 级:禁止访问
威 望:5
帖 子:404
专家分:0
注 册:2006-12-2
收藏
得分:0 

主要代码如下:

//jingziyouxi.java

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
import java.applet.*;
public class jingziyouxi extends Applet implements MouseListener
{

int white;
int black;
final static int moves[] = {4, 0, 2, 6, 8, 1, 3, 5, 7};
static boolean won[] = new boolean[1 << 9];
static final int DONE = (1 << 9) - 1;
static final int OK = 0;
static final int WIN = 1;
static final int LOSE = 2;
static final int STALEMATE = 3;

static void isWon(int pos)
{
for (int i = 0 ; i < DONE ; i++)
{
if ((i & pos) == pos)
{
won[i] = true;
}
}
}

static
{
isWon((1 << 0) | (1 << 1) | (1 << 2));
isWon((1 << 3) | (1 << 4) | (1 << 5));
isWon((1 << 6) | (1 << 7) | (1 << 8));
isWon((1 << 0) | (1 << 3) | (1 << 6));
isWon((1 << 1) | (1 << 4) | (1 << 7));
isWon((1 << 2) | (1 << 5) | (1 << 8));
isWon((1 << 0) | (1 << 4) | (1 << 8));
isWon((1 << 2) | (1 << 4) | (1 << 6));
}

int bestMove(int white, int black)
{
int bestmove = -1;

loop:
for (int i = 0 ; i < 9 ; i++)
{
int mw = moves[i];
if (((white & (1 << mw)) == 0) && ((black & (1 << mw)) == 0)) {
int pw = white | (1 << mw);
if (won[pw])
{

return mw;
}
for (int mb = 0 ; mb < 9 ; mb++)
{
if (((pw & (1 << mb)) == 0) && ((black & (1 << mb)) == 0))
{
int pb = black | (1 << mb);
if (won[pb])
{

continue loop;
}
}
}

if (bestmove == -1)
{
bestmove = mw;
}
}
}
if (bestmove != -1)
{
return bestmove;
}

for (int i = 0 ; i < 9 ; i++)
{
int mw = moves[i];
if (((white & (1 << mw)) == 0) && ((black & (1 << mw)) == 0))
{
return mw;
}
}

return -1;
}

boolean yourMove(int m)
{
if ((m < 0) || (m > 8))
{
return false;
}
if (((black | white) & (1 << m)) != 0)
{
return false;
}
black |= 1 << m;
return true;
}

boolean myMove()
{
if ((black | white) == DONE)
{
return false;
}
int best = bestMove(white, black);
white |= 1 << best;
return true;
}

int status()
{
if (won[white])
{
return WIN;
}
if (won[black])
{
return LOSE;
}
if ((black | white) == DONE)
{
return STALEMATE;
}
return OK;
}

boolean first = true;

Image notImage;
Image crossImage;

public void init()
{
notImage = getImage(getCodeBase(), "images/not.gif");
crossImage = getImage(getCodeBase(), "images/cross.gif");

addMouseListener(this);
}

public void destroy()
{
removeMouseListener(this);
}

public void paint(Graphics g)
{
Dimension d = getSize();
g.setColor(Color.black);
int xoff = d.width / 3;
int yoff = d.height / 3;
g.drawLine(xoff, 0, xoff, d.height);
g.drawLine(2*xoff, 0, 2*xoff, d.height);
g.drawLine(0, yoff, d.width, yoff);
g.drawLine(0, 2*yoff, d.width, 2*yoff);

int i = 0;
for (int r = 0 ; r < 3 ; r++)
{
for (int c = 0 ; c < 3 ; c++, i++)
{
if ((white & (1 << i)) != 0)
{
g.drawImage(notImage, c*xoff + 1, r*yoff + 1, this);
}
else if ((black & (1 << i)) != 0)
{
g.drawImage(crossImage, c*xoff + 1, r*yoff + 1, this);
}
}
}
}
public void mouseReleased(MouseEvent e)
{
int x = e.getX();
int y = e.getY();

switch (status())
{
case WIN:
case LOSE:
case STALEMATE:
play(getCodeBase(), "audio/return.au");
white = black = 0;
if (first)
{
white |= 1 << (int)(Math.random() * 9);
}
first = !first;
repaint();
return;
}

Dimension d = getSize();
int c = (x * 3) / d.width;
int r = (y * 3) / d.height;
if (yourMove(c + r * 3))
{
repaint();

switch (status())
{
case WIN:
play(getCodeBase(), "audio/yahoo1.au");
break;
case LOSE:
play(getCodeBase(), "audio/yahoo2.au");
break;
case STALEMATE:
break;
default:
if (myMove())
{
repaint();
switch (status())
{
case WIN:
play(getCodeBase(), "audio/yahoo1.au");
break;
case LOSE:
play(getCodeBase(), "audio/yahoo2.au");
break;
case STALEMATE:
break;
default:
play(getCodeBase(), "audio/ding.au");
}
}
else
{
play(getCodeBase(), "audio/beep.au");
}
}
}
else
{
play(getCodeBase(), "audio/beep.au");
}
}

public void mousePressed(MouseEvent e)
{

}

public void mouseClicked(MouseEvent e)
{

}

public void mouseEntered(MouseEvent e)
{

}

public void mouseExited(MouseEvent e)
{

}

public String getAppletInfo()
{
return "TicTacToe by Arthur van Hoff";
}
}

//example1.html

<html>
<head>
<title>井字游戏 v1.1</title>
</head>
<body>
<h1>井字游戏 v1.1</h1>
<hr>
<applet code=jingziyouxi.class width=120 height=120>
alt="Your browser understands the &lt;APPLET&gt; tag but isn't running the applet, for some reason."
Your browser is completely ignoring the &lt;APPLET&gt; tag!
</applet>
<hr>
<a href="jingziyouxi.java">The source.</a>
</body>
</html>



科学是永恒之迷...... 我的博客http://blog..cn/u/1267727974 阅读我的blog,懂与不懂都是收获!
2007-05-12 17:40
冰的热度
Rank: 2
等 级:禁止访问
威 望:5
帖 子:404
专家分:0
注 册:2006-12-2
收藏
得分:0 
哈哈......

的确是demo中的例子!

不过我没说是我写的呀,你没看贴子上是[分享]和[开源]吗?

我没有用[原创]呀!

其实有很多人都不知道demo中有这个,所以让他们知道一下

代码看了,思路挺简单的,我要写也会这样写

哈哈......

不过,C和C++版的确是原创的!去看看吧!

哈哈......

科学是永恒之迷...... 我的博客http://blog..cn/u/1267727974 阅读我的blog,懂与不懂都是收获!
2007-05-13 11:21
冰的热度
Rank: 2
等 级:禁止访问
威 望:5
帖 子:404
专家分:0
注 册:2006-12-2
收藏
得分:0 
嗨.我已经说的很清楚了,是demo中的一个例子,您就别这样了

反正大家学到东西就好,这是最重要的!不是吗?

再者说我也不是完全粘贴的原版,我是亲自参考那个自己做了一遍

可以说我已经可以自己编一个了.就冲这点,我觉得就应该给点奖励,

哈哈......

科学是永恒之迷...... 我的博客http://blog..cn/u/1267727974 阅读我的blog,懂与不懂都是收获!
2007-05-13 16:44
快速回复:[分享][开源]井字游戏JAVA applet版隆重推出!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017648 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved