Skip to content
Permalink
Browse files

添加控制流程&完善ai&ai训练

  • Loading branch information...
ltxom committed Mar 13, 2019
1 parent 047c867 commit 5e68fb4d88db8dd08cec51bab01f88b98796253f
@@ -55,6 +55,12 @@
<artifactId>tess4j</artifactId>
<version>4.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.clerezza.ext/org.json.simple -->
<dependency>
<groupId>org.apache.clerezza.ext</groupId>
<artifactId>org.json.simple</artifactId>
<version>0.4</version>
</dependency>

</dependencies>

@@ -5,6 +5,16 @@

@SpringBootApplication
public class TetrisplayerApplication {
public static final String COMMAND_PREFIX = "@";
public static final String COMMAND_SUFFIX = "*";
public static final String COMMAND_TOP = "1";
public static final String COMMAND_DOWN = "2";
public static final String COMMAND_LEFT = "3";
public static final String COMMAND_RIGHT = "4";
public static final String COMMAND_A = "a";
public static final String COMMAND_B = "b";
public static final String COMMAND_R = "r";


public static void main(String[] args) {
System.setProperty("java.awt.headless", "false");
@@ -0,0 +1,129 @@
package me.ltxom.tetrisplayer.controller;

import me.ltxom.tetrisplayer.TetrisplayerApplication;
import me.ltxom.tetrisplayer.entity.tetris.Block;
import me.ltxom.tetrisplayer.entity.tetris.PossibleMove;
import me.ltxom.tetrisplayer.entity.tetris.TetrisBoard;
import me.ltxom.tetrisplayer.service.cv.ScreenStreamService;
import me.ltxom.tetrisplayer.service.serial.SerialCommunicationService;
import me.ltxom.tetrisplayer.service.tetris.TetrisBoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.LinkedList;

@RestController
public class ApplicationController {
@Autowired
private SerialCommunicationService serialCommunicationService;
@Autowired
private ScreenStreamService screenStreamService;
@Autowired
private TetrisBoardService tetrisBoardService;

private static String generateCommand(String command) {
return TetrisplayerApplication.COMMAND_PREFIX + command + TetrisplayerApplication.COMMAND_SUFFIX;
}

@RequestMapping(value = "/startgame", method = {RequestMethod.GET})
public Object start() throws InterruptedException {
LinkedList<Block> nextQueue = new LinkedList<>();

new Thread(() -> {
boolean firstMove = true;
boolean firstMoveFlag2 = false;
Block nextBlock = null;
a:
for (; ; ) {
BufferedImage bufferedImage = screenStreamService.captureScreen(new Rectangle(0, 0, 775, 435));

bufferedImage = ScreenStreamService.imgToGray(bufferedImage);
TetrisBoard tetrisBoard = tetrisBoardService.analyzeBoardByImg(new Date(), bufferedImage);
if (tetrisBoard == null || tetrisBoard.getNextBlocks()[0] == null) {
System.out.println("Game has not started yet");
break a;
}

if (firstMove) {
for (int i = 0; i < tetrisBoard.getNextBlocks().length; i++) {
nextQueue.add(tetrisBoard.getNextBlocks()[i]);
}
} else {
if (firstMoveFlag2) {
nextQueue.add(tetrisBoard.getNextBlocks()[5]);
firstMoveFlag2 = false;
}
for (int i = 0; i < 4; i++) {
if (!tetrisBoard.getNextBlocks()[i].equals(nextQueue.get(i))) {
System.out.println(i + ": " + tetrisBoard.getNextBlocks()[i] + "------------" + nextQueue.get(i));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue a;
}
}
nextQueue.add(tetrisBoard.getNextBlocks()[5]);

}
if (firstMove)
nextBlock = nextQueue.remove();
tetrisBoard.setFallingBlock(nextBlock);
PossibleMove possibleMove = tetrisBoardService.getNextLocation(tetrisBoard);
System.out.println(possibleMove.getScore());
String command = "";

int lessRight = 0;
// if (nextBlock instanceof BlockT && (possibleMove.getRotation() == 1 || possibleMove.getRotation() ==
// 3)) {
// lessRight = 1;
// }
// if (nextBlock instanceof BlockT && (possibleMove.getRotation() == 1 || possibleMove.getRotation() ==
// 3)) {
// lessRight = 1;
// }

// 变化
for (int i = 0; i < possibleMove.getRotation(); i++) {
command += TetrisplayerApplication.COMMAND_A;
}
// 移动到最左边
for (int i = 0; i < 5; i++) {
command += TetrisplayerApplication.COMMAND_LEFT;
}
for (int i = 0; i < possibleMove.getMove().getDeltaX() - lessRight; i++) {
command += TetrisplayerApplication.COMMAND_RIGHT;
}

command += TetrisplayerApplication.COMMAND_TOP;
command = generateCommand(command);

if (firstMove) {
try {
Thread.sleep(4000);
firstMove = false;
firstMoveFlag2 = true;
} catch (InterruptedException e) {
e.printStackTrace();
}

}
if (command != null)
serialCommunicationService.writeString(command);
nextBlock = nextQueue.remove();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
return "started";
}
}
@@ -6,4 +6,6 @@

int[][][] getRotationalSpaceMatrix();

@Override
public boolean equals(Object block2);
}
@@ -1,6 +1,6 @@
package me.ltxom.tetrisplayer.entity.tetris;

public class BlockI implements Block {
public class BlockI extends BlockImp {

private int[][] standardSpaceMatrix = new int[][]{{1, 1, 1, 1}};
private int[][][] rotationalSpaceMatrix = new int[][][]{{{1}, {1}, {1}, {1}}};
@@ -0,0 +1,33 @@
package me.ltxom.tetrisplayer.entity.tetris;

public class BlockImp implements Block {

private int[][] standardSpaceMatrix;
private int[][][] rotationalSpaceMatrix;

@Override
public int[][] getStandardSpaceMatrix() {
return standardSpaceMatrix;
}

@Override
public int[][][] getRotationalSpaceMatrix() {
return rotationalSpaceMatrix;
}

@Override
public boolean equals(Object anotherBlock) {
BlockImp block = (BlockImp) anotherBlock;
if (block == null && this == null)
return true;
if (block.getStandardSpaceMatrix().length != this.getStandardSpaceMatrix().length || block.getStandardSpaceMatrix()[0].length != this.getStandardSpaceMatrix()[0].length)
return false;
for (int y = 0; y < this.getStandardSpaceMatrix().length; y++) {
for (int x = 0; x < this.getStandardSpaceMatrix()[0].length; x++) {
if (block.getStandardSpaceMatrix()[y][x] != this.getStandardSpaceMatrix()[y][x])
return false;
}
}
return true;
}
}
@@ -1,20 +1,21 @@
package me.ltxom.tetrisplayer.entity.tetris;

public class BlockJ implements Block {
public class BlockJ extends BlockImp {

private int[][] standardSpaceMatrix = new int[][]{{1, 0, 0}, {1, 1, 1}};
private int[][][] rotationalSpaceMatrix = new int[][][]{{
{1, 1}, {0, 1}, {0, 1}},
{{1, 1, 1}, {0, 0, 1}},
{{0, 1}, {0, 1}, {1, 1}}};

@Override
public int[][] getStandardSpaceMatrix() {
return standardSpaceMatrix;
}
private int[][] standardSpaceMatrix = new int[][]{{0, 0, 1}, {1, 1, 1}};
private int[][][] rotationalSpaceMatrix = new int[][][]{
{{1, 0}, {1, 0}, {1, 1}},
{{1, 1, 1}, {1, 0, 0}}, {
{1, 1}, {0, 1}, {0, 1}},};

@Override
public int[][][] getRotationalSpaceMatrix() {
return rotationalSpaceMatrix;
}
@Override
public int[][] getStandardSpaceMatrix() {
return standardSpaceMatrix;
}

@Override
public int[][][] getRotationalSpaceMatrix() {
return rotationalSpaceMatrix;
}
}
@@ -1,12 +1,12 @@
package me.ltxom.tetrisplayer.entity.tetris;

public class BlockL implements Block {
public class BlockL extends BlockImp {

private int[][] standardSpaceMatrix = new int[][]{{0, 0, 1}, {1, 1, 1}};
private int[][][] rotationalSpaceMatrix = new int[][][]{{
{1, 0}, {1, 0}, {1, 1}},
{{1, 1, 1}, {1, 0, 0}},
{{1, 1}, {0, 1}, {0, 1}}};
private int[][] standardSpaceMatrix = new int[][]{{1, 0, 0}, {1, 1, 1}};
private int[][][] rotationalSpaceMatrix = new int[][][]{{{1, 1}, {1, 0}, {1, 0}},
{{1, 1, 1}, {0, 0, 1}}, {
{0, 1}, {0, 1}, {1, 1}}
};

@Override
public int[][] getStandardSpaceMatrix() {
@@ -1,6 +1,6 @@
package me.ltxom.tetrisplayer.entity.tetris;

public class BlockO implements Block {
public class BlockO extends BlockImp {

private int[][] standardSpaceMatrix = new int[][]{{1, 1}, {1, 1}};
private int[][][] rotationalSpaceMatrix = new int[][][]{};
@@ -1,7 +1,6 @@
package me.ltxom.tetrisplayer.entity.tetris;

public class BlockS implements Block {

public class BlockS extends BlockImp {
private int[][] standardSpaceMatrix = new int[][]{{0, 1, 1}, {1, 1, 0}};
private int[][][] rotationalSpaceMatrix = new int[][][]{{{1, 0}, {1, 1}, {0, 1}}};

@@ -1,10 +1,9 @@
package me.ltxom.tetrisplayer.entity.tetris;

public class BlockT implements Block {

public class BlockT extends BlockImp {
private int[][] standardSpaceMatrix = new int[][]{{0, 1, 0}, {1, 1, 1}};
private int[][][] rotationalSpaceMatrix = new int[][][]{{{1, 0}, {1, 1}, {1, 0}}, {{1, 1
, 1}, {0, 1, 0}}, {{0, 1}, {1, 1}, {0, 1}}};
private int[][][] rotationalSpaceMatrix = new int[][][]{{{1, 0}, {1, 1}, {1, 0}}, {{1, 1
, 1}, {0, 1, 0}}, {{0, 1}, {1, 1}, {0, 1}}};

@Override
public int[][] getStandardSpaceMatrix() {
@@ -1,17 +1,16 @@
package me.ltxom.tetrisplayer.entity.tetris;

public class BlockZ implements Block {
public class BlockZ extends BlockImp {
private int[][] standardSpaceMatrix = new int[][]{{1, 1, 0}, {0, 1, 1}};
private int[][][] rotationalSpaceMatrix = new int[][][]{{{0, 1}, {1, 1}, {1, 0}}};

private int[][] standardSpaceMatrix = new int[][]{{1, 1, 0}, {0, 1, 1}};
private int[][][] rotationalSpaceMatrix = new int[][][]{{{0, 1}, {1, 1}, {1, 0}}};
@Override
public int[][] getStandardSpaceMatrix() {
return standardSpaceMatrix;
}

@Override
public int[][] getStandardSpaceMatrix() {
return standardSpaceMatrix;
}

@Override
public int[][][] getRotationalSpaceMatrix() {
return rotationalSpaceMatrix;
}
@Override
public int[][][] getRotationalSpaceMatrix() {
return rotationalSpaceMatrix;
}
}

0 comments on commit 5e68fb4

Please sign in to comment.
You can’t perform that action at this time.