Nybörjare, c# problem.
Hej, har tittat lite på c# programmering idag. Har läst någon nybörjar kurs i java på gymnasiet men kommer knappt ihåg något.
Skrev ett litet test program/spel mest för att testa olika typer av argument och loopar etc.
Kort sagt, syftet med spelet är att slåss och vinna mot ett monster. du kan för varje omång välja en av två attacker eller använda en dryck för att återställa din hälsa.
det är riktigt mycket spaggetti kod och det finns fler problem som jag inte brytt mig om att fixa.
Det jag inte förstår och behöver hjälp med är att min do while loop ska sluta när antingen spelaren eller monstret har 0 i hälsa. men det gör den inte.
Rad 119:
1.
using System;
2.
using System.Collections.Generic;
3.
using System.Linq;
4.
using System.Text;
5.
6.
namespace ConsoleApplication1
7.
{
8.
class Program
9.
{
10.
static void Main(string[] args)
11.
{
12.
13.
//Mob data
14.
String MobName = "Evila";
15.
uint MobHealth = 100;
16.
uint MobAttkPower = 20;
17.
18.
//player data
19.
20.
String PlayerName = "Hero";
21.
uint PlayerHealth = 100;
22.
uint PlayerMana = 100;
23.
uint HealthPotion = 1;
24.
uint HealthPotionPwr = 50;
25.
uint PlayerAttkPower = 10;
26.
uint FireBallDmg = 25;
27.
uint FireBallCost = 40;
28.
29.
30.
int val = 0;
31.
Random start = new Random();
32.
33.
34.
CombatStart:
35.
36.
int startbattle = start.Next(1);
37.
38.
do
39.
{
40.
switch (startbattle)
41.
{
42.
case 1:
43.
Console.WriteLine(MobName + " Attacks you!\nYou suffer: " + MobAttkPower);
44.
PlayerHealth = (PlayerHealth - MobAttkPower);
45.
46.
if (startbattle == 1)
47.
goto case 0;
48.
49.
break;
50.
51.
case 0:
52.
//mana regen
53.
54.
if (PlayerMana < 90)
55.
PlayerMana = PlayerMana + 10;
56.
else
57.
PlayerMana = 100;
58.
59.
//write stats, choice.
60.
61.
startchoice:
62.
63.
Console.WriteLine("Your Turn, " + PlayerName);
64.
Console.WriteLine("\nHealth: " + PlayerHealth + "\nMana: " + PlayerMana + "\nHealthPotions: " + HealthPotion);
65.
Console.WriteLine("Choose an action:\n Press:\n 1 to Attack\n 2 to cast a Fireball\n 3 to use a health potion");
66.
67.
val = Convert.ToInt32(Console.ReadLine());
68.
69.
//choice
70.
switch (val)
71.
{
72.
case 1:
73.
MobHealth = MobHealth - PlayerAttkPower;
74.
Console.WriteLine(PlayerName + " attacks " + MobName + ".\n " + MobName + " suffers " + MobAttkPower +
75.
" damage\n\n Mob total health:" + MobHealth);
76.
77.
break;
78.
case 2:
79.
MobHealth = MobHealth - FireBallDmg;
80.
PlayerMana = PlayerMana - FireBallCost;
81.
Console.WriteLine(PlayerName + " Casts a Fireball at " + MobName + ".\n " + MobName +
82.
" suffers " + FireBallDmg + " damage\n\n Mob total health:" + MobHealth);
83.
84.
break;
85.
86.
case 3:
87.
if (HealthPotion == 0)
88.
goto startchoice;
89.
90.
else if (PlayerHealth < 50)
91.
PlayerHealth = PlayerHealth + HealthPotionPwr;
92.
else
93.
PlayerHealth = 100;
94.
95.
Console.WriteLine("You use a health potion\nHealth: " + PlayerHealth);
96.
97.
break;
98.
default:
99.
Console.WriteLine("That action isnt avialable");
100.
goto startchoice;
101.
102.
}
103.
104.
if (startbattle == 0)
105.
goto case 1;
106.
107.
108.
break;
109.
default:
110.
Console.WriteLine("Something went wrong");
111.
PlayerHealth = 0;
112.
break;
113.
114.
115.
}
116.
117.
118.
119.
} while (!(PlayerHealth == 0) || !(MobHealth == 0));
120.
121.
if (PlayerHealth > 0)
122.
{
123.
Console.WriteLine("\n-----------------------------------------");
124.
Console.WriteLine("Congratulations, YOU DFEATED " + MobName + "\n\nStats:\n Player health: " + PlayerHealth + "\nMob health"
125.
+ MobHealth + "\nPlayer mana " + PlayerMana + "\nPotions " + HealthPotion);
126.
127.
HealthPotion++;
128.
129.
Console.WriteLine("Mob dropped a healthpotion\n\nPLay again?");
130.
131.
String continue = Console.ReadLine();
132.
if (continue.Equals("y"))
133.
goto CombatStart;
134.
135.
136.
}
137.
else
138.
{
139.
Console.WriteLine("YOU WERE DEFEATED by " + MobName + "\n\nStats:\n Player health: " + PlayerHealth + "\nMob health"
140.
+ MobHealth + "\nPlayer mana " + PlayerMana + "\nPotions " + HealthPotion);
141.
142.
}
143.
144.
145.
146.
Console.ReadLine();
147.
}
148.
}
149.
}
| Ryzen 5800x | Asus prime x470 pro | Asus rtx 3080 tuf oc | Gskill 32gb 3,6ghz | aw3225qf |