📄 population.java
字号:
package net.leonmax.rpsGame.gaEngine;
import net.leonmax.rpsGame.*;
public class Population {
private static int length; // population length
private int[] gaSeq; // move sequence of GA system
private int[] oppoSeq; // move sequence of the opponent
private int count = 0; // move count of the population
private boolean[] winState; // every win state of the sequence in the
// population
private int winCount = 0; // win count of the sequence
public static void setLength(int num) {
length = num;
}
public Population() {
this.oppoSeq = new int[length];
this.gaSeq = new int[length];
this.winState = new boolean[length];
}
// add the new move of GA system in the last RPS.
public boolean addNewSeq(int ga, int oppo) {
if (!isFull()) {
this.oppoSeq[count] = oppo;
this.gaSeq[count] = ga;
winState[count] = isWin(count);
if (winState[count++]) // add to win count as the fitness
winCount++;
return true;
} else {
return false;
}
}
public boolean isWin(int num) {
try {
if (this.gaSeq[num] == GlobalData.rock) {
if (this.oppoSeq[num] == GlobalData.rock)
return false;
else if (this.oppoSeq[num] == GlobalData.paper)
return false;
else if (this.oppoSeq[num] == GlobalData.scissor)
return true;
else {
throw new ErrorGeneException();
}
} else if (this.gaSeq[num] == GlobalData.paper) {
if (this.oppoSeq[num] == GlobalData.rock)
return true;
else if (this.oppoSeq[num] == GlobalData.paper)
return false;
else if (this.oppoSeq[num] == GlobalData.scissor)
return false;
else {
throw new ErrorGeneException();
}
} else if (this.gaSeq[num] == GlobalData.scissor) {
if (this.oppoSeq[num] == GlobalData.rock)
return false;
else if (this.oppoSeq[num] == GlobalData.paper)
return true;
else if (this.oppoSeq[num] == GlobalData.scissor)
return false;
else {
throw new ErrorGeneException();
}
} else {
throw new ErrorGeneException();
}
} catch (ErrorGeneException e) {
System.out
.println("only rock, paper and scissor is available in this game.");
return false;
}
}
public boolean isFull() {
if (count == length) {
System.out.println("GA system won " + winCount
+ " in this population.");
return true;
} else
return false;
}
public int getWinCount() {
return winCount;
}
public int getGaSeq(int num) {
return gaSeq[num];
}
public int getOppoSeq(int num) {
return oppoSeq[num];
}
public void setGaSeq(int num, int type) {
this.gaSeq[num] = type;
if (winState[num] != isWin(num)) {
winState[num] = isWin(num);
if (!winState[num])
winCount--;
else
winCount++;
}
}
void setOppoSeq(int num, int type) {
this.oppoSeq[num] = type;
if (winState[num] != isWin(num)) {
winState[num] = isWin(num);
if (!winState[num])
winCount--;
else
winCount++;
}
}
// change rps integer to string
public String showHand(int hand) {
if (hand == GlobalData.rock)
return "rock";
else if (hand == GlobalData.paper)
return "paper";
else
return "scissor";
}
public void printWinState(int num) {
System.out.print("Round \t" + (num+1)+"\t\t");
// System.out.println("GA system show\t\t" + showHand(gaSeq[num]));
// System.out.println("opponent show\t\t" + showHand(oppoSeq[num]));
System.out.print(showHand(gaSeq[num]) + "\t\t");
System.out.print(showHand(oppoSeq[num]) + "\t\t");
if (winState[num]) {
System.out.println("won");
// System.out.println("GA system \t\twon");
} else {
System.out.println("lose/draw");
// System.out.println("GA system \t\tdidn't win.");
}
}
public void printAllState() {
for (int j = 0; j < GlobalData.pop_size; j++)
printWinState(j);
isFull();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -