java——棋牌类游戏五子棋(singlewzq1.0)之二
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();
}
}
}
java——棋牌类游戏五子棋(singlewzq1.0)之二的更多相关文章
- UE4开发神秘海域类游戏原型 初阶(二):动画资源的整合
前一篇已经确定神海类游戏原型的目标,首先要做的就是3C's(Character, Controls, Camera)的开发. UE4的3C's的程序部分开发主要也就是基于他的GamePlay Fr ...
- android开发之网络棋牌类在线游戏开发心得(服务器端、Java) 好文章值得收藏
标签: android服务器 2013-10-09 17:28 3618人阅读 评论(0) 收藏 举报 分类: android(11) 转自:http://blog.csdn.net/bromon/a ...
- CCF201512-2 消除类游戏 java(100分)
试题编号: 201512-2 试题名称: 消除类游戏 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 消除类游戏是深受大众欢迎的一种游戏,游戏在一个包含有n行m列的游戏棋盘上进 ...
- 网狐6603 cocos2dx 棋牌、捕鱼、休闲类游戏《李逵捕鱼》手机端完整源码分析及分享
该资源说明: cocos2d 棋牌.捕鱼.休闲类游戏<李逵捕鱼>手机端完整源码,网狐6603配套手机版源码,可以选桌子,适合新手学习参考,小编已亲测试,绝对完整可编译手机端,下载后将文件考 ...
- java导出数据EXCEL的工具类(以spring-webmvc-4.0.4jar为基础)
1.本工具类继承于 spring-webmvc-4.0.4jar文件心中的一个类 AbstractExcelView 2.代码如下 package com.skjd.util; import j ...
- C++ MFC棋牌类小游戏day1
好用没用过C++做一个完整一点的东西了,今天开始希望靠我这点微薄的技术来完成这个小游戏. 我现在的水平应该算是菜鸟中的战斗鸡了,所以又很多东西在设计和技术方面肯定会有很大的缺陷,我做这个小游戏的目的单 ...
- JAVA小项目之五子棋
五子棋V1.0 功能: 人人对战,人机对战(初级) 记录双方分数: 主要知识点: 二维坐标系中,各方向坐标的关系及规律. 效果图: 主框架类: package com.gxlee.wzq; /** * ...
- 《Unity3D/2D游戏开发从0到1》正式出版发行
<Unity3D/2D游戏开发从0到1>正式出版发行 去年个人编写的Unity书籍正式在2015年7月正式发行,现在补充介绍一下个人著作.书籍信息: 书籍的名称: <Uni ...
- Java太阳系小游戏分析和源代码
Java太阳系小游戏分析和源代码 -20150809 近期看了面向对象的一些知识.然后跟着老师的解说做了一个太阳系各行星绕太阳转的小游戏,来练习巩固一下近期学的知识: 用到知识点:类的继承.方法的重载 ...
- 消除类游戏(js版)
最近一直在玩一款消灭星星的消除类游戏,周末无聊就用js也写了一遍,感觉玩比写还困难一直玩不到10000分.废话不多说直接上源码. 效果图(ps 页面有点难看木有美工) 代码总共456行,未经过严格测试 ...
随机推荐
- Angular 18+ 高级教程 – 学以致用
前言 读这么多原理,到底为了什么?真实项目中真的会用得到吗? 你正在疑惑 "知识的力量" 吗? 本篇会给一个非常非常好的案例,让你感悟 -- 知识如何用于实战. 记住,我的目的是让 ...
- DOM – Work with Document.styleSheets and JS/Scss Breakpoint Media Query
前言 为了方便管理, 我们会定义 CSS Variables, 类似于全局变量. 有时候做特效的时候还需要 JavaScript 配合, 这时就会希望 JavaScript 可以获取到 CSC Var ...
- 全网最适合入门的面向对象编程教程:54 Python字符串与序列化-字符串格式化与format方法
全网最适合入门的面向对象编程教程:54 Python 字符串与序列化-字符串格式化与 format 方法 摘要: 在 Python 中,字符串格式化是将变量插入到字符串中的一种方式,Python 提供 ...
- linux java 初始环境配置
linux初始环境配置 1.设置IP 查看虚拟机ip地址:ip addr 修改ip地址 Vi /etc/sysconfig/network~scrips/ifcfg-ens33(不一定是33 动态的) ...
- day04-常用DOS命令
打开cmd的方式 开始-W-windows系统-命令提示符 win键+R键 鼠标在任意文件夹上, shift+鼠标右键 资源管理器的地址栏前面加cmd,然后回车 管理员方式运行:选择命令提示符右键以管 ...
- KubeSphere 3.1.1 发布,可以接入集群已有的 Prometheus
KubeSphere 作为一款面向应用的开源容器平台,经过 3 年的发展和 10 个版本的迭代,收获了两百多位开源贡献者,超过十万次下载,并有数千名社区用户用 KubeSphere 作为企业容器平台. ...
- Paths和Files
Paths 类 Paths 类主要用于操作文件和目录路径.它提供了一些静态方法,用于创建java.nio.file.Path实例,代表文件系统中的路径. // 创建一个Path实例,表示当前目录下的一 ...
- Lambda表达式、方法引用、算法、正则表达式
文章目录 1.Lambda表达式 1.1 Lambda表达式基本使用 1.2 Lambda表达式省略规则 2.JDK8新特性(方法引用) 2.1 静态方法引用 2.2 实例方法引用 2.3 特定类型的 ...
- Nuxt.js 应用中的 server:devHandler 事件钩子详解
title: Nuxt.js 应用中的 server:devHandler 事件钩子详解 date: 2024/10/26 updated: 2024/10/26 author: cmdragon e ...
- 经验总结之 _DEBUGGER _03 _Server Tomcat v8.0 Server at localhost was unable to start within xx seconds
经验总结之 _DEBUGGER _03 _Server Tomcat v8.0 Server at localhost was unable to start within xx seconds 好好 ...