重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
1. 把二维数组当作是一维的:
创新互联是一家专业提供洛川企业网站建设,专注与成都网站设计、网站建设、成都h5网站建设、小程序制作等业务。10年已为洛川众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。
例: 有数组char[3][3], 如 char[2][2]可以下标对应的数值为2×3 + 2 = 8,这个应该是第三行第三列的数据下标
2. 使用随机数生成器生成一定范围的下标(例如,0~8),反算数组中的位置,这个位置作为下子的地方。(下子就是把对应的数组中设成黑子或者白子)
3. 循环第二步,直至放满
(注意,可能会产生重复下标,需要先判断看看有没有已经有棋子在其上)
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);
}
}
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;
}