java alpha的值_处理,无法为ArrayList中的对象设置alpha值

这是question的后续行动 .

我希望得到一堆粒子对象以保持某个'alpha'值 . 该α值将基于其与相邻粒子的接近度而增加/减少 .

目前我的代码导致所有粒子保持最大alpha值 . 我相信这是由于迭代通过ArrayList导致多次重绘alpha . 由于这个原因,该计划运行缓慢 .

class Particle{

PVector velocity, location; //PVector variables for each particle.

Particle(){ //Constructor - random location and speed for each particle.

velocity = new PVector(random(-0.5,0.5), random(-0.5,0.5));

location = new PVector(random(0,width),random(0,width));

}

void update() { location.add(velocity); } //Motion method.

void edge() { //Wraparound case for particles.

if (location.x > width) {location.x = 0;}

else if (location.x < 0) {location.x = width;}

if (location.y > height) {location.y = 0;}

else if (location.y < 0) {location.y = height;}

}

void display(ArrayList p){ //Display method to show lines and ellipses between particles.

for(Particle other: p){ //For every particle in the ArrayList.

float d = PVector.dist(location,other.location); //Get distance between any two particle.

float a = 255 - map(d,0,112,0,255); //Map variable 'a' as alpha based on distance. E.g. if distance is high, d = 100, alpha is low, a = 255 - 225 = 30.

if(other==this){continue;}

println("Lowest distance of any two particle =" + d); //Debug output.

if(d<112){ //If the distance of any two particle falls bellow 112.

noStroke(); //No outline.

fill(0,a); //Particle are coloured black, 'a' to vary alpha.

ellipse(location.x, location.y, 8, 8); //Draw ellipse based on location of particle.

}else{

noStroke(); //No outline.

fill(0,30); //For particles far away, set them to a fix alpha of '30'

ellipse(location.x, location.y, 8, 8); //Draw ellipse based on location of particle.

}

}

}

}

ArrayList particles = new ArrayList(); //Create a new arraylist of type Particle.

void setup(){

size(640,640,P2D); //Setup frame of sketch.

for (int i=0; i<40; i++) {

particles.add(new Particle()); //Add five Particle elements into arraylist.

}

}

void draw(){

background(255); //Set white background.

for(Particle p: particles){ //For every 'p' of type Particle in arraylist particles.

p.update(); //Update location based on velocity.

p.display(particles); //Display each particle in relation to other particles.

p.edge(); //Wraparound if particle reaches edge of screen.

}

}

Alpha-Beta剪枝是一种优化版的博弈树搜索算法,用于下棋游戏寻找最佳策略。由于五子棋的复杂度相对较高,特别是12x12的大棋盘,编写完整的代码会比较冗长。以下是一个简化的框架,展示了如何在Java实现基本的Alpha-Beta剪枝算法: ```java import java.util.ArrayList; import java.util.List; public class GomokuPlayer { private int[][] board; // 12x12棋盘 // ... 其他游戏规则、评估函数等 public Move minimax(BoardState state, int depth, int alpha, int beta, boolean maximizingPlayer) { if (isGameOver(state) || depth == 0) { return evaluatePosition(state); } List<Move> moves = getLegalMoves(state); if (maximizingPlayer) { double maxEval = -Double.MAX_VALUE; for (Move move : moves) { BoardState childState = makeMove(state, move); double eval = minimax(childState, depth - 1, alpha, beta, false); maxEval = Math.max(maxEval, eval); alpha = Math.max(alpha, eval); if (beta <= alpha) break; // 剪枝条件:如果当前分支已经无法提高alpha,则提前终止搜索 } return maximize(maxEval, moves); } else { // minimizing player double minEval = Double.MAX_VALUE; for (Move move : moves) { BoardState childState = makeMove(state, move); double eval = minimax(childState, depth - 1, alpha, beta, true); minEval = Math.min(minEval, eval); beta = Math.min(beta, eval); if (beta <= alpha) break; // 剪枝条件 } return minimize(minEval, moves); } } // ...其他辅助方法,如getLegalMoves(), isGameOver(), makeMove(), evaluatePosition()等 } // 使用示例: BoardState initialState = initBoardState(); Move bestMove = new GomokuPlayer().minimax(initialState, MAX_DEPTH, -Double.MAX_VALUE, Double.MAX_VALUE, true); ``` 请注意,这只是一个简化版本,实际项目还需要考虑边界处理、游戏结束条件检查、评估函数的实现以及其他细节。同时,对于12x12的大棋盘,这个算法可能会非常慢,因此你可能需要对搜索深度和剪枝策略进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值