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

2007-05-12 18:52
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
楼主,做人要厚道


可惜不是你,陪我到最后
2007-05-12 19:08
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

[CODE]/*
* @(#)TicTacToe.java 1.12 06/02/22
*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* -Redistribution of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* -Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
/*
* @(#)TicTacToe.java 1.12 06/02/22
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
import java.applet.*;
/**
* A TicTacToe applet. A very simple, and mostly brain-dead
* implementation of your favorite game! <p>
*
* In this game a position is represented by a white and black
* bitmask. A bit is set if a position is ocupied. There are
* 9 squares so there are 1<<9 possible positions for each
* side. An array of 1<<9 booleans is created, it marks
* all the winning positions.
*
* @version 1.2, 13 Oct 1995
* @author Arthur van Hoff
* @modified 04/23/96 Jim Hagen : winning sounds
* @modified 02/10/98 Mike McCloskey : added destroy()
*/
public
class TicTacToe extends Applet implements MouseListener {
/**
* White's current position. The computer is white.
*/
int white;
/**
* Black's current position. The user is black.
*/
int black;
/**
* The squares in order of importance...
*/
final static int moves[] = {4, 0, 2, 6, 8, 1, 3, 5, 7};
/**
* The winning positions.
*/
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;
/**
* Mark all positions with these bits set as winning.
*/
static void isWon(int pos) {
for (int i = 0 ; i < DONE ; i++) {
if ((i & pos) == pos) {
won[i] = true;
}
}
}
/**
* Initialize all winning positions.
*/
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));
}
/**
* Compute the best move for white.
* @return the square to take
*/
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]) {
// white wins, take it!
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]) {
// black wins, take another
continue loop;
}
}
}
// Neither white nor black can win in one move, this will do.
if (bestmove == -1) {
bestmove = mw;
}
}
}
if (bestmove != -1) {
return bestmove;
}
// No move is totally satisfactory, try the first one that is open
for (int i = 0 ; i < 9 ; i++) {
int mw = moves[i];
if (((white & (1 << mw)) == 0) && ((black & (1 << mw)) == 0)) {
return mw;
}
}
// No more moves
return -1;
}
/**
* User move.
* @return true if legal
*/
boolean yourMove(int m) {
if ((m < 0) || (m > 8)) {
return false;
}
if (((black | white) & (1 << m)) != 0) {
return false;
}
black |= 1 << m;
return true;
}
/**
* Computer move.
* @return true if legal
*/
boolean myMove() {
if ((black | white) == DONE) {
return false;
}
int best = bestMove(white, black);
white |= 1 << best;
return true;
}
/**
* Figure what the status of the game is.
*/
int status() {
if (won[white]) {
return WIN;
}
if (won[black]) {
return LOSE;
}
if ((black | white) == DONE) {
return STALEMATE;
}
return OK;
}
/**
* Who goes first in the next game?
*/
boolean first = true;
/**
* The image for white.
*/
Image notImage;
/**
* The image for black.
*/
Image crossImage;
/**
* Initialize the applet. Resize and load images.
*/
public void init() {
notImage = getImage(getCodeBase(), "images/not.gif");
crossImage = getImage(getCodeBase(), "images/cross.gif");
addMouseListener(this);
}
public void destroy() {
removeMouseListener(this);
}
/**
* Paint it.
*/
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);
}
}
}
}
/**
* The user has clicked in the applet. Figure out where
* and see if a legal move is possible. If it is a legal
* move, respond with a legal move (if possible).
*/
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;
}
// Figure out the row/column
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";
}
}[/CODE]


可惜不是你,陪我到最后
2007-05-12 19:10
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
每个装了JAVA的人电脑上都会有这一款游戏的代码

可惜不是你,陪我到最后
2007-05-12 19:11
Eastsun
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:802
专家分:0
注 册:2006-12-14
收藏
得分:0 
楼主....

ps:这年头,想骗精华贴也要有点职业精神~

My BlogClick Me
2007-05-12 19:38
冰的热度
Rank: 2
等 级:禁止访问
威 望:5
帖 子:404
专家分:0
注 册:2006-12-2
收藏
得分:0 
哈哈......

的确是demo中的例子!

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

我没有用[原创]呀!

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

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

哈哈......

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

哈哈......

科学是永恒之迷...... 我的博客http://blog..cn/u/1267727974 阅读我的blog,懂与不懂都是收获!
2007-05-13 11:21
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 
C++版有这个的?俺去看看哈

Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-05-13 12:50
Eastsun
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:802
专家分:0
注 册:2006-12-14
收藏
得分:0 
估计楼主英语没学好;

/*
* @(#)TicTacToe.java 1.12 06/02/22
*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* -Redistribution of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.

*
* -Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.

My BlogClick Me
2007-05-13 13:05
快速回复:[分享][开源]井字游戏JAVA applet版隆重推出!
数据加载中...
 
   



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

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