【问题标题】:Skipping Turns (4/5 code complete, just need a bit of strategy help)Skipping Turns(4/5 代码完成,只需要一点策略帮助)
【发布时间】:2013-11-16 00:16:19
【问题描述】:

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

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

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

  • 每次移动都包括滚动一个 8 面骰子并向前移动那么多单元格。但是,如果骰子显示为 4,则向后移动那么多单元格(但不要在 START 之前走!);如果骰子显示为 6,则玩家根本不移动并放弃下一回合。如果一个玩家降落在另一个玩家占据的单元格上,那么在单元格上的玩家会立即跳到开始。

输入:五局游戏的 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();
    }
}

感谢您的帮助!

【问题讨论】:

  • 所以基本上,听起来你已经为每个玩家设置了一个位置值,但除了那个循环之外没有其他回合的概念。听起来对吗?
  • 是的,我能够正确跟踪位置(例如,所有不包含任何 6 的测试用例都可以正常工作)但现在我完全依赖于字符串来计算轮到谁了。例如,第一个角色的位置,当模 3 时为 0,对于第二个角色,模数为 3,对于第三个角色,模数为 3 为 2。因此,玩家 A 将模数i为0时移动,为1时玩家B,为2时玩家C。我不知道如何分析谁跳过了哪个回合。
  • 是的,所以看起来你必须在确定转弯时做一些不同的事情。我让你试试看。
  • 起初我想使用 3 个布尔值,skipA、skipB 和 skipC,来确定轮到谁了,但最终失败了。然后我尝试使用一个 for 循环,它只连续运行三个方法(moveA、moveB 和 moveC),所有这些方法都在其参数中接受布尔值,并且仅在对应于它们的 skipX 布尔值为 false 时运行。这也失败了,因为我需要增加自己在字符串上的位置。在那之后有点没有想法了。
  • 第一种方法出了什么问题?

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


【解决方案1】:

应要求,我将在半代码中概述我在 cmets 中提到的内容。发布作为答案,因为这确实解决了这个问题。

我之前在 cmets 中说过,您应该在转弯方面尝试不同的方法。您可以做的一件事是让玩家排队。然后,你的游戏就可以变成这样了

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 之间循环,否则会轮换队列中的玩家。

免责声明:我并不是说这是解决问题的最佳方法。但是,它至少可以给你一些想法,让你自己开发一个可行的解决方案。

【讨论】:

  • 嗨。我将这一切都放入常规代码中,但它不起作用,我知道原因:我需要放弃当前和下一回合。在这种情况下,我应该为跳过的数量设置一个整数吗?所以 switch 会返回 skipA = 2,当 skipA 变为 0 时,A 会再次开始运行
  • 听起来像是一个计划。我的回答旨在引导您朝着正确的方向前进,而不是包罗万象。听起来你在想一个有趣的想法,为什么不试试呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多