You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java如何定位列表特定索引?Mancala游戏winner函数修改求助

Fixing the winner() Function for Your Mancala Game

Hey there! Let's work through modifying your winner() function to correctly handle Mancala's end-game logic. It sounds like you're already on the right track with comparing store counts—let's break this down step by step.

Step 1: First, Check if the Game Has Ended

The game stops when either player's side (all their small pits, not the store) has zero stones left. You'll need to loop through those pits to verify this condition.

Assuming your sideOne object is a collection (like a List) where each index represents a pit, here's how you might check for empty sides:

// Check if Player One's small pits are all empty
boolean playerOnePitsEmpty = true;
for (int i = 0; i < 6; i++) { // Adjust indices to match YOUR small pit range!
    if (sideOne.get(i).number > 0) {
        playerOnePitsEmpty = false;
        break;
    }
}

// Check if Player Two's small pits are all empty
boolean playerTwoPitsEmpty = true;
for (int i = 7; i < 13; i++) { // Again, adjust indices to your layout!
    if (sideOne.get(i).number > 0) {
        playerTwoPitsEmpty = false;
        break;
    }
}

Step 2: Handle Remaining Stones (Optional but Traditional)

In standard Mancala, when one player's pits are empty, the other player moves all remaining stones from their small pits into their store. You'll want to add this if you're following the official rules:

if (playerOnePitsEmpty) {
    // Move all Player Two's remaining stones to their store
    for (int i = 7; i < 13; i++) {
        sideOne.get(14).number += sideOne.get(i).number; // Player Two's store is index 14
        sideOne.get(i).number = 0; // Empty the pit
    }
} else if (playerTwoPitsEmpty) {
    // Move all Player One's remaining stones to their store
    for (int i = 0; i < 6; i++) {
        sideOne.get(13).number += sideOne.get(i).number; // Player One's store is index 13
        sideOne.get(i).number = 0;
    }
}

Step 3: Compare Stores to Determine the Winner

Now that the game has ended (and we've cleaned up remaining stones if needed), compare the two stores to declare a winner:

if (playerOnePitsEmpty || playerTwoPitsEmpty) {
    int playerOneStoreCount = sideOne.get(13).number;
    int playerTwoStoreCount = sideOne.get(14).number;

    if (playerOneStoreCount > playerTwoStoreCount) {
        return "Player One wins with " + playerOneStoreCount + " stones!";
    } else if (playerTwoStoreCount > playerOneStoreCount) {
        return "Player Two wins with " + playerTwoStoreCount + " stones!";
    } else {
        return "It's a tie!";
    }
} else {
    // Game isn't over yet—return a value that signals this (like null or a constant)
    return null;
}

Key Notes for Your Object Structure

  • The sideOne.get(index) call works if your sideOne is a list-like collection where each index maps to a pit object. Just double-check that indices 13 and 14 really correspond to Player One and Player Two's stores (adjust if your layout uses different numbers!).
  • Make sure the number property on each pit object correctly tracks the number of stones in that pit.

If your board's index setup is different (e.g., separate objects for each player's side instead of one combined collection), just tweak the loop ranges and store indices to match your setup!

内容的提问来源于stack exchange,提问作者user8893965

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:11:15