package basegame;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
/***************************************************************************
* TODO
* <br>Created on 2013-7-3 下午8:26:59<br>
* @author daicy
***************************************************************************/
public class ChessBoard extends JPanel implements MouseListener,MouseMotionListener{
private Image chessBoard ;
private Image white ;
private Image black ;
public Image toClickFlag ;
public Image clickedFlag ;
public static int chessSize = 35; //棋子宽度
//棋盘
private static final int GRIDNUM = 15;
public static final int GRID = 35; //棋盘格子的边长
public static int BORDER = 20; //棋盘边框宽度
private int width,height;
private GameFrame mainFrame;
public int[][] chesses = new int[15][15]; //用于记录棋盘上那些位置已落子 0 空,1黑子,2白子
int mousex ;
int mousey ;
public ChessBoard(GameFrame mainFrame){
initImage();
this.mainFrame = mainFrame;
initOther();
addMouseListener(this);
addMouseMotionListener(this);
setVisible(true);
}
// 初始化窗体
private void initOther() {
width = chessBoard.getWidth(this);
height = chessBoard.getHeight(this);
setSize(width,height);
BORDER = (width - (GRIDNUM-1)* GRID)/2;
}
private void initImage(){
//Loading Images
MediaTracker tracker = new MediaTracker(this);//Used to track loading of image
Toolkit toolkit = Toolkit.getDefaultToolkit();
//loading images from jar file
chessBoard = toolkit.getImage("Res//FiveChessBoard.jpg");
white = toolkit.getImage("Res//White.gif");
black = toolkit.getImage("Res//Black.gif");
toClickFlag = toolkit.getImage("Res//Selected.gif");
clickedFlag = toolkit.getImage("Res//Clicked.gif");
tracker.addImage(chessBoard, 1);
tracker.addImage(white, 1);
tracker.addImage(black, 1);
tracker.addImage(toClickFlag, 1);
tracker.addImage(clickedFlag, 1);
try {
//Waiting until image loaded.
tracker.waitForAll();
} catch (InterruptedException e) {
}
chessSize = white.getHeight(this);
}
@Override
/**
* 重写父类的重画方法
*/
public void paint(Graphics g) {
g.drawImage(chessBoard, 0, 0, this);
for (int i = 0; i < mainFrame.getPlayers().length; i++) {
drawMyChess(g);
}
if(this.mainFrame.turnIndex==0){
g.drawImage(toClickFlag, mousex*GRID+BORDER-chessSize/2, mousey*GRID+BORDER-chessSize/2, null);
}else{
int mousex = mainFrame.getPlayers()[mainFrame.turnIndex].getCurrentChesse().x;
int mousey = mainFrame.getPlayers()[mainFrame.turnIndex].getCurrentChesse().y;
g.drawImage(clickedFlag, mousex*GRID+BORDER-chessSize/2, mousey*GRID+BORDER-chessSize/2, null);
}
}
public void drawMyChess(Graphics g) {
// GRID = 35; //棋盘格子的边长
//
// public static int BORDER = 20; //棋盘边框宽度
for (int i = 0; i < chesses.length; i++) {
for (int j = 0; j < chesses[i].length; j++) {
if(chesses[i][j]==1){
g.drawImage(black, i*GRID+BORDER-chessSize/2, j*GRID+BORDER-chessSize/2, null);
}else if(chesses[i][j]==2){
g.drawImage(white, i*GRID+BORDER-chessSize/2, j*GRID+BORDER-chessSize/2, null);
}
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(this.mainFrame.turnIndex==0){
int x = e.getX()/GRID;
int y = e.getY()/GRID;
if(chesses[x][y]==0){
mainFrame.getPlayers()[mainFrame.turnIndex].setCurrentChesse(new Point(x,y));
chesses[x][y] = mainFrame.getPlayers()[mainFrame.turnIndex].getChessNum();
mainFrame.turnIndex = (mainFrame.turnIndex+1)%2;
mainFrame.repaint();
}
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseDragged(MouseEvent e) {
}
//移动
public void mouseMoved(MouseEvent e) {
if(this.mainFrame.id==mainFrame.turnIndex){
mousex = e.getX()/GRID;
mousey = e.getY()/GRID;
repaint();//重画
}else{
setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
}
}
}
package basegame;
import java.util.Random;
public class FiveChessAI {
class Point{
int[] score = new int[6];
// source[i][j].score[0] = maxScore(source[i][j].score[1],
// source[i][j].score[2], source[i][j].score[3],
// source[i][j].score[4]); score[0]存的最大值
// source[i][j].score[5] = (source[i][j].score[1]
// + source[i][j].score[2] + source[i][j].score[3] + source[i][j].score[4]);
}
int[][] baseChessBoard; //棋盘 1黑子 2,白子
int com; //代表电脑子的数
int pea;
public FiveChessAI(int[][] baseChessBoard,int com,int pea) {
// TODO Auto-generated constructor stub
this.baseChessBoard = baseChessBoard;
this.com = com;
this.pea = pea;
}
public int makeResultPoint() {
Point[][] people = makeSource(pea); //我的
int maxP = searchMax(people); //最大可能组成5个的
int[] firstResultP = makeFirst(people, maxP); //所以等于最大值的序列
Point[][] computer = makeSource(com); //电脑的
int maxC = searchMax(computer);
int[] firstResultC = makeFirst(computer, maxC); //所以等于最大值的序列
if ((maxC == 0) && (maxP == 0)) {
return randomResult();
}
if ((maxC > maxP) || ((maxC == maxP) && (maxP < 3))
|| ((maxC == maxP) && (maxC >= 3))) { //电脑占优可以按要求随意走
return makeResult(firstResultC, people, computer);
}
//人占优可以按要求随意走,电脑要把人的位置走了
return makeResult(firstResultP, people, computer);
}
private int randomResult() {
int[] result = { 48, 56, 168, 176, 96, 97, 98, 111, 112, 113, 126, 127,
128 };
Random random = new Random();
int i = random.nextInt(result.length);
return result[i];
}
/***************************************************************************
* 先找最大值,再找综合实力,在随机取
* <br>Created on 2013-7-8 上午9:46:30<br>
* @param firstResult
* @param people
* @param computer
* @return
* @author daicy
***************************************************************************/
private int makeResult(int[] firstResult, Point[][] people, Point[][] computer) {
int max = -10;
//找所有值中的最大值
for (int i = 0; i < firstResult.length; i++) {
int x = firstResult[i] / 15;
int y = firstResult[i] % 15;
if (people[x][y].score[0] > max) {
max = people[x][y].score[0];
}
if (computer[x][y].score[0] > max){
max = computer[x][y].score[0];
}
}
int num = 0; //找所有值中最大值的个数
for (int i = 0; i < firstResult.length; i++) {
int x = firstResult[i] / 15;
int y = firstResult[i] % 15;
if (people[x][y].score[0] == max) {
num++;
}
if (computer[x][y].score[0] == max){
num++;
}
}
int[] secondResult = new int[num]; //找所有值中最大值的结果集
int k = 0;
for (int i = 0; i < firstResult.length; i++) {
int x = firstResult[i] / 15;
int y = firstResult[i] % 15;
if (people[x][y].score[0] == max) {
secondResult[k] = firstResult[i];
k++;
}
if (computer[x][y].score[0] == max){
secondResult[k] = firstResult[i];
k++;
}
}
int maxSum = -10; ////找所有值中的总和实力最大值
for (int i = 0; i < secondResult.length; i++) {
int x = secondResult[i] / 15;
int y = secondResult[i] % 15;
if (people[x][y].score[5] > maxSum) {
maxSum = people[x][y].score[5];
}
if (computer[x][y].score[5] > maxSum){
maxSum = computer[x][y].score[5];
}
}
num = 0;
for (int i = 0; i < secondResult.length; i++) {
int x = secondResult[i] / 15;
int y = secondResult[i] % 15;
if (people[x][y].score[5] == maxSum) {
num++;
}
if (computer[x][y].score[5] == maxSum){
num++;
}
}
int[] lastResult = new int[num];
k = 0;
for (int i = 0; i < secondResult.length; i++) {
int x = secondResult[i] / 15;
int y = secondResult[i] % 15;
if (people[x][y].score[5] == maxSum) {
lastResult[k] = secondResult[i];
k++;
}
if (computer[x][y].score[5] == maxSum){
lastResult[k] = secondResult[i];
k++;
}
}
int result = 0;
if (lastResult.length > 1) {
Random random = new Random();
result = random.nextInt(lastResult.length);
}
return lastResult[result];
}
/***************************************************************************
* 最大可能组成5个的所以情况
* <br>Created on 2013-7-6 下午8:13:05<br>
* @param point
* @param max
* @return
* @author daicy
***************************************************************************/
private int[] makeFirst(Point[][] point, int max) {
int head = 0;
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (point[i][j].score[0] != max)
continue;
head++;
}
}
int[] result = new int[head];
int k = 0;
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (point[i][j].score[0] != max)
continue;
result[k] = (i * 15 + j);
k++;
}
}
return result;
}
private int searchMax(Point[][] point) {
int max = 0;
for (int i = 0; i < 15; i++)
for (int j = 0; j < 15; j++) {
if (point[i][j].score[0] > max)
max = point[i][j].score[0];
}
return max;
}
private Point[][] makeSource(int chessNum) {
Point[][] source = new Point[15][15];
// int t;
// if (b)
// t = 1; //我先走
// else
// t = 2;
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
source[i][j] = new Point();
if (this.baseChessBoard[i][j] != 0)
continue;
source[i][j].score[1] = make1(i, j, chessNum); //x上下
source[i][j].score[2] = make2(i, j, chessNum); //y轴左右
source[i][j].score[3] = make3(i, j, chessNum); //另外两个对角
source[i][j].score[4] = make4(i, j, chessNum);
source[i][j].score[5] = (source[i][j].score[1]
+ source[i][j].score[2] + source[i][j].score[3] + source[i][j].score[4]);
source[i][j].score[0] = maxScore(source[i][j].score[1],
source[i][j].score[2], source[i][j].score[3],
source[i][j].score[4]);
}
}
return source;
}
private int maxScore(int a1, int a2, int a3, int a4) {
int max1 = Math.max(a1, a2);
int max2 = Math.max(a3, a4);
return Math.max(max1, max2);
}
private int make4(int i, int j, int t) {
int tp = 0;
int ntp = 0;
int x = i - 1;
int y = j + 1;
while ((x >= 0) && (y < 15) && (this.baseChessBoard[x][y] == t)) {
tp++;
x--;
y++;
}
if (((x >= 0) && (y < 15) && (this.baseChessBoard[x][y] != 0))
|| (x < 0) || (y >= 15)) {
ntp++;
}
x = i + 1;
y = j - 1;
while ((x < 15) && (y >= 0) && (this.baseChessBoard[x][y] == t)) {
tp++;
x++;
y--;
}
if (((x < 15) && (y >= 0) && (this.baseChessBoard[x][y] != 0))
|| (x >= 15) || (y < 0)) {
ntp++;
}
return makeAns(tp, ntp);
}
private int make3(int i, int j, int t) {
int tp = 0;
int ntp = 0;
int x = i - 1;
int y = j - 1;
while ((x >= 0) && (y >= 0) && (this.baseChessBoard[x][y] == t)) {
tp++;
x--;
y--;
}
if (((((x >= 0) && (y >= 0) && (this.baseChessBoard[x][y] != 0) ? 1 : 0) | (x < 0 ? 1
: 0)) != 0)
|| (y < 0)) {
ntp++;
}
x = i + 1;
y = j + 1;
while ((x < 15) && (y < 15) && (this.baseChessBoard[x][y] == t)) {
tp++;
x++;
y++;
}
if (((x < 15) && (y < 15) && (this.baseChessBoard[x][y] != 0))
|| (x >= 15) || (y >= 15)) {
ntp++;
}
return makeAns(tp, ntp);
}
private int make2(int i, int j, int t) {
int tp = 0;
int ntp = 0;
int x = i - 1;
int y = j;
while ((x >= 0) && (this.baseChessBoard[x][y] == t)) { //y轴上方相同的子数
tp++;
x--;
}
if (((x >= 0) && (this.baseChessBoard[x][y] != 0)) || (x < 0))
ntp++;
x = i + 1;
while ((x < 15) && (this.baseChessBoard[x][y] == t)) { //y轴下方相同的子数
tp++;
x++;
}
if (((x < 15) && (this.baseChessBoard[x][y] != 0)) || (x >= 15)) {
ntp++;
}
return makeAns(tp, ntp);
}
private int make1(int i, int j, int t) {
int tp = 0;
int ntp = 0;
int x = i;
int y = j - 1;
while ((y >= 0) && (this.baseChessBoard[x][y] == t)) { //x轴上方相同的子数
tp++;
y--;
}
if (((y >= 0) && (this.baseChessBoard[x][y] != 0)) || (y < 0))
ntp++;
y = j + 1;
while ((y < 15) && (this.baseChessBoard[x][y] == t)) { //x轴下方相同的子数
tp++;
y++;
}
if (((y < 15) && (this.baseChessBoard[x][y] != 0)) || (y >= 15)) {
ntp++;
}
return makeAns(tp, ntp);
}
private int makeAns(int tp, int ntp) {
if (tp == 4)
return tp;
if (ntp == 2) {
tp = -2;
} else if (ntp == 1) {
tp -= ntp;
}
return tp;
}
public boolean showWin(int x, int y, int now) {
int same = 1;
int xx = x - 1;
int yy = y;
while ((xx >= 0) && (this.baseChessBoard[xx][yy] == now)) { // x轴左边相同的5个
same++;
xx--;
}
if (same == 5)
return true;
xx = x + 1;
while ((xx < 15) && (this.baseChessBoard[xx][yy] == now)) { // x轴有边相同的5个
same++;
xx++;
}
if (same == 5)
return true;
same = 1;
xx = x;
yy = y - 1;
while ((yy >= 0) && (this.baseChessBoard[xx][yy] == now)) { //y轴左边相同的5个
same++;
yy--;
}
if (same == 5)
return true;
yy = y + 1;
while ((yy < 15) && (this.baseChessBoard[xx][yy] == now)) { //y轴右边相同的5个
same++;
yy++;
}
if (same == 5)
return true;
same = 1;
xx = x - 1;
yy = y - 1;
while ((xx >= 0) && (yy >= 0) && (this.baseChessBoard[xx][yy] == now)) { //左上角相同的5个
same++;
xx--;
yy--;
}
if (same == 5)
return true;
xx = x + 1;
yy = y + 1;
while ((xx < 15) && (yy < 15) && (this.baseChessBoard[xx][yy] == now)) { //右下角相同的5个
same++;
xx++;
yy++;
}
if (same == 5)
return true;
same = 1;
xx = x - 1;
yy = y + 1;
while ((xx >= 0) && (yy < 15) && (this.baseChessBoard[xx][yy] == now)) { //左下角相同的5个
same++;
xx--;
yy++;
}
if (same == 5)
return true;
xx = x + 1;
yy = y - 1;
while ((xx < 15) && (yy >= 0) && (this.baseChessBoard[xx][yy] == now)) { //右上角相同的5个
same++;
xx++;
yy--;
}
return same == 5;
}
public void showBoard(int[][] map) {
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
}
}