⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paintbotscontrol.java

📁 美国大学计算机专业大学一年期末作业 在15*15的格子里
💻 JAVA
字号:
import java.awt.Color;
import java.util.Random;
import paintbots.*;

import paintbots.BoardSquare;
import paintbots.MoveRequest;
import paintbots.PaintbotControl;
import paintbots.PaintbotSimulation;
import paintbots.InternalBoardSquare.SquareType;
import paintbots.MoveRequest.MoveType;
import javax.swing.*;
public class PaintBotsControl extends PaintbotControl {
	
	private Color mColor;
	private int mOccupyCount = 0;
	private MoveType[] mMoveTypes = {MoveType.FORWARD, MoveType.ROTATE_RIGHT, MoveType.BACKWARD,
			MoveType.ROTATE_LEFT};
	private boolean flag = false;
	private static boolean toggle = false;
	private int bullets = 0;
	
	public static void main(String[] args) {
		PaintbotControl pc[] = { new PaintBotsControl() };
		PaintbotSimulation.createSimulation(pc);
	}

	public MoveRequest getMove(BoardSquare[][] shortrangescan,
			BoardSquare[][] longrangescan) {
	//	JOptionPane.showMessageDialog(null, mColor.toString());
	//	if (mColor == Color.blue)
	//		return new MoveRequest(MoveType.ROTATE_LEFT, MoveRequest.DONOT_FIRE_PAINT, MoveRequest.REQUEST_LONGRANGE_SCAN);
		
		int mark[] = new int[4], maxMark = -9999;
		int index = 0;
		BoardSquare currSquare = shortrangescan[2][2];
		int currDir = currSquare.robotDirection();
		BoardSquare surround[] = {shortrangescan[1][2], shortrangescan[2][3], 
				shortrangescan[3][2], shortrangescan[2][1]};
		Color invColor = Color.blue;
		
		if (flag)
		{
			flag = false;
			return new MoveRequest(MoveType.FORWARD, MoveRequest.DONOT_FIRE_PAINT, MoveRequest.REQUEST_LONGRANGE_SCAN);
		}
		
		if (mColor == Color.red)
			invColor = Color.blue;
		else
			invColor = Color.red;
		
		MoveType moveType = MoveType.FORWARD;
		
		for (int i=0; i<4; i++)
		{
			mark[i] = getMark(surround[(i+currDir)%4]);
			if (mark[i] > maxMark)
			{
				index = i;
				maxMark = mark[i];
			}
		}
		
		moveType = this.mMoveTypes[(index+currDir)%4];
		
		
		boolean flag = false;
		
		int i = 0;
		switch (currDir)
		{
		case 0:
			for (i=0; i<2; i++)
				if (shortrangescan[i][2] != null)
					if (shortrangescan[i][2].getSquareColor() == invColor)
					{
						flag = true;
						break;
					}
			break;
		case 1:
			for (i=0; i<2; i++)
				if (shortrangescan[2][i] != null)
					if (shortrangescan[2][i].getSquareColor() == invColor)
					{
						flag = true;
						break;
					}
			break;
		case 2:
			for (i=0; i<2; i++)
				if (shortrangescan[i+3][2] != null)
					if (shortrangescan[i+3][2].getSquareColor() == invColor)
					{
						flag = true;
						break;
					}
			break;
		case 3:
			for (i=0; i<2; i++)
				if (shortrangescan[2][i+3] != null)
					if (shortrangescan[2][i+3].getSquareColor() == invColor)
					{
						flag = true;
						break;
					}
			break;
		}
		
		/*
		MoveRequest req;
		if (flag == true && bullets < 20)
		{
			req = new MoveRequest(MoveType.DO_NOTHING, MoveRequest.FIRE_PAINT, MoveRequest.REQUEST_LONGRANGE_SCAN);
			bullets++;
		}
		else
			req = new MoveRequest(moveType, MoveRequest.DONOT_FIRE_PAINT, MoveRequest.REQUEST_LONGRANGE_SCAN);
		
		int diff = currDir - index;
		if (diff == 1 || diff == -1)
			flag = true;
		*/
		MoveRequest req = null; // new MoveRequest(MoveType.DO_NOTHING, MoveRequest.DONOT_FIRE_PAINT, MoveRequest.REQUEST_LONGRANGE_SCAN);
		if (surround[currDir] != null 
				&& surround[currDir].getSquareColor() == invColor
				&& surround[currDir].getSquareColor() == Color.white
				)
			if (mColor == Color.red)
				if (!surround[currDir].blueRobotPresent())
					return new MoveRequest(MoveType.FORWARD, MoveRequest.DONOT_FIRE_PAINT, MoveRequest.REQUEST_LONGRANGE_SCAN);
		
		if (surround[(currDir+2)%4] != null 
				&& surround[(currDir+2)%4].getSquareColor() == invColor
				&& surround[(currDir+2)%4].getSquareColor() == Color.white)
			if (mColor == Color.red)
				if (!surround[(currDir+2)%4].blueRobotPresent())
					return new MoveRequest(MoveType.BACKWARD, MoveRequest.DONOT_FIRE_PAINT, MoveRequest.REQUEST_LONGRANGE_SCAN);
	
		int rnd = (int)(Math.random() * 10);
		switch (rnd)
		{
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
			if (surround[currDir] != null 
					&& surround[currDir].getSquareType() != SquareType.ROCK
					&& surround[currDir].getSquareType() != SquareType.WALL
					&& surround[currDir].getSquareType() != SquareType.FOGROCK)
				req = new MoveRequest(MoveType.FORWARD, MoveRequest.DONOT_FIRE_PAINT, MoveRequest.REQUEST_LONGRANGE_SCAN);
			else
			{
				MoveType t = ((int)(Math.random()*2) == 0)?MoveType.ROTATE_LEFT:MoveType.ROTATE_RIGHT;
				req = new MoveRequest(t, MoveRequest.DONOT_FIRE_PAINT, MoveRequest.REQUEST_LONGRANGE_SCAN);
			}
			break;
		case 5:
			req = new MoveRequest(MoveType.ROTATE_LEFT, MoveRequest.DONOT_FIRE_PAINT, MoveRequest.REQUEST_LONGRANGE_SCAN);
			break;
		case 6:
			req = new MoveRequest(MoveType.ROTATE_RIGHT, MoveRequest.DONOT_FIRE_PAINT, MoveRequest.REQUEST_LONGRANGE_SCAN);
			break;
		case 7:
		case 8:
		case 9:
			if (surround[(currDir+2)%4] != null 
					&& surround[(currDir+2)%4].getSquareType() != SquareType.ROCK
					&& surround[(currDir+2)%4].getSquareType() != SquareType.WALL
					&& surround[(currDir+2)%4].getSquareType() != SquareType.FOGROCK)
				req = new MoveRequest(MoveType.BACKWARD, MoveRequest.DONOT_FIRE_PAINT, MoveRequest.REQUEST_LONGRANGE_SCAN);
			else
			{
				MoveType t = ((int)(Math.random()*2) == 0)?MoveType.ROTATE_LEFT:MoveType.ROTATE_RIGHT;
				req = new MoveRequest(t, MoveRequest.DONOT_FIRE_PAINT, MoveRequest.REQUEST_LONGRANGE_SCAN);
			}
			break;
		default:
			req = new MoveRequest(MoveType.DO_NOTHING, MoveRequest.DONOT_FIRE_PAINT, MoveRequest.REQUEST_LONGRANGE_SCAN);
			break;
		}
		
		return req;
	}

	public String getRobotName() {
		if (mColor == Color.red)
			return "RedKiller";
		else
			return "BlueKiller";
	}

	public String getStudentLastName() {
		return "";
	}

	public String getStudentID() {
		return "";
	}

	public boolean tournamentEntry() {
		return true;
	}

	public void reset(Color c) {
		JOptionPane.showMessageDialog(null, c.toString());
		mColor = c;
	}
	
	private int getMark(BoardSquare square){
		int mark = 0;
		SquareType type = square.getSquareType();
		Color squareColor = square.getSquareColor();
		
		if (square == null)
			return -1000;
		
		if (squareColor == mColor)
			mark = 0;
		else
		if (type == SquareType.ROCK || type == SquareType.WALL
				|| type == SquareType.FOGROCK)
			mark = -10;
		else
		if (type == SquareType.NORMAL || type == SquareType.FOG)
		{
			if (mColor == Color.red)
			{
				if (square.blueRobotPresent())
				{
					mark = -10;
				}
				else
				if (squareColor == Color.blue)
					mark = 20;
			}
			else
			{
				if (square.redRobotPresent())
				{
					mark = -10;
				}
				else
				if (squareColor == Color.red)
					mark = 20;
			}
		}
 
		return mark;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -