Jump to content

  • Log In with Google      Sign In   
  • Create Account

Apparently, no so simple Math.....


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 cavendert   Members   -  Reputation: 162

Like
0Likes
Like

Posted 19 August 2014 - 03:26 PM

Pokemon battling again for practice. Tried to make it a little harder this time with attack and defense. Health - (attack - defense).

 

I get the feeling my order of operations is getting messed up somehow, because no matter, how I re-code it, somebody is always gaining life. 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication9
{
    class Pokemon
    {
        public string Name;
        public int Health;
        public int Attack;
        public int Defense;
    }
    class Program
    {
        static List<Pokemon> shaunPokemon = new List<Pokemon>() {
            new Pokemon {
                Name = "Mega Bidoof",
                Health = 700,
                Attack = 600,
                Defense = 500
            },
            new Pokemon {
                Name = "Bidoof",
                Health = 350,
                Attack = 150,
                Defense = 50,
            },
            new Pokemon {
                Name = "Bidoof", 
                Health = 350, 
                Attack = 150,
                Defense = 50,
            }
        };


        static List<Pokemon> myPokemon = new List<Pokemon>() {
            new Pokemon {
                Name = "Scizor",
                Health = 400, 
                Attack = 180,
                Defense = 100
            },
            new Pokemon {
                Name = "Houndoom",
                Health = 380,
                Attack = 220,
                Defense = 50
            },
            new Pokemon {
                Name = "Tyranitar",
                Health = 350,
                Attack = 100,
                Defense = 80
            }
        };


        static void Main(string[] args)
        {
            //Random Generator
            var rnd = new Random();
            var randomChoice = shaunPokemon[rnd.Next(shaunPokemon.Count)];


            Console.WriteLine("Writer Shaun challenges you to a battle!");
            Console.WriteLine("I choose you, " + randomChoice.Name + "! \n");
            Console.WriteLine("Please choose your Pokemon.");
            for (int i = 0; i < myPokemon.Count; i++)
            {
                Console.WriteLine("Press " + (i + 1) + " for " + myPokemon[i].Name + ".");
            }
            var myInput = Console.ReadLine();
            int index = int.Parse(myInput) - 1;
            Console.WriteLine();
            Pokemon selected = myPokemon[index];


            Console.WriteLine("I choose you, " + selected.Name + "! \n");
            Console.WriteLine("Press Enter to attack.");
            string acceptInput = Console.ReadLine();


            while (selected.Health > 0 && randomChoice.Health > 0)
            {
                selected.Health -= (randomChoice.Attack - selected.Defense);
                randomChoice.Health -= (selected.Attack - randomChoice.Defense);
                Console.WriteLine(selected.Name + "'s health = " + selected.Health);
                Console.WriteLine(randomChoice.Name + "'s health = " + randomChoice.Health);
                Console.WriteLine();
                Console.WriteLine("Press Enter to Attack again.");
                Console.ReadLine();
            };
        }
    }
}

As you can see, I even raised the attacks, to make sure that (Attack-Defense) should always be positive. 

 

Am I missing something that simple?

 

 


Sponsor:

#2 cavendert   Members   -  Reputation: 162

Like
0Likes
Like

Posted 19 August 2014 - 03:31 PM

If it helps, I just found that my compiler got stuck.....so, this might actually work. So, I have a follow up question!! This code should work about 60% of the time, every time. 

 

Until "Mega Bidoof" is chosen. As a joke, his stats are so high, that he is ruining the equation. Other than writing an if/else statement for bidoof, and mega bidoof, with their own while statements, is there a better way to write that?



#3 Lactose!   GDNet+   -  Reputation: 3364

Like
4Likes
Like

Posted 19 August 2014 - 03:42 PM

Something like this should do the trick:

int damageTaken = attack - defense;
if (damageTaken > 0)
    health -= damageTaken;


#4 cavendert   Members   -  Reputation: 162

Like
0Likes
Like

Posted 19 August 2014 - 04:05 PM

 

Something like this should do the trick:

int damageTaken = attack - defense;
if (damageTaken > 0)
    health -= damageTaken;

damn........that's genius.........Then make two? One for my damage, and one for their damage?



#5 cavendert   Members   -  Reputation: 162

Like
0Likes
Like

Posted 19 August 2014 - 04:09 PM

I still have the same issue, where Mega Bidoof throws off the numbers. Though, as I look at it, my health should always hit 0 in the first move anyway, so I'm not sure I care that much. Thanks again for the idea!



#6 TheChubu   Crossbones+   -  Reputation: 4329

Like
5Likes
Like

Posted 19 August 2014 - 04:16 PM

 

Something like this should do the trick:

int damageTaken = attack - defense;
if (damageTaken > 0)
    health -= damageTaken;

health -= max(attack - defense, 0)

 

Doing min/max ops is objectively prettier :P


"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator


#7 Lactose!   GDNet+   -  Reputation: 3364

Like
4Likes
Like

Posted 19 August 2014 - 04:18 PM

I see your min/max ops and raise you blocking logic!

 

int damageTaken = attack - defense;
if (damageTaken > 0)
    health -= damageTaken;
else
    Console.WriteLine("Attack blocked!");


#8 TheChubu   Crossbones+   -  Reputation: 4329

Like
3Likes
Like

Posted 19 August 2014 - 04:22 PM

 

I see your min/max ops and raise you blocking logic!

int damageTaken = attack - defense;
if (damageTaken > 0)
    health -= damageTaken;
else
    Console.WriteLine("Attack blocked!");

I wouldn't put game logic and (arguably) UI code in the same place. Just saiyan.


"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator


#9 slicer4ever   Crossbones+   -  Reputation: 3884

Like
3Likes
Like

Posted 19 August 2014 - 04:24 PM

I see your min/max ops and raise you blocking logic!
 
 

int damageTaken = attack - defense;
if (damageTaken > 0)
    health -= damageTaken;
else
    Console.WriteLine("Attack blocked!");


In pokemon, all offensive attacks do at least one damage even if they should be 100% negated(except for move types that are uneffective against the opponent types(i.e ghost/normal iirc))

so, let me just fix chubu's real quick:
health -= max(attack - defense, 1)

Edited by slicer4ever, 19 August 2014 - 04:31 PM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#10 Lactose!   GDNet+   -  Reputation: 3364

Like
3Likes
Like

Posted 19 August 2014 - 04:24 PM

It could also easily be non-UI related. E.g. every time you block all damage 4 times in a row, gain double damage for next attack. Do counter related things inside the if/else blocks. Just to continue the derail a bit.

 

But we both know you're just typing stuff here to avoid coding!



#11 cavendert   Members   -  Reputation: 162

Like
0Likes
Like

Posted 19 August 2014 - 04:43 PM

 

I see your min/max ops and raise you blocking logic!
 
 

int damageTaken = attack - defense;
if (damageTaken > 0)
    health -= damageTaken;
else
    Console.WriteLine("Attack blocked!");

In pokemon, all offensive attacks do at least one damage even if they should be 100% negated(except for move types that are uneffective against the opponent types(i.e ghost/normal iirc))

so, let me just fix chubu's real quick:
health -= max(attack - defense, 1)

All very nice! I do have one question. In this line of code right at the bottom, what is the 1 for? I get that we are trying to always do atleast 1 damage, but does that effect the code if normal damage is done? 



#12 Ectara   Crossbones+   -  Reputation: 2966

Like
3Likes
Like

Posted 19 August 2014 - 04:43 PM

 

I see your min/max ops and raise you blocking logic!
 
 

int damageTaken = attack - defense;
if (damageTaken > 0)
    health -= damageTaken;
else
    Console.WriteLine("Attack blocked!");

In pokemon, all offensive attacks do at least one damage even if they should be 100% negated(except for move types that are uneffective against the opponent types(i.e ghost/normal iirc))

so, let me just fix chubu's real quick:
health -= max(attack - defense, 1)

I was just about to say that. :)

 


Then make two?

If you find yourself saying that, consider making it a method of the class.



#13 Lactose!   GDNet+   -  Reputation: 3364

Like
3Likes
Like

Posted 19 August 2014 - 04:47 PM


All very nice! I do have one question. In this line of code right at the bottom, what is the 1 for? I get that we are trying to always do atleast 1 damage, but does that effect the code if normal damage is done? 

 

int x = max(attack - defense, 1);
is equivalent to
int x = attack - defense;
if (x < 1)
    x = 1;


#14 cavendert   Members   -  Reputation: 162

Like
0Likes
Like

Posted 20 August 2014 - 11:31 AM

Ah. That's what I was assuming, but I didn't want to assume. Thank you all for the advice!!!!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS