static void Main(string[] args) { string url = null; string s = null; HttpWebRequest request; HttpWebResponse response; StreamReader reader; url = "https://opentdb.com/api.php?amount=1&category=10&difficulty=medium"; request = (HttpWebRequest)WebRequest.Create(url); response = (HttpWebResponse)request.GetResponse(); reader = new StreamReader(response.GetResponseStream()); s = reader.ReadToEnd(); reader.Close(); Trivia trivia = JsonConvert.DeserializeObject <Trivia>(s); trivia.results[0].question = HttpUtility.HtmlDecode(trivia.results[0].question); trivia.results[0].correct_answer = HttpUtility.HtmlDecode(trivia.results[0].correct_answer); for (int i = 0; i < trivia.results[0].incorrect_answers.Count; i++) { trivia.results[0].incorrect_answers[i] = HttpUtility.HtmlDecode(trivia.results[0].incorrect_answers[i]); } Console.WriteLine("~Trivia Time~"); Console.WriteLine("\n" + trivia.results[0].question + "\n"); Random rand = new Random(); int x = rand.Next(0, 4); int questionsDisplayed = 0; for (int i = 0; i < x; i++) { questionsDisplayed++; Console.WriteLine(questionsDisplayed + ". " + trivia.results[0].incorrect_answers[i]); } questionsDisplayed++; Console.WriteLine(questionsDisplayed + ". " + trivia.results[0].correct_answer); for (int j = questionsDisplayed - 1; j < 3; j++) { questionsDisplayed++; Console.WriteLine(questionsDisplayed + ". " + trivia.results[0].incorrect_answers[j]); } string userInput = Console.ReadLine(); Console.WriteLine("\nYour answer was: " + userInput); }
static void Main(string[] args) { string url = null; string s = null; HttpWebRequest request; HttpWebResponse response; StreamReader reader; url = "https://opentdb.com/api.php?amount=10&category=15&difficulty=medium&type=multiple&amount=1"; request = (HttpWebRequest)WebRequest.Create(url); response = (HttpWebResponse)request.GetResponse(); reader = new StreamReader(response.GetResponseStream()); s = reader.ReadToEnd(); reader.Close(); Trivia trivia = JsonConvert.DeserializeObject <Trivia>(s); }
static void Main(string[] args) { string url = null; string s = null; HttpWebRequest request; HttpWebResponse response; StreamReader reader; url = "http://opentdb.com/api.php?amount=1"; request = (HttpWebRequest)WebRequest.Create(url); response = (HttpWebResponse)request.GetResponse(); reader = new StreamReader(response.GetResponseStream()); s = reader.ReadToEnd(); reader.Close(); Trivia trivia = JsonConvert.DeserializeObject <Trivia>(s); trivia.results[0].question = HttpUtility.HtmlDecode(trivia.results[0].question); trivia.results[0].correct_answer = HttpUtility.HtmlDecode(trivia.results[0].correct_answer); /* * for (int i = 0; i < trivia.results[0].incorrect_answers.Count; ++i) * { * trivia.results[0].incorrect_answers[i] = HttpUtility.HtmlDecode(trivia.results[0].question); * }*/ Console.WriteLine(trivia.results[0].question); Random rnd = new Random(); int num; int resultsNum = trivia.results[0].incorrect_answers.Count + 1; int answer = 0; string[] check = new string[resultsNum]; for (int i = 0; i < check.Length; ++i) { check[i] = null; } for (int i = 0; i < resultsNum; ++i) { bool success = false; do { num = rnd.Next(0, resultsNum); if (check[num] == null) { if (num == 0) { Console.WriteLine((i + 1) + ": " + trivia.results[0].correct_answer); check[num] = "Right"; answer = i + 1; success = true; } else { Console.WriteLine((i + 1) + ": " + trivia.results[0].incorrect_answers[num - 1]); check[num] = "Wrong"; success = true; } } } while (!success); } int get = 5; bool getCheck = false; do { try { get = Convert.ToInt32(Console.ReadLine()); } catch { Console.WriteLine("Please input valid integer."); getCheck = false; } if (get <= resultsNum && get >= 1) { getCheck = true; } } while (!getCheck); if (get == answer) { Console.WriteLine("Correct"); } else { Console.WriteLine("Incorrect"); } }
static void Main(string[] args) { string url = null; string s = null; HttpWebRequest request; HttpWebResponse response; StreamReader reader; url = "https://opentdb.com/api.php?amount=1"; request = (HttpWebRequest)WebRequest.Create(url); response = (HttpWebResponse)request.GetResponse(); reader = new StreamReader(response.GetResponseStream()); s = reader.ReadToEnd(); reader.Close(); Trivia trivia = JsonConvert.DeserializeObject <Trivia>(s); trivia.results[0].question = HttpUtility.HtmlDecode(trivia.results[0].question); trivia.results[0].correct_answer = HttpUtility.HtmlDecode(trivia.results[0].correct_answer); for (int i = 0; i < trivia.results[0].incorrect_answers.Count; ++i) { trivia.results[0].incorrect_answers[i] = HttpUtility.HtmlDecode(trivia.results[0].incorrect_answers[i]); } int totalAnswers = 0; List <string> answers = new List <string>(); //asking question Console.WriteLine(trivia.results[0].question); //adding incorrect answers to list for (int i = 0; i < trivia.results[0].incorrect_answers.Count; ++i) { answers.Add(trivia.results[0].incorrect_answers[i]); totalAnswers = i; } answers.Add("hello"); totalAnswers = totalAnswers + 1; //generating random order Random rand = new Random(); int num = rand.Next(0, totalAnswers); answers[totalAnswers] = answers[num]; answers[num] = (trivia.results[0].correct_answer); //printing out the answers for (int i = 0; i < answers.Count; ++i) { Console.WriteLine(answers[i]); } //adding extra line Console.WriteLine("\n"); //guessing string guess = Console.ReadLine(); //Were they right? if (guess == trivia.results[0].correct_answer) { //yes Console.WriteLine("Correct!"); } else { //nope Console.WriteLine("Sorry, the answer was " + trivia.results[0].correct_answer); } }