如何在Java中删除重复的列表遍历

如何在Java中删除重复的列表遍历,java,oop,arraylist,Java,Oop,Arraylist,我正在创建一个solitare纸牌游戏,它将纸牌存储在arraylist中。 我想创建两种方法来检查玩家的移动和游戏 一个布尔函数,通过ArrayList检查是否有任何可能的移动。如果有可能,此方法将返回true 第二个移动。它还会遍历ArrayList并检查是否有任何可能的移动,执行此移动并停止工作。为了阻止它,我使用了returntrue。但我认为这不是一个好办法 两个函数非常相似,代码是重复的。如何修复它? 这个看起来是移动的方法应该返回布尔值还是可以移动的两张牌(a和b) public

我正在创建一个solitare纸牌游戏,它将纸牌存储在arraylist中。 我想创建两种方法来检查玩家的移动和游戏

一个布尔函数,通过ArrayList检查是否有任何可能的移动。如果有可能,此方法将返回true

第二个移动。它还会遍历ArrayList并检查是否有任何可能的移动,执行此移动并停止工作。为了阻止它,我使用了returntrue。但我认为这不是一个好办法

两个函数非常相似,代码是重复的。如何修复它? 这个看起来是移动的方法应该返回布尔值还是可以移动的两张牌(a和b)

public boolean makeMove() {
    int a = 0; //index of one card
    int b; //index of second card
    for (b = getSize() - 1; b > 0; b--) {
        for (a = 0; a < b; a++) {
            if (isCorrectMove(a, b)) {
                swap(a, b); //makes move
                return true; //exit after move
            }
        }
    }
    return false; //if there was no move
}

public boolean isPossibleAnyMove() {int a = 0; //index of one card
        int b; //index of second card
        for (b = getSize() - 1; b > 0; b--) {
            for (a = 0; a < b; a++) {
                if (isCorrectMove(a, b)) {
                    return true; //returns true if there was a move
                }
            }
        }
        return false; //if there was no move
    }
public boolean makeMove(){
int a=0;//一张卡的索引
int b;//第二张卡的索引
对于(b=getSize()-1;b>0;b--){
对于(a=0;a0;b--){
对于(a=0;a
公共布尔someName(布尔应交换){
int a=0;//一张卡的索引
int b;//第二张卡的索引
对于(b=getSize()-1;b>0;b--){
对于(a=0;a
如果交换是唯一的区别,那么这是一种将这两个函数组合在一个函数中的可能方法…

公共布尔someName(boolean shouldSwap){
int a=0;//一张卡的索引
int b;//第二张卡的索引
对于(b=getSize()-1;b>0;b--){
对于(a=0;a

如果交换是唯一的区别,那么这是将这两个函数合并为一个函数的一种可能方式…

您可以创建一个函数,该函数使用布尔值来控制行为

public boolean CheckOrmakeMove(boolean swapBool) {
        int a = 0; //index of one card
        int b; //index of second card
        for (b = getSize() - 1; b > 0; b--) {
            for (a = 0; a < b; a++) {
                if (isCorrectMove(a, b)) {
                    if(swapBool)
                    swap(a, b); //makes move
                    return true; //exit after move
                }
            }
        }
        return false; //if there was no move
    }
public boolean CheckOrmakeMove(boolean swapBool){
int a=0;//一张卡的索引
int b;//第二张卡的索引
对于(b=getSize()-1;b>0;b--){
对于(a=0;a
您可以创建一个使用布尔值控制行为的函数

public boolean CheckOrmakeMove(boolean swapBool) {
        int a = 0; //index of one card
        int b; //index of second card
        for (b = getSize() - 1; b > 0; b--) {
            for (a = 0; a < b; a++) {
                if (isCorrectMove(a, b)) {
                    if(swapBool)
                    swap(a, b); //makes move
                    return true; //exit after move
                }
            }
        }
        return false; //if there was no move
    }
public boolean CheckOrmakeMove(boolean swapBool){
int a=0;//一张卡的索引
int b;//第二张卡的索引
对于(b=getSize()-1;b>0;b--){
对于(a=0;a
您可以创建一个函数来查找第一个正确的移动;此函数将返回一个对象,该对象包含正确移动的
a
b
,如果没有正确移动,则返回
null

private static class Move {

    int a, b;

    Move(int a, int b) {
        this.a = a;
        this.b = b;
    }

}

private Move findFirstCorrectMove() {
    int a = 0; //index of one card
    int b; //index of second card
    for (b = getSize() - 1; b > 0; b--) {
        for (a = 0; a < b; a++) {
            if (isCorrectMove(a, b)) {
                return new Move(a,b);
            }
        }
    }
    return null; //if there was no move
}

public boolean makeMove() {
    Move move = findFirstCorrectMove();
    if (move != null) {
        swap(move.a, move.b);
        return true;
    }
    return false;
}

public boolean isPossibleAnyMove() {
    return findFirstCorrectMove() != null;
}

您可以创建一个函数来查找第一个正确的移动;此函数将返回一个对象,该对象包含正确移动的
a
b
,如果没有正确移动,则返回
null

private static class Move {

    int a, b;

    Move(int a, int b) {
        this.a = a;
        this.b = b;
    }

}

private Move findFirstCorrectMove() {
    int a = 0; //index of one card
    int b; //index of second card
    for (b = getSize() - 1; b > 0; b--) {
        for (a = 0; a < b; a++) {
            if (isCorrectMove(a, b)) {
                return new Move(a,b);
            }
        }
    }
    return null; //if there was no move
}

public boolean makeMove() {
    Move move = findFirstCorrectMove();
    if (move != null) {
        swap(move.a, move.b);
        return true;
    }
    return false;
}

public boolean isPossibleAnyMove() {
    return findFirstCorrectMove() != null;
}

谢谢,但是用面向对象的编程方法正确吗?这个函数做的不是太多吗?是的,对我来说它听起来像是破坏了内聚。谢谢,但它对面向对象编程方法正确吗?这个函数不是做得太多了吗?是的,它听起来确实像是破坏了内聚力。哇,这几乎正是我建议的,尽管你比我强。我会删除我的答案。哈哈,看起来我们写的答案是一样的。你可以打败我。你也可以考虑java的8个可选的谷歌GuAVA选项,而不是使用NulL.OK。非常感谢。也可能是这里使用的策略设计模式?这很好,但这种策略模式是如何工作的?哇,这几乎正是我所建议的,尽管你比我快。我会删除我的答案。哈哈,看起来我们写的答案是一样的。你可以打败我。你也可以考虑java的8个可选的谷歌GuAVA选项,而不是使用NulL.OK。非常感谢。也可能是这里使用的策略设计模式?这很好,但是这种策略模式是如何工作的呢?