为什么我的函数在递归调用时会返回先前的值而不是新的值?

时间:2015-03-03 15:26:22

标签: java multithreading

我要编写一个程序,启动2个玩家,将每个玩家传递给同一个经销商实例。在为每个玩家交易7个牌之后,每个牌手都应将他的牌列表打印到一个名为玩家号码的文本文件中。

现在,在多米诺骨牌游戏中,两个玩家不能拥有相同的牌,即(玩家1:2,4和玩家2:4,2)。

我已经使用了一个布尔标志的2D数组来控制它,但每当我打印出列表时,我发现有2个或更多玩家拥有相同的图块。此外,请注意我必须使用线程来代表玩家。

这是我的玩家运行方法。每位玩家应该获得7个牌。

public synchronized void run() {

try {
    try (PrintWriter writer = new PrintWriter("Player " + player + ".txt")) {
        for (int i = 0; i < 7; i++) {
            Random r = new Random();
            int a = r.nextInt(6);
            int b = r.nextInt(6);
            System.out.println("Player " + player + " is requesting a tile");
            DominoTiles dealt = dealer.deal(a, b);
            tiles[i] = dealt;

            writer.println("Requesting Player " + player + ": ");
            writer.println("Side 1 = " + tiles[i].getSide1() + " Side 2 = " + tiles[i].getSide2());

        }
    } catch (InterruptedException ex) {
        Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null, ex);
    }

} catch (FileNotFoundException ex) {
    Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null, ex);
}

}

这是我的经销商的交易方式。如果它进入if块,我返回该函数,如果它进入else if块,则再次调用它。问题是,我返回使函数进入else if块的值!

 public synchronized DominoTiles deal(int a, int b) throws FileNotFoundException, InterruptedException {
DominoTiles dealt = new DominoTiles(a, b);
System.out.println("Dealer is dealing");

if ((count < 28 && check[a][b] == false)) {
    System.out.println("Checking " + a + " " + b + ": " + check[a][b]);

    System.out.println(a + " " + b + " has been dealt");

    check[a][b] = true;
    check[b][a] = true;
    System.out.println("Checking now : " + check[a][b]);
    count++;
    System.out.println("Count = " + count);

} else if ((count < 28 && (check[a][b] == true || check[b][a] == true)) || (check[b][a] == true)) {
    System.out.println("I tried to deal the same again!" + a + " " + b + " and it was " + check[a][b] + " " + check[b][a
            ]);
    int c = r.nextInt(7);
    int d = r.nextInt(7);
    deal(c, d);
} else {
    System.out.println("All tiles have been dealt");
    System.exit(0);
}

return dealt;

}

2 个答案:

答案 0 :(得分:1)

这是因为你没有将递交调用的结果分配给deal()到处理过的变量。

修复:

public synchronized DominoTiles deal(int a, int b) throws  FileNotFoundException, InterruptedException {
    DominoTiles dealt = new DominoTiles(a, b);
    System.out.println("Dealer is dealing");

    if ((count < 28 && check[a][b] == false)) {
        System.out.println("Checking " + a + " " + b + ": " + check[a][b]);

        System.out.println(a + " " + b + " has been dealt");

        check[a][b] = true;
        check[b][a] = true;
        System.out.println("Checking now : " + check[a][b]);
        count++;
        System.out.println("Count = " + count);


    } else if ((count < 28 && (check[a][b] == true || check[b][a] == true)) || (check[b][a] == true)) {
        System.out.println("I tried to deal the same again!" + a + " " + b + " and it was " + check[a][b] + " " + check[b][a
        ]);
        int c = r.nextInt(7);
        int d = r.nextInt(7);
        dealt = deal(c, d);
    } else {
        System.out.println("All tiles have been dealt");
        System.exit(0);
    }

    return dealt;

}

答案 1 :(得分:0)

您需要将可以在线程中使用的任何变量标记为启动器的易失性。