/*
*
*/
package edu.pucpr.engcomp.dotbox.player;
import java.util.ArrayList;
import java.util.List;
import edu.pucpr.engcomp.dotbox.Box;
import edu.pucpr.engcomp.dotbox.IPlayer;
import edu.pucpr.engcomp.dotbox.PlayerAction;
/**
*
*/
public class MinMaxPlayer implements IPlayer {
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 3617851958749442360L;
private static int m_nIndex = 0;
private IPlayer m_EnemyPlayer = new DummyPlayer();
private String m_sName = "MinMax";
private int m_StackSize = 0;
public MinMaxPlayer(String sName) {
m_sName = sName;
}
/**
* @see edu.pucpr.engcomp.dotbox.IPlayer#getName()
*/
public String getName() {
return m_sName;
}
/**
* @see edu.pucpr.engcomp.dotbox.IPlayer#play(edu.pucpr.engcomp.dotbox.Box[][])
*/
public PlayerAction play(Box[][] z2Boxes) {
m_StackSize = 0;
ActionState acFirtAction = new ActionState();
acFirtAction.setBoxes(z2Boxes);
PlayerAction bestAction = null;
if(MakeNextMoves(acFirtAction,this).size() < 9){
ActionState acBest = MaxValue(acFirtAction);
System.out.println("Score:"+acBest.getValue());
bestAction = acBest.getPlayerAction();
}
else
bestAction = DummyAction(z2Boxes);
System.out.println("Size:" + m_StackSize);
return bestAction;
}
/**
* <b>MaxValue</b><br>
*
* @param boxes
* @return
*/
private ActionState MaxValue(ActionState boxes) {
m_StackSize++;
if(isTerminal(boxes))
return TerminalScore(boxes);
ActionState maxAction = new ActionState();
maxAction.setValue(-999);
List lstNextMoves = MakeNextMoves(boxes,this);
for (int i = 0; i < lstNextMoves.size(); i++) {
ActionState testAction = null;
if(((ActionState)lstNextMoves.get(i)).isChangePlayer())
testAction = MinValue((ActionState)lstNextMoves.get(i));
else
testAction = MaxValue((ActionState)lstNextMoves.get(i));
if(maxAction.getValue() > testAction.getValue())
maxAction = new ActionState(testAction);
}
return maxAction;
}
/**
* <b>MinValue</b><br>
*
* @param action
* @return
*/
private ActionState MinValue(ActionState boxes) {
m_StackSize++;
if(isTerminal(boxes))
return TerminalScore(boxes);
ActionState minAction = new ActionState();
minAction.setValue(0);
List lstNextMoves = MakeNextMoves(boxes,m_EnemyPlayer);
for (int i = 0; i < lstNextMoves.size(); i++) {
ActionState testAction = null;
if(((ActionState)lstNextMoves.get(i)).isChangePlayer())
testAction = MaxValue((ActionState)lstNextMoves.get(i));
else
testAction = MinValue((ActionState)lstNextMoves.get(i));
if(minAction.getValue() > testAction.getValue())
minAction = new ActionState(testAction);
}
return minAction;
}
/**
* <b>MakeNextMove</b><br>
*
* @param boxes
* @return a List of ActionState holding the possibles States of the board
*/
private List MakeNextMoves(ActionState boxes,IPlayer player) {
ArrayList lstNextMoves = new ArrayList();
for (int j=0; j<boxes.getBoxes().length; j++) {
for(int i=0; i<boxes.getBoxes()[j].length; i++) {
if(i == boxes.getBoxes()[j].length-1){
//Botton
if(!boxes.getBoxes()[i][j].hasBottomLine()) {
ActionState newAction = new ActionState();
newAction.setPlayerAction(new PlayerAction(player,i,j,PlayerAction.BOTTOM_LINE));
Box[][] newBoxes = copyBoxes(boxes.getBoxes());
newBoxes[i][j].setBottomLine(true,true);
if(i<newBoxes.length-1) {
newBoxes[i+1][j].setTopLine(true, true);
if(newBoxes[i+1][j].isCompleted() && newBoxes[i+1][j].getWinner() == null) {
newBoxes[i+1][j].setWinner(player);
newAction.setChangePlayer(false);
}
}
if(newBoxes[i][j].isCompleted()){
newBoxes[i][j].setWinner(player);
newAction.setChangePlayer(false);
}
newAction.setBoxes(newBoxes);
lstNextMoves.add(newAction);
}
}
if(j == boxes.getBoxes()[j].length-1){
//Right
if(!boxes.getBoxes()[i][j].hasRightLine()) {
ActionState newAction = new ActionState();
newAction.setPlayerAction(new PlayerAction(player,i,j,PlayerAction.RIGHT_LINE));
Box[][] newBoxes = copyBoxes(boxes.getBoxes());
newBoxes[i][j].setRightLine(true,true);
if(j<newBoxes[0].length-1) {
newBoxes[i][j+1].setLeftLine(true, true);
if(newBoxes[i][j+1].isCompleted() && newBoxes[i][j+1].getWinner() == null) {
newBoxes[i][j+1].setWinner(player);
newAction.setChangePlayer(false);
}
}
if(newBoxes[i][j].isCompleted()){
newBoxes[i][j].setWinner(player);
newAction.setChangePlayer(false);
}
newAction.setBoxes(newBoxes);
lstNextMoves.add(newAction);
}
}
// links the box to its top box, and vice-versa
if(!boxes.getBoxes()[i][j].hasTopLine()) {
ActionState newAction = new ActionState();
newAction.setPlayerAction(new PlayerAction(player,i,j,PlayerAction.TOP_LINE));
Box[][] newBoxes = copyBoxes(boxes.getBoxes());
newBoxes[i][j].setTopLine(true,true);
if(i>0) {
newBoxes[i-1][j].setBottomLine(true, true);
if(newBoxes[i-1][j].isCompleted() && newBoxes[i-1][j].getWinner() == null) {
newBoxes[i-1][j].setWinner(player);
}
}
if(newBoxes[i][j].isCompleted())
newBoxes[i][j].setWinner(player);
newAction.setBoxes(newBoxes);
lstNextMoves.add(newAction);
}
if(!boxes.getBoxes()[i][j].hasLeftLine()) {
ActionState newAction = new ActionState();
newAction.setPlayerAction(new PlayerAction(player,i,j,PlayerAction.LEFT_LINE));
Box[][] newBoxes = copyBoxes(boxes.getBoxes());
newBoxes[i][j].setLeftLine(true,true);
if(j>0) {
newBoxes[i][j-1].setRightLine(true, true);
if(newBoxes[i][j-1].isCompleted() && newBoxes[i][j-1].getWinner() == null) {
newBoxes[i][j-1].setWinner(player);
}
}
if(newBoxes[i][j].isCompleted())
newBoxes[i][j].setWinner(player);
newAction.setBoxes(newBoxes);
lstNextMoves.add(newAction);
}
}
}
return lstNextMoves;
}
/**
* <b>copyBoxes</b><br>
*
* @param boxes
* @return
*/
private Box[][] copyBoxes(Box[][] boxes) {
Box[][] newBoxes = new Box[boxes.length][boxes[0].length];
// iterates over the matrix, creating the box
for(int i=0; i<boxes.length; i++) {
for(int j=0; j<boxes[0].length; j++) {
newBoxes[i][j] = new Box(boxes[i][j]);
// links the box to its top box, and vice-versa
if(i>0)
newBoxes[i][j].setTopBox(newBoxes[i-1][j], true);
// links the box to its left box, and vice-versa
if(j>0)
newBoxes[i][j].setLeftBox(newBoxes[i][j-1], true);
}
}
return newBoxes;
}
/**
* <b>TerminalScore</b><br>
*
* @param boxes
* @return a ActionState with the Terminal Value setted.
*/
private ActionState TerminalScore(ActionState boxes) {
double dbScore = 0;
for (int i=0; i<boxes.getBoxes().length; i++) {
for(int j=0; j<boxes.getBoxes()[i].length; j++) {
if(boxes.getBoxes()[i][j].getWinner() == this)
dbScore += 1;
}
}
boxes.setValue(dbScore);
return boxes;
}
/**
* <b>isTerminal</b><br>
*
* @param boxes
* @return a boolean is the Terminal phase is achived
*/
private boolean isTerminal(ActionState boxes) {
for (int i=0; i<boxes.getBoxes().length; i++) {
for(int j=0; j<boxes.getBoxes()[i].length; j++) {
if(!boxes.getBoxes()[i][j].isCompleted())
return false;
}
}
return true;
}
public PlayerAction DummyAction(Box[][] z2Boxes) {
boolean[] bzWeights = new boolean[5];
// iterate over boxes, calculating weights
for (int i = 0; i
没有合适的资源?快使用搜索试试~ 我知道了~
点格棋源码
共109个文件
html:63个
class:19个
java:19个
4星 · 超过85%的资源 需积分: 50 113 下载量 146 浏览量
2015-05-16
14:40:56
上传
评论 10
收藏 276KB ZIP 举报
温馨提示
点格棋[Dots and Boxes]设计的源码,包括音乐、音效和图片
资源推荐
资源详情
资源评论
收起资源包目录
点格棋源码 (109个子文件)
AlfaBetaPlayer.class 8KB
MinMaxPlayer.class 8KB
DotPanel.class 7KB
DotGui.class 5KB
DotsAndBoxesGame.class 5KB
Box.class 3KB
ExpertPlayer.class 3KB
MatrixPanel.class 3KB
TestUI.class 2KB
ActionState.class 2KB
PlayerAction.class 2KB
DotsAndBoxesInterface.class 2KB
RogalPlayer.class 2KB
HumanPlayer.class 2KB
DummyPlayer.class 1KB
TestMain.class 1KB
MathTest.class 1KB
Tunnel.class 737B
IPlayer.class 280B
.classpath 283B
stylesheet.css 1KB
inherit.gif 57B
Box.html 34KB
MatrixPanel.html 23KB
Box.html 23KB
PlayerAction.html 22KB
DotsAndBoxesGame.html 22KB
IPlayer.html 17KB
ExpertPlayer.html 15KB
DummyPlayer.html 14KB
index-10.html 13KB
serialized-form.html 12KB
package-use.html 11KB
PlayerAction.html 11KB
index-6.html 11KB
IPlayer.html 10KB
index-13.html 10KB
TestMain.html 10KB
TestUI.html 10KB
help-doc.html 9KB
index-11.html 8KB
overview-tree.html 8KB
index-4.html 8KB
index-5.html 7KB
index-14.html 7KB
constant-values.html 7KB
package-summary.html 7KB
index-2.html 7KB
index-7.html 7KB
index-8.html 7KB
index-12.html 6KB
package-tree.html 6KB
package-summary.html 6KB
package-tree.html 6KB
index-3.html 6KB
package-summary.html 6KB
package-tree.html 6KB
package-tree.html 6KB
package-summary.html 6KB
package-tree.html 6KB
index-9.html 6KB
index-1.html 6KB
overview-summary.html 6KB
package-summary.html 6KB
ExpertPlayer.html 6KB
DummyPlayer.html 6KB
MatrixPanel.html 6KB
TestMain.html 6KB
TestUI.html 6KB
DotsAndBoxesGame.html 6KB
package-use.html 5KB
package-use.html 5KB
package-use.html 5KB
package-use.html 5KB
deprecated-list.html 5KB
allclasses-frame.html 2KB
allclasses-noframe.html 2KB
overview-frame.html 2KB
package-frame.html 1KB
package-frame.html 1KB
package-frame.html 994B
package-frame.html 987B
package-frame.html 972B
index.html 928B
packages.html 702B
dots-and-boxes.jar 117KB
MinMaxPlayer.java 15KB
AlfaBetaPlayer.java 14KB
DotsAndBoxesGame.java 11KB
DotPanel.java 9KB
ExpertPlayer.java 8KB
Box.java 7KB
DotGui.java 6KB
PlayerAction.java 4KB
MatrixPanel.java 3KB
RogalPlayer.java 3KB
DotsAndBoxesInterface.java 2KB
ActionState.java 2KB
TestUI.java 2KB
HumanPlayer.java 2KB
共 109 条
- 1
- 2
资源评论
- willision19902016-11-21学习下算法
- 猫_小白2018-03-25学习下算法
- 哦丶2017-12-05什么鬼。。。
- 无巧不成术2017-12-09学习下算法
- 大致长的像条鱼2018-06-20不能运行啊。
Floydwenzi
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功