检测赢家并设置游戏循环

时间:2015-12-01 21:04:55

标签: c#

我正在制作一款游戏,除了游戏没有检测到游戏结束外,一切都完成了。

我尝试为endgame设置方法,程序不会给我错误。它汇编。当玩家的得分达到6时,它只是没有检测到它。在这种情况下,我希望程序将endGame设置为true并突破循环并显示获胜者。相反,它每回合都会继续打印“当前玩家获胜”。我不知道逻辑问题是什么。 BTW运动是让我的球员在板上的动力。

 static void Movement(int i, int rolled, char direction) // movement
 {
    Console.WriteLine("");
    Console.WriteLine("Making a move for Player " + players[i].Name);

    if (direction == 'u' || direction == 'U')
    {
        if (players[i].X - rolled < 0)
            players[i].X = players[i].X + 8 - rolled;
        else
            players[i].X = players[i].X - rolled;
    }
    else if (direction == 'd' || direction == 'D')
    {
        if (players[i].X + rolled > 7)
            players[i].X = players[i].X - 8 + rolled;
        else
            players[i].X = players[i].X + rolled;
    }
    else if (direction == 'l' || direction == 'L')
    {
        if (players[i].Y - rolled < 0)
            players[i].Y = players[i].Y + 8 - rolled;
        else
            players[i].Y = players[i].Y - rolled;
    }
    else if (direction == 'r' || direction == 'R')
    {
        if (players[i].Y + rolled > 7)
            players[i].Y = players[i].Y - 8 + rolled;
        else
            players[i].Y = players[i].Y + rolled;
    }

}
static void Main()
    {
 var gameOver = false; //endgame loop does not work

        while (true)
        {
            for (int round = 0; round < 20 && !gameOver; round++)
            {

                for (int playerturn = 1; playerturn < totalplayers && !gameOver; playerturn++) //Sets up turns for each player

                    for (int i = 0; i < totalplayers && !gameOver; i++)
                    {
                        Console.WriteLine(players[i].Name + " 's turn. Roll it! ");
                        Console.WriteLine();
                        int rolled;

                        if (dicechoice == 1) //do the the random method otherwise choose number
                        {

                            rolled = DiceRead();
                        }


                        else
                        {

                            Console.ReadLine();
                            rolled = DiceThrow();
                        }

                        Console.WriteLine("");
                        Console.WriteLine(players[i].Name + " rolled " + rolled);
                        Console.WriteLine("");

                        Console.WriteLine(" Please pick a direction: (U,D,L,R)");
                        Console.WriteLine("");
                        char direction;

                        do
                        {
                            direction = Console.ReadKey().KeyChar; //store direction as char
                            if (direction != 'U' && direction != 'D' && direction != 'L' && direction != 'R' && direction != 'u' && direction != 'd' && direction != 'l' && direction != 'r')
                            {
                                Console.WriteLine("Invalid input.. (U,D,L,R)");
                                Console.WriteLine();
                            }

                        } while (direction != 'U' && direction != 'D' && direction != 'L' && direction != 'R' && direction != 'u' && direction != 'd' && direction != 'l' && direction != 'r');
                        Console.WriteLine("");




                        Movement(i, rolled, direction);

                        Console.WriteLine("");


                        if (players[i].Stash == 6)
                        {

                            Console.WriteLine(players[i].Name + " wins.");
                            gameOver = true;
                            break;
                        }
      }

            }

            Console.WriteLine("Would you like to play again?");
            gameOver = false;
            if (Console.ReadKey().Key != ConsoleKey.Y)
                break;
        }


    }

2 个答案:

答案 0 :(得分:2)

看起来问题就在这里:

 if (players[i].Stash == 6)
     gameOver=true;
 do
 {
     Console.WriteLine(players[i].Name + " wins.");
 }
 while (gameOver == true);

我不确定将gameOver设置为true后循环的目的是什么。只需在Stash == 6时打印获胜者的姓名。

要让游戏要求重新启动,请将gameOver移动到main方法的范围内,使用while循环将其包围以允许重新启动游戏,并将!gameover添加到for循环让它在游戏中退出。

 var gameover = false;
 while (true)
 {
    for (int round = 0; round < 20 && !gameover; round++)
    {

然后玩家达到6分(注意你的代码不会在任何地方增加点数),将gameover设置为true并跳出循环。

if (players[i].Stash == 6)
{

    Console.WriteLine(players[i].Name + " wins.");
    gameover = true;
    break;
}

在代码的底部,完成while循环,如果用户没有按Y,则退出。

   Console.WriteLine("Would you like to play again?");
   gameover = false;
   if (Console.ReadKey().Key != ConsoleKey.Y)
   break;
}

请注意,您还需要重置分数。

答案 1 :(得分:0)

您似乎缺少部分代码,但您的循环是由此部分代码引起的

if (players[i].Stash == 6)
     gameOver=true;

 do
 {
     Console.WriteLine(players[i].Name + " wins.");
 }
 while (gameOver == true);

这是在说

  

检查gameOver是否等于true,若是,那么Console.WriteLine直到gameOver标志不为真

它会不断循环,因为你的do / while循环中没有任何东西可以改变gameOver标志的值。

听起来你真正想要的是这样的逻辑:

gameOver = false;

do
{
    // whatever steps to each round are goes here

    // check to see if someone has won, and if yes set flag to break loop
    if (players[i].Stash == 6)
        gameOver=true;         

}
while (gameOver == false);

// this will not execute until the do/while loop above completes
Console.WriteLine(players[i].Name + " wins.");

这会说

  

只要gameOver标志为false,请执行每一轮的任何步骤并检查是否有赢家。一旦有胜利者,将gameOver标志设置为true,这将打破循环并打印出胜利者