What's new

C# HELP. Paano ko po i-output yung "you win" pag nahulaan yung word in 2-3 attempts at "game over" pag di nahulaan yung word at nagamit na 3 attempts?

najamjam

Addict
Need ko rin po kasi gumamit ng StringBuilder at ArrayList.


C#:
using System;
using System.Text;
using System.Collections;

namespace GuessingGame
{
    public class GuessingGame
    {
        public static void Main(string[] args)
        {
            bool OutOfAttempts = false;

            Console.WriteLine("Simple Word-Guessing Game");
            Console.WriteLine("Category: Korean Food");
            StringBuilder word = new StringBuilder("Bibimbap");
            string strword = word.ToString();
            word[6] = '_'; word[0] = '_';

            for(int index = 0; index < word.Length; index++)
            {
                Console.Write(word[index] + " ");
            }
            Console.WriteLine("\n");

            for(int i = 0; i <= 3; i++)
            {
                Console.Write("Answer: ");
                string answer = Console.ReadLine();

                if (answer != word.ToString() && !OutOfAttempts)
                {
                    if (answer == "a")
                    {
                        word.Remove(6, 1).Insert(6, 'a');
                        Console.WriteLine(word);
                        i++;
                    }

                    if (answer == "B" || answer == "b")
                    {
                        word.Remove(0, 1).Insert(0, 'B');
                        Console.WriteLine(word);
                        i++;
                    }

                    else if (i == 2)
                    {
                        Console.WriteLine("1 attempt left.");
                        continue;
                    }
                    else if (answer != strword && i == 3)
                    {
                        OutOfAttempts = true;
                        Console.WriteLine("Game Over.\nCorrect answer: " + strword);
                    }
                }
                else
                {
                    Console.WriteLine("You win");
                    break;
                }
            }
        }
    }
}
 
Me minor corrections lang

C#:
using System;
using System.Text;
using System.Collections;

namespace GuessingGame
{
    public class GuessingGame
    {
        public static void Main(string[] args)
        {
            bool OutOfAttempts = false;

            Console.WriteLine("Simple Word-Guessing Game");
            Console.WriteLine("Category: Korean Food");
            string correctAnswer = "Bibimbap";
            StringBuilder clue = new StringBuilder(correctAnswer, 30);
            clue[6] = '_'; clue[0] = '_';

            for (int index = 0; index < clue.Length; index++)
            {
                Console.Write(clue[index] + " ");
            }
            Console.WriteLine("\n");

            for (int i = 0; i < 3; i++)
            {
                Console.Write("Answer: ");
                string answer = Console.ReadLine();

                if (!IsTheAnswerCorrect(answer, correctAnswer) && !OutOfAttempts)
                {
                    if (answer == "a")
                    {
                        clue.Remove(6, 1).Insert(6, 'a');
                        Console.WriteLine(clue);
                        //now you need to check if the clue is completed
                        if (IsTheAnswerCorrect(clue.ToString(), correctAnswer))
                        {
                            break;
                        }
                    }

                    if (answer.ToUpper() == "B")
                    {
                        clue.Remove(0, 1).Insert(0, 'B');
                        Console.WriteLine(clue);
                        //now you need to check if the clue is completed
                        if (IsTheAnswerCorrect(clue.ToString(), correctAnswer))
                        {
                            break;
                        }
                    }

                    else if (i == 1)
                    {
                        Console.WriteLine("1 attempt left.");
                        continue;
                    }
                    else if (!IsTheAnswerCorrect(answer, correctAnswer) && i == 2)
                    {
                        OutOfAttempts = true;
                        Console.WriteLine("Game Over.\nCorrect answer: " + correctAnswer);
                        Console.ReadKey(true);
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("You win");
                    break;
                }
            }
        }

        private static bool IsTheAnswerCorrect(string a, string b)
        {
            bool result = (string.Compare(a, b) == 0);

            if (result)
            {
                Console.WriteLine("You win");
                Console.ReadKey(true);
            }
            return result;
        }
    }
}

C#:
using System;
using System.Text;
using System.Collections;

namespace GuessingGame
{
    public class GuessingGame
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Simple Word-Guessing Game");
            Console.WriteLine("Category: Korean Food");
            string correctAnswer = "Bibimbap";
            StringBuilder clue = new StringBuilder(correctAnswer);
            clue[6] = '_'; clue[0] = '_';

            Console.WriteLine(clue.ToString());

            for (int i = 0; i < 3; i++)
            {
                Console.Write("Answer: ");
                string answer = Console.ReadLine();

                if (IsTheAnswerCorrect(answer,correctAnswer))
                {
                    Console.WriteLine("You win");
                    break;
                }

                else
                {
                    clue = CheckAnswerAndUpdateClueIfNeeded(clue, answer, correctAnswer);
                    Console.WriteLine(clue);
                    if (IsTheAnswerCorrect(clue.ToString(), correctAnswer))
                    {
                        break;
                    }

                    if (i == 1)
                    {
                        Console.WriteLine("1 attempt left.");
                        continue;
                    }

                    if (i == 2)
                    {
                        Console.WriteLine("Game Over.\nCorrect answer: " + correctAnswer);
                        Console.ReadKey(true);
                        break;
                    }
                }
            }
        }

        private static bool IsTheAnswerCorrect(string a, string b)
        {
            bool result = (string.Compare(a, b) == 0);

            if (result)
            {
                Console.WriteLine("You win");
                Console.ReadKey(true);
            }
            return result;
        }

        private static StringBuilder CheckAnswerAndUpdateClueIfNeeded(StringBuilder clue, string answer, string correctAnswer)
        {
            StringBuilder tempClue = clue;

            for (int i=0; i<clue.Length; i++)
            {
                if (clue[i] == '_')
                {
                    if (answer.ToUpper()==correctAnswer[i].ToString().ToUpper())
                    {
                        tempClue[i] = correctAnswer[i];
                    }
                }
            }
            return tempClue;
        }
    }
}
 
Last edited:
Me minor corrections lang

C#:
using System;
using System.Text;
using System.Collections;

namespace GuessingGame
{
    public class GuessingGame
    {
        public static void Main(string[] args)
        {
            bool OutOfAttempts = false;

            Console.WriteLine("Simple Word-Guessing Game");
            Console.WriteLine("Category: Korean Food");
            string correctAnswer = "Bibimbap";
            StringBuilder clue = new StringBuilder(correctAnswer, 30);
            clue[6] = '_'; clue[0] = '_';

            for (int index = 0; index < clue.Length; index++)
            {
                Console.Write(clue[index] + " ");
            }
            Console.WriteLine("\n");

            for (int i = 0; i < 3; i++)
            {
                Console.Write("Answer: ");
                string answer = Console.ReadLine();

                if (!IsTheAnswerCorrect(answer, correctAnswer) && !OutOfAttempts)
                {
                    if (answer == "a")
                    {
                        clue.Remove(6, 1).Insert(6, 'a');
                        Console.WriteLine(clue);
                        //now you need to check if the clue is completed
                        if (IsTheAnswerCorrect(clue.ToString(), correctAnswer))
                        {
                            break;
                        }
                    }

                    if (answer.ToUpper() == "B")
                    {
                        clue.Remove(0, 1).Insert(0, 'B');
                        Console.WriteLine(clue);
                        //now you need to check if the clue is completed
                        if (IsTheAnswerCorrect(clue.ToString(), correctAnswer))
                        {
                            break;
                        }
                    }

                    else if (i == 1)
                    {
                        Console.WriteLine("1 attempt left.");
                        continue;
                    }
                    else if (!IsTheAnswerCorrect(answer, correctAnswer) && i == 2)
                    {
                        OutOfAttempts = true;
                        Console.WriteLine("Game Over.\nCorrect answer: " + correctAnswer);
                        Console.ReadKey(true);
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("You win");
                    break;
                }
            }
        }

        private static bool IsTheAnswerCorrect(string a, string b)
        {
            bool result = (string.Compare(a, b) == 0);

            if (result)
            {
                Console.WriteLine("You win");
                Console.ReadKey(true);
            }
            return result;
        }
    }
}

C#:
using System;
using System.Text;
using System.Collections;

namespace GuessingGame
{
    public class GuessingGame
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Simple Word-Guessing Game");
            Console.WriteLine("Category: Korean Food");
            string correctAnswer = "Bibimbap";
            StringBuilder clue = new StringBuilder(correctAnswer);
            clue[6] = '_'; clue[0] = '_';

            Console.WriteLine(clue.ToString());

            for (int i = 0; i < 3; i++)
            {
                Console.Write("Answer: ");
                string answer = Console.ReadLine();

                if (IsTheAnswerCorrect(answer,correctAnswer))
                {
                    Console.WriteLine("You win");
                    break;
                }

                else
                {
                    clue = CheckAnswerAndUpdateClueIfNeeded(clue, answer, correctAnswer);
                    Console.WriteLine(clue);
                    if (IsTheAnswerCorrect(clue.ToString(), correctAnswer))
                    {
                        break;
                    }

                    if (i == 1)
                    {
                        Console.WriteLine("1 attempt left.");
                        continue;
                    }

                    if (i == 2)
                    {
                        Console.WriteLine("Game Over.\nCorrect answer: " + correctAnswer);
                        Console.ReadKey(true);
                        break;
                    }
                }
            }
        }

        private static bool IsTheAnswerCorrect(string a, string b)
        {
            bool result = (string.Compare(a, b) == 0);

            if (result)
            {
                Console.WriteLine("You win");
                Console.ReadKey(true);
            }
            return result;
        }

        private static StringBuilder CheckAnswerAndUpdateClueIfNeeded(StringBuilder clue, string answer, string correctAnswer)
        {
            StringBuilder tempClue = clue;

            for (int i=0; i<clue.Length; i++)
            {
                if (clue[i] == '_')
                {
                    if (answer.ToUpper()==correctAnswer[i].ToString().ToUpper())
                    {
                        tempClue[i] = correctAnswer[i];
                    }
                }
            }
            return tempClue;
        }
    }
}
pano po kayo nakapag lagay ng ArrayList sa ganitong code?
 
Back
Top