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行,未经过严格测试 ...
随机推荐
- 动态规划——详解leetcode518 零钱兑换 II
动态规划 零钱兑换 II 参考书目:<程序员代码面试指南:IT名企算法与数据结构题目最优解> 给定不同面额的硬币和一个总金额.写出函数来计算可以凑成总金额的硬币组合数.假设每一种面额的硬币 ...
- 深度学习批次(batch)、迭代(iteration)、周期(epoch)、前向传播(forward propagation)、反向传播(backward propagation)、学习率(learning rate)概念解释
虽然现在应该是已经熟练掌握这些基础概念的时候,但是我是鱼的记忆,上一秒的事情,下一秒就忘了,除非是重要的人的重要的事情,呜呜呜呜,我这个破脑子. 还是写一下吧,直接GPT出来的(人类之光,欢呼~). ...
- Azure – Front Door (AFD)
前言 会研究到 Azure Front Door (AFD) 是因为想安装 WAF. 结果研究了一圈, 发现 AFD 好弱啊. 有许多功能都有 limitation. Limitation & ...
- 算法与数据结构——AVL树(平衡二叉搜索树)
AVL树 在"二叉搜索树"章节提到,在多次插入和删除操作后,二叉搜索树可能退化为链表.在这种情况下,所有操作的时间复杂度将从O(logn)劣化为O(n). 如下图,经过两次删除节点 ...
- ModbusRTU通信协议报文剖析
前言 大家好!我是付工.前面给大家介绍了Modbus协议的应用层面.终于有人把Modbus说明白了那么,今天跟大家聊聊关于Modbus协议报文的那些事. 一.真实案例 前段时间有个粉丝朋友,让我帮他解 ...
- [TK] 矩阵取数游戏<简单版> hzoi-tg-906-2
本题是一个坐标DP问题 状态转移 首先我们注意到,一个状态只能由两种前置状态得到:取左边的数和取右边的数,因此我们以状态为阶段定义如下: \(f[a][b][c]\) 为状态转移数组,其中 \(a\) ...
- SXYZ-6.27专题比赛
好的,现在正式定义今天的比赛为一场伤心的比赛. ↑这张图片首先能说明一些问题,但这并不是关键. ↓这才是伤心的关键 ↑第一题文件输入输入爆 ↑第二题文件名直接爆 评语,一个比一个离谱! 然后只是很简单 ...
- RTPS代理与转发服务
Proxy介绍 利用libevent实现网络连接和线程池.通过tcp连接的方式实现rtsp消息转发,再通过udp连接进行rtp与rtcp转发.报文解析使用到了Qt库.请尽量使用qmake进行编译.源码 ...
- 日干算命api接口_json数据_性格/爱情/事业/财运/健康运势免费接口
该API接口基于传统的八字学原理,通过用户提供的日干信息,为用户提供性格.爱情.事业.财运和健康等多方面的运势分析和建议.以下是该接口的详细介绍: 一.功能概述 性格分析:根据用户的日干信 ...
- /proc/pids/smaps
Linux内存管理 -- /proc/{pid}/smaps讲解 基本介绍 /proc/PID/smaps 文件是基于 /proc/PID/maps 的扩展,他展示了一个进程的内存消耗,比同一目录下的 ...