sir_thursday
sir_thursday

Reputation: 5419

how to return bool value from method

I have a while loop that loops until bool done = true; In the method TestMoves(), depending on user input, the method returns the bool done as either true or false. However, I do not know how to "send" this value back to the while loop in my Start() method to stop the loop. Here is my code:

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

namespace ConsoleApplication1
{
    class Program
    {


        public static void Main(string[] args)
        {
            Start("r");
        }

        public static string Start(string move)
        {

            Console.Write("Welcome to the Shotgun App\nEnter s for single player and m for multiplayer: ");
            string gameType = Console.ReadLine();

            if (gameType == "s")
            {

                Console.Write("Single Player Controls:\n r = reload\n s = shield\n f = fire\n ***you start with ammo\n Ready to play?");
                Console.ReadLine();

                int ammo = 1;

                bool done = false;
                while (!done)
                {
                    Console.Write("\nEnter your move: ");
                    move = Console.ReadLine();


                    switch (move)
                    {
                        case "r":
                            Console.Write("\nYou have reloaded, press enter for Genius\n");

                            ammo++;
                            Console.Write("Your ammo is " + ammo);

                            Console.ReadLine();

                            string geniusMove = "";
                            Genius(geniusMove, move, done);
                            Console.ReadLine();




                            break;
                        case "s":
                            Console.Write("\nYou have shielded, press enter for Genius\n");

                            Console.Write("Your ammo is " + ammo);

                            Console.ReadLine();

                            geniusMove = "";
                            Genius(geniusMove, move, done);
                            Console.ReadLine();




                            break;
                        case "f":
                            if (ammo != 0)
                            {
                                Console.Write("\nYou have fired, press enter for Genius\n");

                                ammo--;
                                Console.Write("Your ammo is " + ammo);

                                Console.ReadLine();

                                geniusMove = "";
                                Genius(geniusMove, move, done);
                                Console.ReadLine();
                            }
                            else
                            {
                                Console.Write("You don't have enough ammo, try again");
                                done = false;
                            }
                            break;
                        default:
                            Console.Write("\nInvalid move, try again\n");
                            done = false;
                            break;
                    }


                }
                return move;
            }
            else
            {
                return move;
            }
        }

        static string Genius(string geniusMove, string move, bool done)
        {
            int geniusAmmo = 1;

            geniusMove = "r";
            if (geniusMove == "f")
            {

                geniusAmmo--;
                Console.Write("Genius had decided to fire.\nGenius ammo is " + geniusAmmo + "\n");
            }
            else if (geniusMove == "r")
            {

                geniusAmmo++;
                Console.Write("Genius had decided to reload.\nGenius ammo is " + geniusAmmo + "\n");
            }
            else if (geniusMove == "s")
            {
                Console.Write("Genius had decided to shield.\nGenius ammo is " + geniusAmmo + "\n");
            }
            TestMoves(move, geniusMove, done);
            return geniusMove;
        }


        static bool TestMoves(string move, string geniusMove, bool done)
        {

            if (move == "s" && geniusMove == "f")
            {
                Console.Write("No one has died yet");
                done = false;
                return done;
            }
            else if (move == "f" && geniusMove == "f")
            {
                Console.Write("You both died!  Good game!");
                done = true;
                return done;
            }
            else if (move != "s" && geniusMove == "f")
            {
                Console.Write("You died!  Good game!");
                done = true;
                return done;
            }
            else if (move == "f" && geniusMove == "s")
            {
                Console.Write("No one has died yet");
                done = false;
                return done;
            }
            else if (move == "f" && geniusMove != "s")
            {
                Console.Write("Genius died!  Good game!");
                done = true;
                return done;
            }
            else if (move != "f" && geniusMove != "f")
            {
                Console.Write("No one has died yet");
                done = false;
                return done;
            }
            else
            {
                return done;
            }

        }
    }
}

Upvotes: 0

Views: 15288

Answers (4)

Abe Miessler
Abe Miessler

Reputation: 85096

Is there a reason you need to call TestMoves from inside of Genius instead of from your loop? It seems to me that your code could be rewritten like this:

//Every instance of:

string geniusMove = "";
Genius(geniusMove, move, done);
Console.ReadLine();

//seems like it could be rewritten as:

string geniusMove = "";
Genius(geniusMove, move, done);
done = TestMoves(geniusMove, move, done);
Console.ReadLine();
//and then you can remove the call to TestMoves from Genius

The overall flow of all the code is a bit confusing to me. You have each function returning a value, but don't appear to be doing anything with the return value. I have a feeling that with a bit of refactoring, you could make this code much shorter and more logical.

After looking at your code a bit more it looks like you could place the call to TestMoves at the very end of your loop:

                    default:
                        Console.Write("\nInvalid move, try again\n");
                        done = false;
                        break;
                }

                //add it here:
                done = TestMoves(geniusMove, move, done);

            }
            return move;

Upvotes: 2

thumbmunkeys
thumbmunkeys

Reputation: 20764

You could use the ref or the out keyword on the done parameter

Upvotes: 0

Rag
Rag

Reputation: 6593

Since bool is a value type, not a reference type, you're not able to pass the bool through like that. Use the out keyword to explicitly specify that you want to pass it as a reference:

http://msdn.microsoft.com/en-us/library/ee332485.aspx

Upvotes: 0

Jordão
Jordão

Reputation: 56497

You can pass the value by ref:

static string Genius(string geniusMove, string move, ref bool done) ...

And return it from TestMoves:

static bool TestMoves(string move, string geniusMove) ...

To call it:

Genius(geniusMove, move, ref done);

Upvotes: 0

Related Questions