跳过转(4/5代码完成,只需要一点策略帮助)

时间:2013-11-16 00:16:20

标签: java loops for-loop int java.util.scanner

我有一个小问题。总体问题的方向如下:

  • 游戏板有31个单元格。第一个标记为START,最后一个单元格标记为END,其余单元格标记为1到29。

  • 有三个玩家,A,B和C.游戏从START单元格中的所有玩家开始。玩家A总是先行,然后是B,然后是C,然后是A,依此类推。当玩家到达END单元格或更远时,游戏结束。

  • 每一步都包括滚动8面模具并向前移动那么多细胞。但是,如果模具显示4,则向后移动许多单元格(但不要在START之前移动!);如果模具显示6,则玩家根本不移动并且丧失下一个回合。如果玩家落在另一个玩家占用的小区上,则该小区上的玩家会立即跳到START。

输入:5面比赛的8面模具卷。每个游戏由表示滚动的正整数组成,以0结尾。第一个数字是玩家A,然后是B,然后是C,然后是A,依此类推。每个游戏如上所述或在读取0时结束。

输出:对于每个游戏,打印玩家A,B和C的最终位置。所有三个玩家的位置必须正确才能获得每个输出的积分;没有部分功劳。

因此,在我开始解释问题之前,我已经完成了80%的代码。我决不会找人做我的工作,我只是在找一个可能向我解释一个策略的人,可以用来解决问题的后续部分。

我已完全为实验室的运动部分完成了代码,例如从1到8的所有骰子案例,不包括6.现在,我的问题是我不太清楚如何让程序跳过玩家转如果掷骰子是6,那么接下来转弯。换句话说,我如何实现“跳过转弯并放弃下一回合”的要求?到目前为止我的代码(仅适用于没有数字6的输入)如下:

import java.util.Scanner;

public class seniorLab{
    static String input;
    static double parsedInput;
    static int i = 0;
    static int count = 1;
    static int positionA;
    static int positionB;
    static int positionC;
    static int valueOfRoll;
    static boolean aAtEnd;
    static boolean bAtEnd;
    static boolean cAtEnd;

    public seniorLab(){
    }

    static void labLoop(){
        for (int i = 0; i < input.length(); i++){
            if (input.charAt(i) != '0'){
                if (i % 3 == 0){
                    valueOfRoll = charToNum(input.charAt(i));
                    positionA += valueOfRoll;
                    if (positionA == positionB){
                        positionB = 0;
                    } 
                    if (positionA == positionC){
                        positionC = 0;
                    }
                     if (positionA >= 31){
                        aAtEnd = true;
                        break;
                    }
                } else if (i % 3 == 1){
                    valueOfRoll = charToNum(input.charAt(i));
                    positionB += valueOfRoll;
                    if (positionB == positionA){
                        positionA = 0;
                    } 
                    if (positionB == positionC){
                        positionC = 0;
                    }
                    if (positionB >= 31){
                        bAtEnd = true;;
                        break;
                    }
                 } else if (i % 3 == 2){
                   valueOfRoll = charToNum(input.charAt(i));
                    positionC += valueOfRoll;
                    if (positionC == positionA){
                         positionA = 0;
                    }
                    if (positionC == positionB){
                        positionB = 0;
                    }
                    if (positionC >= 31){
                        cAtEnd = true;
                        break;
                    }
                }
            } else if (input.charAt(i) == '0'){
                break;
            }
        }
        System.out.print("Output #" + count + ":  ");
        System.out.print("A-" + positionA + ", B-" + positionB + ", C-" + positionC);
    }

    static int charToNum(char ch){
        switch (ch) {
            case '1': return 1;
            case '2': return 2;
            case '3': return 3;
            case '4': return -4;
            case '5': return 5;
            case '7': return 7;
            case '8': return 8;
        }
        return 0;
    }

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Line #" + count + ":  ");
        input = in.nextLine();
        input = input.replaceAll("[,]", "");
        seniorLab sL = new seniorLab();
        sL.labLoop();
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

根据要求,我在半代码的评论中概述了我提到的内容。作为答案发布,因为 解决了这个问题。

我之前在评论中说过,你应该尝试一些与转弯不同的东西。你可以做的一件事就是让球员排队。然后,你的游戏可以像这样

set all positions to 0
set all skip booleans to false
put the three players in the queue in order
for each character in the game string
    while the skip variable for the guy in front is true
        set it to false
        move the guy to the back of the line, leaving a new person in front

    // note that we only reach here if the person in front isn't getting
    // skipped this time. thus, the character corresponds to *his* move

    set the roll to the character that we're at right now
    if the roll is 6
        set skip value for that player to be true
    else
        calculate and modify that player's position as normal
    move player to back of queue

以下是这种方法的一些观点:

  1. 现在,我们只是改变位置,如果这不是先前被没收的“下一回合”或者你在本回合中滚动6而失去的转弯。
  2. while the skip variable for the guy in front is true部分可能会变得混乱,具体取决于其他事情的实施方式。由于这是你的计划,我会让你权衡可能的方法的利弊。
  3. 可以 使用int作为变量来表示轮到谁(使用例如:0表示A,1表示B,2表示C),而不是队列并且只记得在0-1-2之间循环,否则你将在队列中轮换球员。

  4. 免责声明:我不是说这是解决问题的最佳方法。但是,它至少可以为您提供想法,让您自己开发出可行的解决方案。