重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java黑白棋代码 java飞行棋代码

java求救,这题怎么编

1. 把二维数组当作是一维的:

我们提供的服务有:成都网站设计、做网站、微信公众号开发、网站优化、网站认证、鄂州ssl等。为上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的鄂州网站制作公司

例: 有数组char[3][3], 如 char[2][2]可以下标对应的数值为2×3 + 2 = 8,这个应该是第三行第三列的数据下标

2. 使用随机数生成器生成一定范围的下标(例如,0~8),反算数组中的位置,这个位置作为下子的地方。(下子就是把对应的数组中设成黑子或者白子)

3. 循环第二步,直至放满

(注意,可能会产生重复下标,需要先判断看看有没有已经有棋子在其上)

急:黑白棋求助,望各位高手帮忙!!!

补充上边的问题:

for(i=0; i8; i++)

{

getchar();

for(t=0; t8; t++)

{

ch = getchar();

if(ch=='1') chesscolor[i][t]=1;

else if(ch=='2') chesscolor[i][t]=2;

else if(ch=='0') chesscolor[i][t]=0;

}

}

int probability[8][8]={10,9,-1,0,0,-1,9,10,9,-10,0,0,0,-10,9,-1,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,-1,9,-10,0,0,0,-10,9,10,9,-1,0,0,-1,9,10,};

for(k=0;k8;k++)

for(l=0;l8;l++)

{

for(x=0;x8;x++)

for(y=0;y8;y++)

{

if(rules(x,y,chesscolor[x][y],chesscolor)==0)

{

xx[m]=x;

yy[n]=y;

m++;

n++;

}

}

for(m=0;m8;m++)

for(n=0;n8;n++)

{

if(probability[xx[m]][yy[n]]==10)

{

flag=1;

chesscolor[xx[m]][yy[n]]=rules(xx[m],yy[n],chesscolor[xx[m]][yy[n]],chesscolor);

}

else if(probability[xx[m]][yy[n]]==9)

{

if(probability[xx[m]--][yy[n]]==-1 || probability[xx[m]][yy[n]--]==-1)

{

flag=1;

chesscolor[xx[m]][yy[n]]=rules(xx[m],yy[n],chesscolor[xx[m]][yy[n]],chesscolor);

}

}

else if(probability[xx[m]][yy[n]]==-10)

{

flag=0;

continue;

}

}

if(flag=0)

{

r=rand()%7;

p=rand()%7;

chesscolor[xx[r]][yy[p]]=rules(xx[r],yy[p],chesscolor[r][p],chesscolor);

}

}

printf("读入数据:\n");

for(i=0; i8; i++)

{

for(t=0; t8; t++)

{

printf("%d", chesscolor[i][t]);

}

printf("\n");

}

}

上述代码可以通过编译,但运行时出错,DEBUG显示xx[m]=x;这行出错,原因不知,求解,急!!!!语言C语言

求JAVA高手帮助 我想写一个黑白棋的JAVA代码,想用键盘的箭头键实现光标的移动,并在输入回车后标记

package com.test;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.image.ImageObserver;

import java.text.AttributedCharacterIterator;

public class GameDemo2 {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

MainWindow mw=new MainWindow();

mw.setSize(400,400);

mw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mw.setVisible(true);

//mw.setResizable(false);

}

}

class MainWindow extends JFrame implements ActionListener{

/**

*

*/

private static final long serialVersionUID = 1L;

NewPanel mp=null;

JButton jbt1;

JButton jbt2;

JButton jbt3;

JButton jbt4;

int flag=-1;

JPanel jp=null;

public MainWindow(){

//初始化组件

mp=new NewPanel();

mp.addMouseListener(mp);

jbt1=new JButton("O 先手");

jbt2=new JButton("X 先手 ");

jbt3=new JButton("RESET");

jbt4=new JButton("EXIT");

jp=new JPanel();

GridLayout gl=new GridLayout(4,1);

gl.setHgap(4);

gl.setVgap(5);

jp.setLayout(gl);

jp.add(jbt1);

jp.add(jbt2);

jp.add(jbt3);

jp.add(jbt4);

this.add(mp,BorderLayout.CENTER);

this.add(jp,BorderLayout.EAST);

jbt1.addActionListener(this);

jbt2.addActionListener(this);

jbt3.addActionListener(this);

jbt4.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(e.getSource()==jbt1){

flag=1;

mp.setHand(0);

jbt1.setEnabled(false);

jbt2.setEnabled(false);

}

if(e.getSource()==jbt2){

flag=2;

mp.setHand(1);

jbt1.setEnabled(false);

jbt2.setEnabled(false);

}

if(e.getSource()==jbt3){

flag=3;

mp.getGraphics().dispose();

this.remove(mp);

mp=new NewPanel();

this.add(mp,BorderLayout.CENTER);

mp.addMouseListener(mp);

this.remove(jp);

jp=new JPanel();

GridLayout gl=new GridLayout(4,1);

gl.setHgap(4);

gl.setVgap(5);

jp.setLayout(gl);

jp.add(jbt1);

jp.add(jbt2);

jp.add(jbt3);

jp.add(jbt4);

this.add(jp,BorderLayout.EAST);

this.validate();

jbt1.setEnabled(true);

jbt2.setEnabled(true);

}

if(e.getSource()==jbt4){

flag=4;

jbt1.setEnabled(false);

jbt2.setEnabled(false);

System.exit(0);

}

}

public int getFlag(){

return flag;

}

}

//画板

class NewPanel extends JPanel implements MouseListener{

/**

*

*/

private static final long serialVersionUID = 1L;

//记录画图的位置

int xx,yy;

//flag表示是否是初始化

int flag;

//0画X,1画0,2不画

int kind;

//记录是有有图形的数组

//a[i]=0代表是空白可以画 a[i]=1代表X a[i]=2代表画O

int []a=null;

//记录当前鼠标位置所在方格,便于判断是否可以继续画

int curLoc;

//记录谁先手 0-0 1-X

int firstHand;

int times;

//0-O 1-X

int whoWin;

public void setFlag(int f){

flag=f;

}

public void setHand(int h){

firstHand=h;

}

public void setWhoWin(){

whoWin=-1;

}

public NewPanel(){

xx=0;

yy=0;

flag=0;

a=new int[9];

kind=-1;

curLoc=0;

whoWin=-1;

firstHand=-1;

times=1;

whoWin=-1;

}

public void paint(Graphics g){

//super.paint(g);

g.drawRect(0, 0, 300, 300);

g.drawRect(0, 100, 300, 100);

g.drawRect(100, 0, 100, 300);

if(flag==1){

if(isFull()==false){

if(testWin()==false){

if((times+firstHand)%2==0){

if(a[curLoc]==0){

g.drawLine(xx,yy,xx+60,yy+60);

g.drawLine(xx+60, yy, xx, yy+60);

a[curLoc]=1;

times++;

if(testWin()==true){

System.out.println("X赢了");

}

}

}

if((times+firstHand)%2==1){

if(a[curLoc]==0){

g.drawOval(xx,yy,60,60);

a[curLoc]=2;

times++;

if(testWin()==true){

System.out.println("O赢了");

}

}

}

}

}else{

System.out.println("it is FULL!!!");

}

}

}

//将鼠标的坐标转换为所画图形的基准坐标 ,返回当前鼠标在记录数组a中的下标

//圆形为外接矩形的左上角

//X为左上角起始点的坐标

public int Format(int x,int y,int kind){

//第一行第一列

int loc=-1;

if(x=0x100y=0y100){

xx=20;

yy=20;

loc= 0;

}

//第一行第二列

if(x=100x200y=0y100){

xx=120;

yy=20;

loc= 1;

}

//第一行第三列

if(x=200x300y=0y100){

xx=220;

yy=20;

loc= 2;

}

//第二行第一列

if(x=0x100y=100y200){

xx=20;

yy=120;

loc= 3;

}

//第二行第二列

if(x=100x200y=100y200){

xx=120;

yy=120;

loc= 4;

}

//第二行第三列

if(x=200x300y=100y200){

xx=220;

yy=120;

loc= 5;

}

//第三行第一列

if(x=0x100y=200y300){

xx=20;

yy=220;

loc= 6;

}

//第三行第二列

if(x=100x200y=200y300){

xx=120;

yy=220;

loc= 7;

}

//第三行第三列

if(x=200x300y=200y300){

xx=220;

yy=220;

loc= 8;

}

return loc;

}

@Override

public void mouseClicked(MouseEvent e) {

// TODO Auto-generated method stub

xx=e.getX();

yy=e.getY();

flag=1;

//kind=new Random().nextInt(3);

curLoc=Format(xx,yy,kind);

this.repaint();

}

//判断是否可以继续画的函数

public boolean isFull(){

for(int i=0;i9;i++)

if(a[i]!=1a[i]!=2)

return false;

return true;

}

@Override

public void mouseEntered(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mouseExited(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mousePressed(MouseEvent e) {

// TODO Auto-generated method stub

}

public boolean testWin(){

boolean isWin=false;

//a[0]=1

if(a[0]!=0a[0]==a[1]a[0]==a[2]){

whoWin=a[0];

isWin=true;

}

if(a[0]!=0a[3]==a[0]a[0]==a[6]){

whoWin=a[0];

isWin=true;

}

if(a[0]!=0a[0]==a[4]a[0]==a[8]){

whoWin=a[0];

isWin=true;

}

if(a[1]!=0a[1]==a[4]a[1]==a[7]){

whoWin=a[1];

isWin=true;

}

if(a[2]!=0a[2]==a[4]a[2]==a[6]){

whoWin=a[2];

isWin=true;

}

if(a[2]!=0a[2]==a[5]a[2]==a[8]){

whoWin=a[2];

isWin=true;

}

if(a[3]!=0a[3]==a[4]a[3]==a[5]){

whoWin=a[3];

isWin=true;

}

if(a[6]!=0a[6]==a[7]a[6]==a[8]){

whoWin=a[6];

isWin=true;

}

return isWin;

}

@Override

public void mouseReleased(MouseEvent e) {

// TODO Auto-generated method stub

}

//重新设置初始值

public void reset(){

this.getGraphics().dispose();

xx=0;

yy=0;

flag=0;

a=new int[9];

kind=-1;

curLoc=0;

whoWin=-1;

firstHand=-1;

times=1;

whoWin=-1;

}

}

求java黑白棋搜索算法。。。。

public boolean fanqi(int x, int y) {

boolean a = false;

if (qi[x][y] == 0) {

if (flag) {// 黑吃白

if (y - 1 0 qi[x][y - 1] == -1) {

for (int i = 2; y - i = 0; i++) {

if (qi[x][y - i] == 1) {

for (int n = 1; n i; n++) {

qi[x][y - n] = 1;

}

a = true;

if (qi[x][y - i] == 0)

i = 9;

}

}

}

if (y + 1 8 qi[x][y + 1] == -1) {

for (int i = 2; y + i 8; i++) {

if (qi[x][y + i] == 1) {

for (int n = 1; n i; n++) {

qi[x][y + n] = 1;

}

a = true;

if (qi[x][y + i] == 0)

i = 9;

}

}

}

if (x + 1 8 qi[x + 1][y] == -1) {

for (int i = 2; x + i 8; i++) {

if (qi[x + i][y] == 1) {

for (int n = 1; n i; n++) {

qi[x + n][y] = 1;

}

a = true;

if (qi[x + i][y] == 0)

i = 9;

}

}

}

if (x - 1 = 0 qi[x - 1][y] == -1) {

for (int i = 2; x - i = 0; i++) {

if (qi[x - i][y] == 1) {

for (int n = 1; n i; n++) {

qi[x - n][y] = 1;

}

a = true;

if (qi[x - i][y] == 0)

i = 9;

}

}

}

if (x - 1 = 0 y - 1 = 0 qi[x - 1][y - 1] == -1) {

for (int i = 2; x - i = 0 y - i = 0; i++) {

if (qi[x - i][y - i] == 1) {

for (int n = 1; n i; n++) {

qi[x - n][y - n] = 1;

}

a = true;

if (qi[x - i][y - i] == 0)

i = 9;

}

}

}

if (x + 1 8 y - 1 = 0 qi[x + 1][y - 1] == -1) {

for (int i = 2; x + i 8 y - i = 0; i++) {

if (qi[x + i][y - i] == 1) {

for (int n = 1; n i; n++) {

qi[x + n][y - n] = 1;

}

a = true;

if (qi[x + i][y - i] == 0)

i = 9;

}

}

}

if (x - 1 = 0 y + 1 8 qi[x - 1][y + 1] == -1) {

for (int i = 2; x - i = 0 y + i 8; i++) {

if (qi[x - i][y + i] == 1) {

for (int n = 1; n i; n++) {

qi[x - n][y + n] = 1;

}

a = true;

if (qi[x - i][y + i] == 0)

i = 9;

}

}

}

if (x + 1 8 y + 1 8 qi[x + 1][y + 1] == -1) {

for (int i = 2; x + i 8 y + i 8; i++) {

if (qi[x + i][y + i] == 1) {

for (int n = 1; n i; n++) {

qi[x + n][y + n] = 1;

}

a = true;

if (qi[x + i][y + i] == 0)

i = 9;

}

}

}

}

else {// 白吃黑

if (y - 1 0 qi[x][y - 1] == 1) {

for (int i = 2; y - i = 0; i++) {

if (qi[x][y - i] == -1) {

for (int n = 1; n i; n++) {

qi[x][y - n] = -1;

}

a = true;

if (qi[x][y - i] == 0)

i = 9;

}

}

}

if (y + 1 8 qi[x][y + 1] == 1) {

for (int i = 2; y + i 8; i++) {

if (qi[x][y + i] == -1) {

for (int n = 1; n i; n++) {

qi[x][y + n] = -1;

}

a = true;

if (qi[x][y + i] == 0)

i = 9;

}

}

}

if (x + 1 8 qi[x + 1][y] == 1) {

for (int i = 2; x + i 8; i++) {

if (qi[x + i][y] == -1) {

for (int n = 1; n i; n++) {

qi[x + n][y] = -1;

}

a = true;

if (qi[x + i][y] == 0)

i = 9;

}

}

}

if (x - 1 = 0 qi[x - 1][y] == 1) {

for (int i = 2; x - i = 0; i++) {

if (qi[x - i][y] == -1) {

for (int n = 1; n i; n++) {

qi[x - n][y] = -1;

}

a = true;

if (qi[x - i][y] == 0)

i = 9;

}

}

}

if (x - 1 = 0 y - 1 = 0 qi[x - 1][y - 1] == 1) {

for (int i = 2; x - i = 0 y - i = 0; i++) {

if (qi[x - i][y - i] == -1) {

for (int n = 1; n i; n++) {

qi[x - n][y - n] = -1;

}

a = true;

if (qi[x - i][y - i] == 0)

i = 9;

}

}

}

if (x + 1 8 y - 1 = 0 qi[x + 1][y - 1] == 1) {

for (int i = 2; x + i 8 y - i = 0; i++) {

if (qi[x + i][y - i] == -1) {

for (int n = 1; n i; n++) {

qi[x + n][y - n] = -1;

}

a = true;

if (qi[x + i][y - i] == 0)

i = 9;

}

}

}

if (x - 1 = 0 y + 1 8 qi[x - 1][y + 1] == 1) {

for (int i = 2; x - i = 0 y + i 8; i++) {

if (qi[x - i][y + i] == -1) {

for (int n = 1; n i; n++) {

qi[x - n][y + n] = -1;

}

a = true;

if (qi[x - i][y + i] == 0)

i = 9;

}

}

}

if (x + 1 8 y + 1 8 qi[x + 1][y + 1] == 1) {

for (int i = 2; x + i 8 y + i 8; i++) {

if (qi[x + i][y + i] == -1) {

for (int n = 1; n i; n++) {

qi[x + n][y + n] = -1;

}

a = true;

if (qi[x + i][y + i] == 0)

i = 9;

}

}

}

}

}

if (a)

return true;

else

return false;

}

急求Java黑白棋设计源代码

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.geom.*;

import javax.swing.JOptionPane.*;

public class FiveChess

{

public static void main(String[] args)

{

JFrame myFrame = new JFrame("快乐五子棋");

myFrame.getContentPane().add(new MyPanel());

Toolkit mykit = myFrame.getToolkit();

Dimension mysize = mykit.getScreenSize();

myFrame.setBounds(0,0,mysize.width,mysize.height - 40);

myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

myFrame.show();

}

}

class MyPanel extends JPanel implements MouseListener

{

private Point start = new Point(0,0);

private Point next = new Point(0,0);

boolean[][] chessBo = new boolean[19][19];

boolean chessCo = true; //判断颜色的,true 黑

boolean winchess = true; //true,为胜利

MyPanel(){

addMouseListener(this);

setBackground(Color.blue);

}

public boolean winkill(int x,int y) //win ?

{

int countBlack = 0,countWhite = 0;

boolean winchess01 = false;

//x,y 取到r01,r02的值

for(int i = 0;i 5;i++)//判断纵向下

{

if((x + i) 20)

break;

if(chessCo chessBo[x + i][y])

countBlack++;

else if(chessBo[x + i][y] (chessCo == false))

countWhite++;

}

for(int i = 0;i 5;i++)//判断纵向上

{

if((x - i) = 0)

break;

if(chessCo chessBo[x - i][y])

countBlack++;

else if(chessBo[x - i][y] (chessCo == false))

countWhite++;

}

if((countBlack == 5) || (countWhite == 5))

winchess01 = true;

return winchess01;

}

public void paint(Graphics g)//画棋盘

{

Graphics2D g2D = (Graphics2D)g;

g2D.setPaint(Color.black);

float pay = 60.0f,pbx = 60.0f;

float lett = 25.0f;

Point2D.Float p1 = new Point2D.Float(60.0f,pay);

Point2D.Float p2 = new Point2D.Float(510.0f,pay);

Point2D.Float p3 = new Point2D.Float(pbx,60.0f);

Point2D.Float p4 = new Point2D.Float(pbx,510.0f);

for(int i=0;i19;i++){

Line2D.Float lineH = new Line2D.Float(p1,p2);

Line2D.Float lineV = new Line2D.Float(p3,p4);

pay += lett;

p1 = new Point2D.Float(60.0f,pay);

p2 = new Point2D.Float(510.0f,pay);

pbx += lett;

p3 = new Point2D.Float(pbx,60.0f);

p4 = new Point2D.Float(pbx,510.0f);

g2D.draw(lineH);

g2D.draw(lineV);

}

}

public void mousePressed(MouseEvent evt)

{

}

public void mouseClicked(MouseEvent evt)

{

int x = evt.getX();

int y = evt.getY();

int clickCount = evt.getClickCount();

if(clickCount =1 ){

if((x 48 x 522) (y 48 y 522))

draw(x,y);

}

}

public void draw(int dx,int dy)

{

int r01 = 0,r02 = 0;

Graphics g = getGraphics();

start.x = dx;

start.y = dy;

r01 = (start.x + 13 - 60) / 25; //r01 、r02当前的格子

r02 = (start.y + 13 - 60) / 25;

//System.out.println(r01 + "-" + r02);

next.x = 25 * r01 + 60 - 11;

next.y = 25 * r02 + 60 - 11;

//System.out.println(next.x + "-" + next.y);

if(chessCo){

chessCo = false;

g.setColor(Color.black);

g.fillOval(next.x,next.y,20,20);

chessBo[next.x][next.y] = true;//用这个时,黑白子可交替出现,

//chessBo[r01][r02] = true;//用这个代替上面那个时黑白子不可交替了,不知道为什么会这样

//加入判断胜负的方法winkill()

if(winchess == winkill(r01,r02))

showMessage();

}

if(!chessCo){

chessCo = true;

g.setColor(Color.white);

g.fillOval(next.x,next.y,20,20);

chessBo[r01][r02] = true;

}

//g.drawOval(next.x,next.y,20,20);

g.dispose();

}

public void mouseReleased(MouseEvent evt)

{

}

public void mouseEntered(MouseEvent evt)

{

}

public void mouseExited(MouseEvent evt)

{

}

public void showMessage()

{

JOptionPane.showMessageDialog(null,

"You are win.",

"wide288 to Message!",

JOptionPane.INFORMATION_MESSAGE);

}

}


网站栏目:java黑白棋代码 java飞行棋代码
网站地址:http://cqcxhl.com/article/docepjs.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP