| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1396 人关注过本帖
标题:求java井字棋
只看楼主 加入收藏
zhuang0812
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-9-26
收藏
 问题点数:0 回复次数:2 
求java井字棋

谁有java井字棋小游戏的代码啊??人与电脑下,用O和X符号

按要求井字棋要有两个classes
第一是 Board 要有这些功能
reset: 还原到开始全空的格子.
status: to find the current status of a specified square of the board.

Try: to temporarily insert a symbol in a square. The method will return the
number of straight lines passing through the square that have 3 identical
symbols on the line – this value can be 0, 1, or 2. Method clear undoes the
effect of a try.

Place: places the specified symbol in an unoccupied square. The method
returns value Continue, Drawn or Win depending on the state of the board
after new symbol has been placed on the board. (In the following description I
use the words try and place with the meanings described in these dot points)

Trying to place a symbol in an occupied square by method try or place
should raise an exception.

第二个是 Class Strategy
方法move要有以下功能
1,如果中间的格子是空的,电脑就要在那格标记.
2,反复插入电脑和符号到空的格子,寻找可以一个就赢,电脑就下这一格.


小弟是新手..有人可以帮我写这两个class么?

搜索更多相关主题的帖子: java 
2007-09-26 22:13
kingstarer
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2007-4-12
收藏
得分:0 
只做过人与人下的
2007-09-26 23:41
Eastsun
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:802
专家分:0
注 册:2006-12-14
收藏
得分:0 

是不是这个东东?
每个装了JDK的电脑上都有一份现成的,自己去JDK安装目录下的demo目录下找吧.

程序代码:

/*
* @(#)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\";
}
}


My BlogClick Me
2007-09-26 23:52
快速回复:求java井字棋
数据加载中...
 
   



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

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