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行,未经过严格测试 ...
随机推荐
- 消息队列的对比测试与RocketMQ使用扩展
消息队列的对比测试与RocketMQ使用扩展 本文的主要内容包括以下几个方面: 原有的消息技术选型 RocketMQ与kafka 测试对比 如何构建自己的消息队列服务 RocketMQ扩展改造 ...
- TypeScript 高级教程 – 把 TypeScript 当编程语言使用 (第二篇)
前言 上一篇, 我们提到, TypeScript 进阶有 3 个阶段. 第一阶段是 "把 TypeScript 当强类型语言使用", 我们已经介绍完了. 第二阶段是 "把 ...
- namespace hdk
没有高精类,因为这玩意太占内存了,正在优化 demap Rander StringAddition_InFix string ordered_vector #include<bits/stdc+ ...
- 第42天:WEB攻防-PHP应用&MYSQL架构&SQL注入&跨库查询&文件读写&权限操作 - 快捷方式
接受的参数值未进行过滤直接带入SQL查询 MYSQL注入:(目的获取当前web权限) 1.判断常见四个信息(系统,用户,数据库名,版本) 2.根据四个信息去选择方案 root用户:先测试读写,后测试获 ...
- Android性能优化:getResources()与Binder交火导致的界面卡顿优化
背景 某轮测试发现,我们的设备运行一个第三方的App时,卡顿感非常明显: 界面加载很慢,菊花转半天 滑屏极度不跟手,目测观感帧率低于15 对比机(竞品)也会稍微一点卡,但是好很多,基本不会有很大感觉的 ...
- 一个SMMU内存访问异常的问题
最近碰到棘手的问题: 以太网进行iperf测试时, 发生了SMMU (System Memory Management Unit)访问异常导致内核崩溃. 原本只是内部测试发现, 后面在试验车上也概率性 ...
- ByConity与主流开源OLAP引擎(Clickhouse、Doris、Presto)性能对比分析
引言: 随着数据量和数据复杂性的不断增加,越来越多的企业开始使用OLAP(联机分析处理)引擎来处理大规模数据并提供即时分析结果.在选择OLAP引擎时,性能是一个非常重要的因素. 因此,本文将使用TPC ...
- ssh建立github连接 基于ssh密钥
1. 建立公钥和私钥 ps:公钥放在github上面的,私钥放在自己本地电脑 : 先生成密钥:打开 gitbash 输入命令: ssh-keygen -t rsa -b 4096 -C "z ...
- springboot admin 整合nacos,context-path问题
1.在使用springboot admin 整合nacos时发现问题,springboot admin server访问admin client的默认地址为http://ip:port/actuato ...
- KubeSphere 社区双周报 | 2024.01.04-01.18
KubeSphere 社区双周报主要整理展示新增的贡献者名单和证书.新增的讲师证书以及两周内提交过 commit 的贡献者,并对近期重要的 PR 进行解析,同时还包含了线上/线下活动和布道推广等一系列 ...