2 Replies - 361 Views - Last Post: 28 April 2020 - 07:07 AM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 915
  • Joined: 15-December 14

Can't figure out how to make random work with if statement

Posted 17 April 2020 - 10:25 PM

I am making C# version of Gothons of Planet Percal #25 and I am stuck on using random with an if and else statement. I'm trying to make it so when the player attacks it he will need to roll greater than 13 to hit, but if the player rolls less it misses. I can't figure out whether the player hits or misses I get the same response.

This is the game play and below will be the game it self.

The Gothons of Planet Percal #25 have invaded your ship and killed
your entire crew. You are the last surviving member and your last
mission is to get the neutron destruct bomb from the Weapons Armoury,
put it in the bridge, and blow the ship up after getting into an
escape pod.

What is your name?
snoopy
Your players name is snoopy

2 Engine room
3 Escape pod
4 Armoury
5 Heal damage
6 Check health
7 PlayerAttack

Choose an option

7
snoopy has to roll over 13 to hit
snoopy rolled a 6
snoopy missed
2 Engine room
3 Escape pod
4 Armoury
5 Heal damage
6 Check health
7 PlayerAttack

Choose an option

7
snoopy has to roll over 13 to hit
snoopy rolled a 18
snoopy missed
2 Engine room
3 Escape pod
4 Armoury
5 Heal damage
6 Check health
7 PlayerAttack

Choose an option

This is the program so far.

using System;
using System.Threading;
namespace Gothans_from_Planet_Percal_25
{
    class Program
    {
        
        public void Heal()//this will heal the player
        {
            Random number = new Random();
            double heal = number.Next(0, 10);
            Console.WriteLine("Your character healed ", heal, " damage");
        }
        public class Player
        {
            string name = ("");
            string[] intro = {"The Gothons of Planet Percal #25 have invaded your ship and killed",
                "your entire crew.  You are the last surviving member and your last",
                "mission is to get the neutron destruct bomb from the Weapons Armoury,",
                "put it in the bridge, and blow the ship up after getting into an ",
                "escape pod.\n"};

            string[] quips = {"You died. You kinda suck at this.",
                "Your mom would be proud...if she were smarter.",
                "Such a luser.","I have a small puppy that's better at this." };
            public void Intro()
            {
                for (int i = 0; i < intro.Length; i++)
                {
                    Console.WriteLine(intro[i]);
                }
            }
            public string GetName()
            {
                Console.WriteLine("What is your name?");
                name = Console.ReadLine();
                Console.WriteLine("Your players name is {0}\n", name);

                return name;
            }
            public void PlayerHealth(int health)
            {//This will display how much health the player has and can be accessed at anytime
                if (health >= 10)
                    Console.WriteLine(name," You're at full health {0}", health);
                else if (health < 10)
                    Console.WriteLine("You have {0} health", health);
                else
                    Console.WriteLine("You're dead");
            }
            public void Initiative()
            {//This will handle the initiavite to see who goes first in a fight
                Random init = new Random();
                int roll = init.Next(1, 10);
            }
            
            public void Encounters()//random show Gothoms
            {
                Random rand = new Random();
                int bg = rand.Next(1,10);
                if (bg % 2 == 0)
                    Initiative();
                    //Console.WriteLine("You see a Gothom\n");
                
            }
            public double HealPlayer()
            {
                Random health = new Random();
                double healdmg = health.Next(5);
                Console.WriteLine("Your player healed {0}", healdmg, " health");

                return healdmg;
            }
            public void Bridge()
            {
                
                Console.WriteLine("Bridge");

            }
            public void PlayerAttack()
            {

                Random attack = new Random();
                int at = attack.Next(1, 20);
                //playerattack has to roll above a 13 to hit otherwise it hits the enemies armour
                Console.WriteLine("{0} has to roll over 13 to hit", name);
                Console.Write("{0} rolled a ",name);
                if (at > 13)
                {
                    Console.WriteLine(attack.Next(1, 20));
                    Console.WriteLine("You hit the enemy");
                }
                else
                {
                    Console.WriteLine(attack.Next(1, 20));
                    Console.WriteLine("{0} missed", name);
                }

            }
        }
        static void Main(string[] args)
        {
            Player player = new Player();
            player.Intro();
            player.GetName();
            int pick;
            string[] choice = { " Bridge", " Engine room", " Escape pod", " Armoury",
                " Heal damage"," Check health"," PlayerAttack"};
            while(true)
            {
                for(int x = 1; x < choice.Length; x++)
                {
                    Console.Write(x + 1);
                    Console.WriteLine(choice[x]);
                }
                Console.WriteLine("\nChoose an option\n");
                pick = Convert.ToInt32(Console.ReadLine());
                if (pick == 1)
                {
                    player.Bridge();
                }
                else if (pick == 5)
                {
                    player.HealPlayer();
                }
                else if (pick == 7)
                    player.PlayerAttack();
            }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Can't figure out how to make random work with if statement

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15893
  • View blog
  • Posts: 63,606
  • Joined: 12-June 08

Re: Can't figure out how to make random work with if statement

Posted 17 April 2020 - 10:34 PM

First up - you would want to only have one Random object. So make that a global.

Why are you printing to console a new random number? I would figure you would want variable 'at' in lines 89 and 94.

083	                int at = attack.Next(1, 20);
084	                //playerattack has to roll above a 13 to hit otherwise it hits the enemies armour
085	                Console.WriteLine("{0} has to roll over 13 to hit", name);
086	                Console.Write("{0} rolled a ",name);
087	                if (at > 13)
088	                {
089	                    Console.WriteLine(attack.Next(1, 20));
090	                    Console.WriteLine("You hit the enemy");
091	                }
092	                else
093	                {
094	                    Console.WriteLine(attack.Next(1, 20));
095	                    Console.WriteLine("{0} missed", name);
096	                }

Was This Post Helpful? 1
  • +
  • -

#3 albert003   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 915
  • Joined: 15-December 14

Re: Can't figure out how to make random work with if statement

Posted 28 April 2020 - 07:07 AM

Sorry I thought I replied back to this post. Youre right I corrected that mistake and it works perfectly. Thank you very much.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1