StreamReader
現在のハイスコアを超えている場合にのみゲームのハイスコアを上書きするファイルを作成するにはどうすればよいですか?現在、現在のスコア/ txtファイルを超えるスコアだけでなく、すべてのスコアがリストボックスに更新されています。
前もって感謝します
public partial class Form1 : Form
{
int Dice;
int RunningTotal;
int MaxRolls;
int RollCount;
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
RollDiebutton.Enabled = true;
StartOverbutton.Enabled = true;
ClickBeginGametoStartlabel.Visible = false;
int beginTotal = 0;
TotalMoneyEarnedlabel.Text = beginTotal.ToString("c");
MaxRolls = rand.Next(3) + 2;
}
private void button4_Click(object sender, EventArgs e)
{
try
{
string highScore;
StreamReader inputFile;
inputFile = File.OpenText("Highscore.txt");
HighscoreBox.Items.Clear();
while (!inputFile.EndOfStream)
{
highScore = inputFile.ReadLine();
HighscoreBox.Items.Add(highScore);
}
inputFile.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
RollDiebutton.Enabled = false;
BeginGamebutton.Enabled = true;
StartOverbutton.Enabled = false;
TotalMoneyEarnedlabel.Text = "";
BeginpictureBox.Image = P2Kbembow.Properties.Resources.Begin;
Pressyourluckandrollagainlabel.Visible = false;
ClickBeginGametoStartlabel.Visible = true;
}
private void button2_Click(object sender, EventArgs e)
{
//Close Form
this.Close();
}
private void RollDiebutton_Click(object sender, EventArgs e)
{
Random rand = new Random();
Dice = rand.Next(6) + 1;
RunningTotal += 0;
int DollarAmount = 100;
int Sum = (Dice * DollarAmount);
BeginGamebutton.Enabled = false;
Pressyourluckandrollagainlabel.Visible = true;
RunningTotal += Sum;
TotalMoneyEarnedlabel.Text = RunningTotal.ToString("c");
RollCount += 1;
if (MaxRolls == 0)
{
Random getMax = new Random();
TotalMoneyEarnedlabel.Text = "";
}
else
if (RollCount >= MaxRolls)
{
MaxRolls = 6;
RollCount = 0;
RunningTotal = 0;
TotalMoneyEarnedlabel.Text = "$0.0";
Show(); MessageBox.Show("Sorry! You lose!");
RollDiebutton.Enabled = false;
BeginGamebutton.Enabled = true;
TotalMoneyEarnedlabel.Text = "";
BeginpictureBox.Image = P2Kbembow.Properties.Resources.Begin;
Pressyourluckandrollagainlabel.Visible = false;
ClickBeginGametoStartlabel.Visible = true;
StartOverbutton.Enabled = false;
return;
}
StreamWriter outputFile;
outputFile = File.CreateText("HighScore.txt");
outputFile.WriteLine(TotalMoneyEarnedlabel.Text);
outputFile.Close();
if (Dice == 1)
{
//shows Image of dice 1
BeginpictureBox.Image = P2Kbembow.Properties.Resources._1Die;
}
if (Dice == 2)
{
//shows Image of dice 2
BeginpictureBox.Image = P2Kbembow.Properties.Resources._2Die;
}
if (Dice == 3)
{
//shows Image of dice 3
BeginpictureBox.Image = P2Kbembow.Properties.Resources._3Die;
}
if (Dice == 4)
{
//shows Image of dice 4
BeginpictureBox.Image = P2Kbembow.Properties.Resources._4Die;
}
if (Dice == 5)
{
//shows Image of dice 5
BeginpictureBox.Image = P2Kbembow.Properties.Resources._5Die;
}
if (Dice == 6)
{
//shows Image of dice 6
BeginpictureBox.Image = P2Kbembow.Properties.Resources._6Die;
}
//Display Message Box of dice rolled
Show(); MessageBox.Show(" You rolled a " + Dice + "!");
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
string highScore;
StreamReader inputFile;
inputFile = File.OpenText("Highscore.txt");
HighscoreBox.Items.Clear();
while (!inputFile.EndOfStream)
{
highScore = inputFile.ReadLine();
HighscoreBox.Items.Add(highScore);
}
inputFile.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}