✅ Keeping Track Of Previous Games, Reset On Game Close - ReWrite
According to The C# Academy, I'm supposed to have a menu option where the user can see the previous games they played. I have this option located in my EndGame class where it asks the user do they want to play again, y/n, or view the history. I have started a previous games class https://pastebin.com/KjvFDFhB but I'm not sure on how to add the 4 lists (addGame, subGame, multGame, and divGames) into the main list previousHistory nor do I know how to iterate through the main list and print everything to the screen. Secondly, in my solution checker file https://pastebin.com/i9RaDz6m I have started the process on line 16, but I have a couple of problems there too. 1) how do I get get the
int gameCount = 0;
to keep track of the number game the user is on, and 2) how do I take my string writeString
and insert it into the addGames list that is in a separate file? I know this probably sounds complicated, but to sum it up I have to be able to allow the user to see the previous games they played. I though it would be easier to keep track of the previous games in a different file, and just call that file to print the items to the console when the user chooses to at the end of the game. Thanks.156 Replies
you cant put a List<string> instance in a List<string> instance
List<string> only accepts
string
its not List<List<string>>right. they're all going to be strings.
you could create a class and name it GameHistory or something
I did
except I named it PreviousHistory
that's the first link in the main message
i know
oh my fault
but PreviousHistory will contain a collection of Previously played Games right?
right
ok so I renamed my file and class to GameHistory
dont
keep PreviousHistory
ok I reverted it
addGames and the rest, what are you trying to add to their list?
and as far as trying to do nested lists,
is giving an error on
addGames
saying it's a syntax errorthats invalid code
the
string writeString
that is located on line 16 of the second link in the main messageso an example would look like
the solution checker is setup based off the operation
+ = * /
to determine how to check the solution based off the game so I figured I could piggy back off of that to go inside the case "+"
for the addition game, have the integer gameCount and the string being written to the addGames list in the previous games
it would be a lot easier to explain if I could do a voice chat and show my screen
?
right
it's all a string
but the
previousHistory
list is sub listed by the game types
so how would I write the sub list into the main list?you can just create a class called
Game
and store these details into its Propertiesi'm lost
create a class called Game
do I delete my PreviousHistory class?
no
ok I have my game class
do u know what a property is?
a variable declared within the class like
close but thats a field
oh then no. I haven't learned getters and setters yet
then you can use fields
I know this sounds weird, but if I could show you my code/screen it would be a lot easier I guess for me. I have a
Program.cs - main file
MainMenu.cs
AdditionGame.cs
SubtractionGame.cs
MultiplicationGame.cs
DivisionGame.cs
SolutionChecker.cs
EndGame.cs
and like the program.cs is called first, then it calls main menu, then that calls to whichever game the user selects and all 4 game screens share the solution checker and end game files.
hmm you already have classes for AdditionGame and the rest?
very hard, try using paste bin
right
$paste
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
it looks like vscode, you can upload your files there
is visual studio
ah well i did tell you to use that site, guess ill have to copy paste manually
you also said that I could upload my files....
i mean onto that site
here is
Program.cs
, MainMenu.cs
, AdditionGame.cs
, SubtractionGame.cs
, MultiplicationGame.cs
and DivisionGame.cs
https://paste.mod.gg/esqhjnxmzvhb/5BlazeBin - esqhjnxmzvhb
A tool for sharing your source code with the world!
and here is
SolutionChecker.cs
, EndGame.cs
and PreviousHistory.cs
https://paste.mod.gg/tbpyzyahkjgv/2BlazeBin - tbpyzyahkjgv
A tool for sharing your source code with the world!
please don't judge my code. I'm brand new to this language
yea
we dont judge people's code here
the main reason we are here is to help people
I appreciate that. I tried really hard to make this code as efficient and clean as possible
that's why there are so many files. I mean I could've written all 4 games in the same file, but that was too much code for me to keep up with honestly
ok looking at ur code, it seems ur stuck in the SolutionChecker class, correct?
yes and no
how can I explain it
on line 16 of the solution checker I'm trying to keep up with the game count. the game being the number of questions the user has answered so if they put in that they wanted to answer 6 questions, then that would be 6 games and then I have on the next line the string that would be appeneded to the addition previous games list. So like if they entered that they wanted to answer 6 questions, then it would add each question to the list as they answered them.
like
i get it
ok good lol I'm sweating tryign to figure out how to explain this
first your gameCount is a local variable
it should be global instead?
which means it only exists in brackets between line 15 and line 25
ok I'll make it global within the class
thats also bad
oh.....
I"m sorry.
so my only other option would be to make it global within the function
not global but public
everytime you are creating a new instance of SolutionChecker when you call the StartGame method
only one game is played at a time, so what should I do with it then?
well you can probably store the gameCount as a static field in the PreviousHistory class
since it would make sense to be placed there
above or inside of the ViewHistory method?
my brain hurts >_<
u cant define a field in a method
like in python, I would just put the game count variable as a self. in the init method
what would be the c# equivalent to that
a field
idk if you speak python or not lol soryr
ok what would be an example of a field?
this?
class PreviousHistory
{
public static int gameCount = 0;
is equivalent to a global variable in python
ok I have that now
you should know how to increment it then
do u start with Game 0 or Game 1?
yeah lets keep that you should realize ur mistake soon
anyway your class PreviousHistory should be static, you dont want to create many instances of it, right?
i'd rather not screw it up already please. what do I have wrong? I'm here to learn it right the first time please
dont worry you will learn when you realize
and im here with you
😟 ok. I changed my method to static instead of public
what method?
those 2 are different things
oh the class. not the method. sorry
public static class PreviousHistory
u want it public so you can access it from anywhere else
you want it static so that you dont need to create instances of it, you can directly access the type
ok bet. got that
back to solutionchecker
ok
you want to add to the History the what you wrote in writeString, correct?
correct
first make those
as fields in the PreviousHistory class
you want them to exist forever, not temporarly
like I did the game count right?
yes
and i want those public static as well right
a local variable means that variable only exists in the method and is only accessible inside the method
and gets deleted when the method ends
sure
ok got those
is
writeString
's purpose only to write into the PreviousHistory?
?correct and what it writes is based off the game being played
writeString will get ugly if you write the same code for each operator symbol
so create a method in the PreviousHistory class that should do it for you
you can call it AddHistory
with parameters for num1, operatorSymbol, num2 and computerSolutionAdd ofcourse
ok got that
I added a parameter though for the if statement....or should I not have?
yeah cool but why not use a bool?
so instead of string rightWrong do bool rightWrong
ok changed it. I like that better lol less code
yes
you should handle each operator
and write your history to their correct list
ok one sec
note, string interpolation is a thing
https://paste.mod.gg/ncvhgoliauht/0 what about this?
BlazeBin - ncvhgoliauht
A tool for sharing your source code with the world!
that's the same as JavaScript except you wrap the string in back ticks instead. I couldn't figure that out for this language lol
ok I fixed my string interpolation
whats the point of the string with the
solution wrong
if you will never add it to the history?oh.forgot about that part. copying the switch case down
what do u mean?
BlazeBin - ocunqbkeslmp
A tool for sharing your source code with the world!
good but
I feel like there's a cleaner way to write that
yes
try to write repeated code as less as possible
example you already used switch above,
try to handle both writeStrings into the same switch statement
like that?
close, theres a better way
handle it before you enter the switch statement
ok help me. I'm drawing blanks
But, writeString feels repeated, only to modify 1 word
so id do
that's such a pretty line lol one-liners haven't been taught yet in this academy
but it makes your code cleaner
ok here we are
ud need to handle that too
Console.WriteLine("Invalid Operation Writing Game History.");
for right and wrong
I feel smart af boy lmao
do u really want it to print
Invalid Operation Writing Game History. correct
for the right answer?
what does that even mean 😅idk lol I don't know anything about error handling in c#
use if? else?
or ternary operator
ternary operator is just equivalent of if else but makes it one lined
eg this is a ternary operator
rightWrong ? "correct" : "wrong"
right. I get that. I just don't know what to put
like I feel dumb again
something like
Operation successfully added to Game History
?but if the switch case defaults, then the operation failed
oh right
didnt realize
so I put it back to
Console.WriteLine("Invalid Operation Writing Game History.");
sure
ok so we have a function for adding the statement to the correct list so what's next?
whats missing?
trying to print the history, correct?
ok so back in solution checker
when I try to call the PreviousHistory class to create an instance
it tells me
Cannot declare a variable of static type 'PreviousHistory'
like we have a way to add the games to the correct lists, but we haven't called it in the solution checker, and haven't written a way for the user to see it when they reach EndGame and it asks them do they want to see their previous gameswell its a static class, u cant create an instance of it
so delete that line
I did that and realized you said to call it directly and now where I'm calling it, it's telling me that I can't call it because of it's protection level so should the method be a public void instead of a static void?
and where am I incrementing the game count variable?
public static void
u want it to be public AND static
ok gotcha gotcha
and I'm presuming that I would increment
gameCount
inside of the AddHistory
function instead of the ViewHistory
functionthis one looks wrong
operatorSymbol + num2 ?
yep definitely the AI's doing . lol not mine
updated and fixed solution checker
https://paste.mod.gg/umkjjyglnawy/0
updated and fixed PreviousHistory class https://paste.mod.gg/zrywqxrxymhp/0
looks line so far
ok bet. so now printing it all to the screen.
nested lists
yet ur code can be cleaner
I want it to show all the games at the same time
oh? how?
and which file?
like we said
try to write repeated code as less as possible
thats why methods exist
you can clean it later tho, not now
the CheckSolution methodyep figured that file lol
you can see each case has the same code, except only 1 line is different
we will clean it later tho
ok good lol because I"m struggling
what are u missing now, printing the history?
bringing all the list down into one, then iterating that main list, and having it print like
you can define a method called PrintHistory in the PreviousHistory class
and so on
and let that method do the printing
ok I have my method
i need to do something, brb in 30 minutes
ok np
Yay!!! I was able to figure out how to get the list nested, and print the items to the screen, and I added in another option of being able to play again or exit the game from the previous games screen. Thank you so much!
back
finished already?
https://github.com/mekasu0124/MeksMathGame yes :3
the only things i missed for the academy's challenges was to
add a timer to track how long that user takes to finish the game
and create a random game option where the players will be presented with questions from random operations
if you care about performance
its a waste to create a list
history
in the print history method
you can just print directly from the listsok what do you suggest I write it as? I'm all ears
something like
btw foreach is better than a normal for loop in this context
but wouldn't that be repetative?
not as repetitive as the history code
ok I got you. I fixed it
how would I go about putting a timer? It would start when it launched one of the 4 games, and end when the game ended?
try and see what happens
I could create a timer class to handle the formatting and such
so something like
and then I have
the only problem is being able to get that timer added to the history statements
unless I write the timer directly into the games
no I can't do that. it's ok. It's just a challenge. not a requirement
I wouldn't mind to find a way to incorporate a random game that would randomly pick one of the four math equations
yea I can't figure that out either. It's a bit out of my depth. thank you bunches for your help! ❤️