0

我正在尝试优化此代码中存在的大量 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 只能向前移动,不能向后移动。

4

6 回答 6

4

为什么不创建一个堆栈数组?然后你可以减少 if 语句:

Stack[] stacks = new Stack[27];
Arrays.fill(stacks, new Stack<String>);
if (temp.equals("x")) {
   Stack stack = stacks[distance];
   //all other stuff
}
于 2013-04-04T18:42:15.493 回答
1

声明:if(distance==1){ 可以用这样的东西代替:

for(int i= 0 ;i< 13;i++) {
        if(distance==i) {
            // some code
            break; // break the loop once match is found
        }
    }
于 2013-04-04T18:42:20.477 回答
0

您需要做的事情很少:

如果你发现自己做了很多 if if(distance==1){... pos1.pop();then if(distance==2){... pos2.pop();etc.,使用ArrayList<Stack<String>>你已经声明的,然后得到你需要的。

if (/* distance is within bounds */)
{
    Stack<String> pos = positions.get(distance);
    ...

您可以对temp返回的值执行类似的操作,以确定您需要推送到哪个值。这将消除所有那些重复的代码if块。

接下来,在每种情况下,您都将字符串值与==运算符进行比较。运算符比较引用以查看它们是否相同,这==可能不是您想要的。用于String#equals比较String值。

于 2013-04-04T18:48:12.717 回答
0

如果你想优化,你可以从使用else if90% 的重复 if 语句开始。这样一来,您只打到您需要的那个,而不是全部。这也将减少错误。

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.equals("x")){ // only make move if player one's chip is on top of the stack
                        pos1.pop();
                        pos2.push(temp);
                    }
                }
               else if(distance==2){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos3.push(temp);
                    }
                }
               else if(distance==3){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos7.push(temp);
                    }
                }
                else if(distance==4){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos8.push(temp);
                    }
                }
                else if(distance==5){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos9.push(temp);
                    }
                }
                else if(distance==6){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos10.push(temp);
                    }
                }
                else if(distance==7){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos11.push(temp);
                    }
                }
                else if(distance==8){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos12.push(temp);
                    }
                }
                else if(distance==9){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos13.push(temp);
                    }
                }
                else if(distance==10){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos14.push(temp);
                    }
                }
                else if(distance==11){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos15.push(temp);
                    }
                }
                else if(distance==12){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos16.push(temp);
                    }
                }
                else if(distance==13){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos16.push(temp);
                    }
                }
                else if(distance>13){
                    String temp = pos1.peek();
                    pos1.pop();
                    if(temp.equals("x")){
                        pos26.push(temp); // push to player one's winners circle
                    }
                }

            }
            // end position 1 actions
于 2013-04-04T18:41:04.973 回答
0

您可以使用嵌套for循环,然后if根据以下值进行测试i

for(int i = 1;i<=15;i++)//position
{
for(int c = 1;c<=4;c++)//distance
{
if(position==i)//etc.
}
}
于 2013-04-04T18:43:10.903 回答
0

这里有一些提示。

动态创建堆栈:

int total = 27
List<Stack<String>> positions = new ArrayList<Stack<String>>();
for (i = 0; i < toal; i++){
   positions.add(new Stack<String>());
}

您现在可以使用positions.get(position);

您可以大大简化代码:

Stack<String> positionStack = positions.get(position);
int distance = positionStack.size();
if (positionStack.peek().equals("x")){
   positions(nextMove(position).push(positionStack.pop());
}

nextMove(int position)是一个从给定位置计算下一个位置的函数。我不知道那个功能是什么,但你可以想出它。

于 2013-04-04T19:08:49.550 回答