/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bigwork;import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;class Xiao extends JFrame implements ActionListener, MouseListener, MouseMotionListener, ItemListener { int i, j;
Checkbox ckbHB[] = new Checkbox[2];
CheckboxGroup ckgHB = new CheckboxGroup();
int color = 0;// 旗子的颜色标识 0:白子 1:黑子
boolean isStart = false;// 游戏开始标志
int bodyArray[][] = new int[16][16]; // 设置棋盘棋子状态 0 无子 1 白子 2 黑子
JButton b3 = new JButton("Restare");
JButton b4 = new JButton("Begin"); public void gameInit() // 游戏开始初始化
{
isStart = false;
enableGame(true);
b3.setEnabled(false);
ckbHB[0].setState(true);
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
bodyArray[i][j] = 0;
}
}
} public void enableGame(boolean e) // 设置组件状态
{
b4.setEnabled(e);
b3.setEnabled(e);
ckbHB[0].setEnabled(e);
ckbHB[1].setEnabled(e);
} public void Init() {
this.setSize(new Dimension(660, 670));
this.setLocation(15, 15);
this.setVisible(true);
Container con = getContentPane();
JPanel panel = new JPanel();
con.add(panel);
panel.setLayout(null); ckbHB[0] = new Checkbox("white first", ckgHB, false);
ckbHB[0].setBounds(500, 20, 80, 30);
ckbHB[1] = new Checkbox("black first", ckgHB, false);
ckbHB[1].setBounds(500, 60, 80, 30);
panel.add(ckbHB[0]);
panel.add(ckbHB[1]);
ckbHB[0].addItemListener(this);
ckbHB[1].addItemListener(this); b3.setBounds(500, 100, 80, 30);
panel.add(b3);
b4.setBounds(500, 140, 80, 30);
panel.add(b4);
JLabel Lab1 = new JLabel("白子赢了!");
JLabel Lab2 = new JLabel("黑子赢了!");
Font fnt = new Font("Serief", Font.ITALIC + Font.BOLD, 28);
Lab1.setBounds(400, 550, 280, 30);
Lab1.setFont(fnt);
panel.add(Lab1);
Lab2.setBounds(100, 550, 280, 30);
Lab2.setFont(fnt);
panel.add(Lab2);
addMouseListener(this);
addMouseMotionListener(this); } @Override
public void paint(Graphics g) {
g.setColor(Color.pink);
g.fill3DRect(10, 40, 490, 490, true);
g.setColor(Color.black);
for (i = 0; i < 16; i++) {
g.drawLine(30 + 30 * i, 60, 30 + 30 * i, 510);
g.drawLine(30, 60 + 30 * i, 480, 60 + 30 * i);
} } public void itemStateChanged(ItemEvent e) {
if (ckbHB[0].getState()) // 选择黑子先还是白子先
{
color = 0;
} else {
color = 1;
}
} public void setDown(int x, int y) // 落子
{
if (!isStart) // 判断游戏未开始
{
return;
}
if (bodyArray[x / 30][y / 30] != 0) {
return;
}
Graphics g = getGraphics();
if (color == 1)// 判断黑子还是白子
{
g.setColor(Color.black);
color = 0;
} else {
g.setColor(Color.white);
color = 1;
}
g.fillOval(x - 15, y - 15, 30, 30);
bodyArray[x / 30][y / 30] = color + 1;
if (gameWin1(x / 30, y / 30)) // 判断输赢
{
// lblWin.setText(startColor(color) + "赢了!");
isStart = false;
}
if (gameWin2(x / 30, y / 30)) // 判断输赢
{
// lblWin.setText(startColor(color) + "赢了!");
isStart = false;
}
if (gameWin3(x / 30, y / 30)) // 判断输赢
{
// lblWin.setText(startColor(color) + "赢了!");
isStart = false;
}
if (gameWin4(x / 30, y / 30)) // 判断输赢
{
// lblWin.setText(startColor(color) + "赢了!");
isStart = false;
}
} public void gameStart() // 游戏开始
{
isStart = true;
enableGame(false);
b3.setEnabled(true);
} public void reStart() // 游戏重新开始
{
repaint();
gameInit();
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == b4) {
gameStart();
} else {
reStart();
}
} public void mouseClicked(MouseEvent e) {
int x1, y1;
x1 = e.getX();
y1 = e.getY();
if (e.getX() < 16 || e.getX() > 494 || e.getY() < 46 || e.getY() > 524) {
return;
}
if (x1 % 30 > 15) {
x1 += 30;
}
if (y1 % 30 > 15) {
y1 += 30;
}
x1 = x1 / 30 * 30;
y1 = y1 / 30 * 30;
setDown(x1, y1);
} public void mousePressed(MouseEvent e) {
} public void mouseReleased(MouseEvent e) {
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
} public void mouseDragged(MouseEvent e) {
} public void mouseMoved(MouseEvent e) {
} public boolean gameWin1(int x, int y) // 判断输赢 横
{
int count = 1;
for (i = 1; i < 5; i++) { //正向
if (x + i > 15) {
break; //到边缘了
}
if (bodyArray[x + i][y] == bodyArray[x][y]) {
count++;
} else {
break;
}
}
for (i = 1; i < 5; i++) { //逆向
if (x - i < 0) {
break;
}
if (bodyArray[x - i][y] == bodyArray[x][y]) {
count++;
} else {
break;
}
}
if (count > 4) {
return true;
} else {
return false;
}
} public boolean gameWin2(int x, int y) // 判断输赢 竖
{
int count = 1;
for (i = 1; i < 5; i++) {
if (y + i > 15) {
break;
}
if (bodyArray[x][y + i] == bodyArray[x][y]) {
count += 1;
} else {
break;
}
}
for (i = 1; i < 5; i++) {
if (y - i < 0) {
break;
}
if (bodyArray[x][y - i] == bodyArray[x][y]) {
count += 1;
} else {
break;
}
}
if (count > 4) {
return true;
} else {
return false;
}
} public boolean gameWin3(int x, int y) // 判断输赢 左斜
{
int t = 1;
for (i = 1; i < 5; i++) {
if (x + i > 15 || y - i < 0) {
break;
}
if (bodyArray[x + i][y - i] == bodyArray[x][y]) {
t += 1;
} else {
break;
}
}
for (i = 1; i < 5; i++) {
if (x - i < 0 || y + i > 15) {
break;
}
if (bodyArray[x - i][y + i] == bodyArray[x][y]) {
t += 1;
} else {
break;
}
}
if (t > 4) {
return true;
} else {
return false;
}
} public boolean gameWin4(int x, int y) // 判断输赢 左斜
{
int t = 1;
for (i = 1; i < 5; i++) {
if (x + i > 15 || y + i > 15) {
break;
}
if (bodyArray[x + i][y + i] == bodyArray[x][y]) {
t += 1;
} else {
break;
}
}
for (i = 1; i < 5; i++) {
if (x - i < 0 || y - i < 0) {
break;
}
if (bodyArray[x - i][y - i] == bodyArray[x][y]) {
t += 1;
} else {
break;
}
}
if (t > 4) {
return true;
} else {
return false;
}
}
}public class MyWork { public static void main(String args[]) {
Xiao X = new Xiao();
X.Init(); }
}
为什么不能实现落子,急急急急啊,
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bigwork;import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;class Xiao extends JFrame implements ActionListener, MouseListener, MouseMotionListener, ItemListener { int i, j;
Checkbox ckbHB[] = new Checkbox[2];
CheckboxGroup ckgHB = new CheckboxGroup();
int color = 0;// 旗子的颜色标识 0:白子 1:黑子
boolean isStart = false;// 游戏开始标志
int bodyArray[][] = new int[16][16]; // 设置棋盘棋子状态 0 无子 1 白子 2 黑子
JButton b3 = new JButton("Restare");
JButton b4 = new JButton("Begin"); public void gameInit() // 游戏开始初始化
{
isStart = false;
enableGame(true);
b3.setEnabled(false);
ckbHB[0].setState(true);
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
bodyArray[i][j] = 0;
}
}
} public void enableGame(boolean e) // 设置组件状态
{
b4.setEnabled(e);
b3.setEnabled(e);
ckbHB[0].setEnabled(e);
ckbHB[1].setEnabled(e);
} public void Init() {
this.setSize(new Dimension(660, 670));
this.setLocation(15, 15);
this.setVisible(true);
Container con = getContentPane();
JPanel panel = new JPanel();
con.add(panel);
panel.setLayout(null); ckbHB[0] = new Checkbox("white first", ckgHB, false);
ckbHB[0].setBounds(500, 20, 80, 30);
ckbHB[1] = new Checkbox("black first", ckgHB, false);
ckbHB[1].setBounds(500, 60, 80, 30);
panel.add(ckbHB[0]);
panel.add(ckbHB[1]);
ckbHB[0].addItemListener(this);
ckbHB[1].addItemListener(this); b3.setBounds(500, 100, 80, 30);
panel.add(b3);
b4.setBounds(500, 140, 80, 30);
panel.add(b4);
JLabel Lab1 = new JLabel("白子赢了!");
JLabel Lab2 = new JLabel("黑子赢了!");
Font fnt = new Font("Serief", Font.ITALIC + Font.BOLD, 28);
Lab1.setBounds(400, 550, 280, 30);
Lab1.setFont(fnt);
panel.add(Lab1);
Lab2.setBounds(100, 550, 280, 30);
Lab2.setFont(fnt);
panel.add(Lab2);
addMouseListener(this);
addMouseMotionListener(this); } @Override
public void paint(Graphics g) {
g.setColor(Color.pink);
g.fill3DRect(10, 40, 490, 490, true);
g.setColor(Color.black);
for (i = 0; i < 16; i++) {
g.drawLine(30 + 30 * i, 60, 30 + 30 * i, 510);
g.drawLine(30, 60 + 30 * i, 480, 60 + 30 * i);
} } public void itemStateChanged(ItemEvent e) {
if (ckbHB[0].getState()) // 选择黑子先还是白子先
{
color = 0;
} else {
color = 1;
}
} public void setDown(int x, int y) // 落子
{
if (!isStart) // 判断游戏未开始
{
return;
}
if (bodyArray[x / 30][y / 30] != 0) {
return;
}
Graphics g = getGraphics();
if (color == 1)// 判断黑子还是白子
{
g.setColor(Color.black);
color = 0;
} else {
g.setColor(Color.white);
color = 1;
}
g.fillOval(x - 15, y - 15, 30, 30);
bodyArray[x / 30][y / 30] = color + 1;
if (gameWin1(x / 30, y / 30)) // 判断输赢
{
// lblWin.setText(startColor(color) + "赢了!");
isStart = false;
}
if (gameWin2(x / 30, y / 30)) // 判断输赢
{
// lblWin.setText(startColor(color) + "赢了!");
isStart = false;
}
if (gameWin3(x / 30, y / 30)) // 判断输赢
{
// lblWin.setText(startColor(color) + "赢了!");
isStart = false;
}
if (gameWin4(x / 30, y / 30)) // 判断输赢
{
// lblWin.setText(startColor(color) + "赢了!");
isStart = false;
}
} public void gameStart() // 游戏开始
{
isStart = true;
enableGame(false);
b3.setEnabled(true);
} public void reStart() // 游戏重新开始
{
repaint();
gameInit();
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == b4) {
gameStart();
} else {
reStart();
}
} public void mouseClicked(MouseEvent e) {
int x1, y1;
x1 = e.getX();
y1 = e.getY();
if (e.getX() < 16 || e.getX() > 494 || e.getY() < 46 || e.getY() > 524) {
return;
}
if (x1 % 30 > 15) {
x1 += 30;
}
if (y1 % 30 > 15) {
y1 += 30;
}
x1 = x1 / 30 * 30;
y1 = y1 / 30 * 30;
setDown(x1, y1);
} public void mousePressed(MouseEvent e) {
} public void mouseReleased(MouseEvent e) {
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
} public void mouseDragged(MouseEvent e) {
} public void mouseMoved(MouseEvent e) {
} public boolean gameWin1(int x, int y) // 判断输赢 横
{
int count = 1;
for (i = 1; i < 5; i++) { //正向
if (x + i > 15) {
break; //到边缘了
}
if (bodyArray[x + i][y] == bodyArray[x][y]) {
count++;
} else {
break;
}
}
for (i = 1; i < 5; i++) { //逆向
if (x - i < 0) {
break;
}
if (bodyArray[x - i][y] == bodyArray[x][y]) {
count++;
} else {
break;
}
}
if (count > 4) {
return true;
} else {
return false;
}
} public boolean gameWin2(int x, int y) // 判断输赢 竖
{
int count = 1;
for (i = 1; i < 5; i++) {
if (y + i > 15) {
break;
}
if (bodyArray[x][y + i] == bodyArray[x][y]) {
count += 1;
} else {
break;
}
}
for (i = 1; i < 5; i++) {
if (y - i < 0) {
break;
}
if (bodyArray[x][y - i] == bodyArray[x][y]) {
count += 1;
} else {
break;
}
}
if (count > 4) {
return true;
} else {
return false;
}
} public boolean gameWin3(int x, int y) // 判断输赢 左斜
{
int t = 1;
for (i = 1; i < 5; i++) {
if (x + i > 15 || y - i < 0) {
break;
}
if (bodyArray[x + i][y - i] == bodyArray[x][y]) {
t += 1;
} else {
break;
}
}
for (i = 1; i < 5; i++) {
if (x - i < 0 || y + i > 15) {
break;
}
if (bodyArray[x - i][y + i] == bodyArray[x][y]) {
t += 1;
} else {
break;
}
}
if (t > 4) {
return true;
} else {
return false;
}
} public boolean gameWin4(int x, int y) // 判断输赢 左斜
{
int t = 1;
for (i = 1; i < 5; i++) {
if (x + i > 15 || y + i > 15) {
break;
}
if (bodyArray[x + i][y + i] == bodyArray[x][y]) {
t += 1;
} else {
break;
}
}
for (i = 1; i < 5; i++) {
if (x - i < 0 || y - i < 0) {
break;
}
if (bodyArray[x - i][y - i] == bodyArray[x][y]) {
t += 1;
} else {
break;
}
}
if (t > 4) {
return true;
} else {
return false;
}
}
}public class MyWork { public static void main(String args[]) {
Xiao X = new Xiao();
X.Init(); }
}
为什么不能实现落子,急急急急啊,
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货