真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java黑白棋判斷代碼,java寫黑白棋思路

java自編黑白棋落子報(bào)錯(cuò)

這種報(bào)錯(cuò),有兩種解決辦法;

成都創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計(jì)、網(wǎng)站制作與策劃設(shè)計(jì),城區(qū)網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:城區(qū)等地區(qū)。城區(qū)做網(wǎng)站價(jià)格咨詢:13518219792

首先,你可以上網(wǎng)找一些黑白子落子的代碼,基本上都是差不多的;

其次,報(bào)錯(cuò),根據(jù)步驟看是哪行出問題了,

比如,運(yùn)行的時(shí)候,界面沒有顯示,那你就到界面顯示查看;

如果是黑白子落子問題,說明你的黑白子邊代碼設(shè)計(jì)錯(cuò)誤;

遇到問題一步步來,這也是成長的過程。

急求Java黑白棋設(shè)計(jì)源代碼

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當(dāng)前的格子

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;//用這個(gè)時(shí),黑白子可交替出現(xiàn),

//chessBo[r01][r02] = true;//用這個(gè)代替上面那個(gè)時(shí)黑白子不可交替了,不知道為什么會(huì)這樣

//加入判斷勝負(fù)的方法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編程的黑白棋代碼???急用!?。?/h2>

/**

* 實(shí)驗(yàn)課作業(yè)

* @author Administrator

* 這個(gè)實(shí)現(xiàn)允許重畫

*/

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;

//記錄是有有圖形的數(shù)組

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

int []a=null;

//記錄當(dāng)前鼠標(biāo)位置所在方格,便于判斷是否可以繼續(xù)畫

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!!!");

}

}

}

//將鼠標(biāo)的坐標(biāo)轉(zhuǎn)換為所畫圖形的基準(zhǔn)坐標(biāo) ,返回當(dāng)前鼠標(biāo)在記錄數(shù)組a中的下標(biāo)

//圓形為外接矩形的左上角

//X為左上角起始點(diǎn)的坐標(biāo)

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();

}

//判斷是否可以繼續(xù)畫的函數(shù)

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

}

//重新設(shè)置初始值

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;

}

}

就是這個(gè)了吧

額們實(shí)驗(yàn)課的作業(yè)

java黑白棋代碼,急求

一個(gè)小程序 簡單的21點(diǎn)

package com.citicbank.other;

import java.awt.Color;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.SwingConstants;

import javax.swing.border.LineBorder;

public class gameTest extends JFrame {

private JLabel label_2;

private int number;

private int sum;

final JLabel label = new JLabel();

final JLabel label_1 = new JLabel();

public static void main(String[] args) {

new gameTest();

}

public gameTest() {

super("21點(diǎn)?!");

getContentPane().setLayout(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JButton button = new JButton();

button.addActionListener(new ActionListener() {

public void actionPerformed(final ActionEvent arg0) {

onClick();

}

});

button.setText("出牌");

button.setBounds(170, 350, 106, 28);

getContentPane().add(button);

label.setBorder(new LineBorder(Color.black, 1, false));

label.setHorizontalAlignment(SwingConstants.CENTER);

label.setFont(new Font("", Font.BOLD, 26));

label.setText("背面");

label.setBounds(158, 81, 137, 153);

getContentPane().add(label);

label_1.setText("你已經(jīng)擁有的牌:");

label_1.setBounds(109, 22, 270, 45);

getContentPane().add(label_1);

this.setBounds(200, 300, 501, 528);

this.setVisible(true);

getContentPane().add(getLabel_2());

}

public int randNumber() {

try {

Thread.sleep(10);

} catch (InterruptedException e) {

e.printStackTrace();

}

return (int) (Math.random() * 10 + 1);

}

public void onClick() {

number = this.randNumber();

this.sum += number;

label.setText("" + number);

String strTemp = this.label_1.getText();

strTemp += "" + number + " ";

label_1.setText(strTemp);

String temp = "合計(jì):" + sum;

label_2.setText(temp);

isWin();

}

public void isWin() {

if (sum 21) {

JOptionPane.showMessageDialog(this, "你輸了");

clear();

return;

} else if (sum == 21) {

JOptionPane.showMessageDialog(this, "你贏了");

clear();

return;

} else {

int i = JOptionPane.showOptionDialog(this, "是否繼續(xù)?", "提示",

JOptionPane.OK_CANCEL_OPTION,

JOptionPane.INFORMATION_MESSAGE, null, null, null);

if (i == JOptionPane.OK_OPTION) {

onClick();

} else

return;

}

}

private void clear() {

label_2.setText("合計(jì):");

sum = 0;

number = 0;

label_1.setText("你已經(jīng)擁有的牌:");

}

/**

* @return

*/

protected JLabel getLabel_2() {

if (label_2 == null) {

label_2 = new JLabel();

label_2.setText("合計(jì):");

label_2.setBounds(313, 35, 66, 18);

}

return label_2;

}

}


名稱欄目:java黑白棋判斷代碼,java寫黑白棋思路
文章來源:http://weahome.cn/article/dssdejs.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部