Programowanie to nie magia. Nie mamy magicznych kul by widzieć co zrobiłeś źle, nie stoimy tez za twoimi plecami i nie widźmy twojego ekranu.
Uzupełniając braki z kodu który dałeś, u mnie działa.
Na razie za mało umiesz, żeby rozsądnie dać problematyczny fragment kodu.
Ergo najlepiej jak wkleisz wszystko, tak byćmy mogli to skopiować u siebie bez problemów.
Porzyczając kryształową kulę od sasiada przypuszczam, że name
jest tablicą char
i wpisałeś tam nazwę, które wykracza poza zakres tej tablicy.
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
struct Weapon
{
string name;
short damage[3] = {0, 0, 0};
short damageK;
short minAttack;
short minDefense;
short strength;
short safeZone;
};
struct Hero
{
string race;
string name;
short fightPoints;
short fightPointsTemp;
short health;
struct Weapon myWeapon;
}myHero[9];
short heroA, heroB, tempA1, tempB1, tempA2, tempB2, j = 0;
bool heroAattack, heroAdefense, heroAcounter;
bool heroBattack, heroBdefense, heroBcounter;
char wybor = 'n';
// Postac 1
myHero[0].race = "Czlowiek";
myHero[0].name = "Vigo";
myHero[0].fightPoints = 12;
myHero[0].health = 20;
myHero[0].myWeapon.name = "Miecz dlugi";
myHero[0].myWeapon.damageK = 1;
for (short i = 0 ; i < myHero[0].myWeapon.damageK ; ++i)
myHero[0].myWeapon.damage[i] = 8;
myHero[0].myWeapon.minAttack = 1;
myHero[0].myWeapon.minDefense = 1;
myHero[0].myWeapon.strength = 11;
myHero[0].myWeapon.safeZone = 3;
// Postac 2
myHero[1].race = "Elf";
myHero[1].name = "Idril";
myHero[1].fightPoints = 13;
myHero[1].health = 15;
myHero[1].myWeapon.name = "Szabla";
myHero[1].myWeapon.damageK = 1;
for (short i = 0 ; i < myHero[1].myWeapon.damageK ; ++i)
myHero[1].myWeapon.damage[i] = 6;
myHero[1].myWeapon.minAttack = 0;
myHero[1].myWeapon.minDefense = 0;
myHero[1].myWeapon.strength = 9;
myHero[1].myWeapon.safeZone = 3;
/*
// Postac 3
myHero[2].race = "pol - Ork";
myHero[2].name = "Gnod";
myHero[2].fightPoints = 8;
myHero[2].health = 25;
myHero[2].myWeapon.name = "Topor";
if (myHero[2].myWeapon.name == "Topor")
temp = 2;
for (short i = 0 ; i < temp ; ++i)
myHero[2].myWeapon.damage[i] = 6;
myHero[2].myWeapon.minAttack = 3;
myHero[2].myWeapon.minDefense = 4;
myHero[2].myWeapon.strength = 16;
myHero[2].myWeapon.safeZone = 1;
*/
// Wyswietlenie
cout<<"Bohater pierwszy:"<<endl<<endl;
cout<<"Rasa: "<<myHero[0].race<<endl;
cout<<"Imie: "<<myHero[0].name<<endl;
cout<<"Bieglosc w broni: "<<myHero[0].fightPoints<<endl;
cout<<"Zywotnosc: "<<myHero[0].health<<endl;
cout<<endl;
cout<<"Nazwa broni: "<<myHero[0].myWeapon.name<<endl;
cout<<"Obrazenia: ";
for (short i = 0 ; i < myHero[0].myWeapon.damageK ; ++i)
cout<<myHero[0].myWeapon.damage[i]<<", ";
cout<<endl;
cout<<"Minimalny atak: "<<myHero[0].myWeapon.minAttack<<endl;
cout<<"Minimalna obrona: "<<myHero[0].myWeapon.minDefense<<endl;
cout<<"Wymagana sila: "<<myHero[0].myWeapon.strength<<endl;
cout<<"Strefa bezpieczna: "<<myHero[0].myWeapon.safeZone<<endl;
cout<<endl;
cout<<"Bohater drugi:"<<endl<<endl;
cout<<"Rasa: "<<myHero[1].race<<endl;
cout<<"Imie: "<<myHero[1].name<<endl;
cout<<"Bieglosc w broni: "<<myHero[1].fightPoints<<endl;
cout<<"Zywotnosc: "<<myHero[1].health<<endl;
cout<<endl;
cout<<"Nazwa broni: "<<myHero[1].myWeapon.name<<endl;
cout<<"Obrazenia: ";
for (short i = 0 ; i < myHero[1].myWeapon.damageK ; ++i)
cout<<myHero[1].myWeapon.damage[i]<<", ";
cout<<endl;
cout<<"Minimalny atak: "<<myHero[1].myWeapon.minAttack<<endl;
cout<<"Minimalna obrona: "<<myHero[1].myWeapon.minDefense<<endl;
cout<<"Wymagana sila: "<<myHero[1].myWeapon.strength<<endl;
cout<<"Strefa bezpieczna: "<<myHero[1].myWeapon.safeZone<<endl;
cout<<endl;
/*
cout<<"Bohater trzeci:"<<endl<<endl;
cout<<"Rasa: "<<myHero[2].race<<endl;
cout<<"Imie: "<<myHero[2].name<<endl;
cout<<"Bieglosc w broni: "<<myHero[2].fightPoints<<endl;
cout<<"Zywotnosc: "<<myHero[2].health<<endl;
cout<<endl;
cout<<"Nazwa broni: "<<myHero[2].myWeapon.name<<endl;
cout<<"Obrazenia: ";
for (short i = 0 ; i < 3 ; ++i)
{
if (myHero[2].myWeapon.damage[i] != 0)
cout<<myHero[2].myWeapon.damage[i]<<", ";
}
cout<<endl;
cout<<"Minimalny atak: "<<myHero[2].myWeapon.minAttack<<endl;
cout<<"Minimalna obrona: "<<myHero[2].myWeapon.minDefense<<endl;
cout<<"Wymagana sila: "<<myHero[2].myWeapon.strength<<endl;
cout<<"Strefa bezpieczna: "<<myHero[2].myWeapon.safeZone<<endl;
cout<<endl;
*/
cout<<"Wybierz swoja postac 1 lub 2:"<<endl;
cin>>heroA;
cin.ignore();
heroA--;
cout<<"Wybierz postac przeciwnika 1 lub 2:"<<endl;
cin>>heroB;
cin.ignore();
heroB--;
cout<<endl;
cout<<"Witaj "<<myHero[heroA].name<<". Na Twojej drodze stanal "<<myHero[heroB].name<<'.'<<endl<<endl;
for (short i = 1 ; ; i++ )
{
j++;
if (j == 1) // nowa runda
{
myHero[heroA].fightPointsTemp = myHero[heroA].fightPoints;
myHero[heroB].fightPointsTemp = myHero[heroB].fightPoints;
}
cout<<"Runda: "<<j<<". Segment: "<<i<<"."<<endl<<endl;
cout<<myHero[heroA].name<<" masz "<<myHero[heroA].fightPointsTemp<<" punktow walki."<<endl;
cout<<myHero[heroB].name<<" ma "<<myHero[heroB].fightPointsTemp<<" punktow walki."<<endl<<endl;
{ // dostepne opcje
cout<<myHero[heroA].name<<endl;
if (myHero[heroA].fightPointsTemp >= myHero[heroA].myWeapon.minAttack)
{
heroAattack = true;
cout<<"Mozesz atakowac."<<endl;
}
else
{
heroAattack = false;
cout<<"Nie mozesz atakowac."<<endl;
}
if (myHero[heroA].fightPointsTemp >= myHero[heroA].myWeapon.minDefense)
{
heroAdefense = true;
cout<<"Mozesz parowac cios wlasna bronia."<<endl;
}
else
{
heroAdefense = false;
cout<<"Nie mozesz juz parowac."<<endl;
}
if (myHero[heroA].fightPointsTemp >= myHero[heroA].myWeapon.minAttack + 2)
{
heroAcounter = true;
cout<<"Jestes w stanie wykonac kontratak."<<endl;
}
else
{
heroAcounter = false;
cout<<"Nie wykonasz juz kontrataku."<<endl;
}
// przeciwnik
cout<<endl<<myHero[heroB].name<<endl;
if (myHero[heroB].fightPointsTemp >= myHero[heroB].myWeapon.minAttack)
{
heroBattack = true;
cout<<"Moze atakowac."<<endl;
}
else
{
heroBattack = false;
cout<<"Nie moze atakowac."<<endl;
}
if (myHero[heroB].fightPointsTemp >= myHero[heroB].myWeapon.minDefense)
{
heroBdefense = true;
cout<<"Moze parowac cios wlasna bronia."<<endl;
}
else
{
heroBdefense = false;
cout<<"Nie moze juz parowac."<<endl;
}
if (myHero[heroB].fightPointsTemp >= myHero[heroB].myWeapon.minAttack + 2)
{
heroBcounter = true;
cout<<"Jest w stanie wykonac kontratak."<<endl;
}
else
{
heroBcounter = false;
cout<<"Nie wykona juz kontrataku."<<endl;
}
}
if (myHero[heroA].fightPointsTemp < myHero[heroB].fightPointsTemp) // zaczynasz
{
cout<<endl<<myHero[heroA].name<<" okresl swoja akcje: "<<endl;
if (heroAattack == true)
cout<<"1 - Atak."<<endl;
if (heroAdefense == true)
cout<<"2 - Parowanie."<<endl;
if (heroAcounter == true)
cout<<"3 - Kontratak."<<endl;
cin>>tempA1;
cin.ignore();
if (heroAattack == true && tempA1 == 1) // atak
{
for (short ii = 0 ; ; )
{
cout<<"Min. atak: "<<myHero[heroA].myWeapon.minAttack<<". Podaj wartosc: "<<endl;
cin>>tempA2;
cin.ignore();
if (tempA2 < myHero[heroA].myWeapon.minAttack)
{
cout<<"Zla wartosc, sprobuj jeszcze raz."<<endl;
}
else
break;
}
if (heroBdefense == true && (rand()%(myHero[heroA].fightPointsTemp + 1) != 0 || rand()%(myHero[heroB].fightPointsTemp + 1) == 0)) // parowanie
{
cout<<endl<<myHero[heroA].name<<" atakuje za: "<<tempA2<<endl;
for (short ii = 0 ; ; )
{
tempB2 = rand()%(tempA2 + tempA2 / 2) + 1;
if (tempB2 >= myHero[heroB].myWeapon.minDefense && tempB2 <= tempA2)
break;
}
cout<<myHero[heroB].name<<" paruje swoja bronia za: "<<tempB2<<endl;
}
else if (heroBcounter == true && (rand()%(myHero[heroA].fightPointsTemp + 1) == 0 || rand()%(myHero[heroB].fightPointsTemp + 1) != 0)) // kontra
{
}
else if (heroBattack == true)
{
}
else
myHero[heroA].fightPointsTemp -= tempA2;
myHero[heroB].fightPointsTemp -= tempB2;
cout<<"wartosc tempA2:"<<tempA2<<endl;
cout<<"wartosc tempB2:"<<tempB2<<endl;
cout<<myHero[heroA].name<<" masz "<<myHero[heroA].fightPointsTemp<<" punktow walki."<<endl; // ???!!!
cout<<myHero[heroB].name<<" ma "<<myHero[heroB].fightPointsTemp<<" punktow walki."<<endl<<endl;
getchar();
}
else if (heroAdefense == true && tempA1 == 2) // parowanie
{
for (short ii = 0 ; ; )
{
cout<<"Min. parowanie: "<<myHero[heroA].myWeapon.minDefense<<". Podaj wartosc: "<<endl;
cin>>tempA2;
cin.ignore();
if (tempA2 < myHero[heroA].myWeapon.minDefense)
{
cout<<"Zla wartosc, sprobuj jeszcze raz."<<endl;
}
else
{
myHero[heroA].fightPointsTemp -= tempA2;
break;
}
}
}
else if (heroAcounter == true && tempA1 == 3) //kontra
{
for (short ii = 0 ; ; )
{
cout<<"Min. atak: "<<myHero[heroA].myWeapon.minAttack<<" + 2 pkt. Podaj wartosc: "<<endl;
cin>>tempA2;
cin.ignore();
if (tempA2 < myHero[heroA].myWeapon.minAttack)
{
cout<<"Zla wartosc, sprobuj jeszcze raz."<<endl;
}
else
{
myHero[heroA].fightPointsTemp -= tempA2 + 2;
break;
}
}
}
else
{
cout<<"Zla wartosc, sprobuj jeszcze raz."<<endl<<endl;
i--;
j--;
continue;
}
}
getchar();
} // for nieskonczony
cout<<"ENTER..."<<endl;
getchar();
return 0;
}
Moja postać 1 , przeciwnik 2, Zawsze atak czyli 1 i dalej wartosć powiedzmy 6. I te 6 powinno sie odjąć od fightPoints tego gościa. Przy B działa a dla A nie.