C
C#16mo ago
cgxlm

❔ (Unsolved) writing from binary file to listbox

My teacher gave me code to implement for a highscore thing with binary files but in the code she gave us theres 2 undefined methods and ive no idea what they are supposed to even do. Anyone have any idea?
34 Replies
cgxlm
cgxlm16mo ago
public static void WriteEndScore()
{
FileStream highscoreFilstream = new FileStream("highscore.bin", FileMode.Append); // allows the scores to be appeneded
BinaryWriter binWriter = new BinaryWriter(highscoreFilstream);

binWriter.Write(Convert.ToString(Game.currentUsername));
binWriter.Write(Convert.ToString(Game.currentScore));

binWriter.Flush();
binWriter.Close();
}

public static int countScores()
{
FileStream scoreFilstream = File.OpenRead("highscore.bin");
BinaryReader binaryReader = new BinaryReader(scoreFilstream);
int countScores = 0;

while (binaryReader.PeekChar() != -1)
{
string username = binaryReader.ReadString();
string score = binaryReader.ReadString();
countScores++;
}
binaryReader.Close();
return countScores;
}
public static void WriteEndScore()
{
FileStream highscoreFilstream = new FileStream("highscore.bin", FileMode.Append); // allows the scores to be appeneded
BinaryWriter binWriter = new BinaryWriter(highscoreFilstream);

binWriter.Write(Convert.ToString(Game.currentUsername));
binWriter.Write(Convert.ToString(Game.currentScore));

binWriter.Flush();
binWriter.Close();
}

public static int countScores()
{
FileStream scoreFilstream = File.OpenRead("highscore.bin");
BinaryReader binaryReader = new BinaryReader(scoreFilstream);
int countScores = 0;

while (binaryReader.PeekChar() != -1)
{
string username = binaryReader.ReadString();
string score = binaryReader.ReadString();
countScores++;
}
binaryReader.Close();
return countScores;
}
public static void ScoreArray(string[,] scoreArray)
{
FileStream highscoreFilstream = File.OpenRead("highscore.bin");
BinaryReader binaryReader = new BinaryReader(highscoreFilstream);

int arraySize = countScores();

for (int x = 0; x < arraySize; x++)
{
scoreArray[x, 0] = binaryReader.ReadString();
scoreArray[x, 1] = binaryReader.ReadString();
}
binaryReader.Close();
}

public static string[,] HighScoreArray(string[,] highscoreArray)
{
int countScore = countScores();

if (countScore > 1)
{
SortHighScore(highscoreArray, countScore);
}
return highscoreArray;
}

public static void SortHighScore(string[,] highscoreArray, int countScores)
{
string username = "";
string score = "";
for (int i = 0; i < countScores; i++)
{
for (int scoreNumber = 0; scoreNumber < countScores - 1; scoreNumber++)
{
if (Convert.ToInt32(highscoreArray[scoreNumber, 1]) < Convert.ToInt32(highscoreArray[scoreNumber + 1, 1]))
{
username = highscoreArray[scoreNumber + 1, 0];
score = highscoreArray[scoreNumber + 1, 1];

SetNextItem(highscoreArray, scoreNumber);
SetCurrentItem(highscoreArray, scoreNumber, username, score);
}
}
}
}
public static void ScoreArray(string[,] scoreArray)
{
FileStream highscoreFilstream = File.OpenRead("highscore.bin");
BinaryReader binaryReader = new BinaryReader(highscoreFilstream);

int arraySize = countScores();

for (int x = 0; x < arraySize; x++)
{
scoreArray[x, 0] = binaryReader.ReadString();
scoreArray[x, 1] = binaryReader.ReadString();
}
binaryReader.Close();
}

public static string[,] HighScoreArray(string[,] highscoreArray)
{
int countScore = countScores();

if (countScore > 1)
{
SortHighScore(highscoreArray, countScore);
}
return highscoreArray;
}

public static void SortHighScore(string[,] highscoreArray, int countScores)
{
string username = "";
string score = "";
for (int i = 0; i < countScores; i++)
{
for (int scoreNumber = 0; scoreNumber < countScores - 1; scoreNumber++)
{
if (Convert.ToInt32(highscoreArray[scoreNumber, 1]) < Convert.ToInt32(highscoreArray[scoreNumber + 1, 1]))
{
username = highscoreArray[scoreNumber + 1, 0];
score = highscoreArray[scoreNumber + 1, 1];

SetNextItem(highscoreArray, scoreNumber);
SetCurrentItem(highscoreArray, scoreNumber, username, score);
}
}
}
}
at the very botton its the SetNextItem(highscoreArray, scoreNumber); SetCurrentItem(highscoreArray, scoreNumber, username, score);
Angius
Angius16mo ago
OOF at the use of BinaryWriter and Convert, but I guess teachers be teachers and they're all stuck in 1999 Judging by the name of those methods, they're supposed to set an item to some value in the array SetNextItem is probably supposed to do something like highScoreArray[i + 1] = scoreNumber Although, scoreNumber seems to be an index in the array So rather something like highScoreArray[scoreNumber + 1] = ??? SetCurrentItem seems a bit more obvious, probably something akin to
highScoreArray[scoreNumber] = { username, score.ToString() }
highScoreArray[scoreNumber] = { username, score.ToString() }
Which is kinda atrocious in its own right But, again, teachers being shit is kind of the norm
cgxlm
cgxlm16mo ago
yeah idek she gave like me and 2 others this way of doing it as the 'harder' way and honestly i dont understand arrays much cause she skipped over that when teaching so i just try and avoid them as much as possible. Ill try putting something like those you gave in to see
Angius
Angius16mo ago
Arrays are super simple tbh An array is a collection of items That's it
cgxlm
cgxlm16mo ago
yeah like i understand what it is i just dk how to use them properly
Angius
Angius16mo ago
To store multiple of the same kind of item That's their one and only use
cgxlm
cgxlm16mo ago
Yeah i mean as in like the actual code like what i write to use them, like its mainly the 2d arrays that idk
Angius
Angius16mo ago
0 1 2 3 4
0 a|b|c|d|e
1 f|g|h|i|j
2 k|l|m|n|o
0 1 2 3 4
0 a|b|c|d|e
1 f|g|h|i|j
2 k|l|m|n|o
arr[0, 2] will refer to k in this case That's all about a 2D array
cgxlm
cgxlm16mo ago
Ohh k when i was trying to learn them in like the theory books she gave us it was so confusing
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
cgxlm
cgxlm16mo ago
Ok so i have a new error now
cgxlm
cgxlm16mo ago
cgxlm
cgxlm16mo ago
cgxlm
cgxlm16mo ago
the scoreboard for some reason keeps repeating admin: 2 whn it should only be there once
private void Score_Load(object sender, EventArgs e)
{
listBox1.Items.Clear();
listBox1.SelectionMode = SelectionMode.None;
int scoresAmount = Tools.countScores();

// Create a new array for blackjack scores
string[,] blackjackScores = new string[scoresAmount, 2];

// Populate the blackjack scores array
Tools.ScoreArray(blackjackScores);

// Sort the blackjack scores in descending order
blackjackScores = Tools.HighScoreArray(blackjackScores);

// Display the top 5 scores in the list box
for (int i = 0; i < 5 && i < blackjackScores.GetLength(0); i++)
{
string username = blackjackScores[i, 0];
string score = blackjackScores[i, 1];
listBox1.Items.Add($"{username}:\t {score}\n");
}
}
private void Score_Load(object sender, EventArgs e)
{
listBox1.Items.Clear();
listBox1.SelectionMode = SelectionMode.None;
int scoresAmount = Tools.countScores();

// Create a new array for blackjack scores
string[,] blackjackScores = new string[scoresAmount, 2];

// Populate the blackjack scores array
Tools.ScoreArray(blackjackScores);

// Sort the blackjack scores in descending order
blackjackScores = Tools.HighScoreArray(blackjackScores);

// Display the top 5 scores in the list box
for (int i = 0; i < 5 && i < blackjackScores.GetLength(0); i++)
{
string username = blackjackScores[i, 0];
string score = blackjackScores[i, 1];
listBox1.Items.Add($"{username}:\t {score}\n");
}
}
and thats the code for putting it in the listbox the other methods are up above
cgxlm
cgxlm16mo ago
BlazeBin - wcsrmzqpvwba
A tool for sharing your source code with the world!
cgxlm
cgxlm16mo ago
i cant get it to work no matter what i do ive been trying the past like 2 hours nearly hello
Angius
Angius16mo ago
Use the debugger, see what blackjackScores are
cgxlm
cgxlm16mo ago
by like putting in something like this? MessageBox.Show(Convert.ToString(blackjackScores));
Angius
Angius16mo ago
$debug
MODiX
MODiX16mo ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
cgxlm
cgxlm16mo ago
Angius
Angius16mo ago
Expand it
cgxlm
cgxlm16mo ago
They all say null
cgxlm
cgxlm16mo ago
Angius
Angius16mo ago
Step forward Does Tools.ScoreArray() fill the array with values?
cgxlm
cgxlm16mo ago
this is the ScoreArray method
cgxlm
cgxlm16mo ago
cgxlm
cgxlm16mo ago
its filled with the correct values but it displays it wrong just
cgxlm
cgxlm16mo ago
it must be something to do with the sorting then cause after that line it goes to this
Angius
Angius16mo ago
Probably, yes
cgxlm
cgxlm16mo ago
public static void SetNextItem(string[,] HighScores, int ScoreNo)
{
for (int i = HighScores.GetLength(0) - 1; i > ScoreNo; i--)
{
HighScores[i, 0] = HighScores[i - 1, 0];
HighScores[i, 1] = HighScores[i - 1, 1];
}
}
public static void SetNextItem(string[,] HighScores, int ScoreNo)
{
for (int i = HighScores.GetLength(0) - 1; i > ScoreNo; i--)
{
HighScores[i, 0] = HighScores[i - 1, 0];
HighScores[i, 1] = HighScores[i - 1, 1];
}
}
its this method is where it goes wrong
Angius
Angius16mo ago
¯\_(ツ)_/¯ I'll be honest, using a 2D array here instead of good and proper array of classes/records/structs muddies the waters a little too much for my liking Use the debugger and see ig
cgxlm
cgxlm16mo ago
alright thanks for trying though
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.