I'm a newbie self-learned programmer, I've started poking around a bit at C# around two months ago, since I will be starting formal programming studies in about 7 months. I heard the best way to learn how to code is to code so I've written this trivia application and I would love some constructive feedback, what are the good things? And what are the things I could improve on / have done better.
Progam.cs
namespace EasyTrivia
{
class Program
{
const bool DEBUG_MODE = false;
public static void DebugPrint(string printOut) {
if (DEBUG_MODE) { Console.WriteLine("[DEBUG]: {0}", printOut); }
}
static void Main(string[] args)
{
//PLACEHOLDER, check for command line arguments
for (int y = 0; y < args.Length; y++)
{
switch (args[y])
{
case "debug1":
Console.WriteLine("DEBUG {0} ARGUMENT", y);
break;
case "debug2":
Console.WriteLine("DEBUG {0} ARGUMENT", y);
break;
case "debug3":
Console.WriteLine("DEBUG {0} ARGUMENT", y);
break;
}
}
const int MAX_PLAYERS = 4; // TODO: Add more freedom here, so you can have any number of players
string[] pNames = new string[4];
Player[] player = new Player[4];
Console.WriteLine("How many players wish to play? (max 4)");
string input = Console.ReadLine();
int noOfPlayers;
bool parseInput = Int32.TryParse(input, out noOfPlayers); // We're doing the easy check
while (noOfPlayers > MAX_PLAYERS || noOfPlayers < 1)
{
Console.WriteLine("Incorrect input. Please enter a valid number of players inbetween 1 and 4");
input = Console.ReadLine();
parseInput = Int32.TryParse(input, out noOfPlayers);
} Console.Clear();
for (int i = 0; i < noOfPlayers; i++)
{
Console.WriteLine("Please enter the name for player {0}", i + 1);
pNames[i] = Console.ReadLine();
player[i] = new Player();
player[i].Name = pNames[i];
player[i].Score = 0;
} Console.Clear();
GameLoop(player, noOfPlayers);
}
static void GameLoop(Player[] player, int noOfPlayers)
{
bool GameState = true;
List<int> blacklist = new List<int>();
Random RNG = new Random();
const int WINNING_SCORE = 3; // TODO: Custom winning score should be configurable through commandline argument.
TriviaHandler trivia = new TriviaHandler();
trivia.InitializeQuestion();
//Begin Game Loop
while (GameState)
{
DebugPrint("Game loop initializing...");
string questionString = trivia.FetchQuestion(blacklist, RNG);
//If it's a duplicate entry FetchQuestion() will return null
while (questionString == null) { questionString = trivia.FetchQuestion(blacklist, RNG); }
Console.WriteLine(questionString);
for (int i = 0; i < noOfPlayers; i++)
{
Console.WriteLine("Player {0}, type in your answer:", i+1);
string pInput = Console.ReadLine();
if (trivia.CheckAnswer(pInput))
{
Console.WriteLine("Correct! You have been awarded 1 point");
player[i].Score += 1;
// If they get enough pts to win we gotta end the game here too.
if (player[i].Score == WINNING_SCORE)
{
Console.WriteLine("Congratulations player {0} has won the game! Would you like to play another one? (Y/N)", player[i].Name);
string choice = Console.ReadLine().ToUpper();
while (choice != "Y" && choice != "N")
{
Console.WriteLine("Incorrect input! Please press Y for yes or N for no");
choice = Console.ReadLine().ToUpper();
}
if (choice == "Y")
{
//Reset player score first
for (int z = 0; i < player.Length; i++)
{
player[z].Score = 0;
}
GameLoop(player, noOfPlayers);
}
else { Environment.Exit(0); }
}
//If the answer is correct break the loop
break;
}
else { Console.WriteLine("Incorrect Answer, question is passed on to next player"); }
}
}
}
}
}
Player.cs
namespace EasyTrivia
{
class Player
{
private string name;
public string Name { get; set; }
private int score;
public int Score { get; set; }
}
}
TriviaHandler
namespace EasyTrivia
{
class TriviaHandler
{
Question[] question = new Question[Question.GetMaxQuestions(true)];
private int qID;
public int QID { get; set; }
public void InitializeQuestion()
{
List<string> questions = new List<string>(Question.ListA);
//questions.AddRange(Question.ListA);
List<string> answers = new List<string>();
answers.AddRange(Question.ListB);
Program.DebugPrint("Initializing questions...");
for (int i = 0; i < answers.Count; i++)
{
question[i] = new Question();
question[i].Pretext = questions[i];
question[i].Answer = answers[i];
//if (options) { question[i].LoadOptions() }
}
}
public bool CheckAnswer(string input)
{
//if (input == "debug") { return true; }
if (input.ToUpper() == question[QID].Answer.ToUpper()) { return true; }
else { return false; }
}
public string FetchQuestion(List<int> blacklist, Random RNG)
{
bool blisted = false;
int QuestionID = RNG.Next(Question.counter);
if (blacklist.Contains(QuestionID)) { blisted = true; }
if (!blisted)
{
QID = QuestionID;
blacklist.Add(QuestionID);
return question[QuestionID].Pretext;
}
else { return null; }
}
}
}
Question.cs
namespace EasyTrivia
{
class Question
{
private string pretext;
private string answer;
private string[] options;
private string[] listA;
private string[] listB;
public static string[] ListA { get; set; }
public static string[] ListB { get; set; }
public string Pretext { get; set; }
public string Answer { get; set; }
public static int counter { get; set; }
public static int GetMaxQuestions(bool initialize)
{
int count = new int();
List<string> questions = new List<string>();
List<string> answers = new List<string>();
Program.DebugPrint("Getting max questions...");
XElement rawXML = XElement.Load(@"C:\Users\Gabriel\Documents\Visual Studio 2013\Projects\EasyTrivia\EasyTrivia\Questions.xml");
IEnumerable<XElement> elements = rawXML.Elements();
//So I've written three ways of fetching the Q/A data
//Method 1
//foreach (XElement question in elements)
//{
// if (initialize) { count++; }
// questions.Add(question.Element("Pretext").Value);
// answers.Add(question.Element("Answer").Value);
//}
//if (initialize)
//{
// ListA = questions.ToArray();
// ListB = answers.ToArray();
//}
//Method 2
var pQuery = from lQ in elements.Elements()
where lQ.Name == "Pretext"
select lQ.Value;
var aQuery = from aQ in elements.Elements()
where aQ.Name == "Answer"
select aQ.Value;
foreach (var pretext in pQuery)
{
if (initialize) { count++; }
questions.Add(pretext);
}
foreach (var answer in aQuery)
{
answers.Add(answer);
}
if (initialize)
{
ListA = questions.ToArray();
ListB = answers.ToArray();
}
//Method 3
//string[] lines = File.ReadAllLines("Questions.txt");
//for (int i = 0; i < lines.Length; i++)
//{
// if (lines[i].Contains("[QUESTION") == true) { count++; }
// if (initialize && lines[i].Contains("[QUESTION"))
// {
// string[] delimiter1 = lines[i + 1].Split('=');
// string[] delimiter2 = lines[i + 2].Split('=');
// questions.Add(delimiter1[1]);
// answers.Add(delimiter2[1]);
// ListA = questions.ToArray();
// ListB = answers.ToArray();
// }
//}
if (initialize) { counter = count; }
return count;
}
}
}
Questions.xml
<?xml version="1.0" encoding="utf-8" ?>
<QUESTIONS>
<Question id="1">
<Pretext>This is the first test question</Pretext>
<Answer>test1</Answer>
</Question>
<Question id="2">
<Pretext>This is the second test question</Pretext>
<Answer>test2</Answer>
</Question>
<Question id="3">
<Pretext>This is the third test question</Pretext>
<Answer>test3</Answer>
</Question>
</QUESTIONS>