1. 程式人生 > >關於五子棋的java源碼

關於五子棋的java源碼

ner AR sta oval jframe str mes i++ see

package XXXX;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import static javax.swing.JOptionPane.*;


class fiveChessJFrame extends JFrame implements MouseListener {
public int x = 0, y = 0;
private int[][] allChess = new int[19][19];
private boolean

isBlack = true;
private boolean canPlay = true;
private String messageWin = "";

private String message = "黑色先行!";

fiveChessJFrame() {
myPanel fiveChess = new myPanel();
this.setSize(460, 540);
this.setLocation(30, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE
);
this.setTitle("五子棋遊戲");
this.setVisible(true);
this.add(fiveChess);
addMouseListener(this);
}

class myPanel extends JPanel {
public void paintComponent(Graphics graphics) {
//這是設置通知顏色;
graphics.setColor(Color.BLACK);
graphics.setFont(new
Font("黑體", Font.BOLD, 20));
graphics.drawString("遊戲信息:" + message, 120, 40);
graphics.setFont(new Font("宋體", Font.PLAIN, 28));
graphics.setColor(Color.RED);
graphics.drawString(messageWin, 120, 470);

graphics.setColor(Color.BLACK);
//每一格是20*20小方格。第一個點是4070,最後一個點是400430
//一共有18*18個方格、19+19個線,所以一共有19*19的點。
for (int i = 0; i < 19; i++) {
graphics.drawLine(40, 70 + 20 * i, 400, 70 + 20 * i);
graphics.drawLine(40 + 20 * i, 70, 40 + 20 * i, 430);
}
graphics.fillOval(97, 127, 6, 6);
graphics.fillOval(337, 127, 6, 6);
graphics.fillOval(337, 367, 6, 6);
graphics.fillOval(97, 367, 6, 6);
graphics.fillOval(337, 247, 6, 6);
graphics.fillOval(217, 127, 6, 6);
graphics.fillOval(97, 247, 6, 6);
graphics.fillOval(217, 367, 6, 6);
graphics.fillOval(217, 247, 6, 6);

//allChess[][]== 0 ; allChess[][] ==1:黑色; allChess[][]== 2 白色;
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
if (allChess[i][j] == 1) {
graphics.setColor(Color.BLACK);
graphics.fillOval(i * 20 + 40 - 7, j * 20 + 70 - 7, 14, 14);
} else if (allChess[i][j] == 2) {
//填充白色;
graphics.setColor(Color.white);
graphics.fillOval(i * 20 + 40 - 7, j * 20 + 70 - 7, 14, 14);
//黑色描邊;
graphics.setColor(Color.BLACK);
graphics.drawOval(i * 20 + 40 - 7, j * 20 + 70 - 7, 14, 14);
}
}
}
}
}


public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
if (canPlay) {
if (x >= 30 && x <= 430 && y >= 90 && y <= 490) {
double x_around = Math.round((double) x / 10) * 10;
double y_around = Math.round((double) y / 10) * 10;

//打印像素和鼠標像素橫向有一個10的誤差,縱向有一個40的誤差
int xPonit = ((int) x_around - 40 - 10) / 20;
int yPonit = ((int) y_around - 70 - 40) / 20;
if (allChess[xPonit][yPonit] == 0) {
if (isBlack) {
allChess[xPonit][yPonit] = 1;
isBlack = false;
message = "輪到白方!";
} else {
allChess[xPonit][yPonit] = 2;
isBlack = true;
message = "輪到黑方!";
}

if (checkIfWin(xPonit, yPonit) == 1) {
this.messageWin = "黑色勝利啦!";
canPlay = false;
} else if (checkIfWin(xPonit, yPonit) == 2) {
this.messageWin = "白色勝利啦!";
canPlay = false;
}
} else {
showMessageDialog(this, "當前位置有棋子了,請重新落子!");
}
this.repaint();
}
} else {
showMessageDialog(this, "遊戲已經結束!");

if (JOptionPane.showConfirmDialog(this, "重新開始嗎?") == YES_OPTION) {
showMessageDialog(this, "開始遊戲!");
allChess = new int[19][19];
message = "黑色先行!";
messageWin = "";
isBlack = true;
canPlay = true;
this.repaint();
} else {
showMessageDialog(this, "關閉遊戲!");
this.setVisible(false);
}
}
}
private int checkIfWin(int x, int y) {
//1黑色勝利,2白色勝利,0沒有勝利者;
int[] aroundColorHeng = new int[9];
int[] aroundColorShu = new int[9];
int[] aroundColorPie = new int[9];
int[] aroundColorNa = new int[9];
for (int i = 0; i < 9; i++) {
if (x - 4 + i >= 0 && x - 4 + i < 19) {
aroundColorHeng[i] = allChess[x - 4 + i][y];
} else aroundColorHeng[i] = 0;

if (y - 4 + i >= 0 && y - 4 + i < 19) {
aroundColorShu[i] = allChess[x][y - 4 + i];
} else aroundColorShu[i] = 0;

if (x - 4 + i >= 0 && x - 4 + i < 19 && y - 4 + i >= 0 && y - 4 + i < 19) {
aroundColorPie[i] = allChess[x - 4 + i][y - 4 + i];
} else aroundColorPie[i] = 0;

if (x + 4 - i >= 0 && x + 4 - i < 19 && y - 4 + i >= 0 && y - 4 + i < 19) {
aroundColorNa[i] = allChess[x + 4 - i][y - 4 + i];
} else aroundColorNa[i] = 0;
}
int countNumHeng = 1, countNumShu = 1, countNumPie = 1, countNumNa = 1;
if (allChess[x][y] == 1) {
for (int i = 0; i < 8; i++) {
if (aroundColorHeng[i] == 1 && aroundColorHeng[i + 1] == 1) {
countNumHeng++;
} else countNumHeng = 1;

if (aroundColorShu[i] == 1 && aroundColorShu[i + 1] == 1) {
countNumShu++;
} else countNumShu = 1;

if (aroundColorPie[i] == 1 && aroundColorPie[i + 1] == 1) {
countNumPie++;
} else countNumPie = 1;

if (aroundColorNa[i] == 1 && aroundColorNa[i + 1] == 1) {
countNumNa++;
} else countNumNa = 1;
if (countNumNa == 5 || countNumPie == 5 || countNumShu == 5 || countNumHeng == 5) return 1;
}


} else if (allChess[x][y] == 2) {
for (int i = 0; i < 8; i++) {
if (aroundColorHeng[i] == 2 && aroundColorHeng[i + 1] == 2) {
countNumHeng++;
} else countNumHeng = 1;

if (aroundColorShu[i] == 2 && aroundColorShu[i + 1] == 2) {
countNumShu++;
} else countNumShu = 1;

if (aroundColorPie[i] == 2 && aroundColorPie[i + 1] == 2) {
countNumPie++;
} else countNumPie = 1;

if (aroundColorNa[i] == 2 && aroundColorNa[i + 1] == 2) {
countNumNa++;
} else countNumNa = 1;
if (countNumNa == 5 || countNumPie == 5 || countNumShu == 5 || countNumHeng == 5) return 2;
}

}
return 0;
}


public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}

public class testProject {
public static void main(String args[]) {
//設置一個新的框。
fiveChessJFrame myChessJFrame = new fiveChessJFrame();

if (JOptionPane.showConfirmDialog(myChessJFrame, "現在開始遊戲嗎?") == YES_OPTION) {
showMessageDialog(myChessJFrame, "開始遊戲!");
} else {
showMessageDialog(myChessJFrame, "關閉遊戲!");
myChessJFrame.setVisible(false);
}
}
}

關於五子棋的java源碼