改变骰子的眼睛

时间:2018-09-19 13:41:07

标签: java dice

学校给我分配了一个制作骰子游戏的机会,用户可以在其中改变骰子的眼睛,他们给我的唯一提示就是使用ASCII表。

到目前为止,这是我的代码,我正在碰壁,因为我如何使数字成为用户的输入(我不是很有创造力):

 System.out.println("Which character should be used as the eye of the dice:");
    char eyeDice = input.next().charAt(0);
    System.out.println(eyeDice);


    int Dice;
    Dice = (int)(Math.random()* 6 + 1);

    while (Dice < 6) {
        Dice = (int)(Math.random()* 6 + 1);
        System.out.println(Dice);

    }

代码输出如下:

Which character should be used as the eye of the dice:
$
$
1
4
1
1
1
1
4
1
2
2
6

Process finished with exit code 0

这应该是最终的样子:

Which character should be used as the eye of the dice:

#
  #
    #

#   #
  #
#   #

#   #

#   #
#   #
#   #


Process finished with exit code 0

任何朝着正确方向的提示或提示将不胜感激!

6 个答案:

答案 0 :(得分:2)

计算机没有附带将数字“ 4”转换为ascii图形的代码。

您必须自己编写。我建议将它们画在一张纸上。在您的Java代码中,您可以有一堆if / elseif语句,每6个面中有一个。每个块将打印3行。首先锁定要用于眼睛的角色,然后再进行制作,以便用户以后可以配置。

这是您入门的一部分:

if (dieRoll == 5) {
    System.out.println("* *");
    System.out.println(" * ");
    System.out.println("* *");
} else if (dieRoll == 6) {
    // you figure it out from here...

答案 1 :(得分:0)

在给定的示例中,eyeDice'#'。并且抛出的dice(在Java中请加上一个小的d)将是3、5、4和6。

因此,您需要使用以下方法:

void printDice(int dice, char eyDice) {
    ... System.out.println ...
}

和您的代码

int dice = (int)(Math.random()* 6 + 1);

while (dice < 6) {
    printDice(dice, eyeDice);
    dice = (int)(Math.random()* 6 + 1)
}

将打印出5(忽略眼睛):

System.out.println("?   ?");
System.out.println("  ?  ");
System.out.println("?   ?");

答案 2 :(得分:0)

这不是一个完整的示例,但可以让您知道该怎么做

public static void main(String[] args){
    System.out.println("Which character should be used as the eye of the dice:");
    char eyeDice = input.next().charAt(0);
    System.out.println(eyeDice);


    int Dice;
    Dice = (int)(Math.random()* 6 + 1);

    while (Dice < 6) {
        Dice = (int)(Math.random()* 6 + 1);
        System.out.println(Dice);
        //If statement calling print
    }
}

private void printOne(char character){
    String dice = "\n  #  \n";
    System.out.println(dice.replace('#', character));
}

private void printTwo(char character){
    String dice = "#   #\n\n#   #";
    System.out.println(dice.replace('#', character));
}

private void printThree(char character){
    String dice = "#\n  #\n   #";
    System.out.println(dice.replace('#', character));
}

答案 3 :(得分:0)

我知道您已经有了答案,但是另一种可能的解决方案是使用ENUM。例如:

public class Dice 
{
    private enum DieFace
    {
        ONE('1', "   \n * \n   "),
        TWO('2', "*  \n   \n  *"),
        THREE('3', "*  \n * \n  *"),
        FOUR('4', "* *\n   \n* *"),
        FIVE('5', "* *\n * \n* *"),
        SIX('6', "* *\n* *\n* *");

        private char characterCode;
        private String representation;

        DieFace(char characterCode, String representation)
        {
            this.characterCode = characterCode;
            this.representation = representation;
        }

        public static DieFace getDieFaceFromCharacterCode(char characterCode)
        {
            DieFace dieFaceFound = null;

            for (DieFace dieFace : values())
            {
                if (dieFace.characterCode == characterCode)
                {
                    dieFaceFound = dieFace;
                    break;
                }
            }

            return dieFaceFound;
        }

        @Override
        public String toString()
        {
            return this.representation;
        }
    }

    public static String getDieFaceFromCharacter(char characterInput)
    {
        DieFace dieFace = DieFace.getDieFaceFromCharacterCode(characterInput);

        return dieFace == null ? null : dieFace.toString();
    }
}

这是对课程的测试:

public class DieTest 
{
    @Test
    public void testGetOne()
    {
        String expectedResult = "   \n * \n   ";
        assertEquals(expectedResult, Dice.getDieFaceFromCharacter('1'));
    }

    @Test
    public void testGetTwo()
    {
        String expectedResult = "*  \n   \n  *";
        assertEquals(expectedResult, Dice.getDieFaceFromCharacter('2'));
    }

    @Test
    public void testGetThree()
    {
        String expectedResult = "*  \n * \n  *";
        assertEquals(expectedResult, Dice.getDieFaceFromCharacter('3'));
    }

    @Test
    public void testGetFour()
    {
        String expectedResult = "* *\n   \n* *";
        assertEquals(expectedResult, Dice.getDieFaceFromCharacter('4'));
    }

    @Test
    public void testGetFive()
    {
        String expectedResult = "* *\n * \n* *";
        assertEquals(expectedResult, Dice.getDieFaceFromCharacter('5'));
    }

    @Test
    public void testGetSix()
    {
        String expectedResult = "* *\n* *\n* *";
        assertEquals(expectedResult, Dice.getDieFaceFromCharacter('6'));
    }

    @Test
    public void testGetInvalid()
    {
        // < 1 is invalid
        assertNull(Dice.getDieFaceFromCharacter('0'));

        // invalid character (non-number)
        assertNull(Dice.getDieFaceFromCharacter('a'));

        // > 6 is invalid
        assertNull(Dice.getDieFaceFromCharacter('7'));
    }
}

答案 4 :(得分:0)

简单的骰子程序

csv_file2 = csv.reader(open(file[0], "r"), delimiter=",")

答案 5 :(得分:0)

如果您希望最大程度地减少重用,则可以使用switch语句。例如:

public class Dice 
{
    private static final String LINE_BREAK = "\n";
    public static String getDieFaceFromCharacter(char characterInput)
    {
        String top = "   ";
        String middle = "   ";
        String bottom = "   ";

        switch(characterInput)
        {
            case '5':
                top = "* *";
                bottom = "* *";
            case '1':
                middle = " * ";
                break;
            case '3':
                top = "*  ";
                middle = " * ";
                bottom = "  *";
                break;
            case '2':
                top = "*  ";
                bottom = "  *";
                break;
            case '6':
                middle = "* *";
            case '4':
                top = "* *";
                bottom = "* *";
        }

        return top + LINE_BREAK + middle + LINE_BREAK + bottom;
    }
}