Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- import java.util.List;
- import java.util.ArrayList;
- import java.util.Random;
- import java.util.Iterator;
- import com.sun.javafx.runtime.SystemProperties;
- import game.engine.utils.Pair;
- import game.mc.MCAction;
- import game.mc.MCPlayer;
- import game.mc.MCGame;
- public class h353036 extends MCPlayer {
- private MCAction prevAction;
- private MCAction shadowPrevAction;
- private int myScore;
- private int shadowMyScore;
- private int enemyScore;
- private int shadowEnemyScore;
- private MCAction bestAction = null;
- private int playersParity = 1;
- private int deep = 0;
- private MCAction lastAction = null;
- private List<MCAction> ancestors = new ArrayList<MCAction>();
- private int count = 0;
- Paritys par;
- public h353036(int color, int[][] board, Random r) {
- super(color, board, r);
- myScore = 0;
- enemyScore = 0;
- }
- public class Paritys
- {
- Pair<Integer, Integer> IDandSIZE;
- int size;
- public Paritys(int ID, int size)
- {
- this.IDandSIZE = new Pair<>(ID, size);
- this.size = size;
- }
- public void decrementSize(){
- size--;
- }
- public boolean isEmpty(){
- if(size == 0){
- return true;
- }
- return false;
- }
- }
- public class Node
- {
- List<Node> children = null;
- MCAction ancestor = null;
- int value;
- public Node(int value, MCAction ancestor)
- {
- this.ancestor = ancestor;
- this.children = new LinkedList<>();
- this.value = value;
- }
- public void addChild(Node child)
- {
- children.add(child);
- }
- public int getValue(int value){
- return this.value;
- }
- public void setValue(int value){
- this.value = value;
- }
- public MCAction getAncestor(){
- return this.ancestor;
- }
- }
- @Override
- public MCAction getAction(List<Pair<Integer, MCAction>> prevActions) {
- int prevScore = 0;
- for (Pair<Integer, MCAction> action : prevActions) {
- if (action.second == null) {
- continue;
- }
- prevAction = action.second;
- boolean samePart = (action.second.x1 < 4 && action.second.x2 < 4) || (4 <= action.second.x1 && 4 <= action.second.x2);
- if (samePart) {
- // merge / move
- board[action.second.x2][action.second.y2] += board[action.second.x1][action.second.y1];
- } else {
- // capture / move
- prevScore = board[action.second.x2][action.second.y2];
- board[action.second.x2][action.second.y2] = board[action.second.x1][action.second.y1];
- }
- board[action.second.x1][action.second.y1] = MCGame.empty;
- }
- enemyScore += prevScore;
- int maxScore = 0;
- List<Pair<Integer, MCAction>> actions = new LinkedList<Pair<Integer, MCAction>>();
- // generate actions
- for (int i = 4 * color; i < 4 * (color + 1); i++) {
- for (int j = 0; j < 4; j++) {
- if (board[i][j] != MCGame.empty) {
- for (int i2 = 0; i2 < 8; i2++) {
- for (int j2 = 0; j2 < 4; j2++) {
- MCAction action = new MCAction(i, j, i2, j2);
- int score = -1;
- try {
- score = score(board, prevAction, prevScore, color, action, myScore - enemyScore);
- } catch (Exception e) {
- System.out.println("ACTION: " + action);
- e.printStackTrace();
- System.exit(1);
- }
- if (maxScore < score) {
- maxScore = score;
- actions.clear();
- }
- if (maxScore == score) {
- actions.add(new Pair<Integer, MCAction>(maxScore, action));
- }
- }
- }
- }
- }
- }
- List<Integer> indexList = new ArrayList<Integer>();
- List<Integer> scoreList = new ArrayList<Integer>();
- List<MCAction> actionList = new ArrayList<MCAction>();
- int iteratorIndex = 0;
- Iterator<Pair<Integer,MCAction>> it = actions.iterator();
- while (it.hasNext()) {
- Pair<Integer, MCAction> pair = it.next();
- scoreList.add(pair.first);
- actionList.add(pair.second);
- indexList.add(iteratorIndex);
- iteratorIndex++ ;
- }
- MCAction localAction = null;
- int localMaxScore = Integer.MIN_VALUE;
- int localIndex = Integer.MIN_VALUE;
- int[][] shadowBoard = board;
- MCAction action = null;
- shadowMyScore = myScore;
- shadowEnemyScore = enemyScore;
- for(int i = 0; i < actionList.size(); i++){
- localAction = actionList.get(i);
- localMaxScore = scoreList.get(i);
- localIndex = indexList.get(i);
- action = getShadowAction(localAction, localMaxScore, shadowMyScore, shadowEnemyScore, localIndex, shadowBoard, 1);
- }
- for(int i = 0; i < 8; i++){
- System.out.println(board[i][0] + "" + board[i][1] + "" + board[i][2] + "" + board[i][3]);
- }
- if (maxScore == 0 && board[action.x2][action.y2] != MCGame.empty) {
- board[action.x2][action.y2] += board[action.x1][action.y1];
- } else {
- board[action.x2][action.y2] = board[action.x1][action.y1];
- }
- board[action.x1][action.y1] = MCGame.empty;
- myScore += maxScore;
- return action;
- }
- public MCAction getShadowAction(MCAction localAction, int localMaxScore, int shadowMyScore, int shadowEnemyScore, int localIndex, int[][] shadowBoard, int isFirst) {
- //!!!!!!!!!!!!!!!!!!!!!!!
- //!!!!!!!!!!!!!!!!!!!!!!!
- //!!!!!!!!!!!!!!!!!!!!!!!
- //!!!!!!!!!!!!!!!!!!!!!!!
- //!!!!!!!!!!!!!!!!!!!!!!!
- //!!!!!!!!!!!!!!!!!!!!!!!
- //!!!!!!!!!!!!!!!!!!!!!!!
- //!!!!!!!!!!!!!!!!!!!!!!!
- /*itt kellene valahogy allítani a player parityt, de mivel tobb action-re is meg van hivva, igy nem tudom
- hogy kellene megallapitani, hogy mikor melyik jatekos jon, illetve a deep inkrementalasa (lejjebb)
- is hasonlo oknal fogva lesz rossz, es mar csak ez hianyzik, hogy faba tudjam epiteni*/
- //!!!!!!!!!!!!!!!!!!!!!!!
- //!!!!!!!!!!!!!!!!!!!!!!!
- //!!!!!!!!!!!!!!!!!!!!!!!
- //!!!!!!!!!!!!!!!!!!!!!!!
- //!!!!!!!!!!!!!!!!!!!!!!!
- //!!!!!!!!!!!!!!!!!!!!!!!
- //!!!!!!!!!!!!!!!!!!!!!!!
- //!!!!!!!!!!!!!!!!!!!!!!!
- //!!!!!!!!!!!!!!!!!!!!!!!
- //!!!!!!!!!!!!!!!!!!!!!!!
- if(isFirst == 1){
- ancestors.add(localAction);
- }
- if (localMaxScore == 0 && board[localAction.x2][localAction.y2] != MCGame.empty) {
- shadowBoard[localAction.x2][localAction.y2] += shadowBoard[localAction.x1][localAction.y1];
- } else {
- shadowBoard[localAction.x2][localAction.y2] = shadowBoard[localAction.x1][localAction.y1];
- }
- shadowBoard[localAction.x1][localAction.y1] = MCGame.empty;
- if(playersParity % 2 == 1){
- shadowMyScore += localMaxScore;
- }else{
- shadowEnemyScore += localMaxScore;
- }
- int[][] reversedShadowBoard;
- for(int i = 0; i < 8; i++){
- for(int j = 0; j < 4; j++){
- reversedShadowBoard[i][j] = shadowBoard[7-i][3-j];
- }
- }
- // innentol az ellenfel szemevel nezzuk a tablat
- int shadowEnemyMaxScore = 0;
- List<Pair<Integer, MCAction>> shadowEnemyActions = new LinkedList<Pair<Integer, MCAction>>();
- // generate shadowEnemyActions
- for (int i = 4 * color; i < 4 * (color + 1); i++) {
- for (int j = 0; j < 4; j++) {
- if (reversedShadowBoard[i][j] != MCGame.empty) {
- for (int i2 = 0; i2 < 8; i2++) {
- for (int j2 = 0; j2 < 4; j2++) {
- MCAction action = new MCAction(i, j, i2, j2);
- int score = -1;
- try {
- score = score(reversedShadowBoard, localAction, localMaxScore, color, action, shadowEnemyScore - shadowMyScore);
- //score = score(board, prevAction, prevScore, color, action, myScore - enemyScore);
- } catch (Exception e) {
- System.out.println("ACTION: " + action);
- e.printStackTrace();
- System.exit(1);
- }
- if (shadowEnemyMaxScore < score) {
- shadowEnemyMaxScore = score;
- shadowEnemyActions.clear();
- }
- if (shadowEnemyMaxScore == score) {
- shadowEnemyActions.add(new Pair<Integer, MCAction>(shadowEnemyMaxScore, action));
- }
- }
- }
- }
- }
- }
- List<Integer> indexEnemyList = new ArrayList<Integer>();
- List<Integer> scoreEnemyList = new ArrayList<Integer>();
- List<MCAction> actionEnemyList = new ArrayList<MCAction>();
- int iteratorIndex = 0;
- Iterator<Pair<Integer,MCAction>> it = shadowEnemyActions.iterator();
- while (it.hasNext()) {
- Pair<Integer, MCAction> pair = it.next();
- scoreEnemyList.add(pair.first);
- actionEnemyList.add(pair.second);
- indexEnemyList.add(iteratorIndex);
- iteratorIndex++ ;
- }
- MCAction localEnemyAction = null;
- int localEnemyMaxScore = Integer.MIN_VALUE;
- int localEnemyIndex = Integer.MIN_VALUE;
- int[][] shadowEnemyBoard = reversedShadowBoard;
- MCAction action = null;
- for(int i = 0; i < actionEnemyList.size(); i++){
- localEnemyAction = actionEnemyList.get(i);
- localEnemyMaxScore = scoreEnemyList.get(i);
- localEnemyIndex = indexEnemyList.get(i);
- if(localEnemyAction == null || localEnemyMaxScore == Integer.MIN_VALUE || localEnemyIndex == Integer.MIN_VALUE){
- if(playersParity % 2 == 1){
- break; //csak akkor torjon meg, ha az en lepesemet akarja visszaadni
- }else{
- return lastAction; //ha az ellenfelet, akkor a legutobbi lepesemet adja vissza
- }
- }else if(deep<4){
- deep ++;
- action = getShadowAction(localEnemyAction, localEnemyMaxScore, shadowEnemyScore, shadowMyScore, localEnemyIndex, shadowEnemyBoard, 2)
- }else{
- break;
- }
- }
- action = actionEnemyList.size() == 0 ? null : actionEnemyList.get(r.nextInt(actionEnemyList.size()));
- lastAction = action;
- return action;
- }
- private int score(int[][] board, MCAction prevAction, int prevScore, int color, MCAction action, int scoreDiff) {
- int[][] figures = new int[2][3];
- for (int i = 0; i < board.length; i++) {
- for (int j = 0; j < board[i].length; j++) {
- if (board[i][j] != MCGame.empty) {
- if (i < board.length / 2) {
- figures[0][board[i][j] - 1]++;
- } else {
- figures[1][board[i][j] - 1]++;
- }
- }
- }
- }
- int numFigures = figures[color][0] + figures[color][1] + figures[color][2];
- int score = MCGame.score(board, figures, prevAction, prevScore, color, action);
- boolean samePart = (action.x1 < 4 && action.x2 < 4) || (4 <= action.x1 && 4 <= action.x2);
- if (0 <= score && !samePart && numFigures == 1) {
- if (scoreDiff < 0) {
- score = -1;
- } else {
- score = 10;
- }
- }
- return score;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement