如何为单人游戏制作 PIG 游戏?
How can I make PIG game for single player?
我已经创建了一个游戏,我想在下面的代码中进行更改。
每次滚动显示TURN number,
但我想在每回合显示一次。
我该如何解决?
using System;
namespace PigGamePractice
{
internal class Program
{
public static int turn=1;
public static int turnScore;
public static int totalScore = 0;
static void Main(string[] args)
{
Dice();
}
static void Dice()
{
while (turn<=4)
{
Console.WriteLine("**This is Turn " + turn);
Console.WriteLine("Role or Hold ? (r/h) : ");
char userChoice = Console.ReadLine()[0];
if (userChoice == 'r')
{
Random random = new Random();
int diceRoll = random.Next(1, 7);
turnScore += diceRoll;
totalScore += diceRoll;
Console.WriteLine("Dice: "+diceRoll);
if (totalScore >= 20)
{
Console.WriteLine("**You won..!**");
Console.WriteLine("Your Total Score is: " + totalScore);
break;
}
if (diceRoll == 1)
{
totalScore -= turnScore;
Console.WriteLine("Turn over..!");
Console.WriteLine("Turn Score: 0");
Console.WriteLine("Total score: " + totalScore);
turnScore = 0;
turn++;
}
Console.WriteLine();
}
if (userChoice == 'h')
{
Console.WriteLine("Turn Score: "+turnScore);
Console.WriteLine("Total Score: " + totalScore);
Console.WriteLine();
turnScore = 0;
turn++;
}
}
}
}
}
每次滚动都会显示转数,
但我想在每个回合 显示一次 。
我该如何解决?
现在我得到这样的输出
首先你可以试试Wyck的方法,添加一个变量,然后将它的值与turn
进行比较。
其次你可以像我一样加个bool来判断
using System;
namespace ConsoleApp8
{
internal class Program
{
public static int turn = 1;
public static int turnScore;
public static int totalScore = 0;
//ADD
public static bool flag = true;
static void Main(string[] args)
{
Dice();
}
static void Dice()
{
while (turn <= 4)
{
//ADD
if (flag)
Console.WriteLine("**This is Turn " + turn);
Console.WriteLine("Role or Hold ? (r/h) : ");
char userChoice = Console.ReadLine()[0];
if (userChoice == 'r')
{
Random random = new Random();
int diceRoll = random.Next(1, 7);
turnScore += diceRoll;
totalScore += diceRoll;
//ADD
flag = false;
Console.WriteLine("Dice: " + diceRoll);
if (totalScore >= 20)
{
Console.WriteLine("**You won..!**");
Console.WriteLine("Your Total Score is: " + totalScore);
break;
}
if (diceRoll == 1)
{
totalScore -= turnScore;
Console.WriteLine("Turn over..!");
Console.WriteLine("Turn Score: 0");
Console.WriteLine("Total score: " + totalScore);
turnScore = 0;
turn++;
//ADD
flag = true;
}
Console.WriteLine();
}
if (userChoice == 'h')
{
Console.WriteLine("Turn Score: " + turnScore);
Console.WriteLine("Total Score: " + totalScore);
Console.WriteLine();
turnScore = 0;
turn++;
//ADD
flag = true;
}
}
}
}
}
输出:
我已经创建了一个游戏,我想在下面的代码中进行更改。
每次滚动显示TURN number,
但我想在每回合显示一次。
我该如何解决?
using System;
namespace PigGamePractice
{
internal class Program
{
public static int turn=1;
public static int turnScore;
public static int totalScore = 0;
static void Main(string[] args)
{
Dice();
}
static void Dice()
{
while (turn<=4)
{
Console.WriteLine("**This is Turn " + turn);
Console.WriteLine("Role or Hold ? (r/h) : ");
char userChoice = Console.ReadLine()[0];
if (userChoice == 'r')
{
Random random = new Random();
int diceRoll = random.Next(1, 7);
turnScore += diceRoll;
totalScore += diceRoll;
Console.WriteLine("Dice: "+diceRoll);
if (totalScore >= 20)
{
Console.WriteLine("**You won..!**");
Console.WriteLine("Your Total Score is: " + totalScore);
break;
}
if (diceRoll == 1)
{
totalScore -= turnScore;
Console.WriteLine("Turn over..!");
Console.WriteLine("Turn Score: 0");
Console.WriteLine("Total score: " + totalScore);
turnScore = 0;
turn++;
}
Console.WriteLine();
}
if (userChoice == 'h')
{
Console.WriteLine("Turn Score: "+turnScore);
Console.WriteLine("Total Score: " + totalScore);
Console.WriteLine();
turnScore = 0;
turn++;
}
}
}
}
}
每次滚动都会显示转数, 但我想在每个回合 显示一次 。 我该如何解决?
现在我得到这样的输出
首先你可以试试Wyck的方法,添加一个变量,然后将它的值与turn
进行比较。
其次你可以像我一样加个bool来判断
using System;
namespace ConsoleApp8
{
internal class Program
{
public static int turn = 1;
public static int turnScore;
public static int totalScore = 0;
//ADD
public static bool flag = true;
static void Main(string[] args)
{
Dice();
}
static void Dice()
{
while (turn <= 4)
{
//ADD
if (flag)
Console.WriteLine("**This is Turn " + turn);
Console.WriteLine("Role or Hold ? (r/h) : ");
char userChoice = Console.ReadLine()[0];
if (userChoice == 'r')
{
Random random = new Random();
int diceRoll = random.Next(1, 7);
turnScore += diceRoll;
totalScore += diceRoll;
//ADD
flag = false;
Console.WriteLine("Dice: " + diceRoll);
if (totalScore >= 20)
{
Console.WriteLine("**You won..!**");
Console.WriteLine("Your Total Score is: " + totalScore);
break;
}
if (diceRoll == 1)
{
totalScore -= turnScore;
Console.WriteLine("Turn over..!");
Console.WriteLine("Turn Score: 0");
Console.WriteLine("Total score: " + totalScore);
turnScore = 0;
turn++;
//ADD
flag = true;
}
Console.WriteLine();
}
if (userChoice == 'h')
{
Console.WriteLine("Turn Score: " + turnScore);
Console.WriteLine("Total Score: " + totalScore);
Console.WriteLine();
turnScore = 0;
turn++;
//ADD
flag = true;
}
}
}
}
}
输出: