我正在尝试优化此代码中存在的大量 if 语句:
public class MyClass{
ArrayList<Stack<String>> positions = new ArrayList<Stack<String>>();
Stack<String> pos1;
Stack<String> pos2;
Stack<String> pos3;
Stack<String> pos4;
Stack<String> pos5;
Stack<String> pos6;
Stack<String> pos7;
Stack<String> pos8;
Stack<String> pos9;
Stack<String> pos10;
Stack<String> pos11;
Stack<String> pos12;
Stack<String> pos13;
Stack<String> pos14;
Stack<String> pos15;
Stack<String> pos16;
Stack<String> pos17;
Stack<String> pos18;
Stack<String> pos19;
Stack<String> pos20;
Stack<String> pos21;
Stack<String> pos22;
Stack<String> pos23;
Stack<String> pos24;
Stack<String> pos25;
Stack<String> pos26;
Stack<String> pos27;
public ClassName{
for (int i=0; i<28; i++) {
positions.add(i,new Stack<String>());
}
}
public void Updater (int position, boolean player){
if(player){ // if player one is up
// start position 1 actions
if(position==1){
int distance = pos1.size();
if(distance==1){
String temp = pos1.peek();
if(temp=="x"){ // only make move if player one's chip is on top of the stack
pos1.pop();
pos2.push(temp);
}
}
if(distance==2){
String temp = pos1.peek();
if(temp=="x"){
pos1.pop();
pos3.push(temp);
}
}
if(distance==3){
String temp = pos1.peek();
if(temp=="x"){
pos1.pop();
pos7.push(temp);
}
}
if(distance==4){
String temp = pos1.peek();
if(temp=="x"){
pos1.pop();
pos8.push(temp);
}
}
if(distance==5){
String temp = pos1.peek();
if(temp=="x"){
pos1.pop();
pos9.push(temp);
}
}
if(distance==6){
String temp = pos1.peek();
if(temp=="x"){
pos1.pop();
pos10.push(temp);
}
}
if(distance==7){
String temp = pos1.peek();
if(temp=="x"){
pos1.pop();
pos11.push(temp);
}
}
if(distance==8){
String temp = pos1.peek();
if(temp=="x"){
pos1.pop();
pos12.push(temp);
}
}
if(distance==9){
String temp = pos1.peek();
if(temp=="x"){
pos1.pop();
pos13.push(temp);
}
}
if(distance==10){
String temp = pos1.peek();
if(temp=="x"){
pos1.pop();
pos14.push(temp);
}
}
if(distance==11){
String temp = pos1.peek();
if(temp=="x"){
pos1.pop();
pos15.push(temp);
}
}
if(distance==12){
String temp = pos1.peek();
if(temp=="x"){
pos1.pop();
pos16.push(temp);
}
}
if(distance==13){
String temp = pos1.peek();
if(temp=="x"){
pos1.pop();
pos16.push(temp);
}
}
if(distance>13){
String temp = pos1.peek();
pos1.pop();
if(temp=="x"){
pos26.push(temp); // push to player one's winners circle
}
}
}
// end position 1 actions
// start position 2 actions
if(position==2){
int distance = pos2.size();
if(distance==1){
String temp = pos2.peek();
if(temp=="x"){
pos2.pop();
pos3.push(temp);
}
}
if(distance==2){
String temp = pos2.peek();
if(temp=="x"){
pos2.pop();
pos7.push(temp);
}
}
if(distance==3){
String temp = pos2.peek();
if(temp=="x"){
pos2.pop();
pos8.push(temp);
}
}
if(distance==4){
String temp = pos2.peek();
if(temp=="x"){
pos2.pop();
pos9.push(temp);
}
}
if(distance==5){
String temp = pos2.peek();
if(temp=="x"){
pos2.pop();
pos10.push(temp);
}
}
if(distance==6){
String temp = pos2.peek();
if(temp=="x"){
pos2.pop();
pos11.push(temp);
}
}
if(distance==7){
String temp = pos2.peek();
if(temp=="x"){
pos2.pop();
pos12.push(temp);
}
}
if(distance==8){
String temp = pos2.peek();
if(temp=="x"){
pos2.pop();
pos13.push(temp);
}
}
if(distance==8){
String temp = pos2.peek();
if(temp=="x"){
pos2.pop();
pos14.push(temp);
}
}
if(distance==9){
String temp = pos2.peek();
if(temp=="x"){
pos2.pop();
pos15.push(temp);
}
}
if(distance==10){
String temp = pos2.peek();
if(temp=="x"){
pos2.pop();
pos16.push(temp);
}
}
if(distance==11){
String temp = pos2.peek();
if(temp=="x"){
pos2.pop();
pos17.push(temp);
}
}
if(distance>11){
String temp = pos2.peek();
pos2.pop();
if(temp=="x"){
pos26.push(temp); // push to player one's winners circle
}
}
}
// end position 2 actions
// start position 3 actions
if(position==3){
int distance = pos3.size();
if(distance==1){
String temp = pos3.peek();
if(temp=="x"){
pos3.pop();
pos7.push(temp);
}
}
if(distance==2){
String temp = pos3.peek();
if(temp=="x"){
pos3.pop();
pos8.push(temp);
}
}
if(distance==3){
String temp = pos3.peek();
if(temp=="x"){
pos3.pop();
pos9.push(temp);
}
}
if(distance==4){
String temp = pos3.peek();
if(temp=="x"){
pos3.pop();
pos10.push(temp);
}
}
if(distance==5){
String temp = pos3.peek();
if(temp=="x"){
pos3.pop();
pos11.push(temp);
}
}
if(distance==6){
String temp = pos3.peek();
if(temp=="x"){
pos3.pop();
pos12.push(temp);
}
}
if(distance==7){
String temp = pos3.peek();
if(temp=="x"){
pos3.pop();
pos13.push(temp);
}
}
if(distance==8){
String temp = pos3.peek();
if(temp=="x"){
pos3.pop();
pos14.push(temp);
}
}
if(distance==9){
String temp = pos3.peek();
if(temp=="x"){
pos3.pop();
pos15.push(temp);
}
}
if(distance==10){
String temp = pos3.peek();
if(temp=="x"){
pos3.pop();
pos16.push(temp);
}
}
if(distance==11){
String temp = pos3.peek();
if(temp=="x"){
pos3.pop();
pos17.push(temp);
}
}
if(distance>11){
String temp = pos3.peek();
pos3.pop();
if(temp=="x"){
pos26.push(temp); // push to player one's winners circle
}
}
}
// end position 3 actions
// start position 7 actions
if(position==7){
int distance = pos7.size();
if(distance==1){
String temp = pos7.peek();
if(temp=="x"){
pos7.pop();
pos8.push(temp);
}
}
if(distance==2){
String temp = pos7.peek();
if(temp=="x"){
pos7.pop();
pos9.push(temp);
}
}
if(distance==3){
String temp = pos7.peek();
if(temp=="x"){
pos7.pop();
pos10.push(temp);
}
}
if(distance==4){
String temp = pos7.peek();
if(temp=="x"){
pos7.pop();
pos11.push(temp);
}
}
if(distance==5){
String temp = pos7.peek();
if(temp=="x"){
pos7.pop();
pos12.push(temp);
}
}
if(distance==6){
String temp = pos7.peek();
if(temp=="x"){
pos7.pop();
pos13.push(temp);
}
}
if(distance==7){
String temp = pos7.peek();
if(temp=="x"){
pos7.pop();
pos14.push(temp);
}
}
if(distance==8){
String temp = pos7.peek();
if(temp=="x"){
pos7.pop();
pos15.push(temp);
}
}
if(distance==9){
String temp = pos7.peek();
if(temp=="x"){
pos7.pop();
pos16.push(temp);
}
}
if(distance==10){
String temp = pos7.peek();
if(temp=="x"){
pos7.pop();
pos17.push(temp);
}
}
if(distance>10){
String temp = pos7.peek();
pos7.pop();
if(temp=="x"){
pos26.push(temp); // push to player one's winners circle
}
}
}
// end position 7 actions
// start position 8 actions
if(position==8){
int distance = pos8.size();
if(distance==1){
String temp = pos8.peek();
if(temp=="x"){
pos8.pop();
pos9.push(temp);
}
}
if(distance==2){
String temp = pos8.peek();
if(temp=="x"){
pos8.pop();
pos10.push(temp);
}
}
if(distance==3){
String temp = pos8.peek();
if(temp=="x"){
pos8.pop();
pos11.push(temp);
}
}
if(distance==4){
String temp = pos8.peek();
if(temp=="x"){
pos8.pop();
pos12.push(temp);
}
}
if(distance==5){
String temp = pos8.peek();
if(temp=="x"){
pos8.pop();
pos13.push(temp);
}
}
if(distance==6){
String temp = pos8.peek();
if(temp=="x"){
pos8.pop();
pos14.push(temp);
}
}
if(distance==7){
String temp = pos8.peek();
if(temp=="x"){
pos8.pop();
pos15.push(temp);
}
}
if(distance==8){
String temp = pos8.peek();
if(temp=="x"){
pos8.pop();
pos16.push(temp);
}
}
if(distance==9){
String temp = pos8.peek();
if(temp=="x"){
pos8.pop();
pos17.push(temp);
}
}
if(distance>9){
String temp = pos8.peek();
pos8.pop();
if(temp=="x"){
pos26.push(temp); // push to player one's winners circle
}
}
}
// end position 8 actions
// start position 9 actions
if(position==9){
int distance = pos9.size();
if(distance==1){
String temp = pos9.peek();
if(temp=="x"){
pos9.pop();
pos10.push(temp);
}
}
if(distance==2){
String temp = pos9.peek();
if(temp=="x"){
pos9.pop();
pos11.push(temp);
}
}
if(distance==3){
String temp = pos9.peek();
if(temp=="x"){
pos9.pop();
pos12.push(temp);
}
}
if(distance==4){
String temp = pos9.peek();
if(temp=="x"){
pos9.pop();
pos13.push(temp);
}
}
if(distance==5){
String temp = pos9.peek();
if(temp=="x"){
pos9.pop();
pos14.push(temp);
}
}
if(distance==6){
String temp = pos9.peek();
if(temp=="x"){
pos9.pop();
pos15.push(temp);
}
}
if(distance==7){
String temp = pos9.peek();
if(temp=="x"){
pos9.pop();
pos16.push(temp);
}
}
if(distance==8){
String temp = pos9.peek();
if(temp=="x"){
pos9.pop();
pos17.push(temp);
}
}
if(distance>8){
String temp = pos9.peek();
pos9.pop();
if(temp=="x"){
pos26.push(temp); // push to player one's winners circle
}
}
}
// end position 9 actions
// start position 10 actions
if(position==10){
int distance = pos10.size();
if(distance==1){
String temp = pos10.peek();
if(temp=="x"){
pos10.pop();
pos11.push(temp);
}
}
if(distance==2){
String temp = pos10.peek();
if(temp=="x"){
pos10.pop();
pos12.push(temp);
}
}
if(distance==3){
String temp = pos10.peek();
if(temp=="x"){
pos10.pop();
pos13.push(temp);
}
}
if(distance==4){
String temp = pos10.peek();
if(temp=="x"){
pos10.pop();
pos14.push(temp);
}
}
if(distance==5){
String temp = pos10.peek();
if(temp=="x"){
pos10.pop();
pos15.push(temp);
}
}
if(distance==6){
String temp = pos10.peek();
if(temp=="x"){
pos10.pop();
pos16.push(temp);
}
}
if(distance==7){
String temp = pos10.peek();
if(temp=="x"){
pos10.pop();
pos17.push(temp);
}
}
if(distance>7){
String temp = pos10.peek();
pos10.pop();
if(temp=="x"){
pos26.push(temp); // push to player one's winners circle
}
}
}
// end position 10 actions
// start position 11 actions
if(position==11){
int distance = pos11.size();
if(distance==1){
String temp = pos11.peek();
if(temp=="x"){
pos11.pop();
pos12.push(temp);
}
}
if(distance==2){
String temp = pos11.peek();
if(temp=="x"){
pos11.pop();
pos13.push(temp);
}
}
if(distance==3){
String temp = pos11.peek();
if(temp=="x"){
pos11.pop();
pos14.push(temp);
}
}
if(distance==4){
String temp = pos11.peek();
if(temp=="x"){
pos11.pop();
pos15.push(temp);
}
}
if(distance==5){
String temp = pos11.peek();
if(temp=="x"){
pos11.pop();
pos16.push(temp);
}
}
if(distance==6){
String temp = pos11.peek();
if(temp=="x"){
pos11.pop();
pos17.push(temp);
}
}
if(distance>6){
String temp = pos11.peek();
pos11.pop();
if(temp=="x"){
pos26.push(temp); // push to player one's winners circle
}
}
}
// end position 11 actions
// start position 12 actions
if(position==12){
int distance = pos12.size();
if(distance==1){
String temp = pos12.peek();
if(temp=="x"){
pos12.pop();
pos13.push(temp);
}
}
if(distance==2){
String temp = pos12.peek();
if(temp=="x"){
pos12.pop();
pos14.push(temp);
}
}
if(distance==3){
String temp = pos12.peek();
if(temp=="x"){
pos12.pop();
pos15.push(temp);
}
}
if(distance==4){
String temp = pos12.peek();
if(temp=="x"){
pos12.pop();
pos16.push(temp);
}
}
if(distance==5){
String temp = pos12.peek();
if(temp=="x"){
pos12.pop();
pos17.push(temp);
}
}
if(distance>5){
String temp = pos12.peek();
pos12.pop();
if(temp=="x"){
pos26.push(temp); // push to player one's winners circle
}
}
}
// end position 12 actions
// start position 13 actions
if(position==13){
int distance = pos13.size();
if(distance==1){
String temp = pos13.peek();
if(temp=="x"){
pos13.pop();
pos14.push(temp);
}
}
if(distance==2){
String temp = pos13.peek();
if(temp=="x"){
pos13.pop();
pos15.push(temp);
}
}
if(distance==3){
String temp = pos13.peek();
if(temp=="x"){
pos13.pop();
pos16.push(temp);
}
}
if(distance==4){
String temp = pos13.peek();
if(temp=="x"){
pos13.pop();
pos17.push(temp);
}
}
if(distance>4){
String temp = pos13.peek();
pos13.pop();
if(temp=="x"){
pos26.push(temp); // push to player one's winners circle
}
}
}
// end position 13 actions
// start position 14 actions
if(position==14){
int distance = pos14.size();
if(distance==1){
String temp = pos14.peek();
if(temp=="x"){
pos14.pop();
pos15.push(temp);
}
}
if(distance==2){
String temp = pos14.peek();
if(temp=="x"){
pos14.pop();
pos16.push(temp);
}
}
if(distance==3){
String temp = pos14.peek();
if(temp=="x"){
pos14.pop();
pos17.push(temp);
}
}
if(distance>3){
String temp = pos14.peek();
pos14.pop();
if(temp=="x"){
pos26.push(temp); // push to player one's winners circle
}
}
}
// end position 14 actions
// start position 15 actions
if(position==15){
int distance = pos15.size();
if(distance==1){
String temp = pos15.peek();
if(temp=="x"){
pos15.pop();
pos16.push(temp);
}
}
if(distance==2){
String temp = pos15.peek();
if(temp=="x"){
pos15.pop();
pos17.push(temp);
}
}
if(distance>2){
String temp = pos15.peek();
pos15.pop();
if(temp=="x"){
pos26.push(temp); // push to player one's winners circle
}
}
}
// end position 15 actions
// start position 16 actions
if(position==15){
int distance = pos16.size();
if(distance==1){
String temp = pos16.peek();
if(temp=="x"){
pos16.pop();
pos17.push(temp);
}
}
if(distance>1){
String temp = pos16.peek();
pos16.pop();
if(temp=="x"){
pos26.push(temp); // push to player one's winners circle
}
}
}
// end position 16 actions
// start position 17 actions
if(position==17){
int distance = pos17.size();
if(distance>=1){
String temp = pos17.peek();
pos17.pop();
if(temp=="x"){
pos26.push(temp); // push to player one's winners circle
}
}
}
// end position 17 actions
player = false;
frame.player.setText("Current Player's Turn: Player Two");
}
}
}
基本上,有 25 个堆栈,每个堆栈存储各种 x 和 o 字符串。updater 方法根据参数将 x 和 o 从一个堆栈移动到另一个堆栈。是否真的有必要像我在这里那样编写尽可能多的 if 语句来涵盖每种情况?有没有我可以使用的循环?
x 和 o 只能向前移动,不能向后移动。