一、java小游戏:五子棋人机大战
1.绘制窗口
package wuziqi;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GameWindow {
private JFrame frame = new JFrame();
private Chessboard chessboard = new Chessboard();
private static int WINDOW_WIDTH = 518;
private static int WINDOW_HEIGHT = 540;
public void init() {
frame.setTitle("五子棋");
frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(chessboard);
frame.setVisible(true);
chessboard.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
play(e);
}
});
}
private void play(MouseEvent e) {
int cellsize = chessboard.getCellSize();
int x = (e.getX() - 5) / cellsize;
int y = (e.getY() - 5) / cellsize;
if (chessboard.isLegal(x, y)) {
chessboard.addChess(new Location(x, y, 1));
chessboard.addChess(x, y, 1);
if (chessboard.isWin(x, y, 1)) {
JOptionPane.showMessageDialog(frame, "人类获胜了", "您获胜了", JOptionPane.PLAIN_MESSAGE);
}
Location location = chessboard.searchLocation();
chessboard.addChess(location);
chessboard.addChess(location.getX(),location.getY(),location.getOwner());
if(chessboard.isWin(location.getX(),location.getY(),location.getOwner())){
JOptionPane.showMessageDialog(frame,"机器获胜","Congratulations,您输了",JOptionPane.PLAIN_MESSAGE);
}
}
}
public static void main(String[] args) {
new GameWindow().init();
}
}
2.这个类是实现功能的主要代码
package wuziqi;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class Chessboard extends JPanel {
private static final int SIZE = 15;
private int margin = 20;
private List<Location> locationList = new ArrayList<>();
private int[][] location = new int[SIZE][SIZE];
private int[][] score = new int[SIZE][SIZE];
@Override
public void paint(Graphics g) {
super.paint(g);
drawChessBoard(g);
drawChess(g);
}
private void drawChessBoard(Graphics g) {
g.setColor(new Color(240, 210, 160));
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
int cellsize = (getWidth() - 2 * margin) / (SIZE - 1);
for (int i = 0; i < SIZE; i++) {
g.drawLine(margin, margin + cellsize * i, getWidth() - 20, margin + (cellsize * i));
g.drawLine(margin + cellsize * i, margin, margin + cellsize * i, getHeight() - margin);
}
}
public void drawChess(Graphics g) {
for (int i = 0; i < locationList.size(); i++) {
Location loc = locationList.get(i);
if (loc.getOwner() == 1) {
g.setColor(Color.BLACK);
} else {
g.setColor(Color.WHITE);
}
int cellsize = (getWidth() - 2 * margin) / (SIZE - 1);
g.fillOval(loc.getX() * cellsize + margin - cellsize / 2, loc.getY() * cellsize + margin - cellsize / 2, cellsize, cellsize);
if (i == locationList.size() - 1) {
g.setColor(Color.red);
g.drawRect(loc.getX() * cellsize + margin - cellsize / 2, loc.getY() * cellsize + margin - cellsize / 2, cellsize, cellsize);
}
}
}
public void addChess(Location location) {
locationList.add(location);
repaint();
}
public int getCellSize() {
return (getWidth() - 2 * margin) / (SIZE - 1);
}
public boolean isLegal(int x, int y) {
if (x >= 0 && x <= SIZE && y >= 0 && y <= SIZE && location[x][y] == 0) {
return true;
}
return false;
}
public void addChess(int x, int y, int owner) {
location[x][y] = owner;
}
public boolean isWin(int x, int y, int owner) {
int sum = 0;
for (int i = x - 1; i >= 0; i--) {
if (location[i][y] == owner) {
sum++;
} else {
break;
}
}
for (int i = x + 1; i < SIZE; i++) {
if (location[i][y] == owner) {
sum++;
} else {
break;
}
}
if (sum >= 4) {
return true;
}
sum = 0;
for (int i = y - 1; i >= 0; i--) {
if (location[x][i] == owner) {
sum++;
} else {
break;
}
}
for (int i = y + 1; i < SIZE; i++) {
if (location[x][i] == owner) {
sum++;
} else {
break;
}
}
if (sum >= 4) {
return true;
}
sum = 0;
for (int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) {
if (location[i][j] == owner) {
sum++;
} else {
break;
}
}
for (int i = x + 1, j = y + 1; i < SIZE && j < SIZE; i++, j++) {
if (location[i][j] == owner) {
sum++;
} else {
break;
}
}
if (sum >= 4) {
return true;
}
sum = 0;
for (int i = x + 1, j = y - 1; i < SIZE && j >= 0; i++, j--) {
if (location[i][j] == owner) {
sum++;
} else {
break;
}
}
for (int i = x - 1, j = y + 1; i >= 0 && j < SIZE; i--, j++) {
if (location[i][j] == owner) {
sum++;
} else {
break;
}
}
if (sum >= 4) {
return true;
}
return false;
}
public Location searchLocation() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
score[i][j] = 0;
}
}
int humanChessmanNum = 0;
int machineChessmanNum = 0;
int tupleScoreTmp = 0;
int goalX = -1;
int goalY = -1;
int maxScore = -1;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE - 4; j++) {
int k = j;
while (k < j + 5) {
if (location[i][k] == -1) machineChessmanNum++;
else if (location[i][k] == 1) humanChessmanNum++;
k++;
}
tupleScoreTmp = tupleScore(humanChessmanNum, machineChessmanNum);
for (k = j; k < j + 5; k++) {
score[i][k] += tupleScoreTmp;
}
humanChessmanNum = 0;
machineChessmanNum = 0;
}
}
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE - 4; j++) {
int k = j;
while (k < j + 5) {
if (location[k][i] == -1) machineChessmanNum++;
else if (location[k][i] == 1) humanChessmanNum++;
k++;
}
tupleScoreTmp = tupleScore(humanChessmanNum, machineChessmanNum);
for (k = j; k < j + 5; k++) {
score[k][i] += tupleScoreTmp;
}
humanChessmanNum = 0;
machineChessmanNum = 0;
}
}
for (int i = SIZE - 1; i >= 4; i--) {
for (int k = i, j = 0; j < SIZE && k >= 0; j++, k--) {
int m = k;
int n = j;
while (m > k - 5 && k - 5 >= -1) {
if (location[m][n] == -1) machineChessmanNum++;
else if (location[m][n] == 1) humanChessmanNum++;
m--;
n++;
}
if (m == k - 5) {
tupleScoreTmp = tupleScore(humanChessmanNum, machineChessmanNum);
for (m = k, n = j; m > k - 5; m--, n++) {
score[m][n] += tupleScoreTmp;
}
}
humanChessmanNum = 0;
machineChessmanNum = 0;
}
}
for (int i = 1; i < SIZE; i++) {
for (int k = i, j = SIZE - 1; j >= 0 && k < SIZE; j--, k++) {
int m = k;
int n = j;
while (m < k + 5 && k + 5 <= SIZE) {
if (location[n][m] == -1) machineChessmanNum++;
else if (location[n][m] == 1) humanChessmanNum++;
m++;
n--;
}
if (m == k + 5) {
tupleScoreTmp = tupleScore(humanChessmanNum, machineChessmanNum);
for (m = k, n = j; m < k + 5; m++, n--) {
score[n][m] += tupleScoreTmp;
}
}
humanChessmanNum = 0;
machineChessmanNum = 0;
}
}
for (int i = 0; i < SIZE - 4; i++) {
for (int k = i, j = 0; j < SIZE - 4 && k < SIZE - 4; j++, k++) {
int m = k;
int n = j;
while (m < k + 5 && n < j + 5) {
if (location[m][n] == -1) machineChessmanNum++;
else if (location[m][n] == 1) humanChessmanNum++;
m++;
n++;
}
if (m == k + 5) {
tupleScoreTmp = tupleScore(humanChessmanNum, machineChessmanNum);
for (m = k, n = j; m < k + 5; m++, n++) {
score[m][n] += tupleScoreTmp;
}
}
humanChessmanNum = 0;
machineChessmanNum = 0;
}
}
for (int i = 0; i < SIZE - 4; i++) {
for (int k = i, j = 0; j < SIZE - 4 && k < SIZE - 4; j++, k++) {
if (j == k) break;
int m = k;
int n = j;
while (m < k + 5 && n < j + 5) {
if (location[n][m] == -1) machineChessmanNum++;
else if (location[n][m] == 1) humanChessmanNum++;
m++;
n++;
}
if (m == k + 5) {
tupleScoreTmp = tupleScore(humanChessmanNum, machineChessmanNum);
for (m = k, n = j; m < k + 5; m++, n++) {
score[n][m] += tupleScoreTmp;
}
}
humanChessmanNum = 0;
machineChessmanNum = 0;
}
}
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (location[i][j] == 0 && score[i][j] > maxScore) {
goalX = i;
goalY = j;
maxScore = score[i][j];
}
}
}
if (goalX != -1 && goalY != -1) {
return new Location(goalX, goalY, -1);
}
return new Location(-1, -1, -1);
}
public int tupleScore(int humanCHessNum, int machineChessNum) {
if (humanCHessNum > 0 && machineChessNum > 0) {
return 0;
}
if (humanCHessNum == 0 && machineChessNum == 0) {
return 7;
}
if (machineChessNum == 1) {
return 35;
}
if (machineChessNum == 2) {
return 800;
}
if (machineChessNum == 3) {
return 3000;
}
if (machineChessNum == 4) {
return 800000;
}
if (humanCHessNum == 1) {
return 15;
}
if (humanCHessNum == 2) {
return 400;
}
if (humanCHessNum == 3) {
return 2000;
}
if (humanCHessNum == 4) {
return 100000;
}
return -1;
}
}
3.棋子对象类
package wuziqi;
public class Location {
private int x;
private int y;
private int owner;
public Location(int x, int y, int owner) {
this.x = x;
this.y = y;
this.owner = owner;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getOwner() {
return owner;
}
public void setOwner(int owner) {
this.owner = owner;
}
}