samedi 4 juin 2016

Not creating a text file to each user in C++

I have completed almost everything in my program. I just need to fix one problem.

These are the requirements for my program:

  • For each username, create a folder containing their name outside of the program
  • In each folder, create a text file
  • In the text file, input some data from the program (I'm doing dates accessed the program)
  • Display the data back to the user

I have figured out how to do all of these things, but I am not creating a text file to each user. I have to send all the code from a Word document because it won't open up if I send the program. I have only included Blackjack because I am not creating files in those games.

This is the code I have so far:

namespace Capstone_Project { public partial class EnterName : Form {

    public EnterName()
    {
        InitializeComponent();
    }

    string fileread;
    private void btnEnter_Click(object sender, EventArgs e)
    {
        string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Logins\\";
        Save_Scores1 ss = new Save_Scores1();
        if (Directory.Exists(path + txtName.Text))//returning p[layer
        {
            ChooseGame exists = new ChooseGame("Welcome back " + txtName.Text + " !!!");
            exists.Show();

           // this.Hide();
        }
        else//new player
        {
            Directory.CreateDirectory(path + txtName.Text);
        ss.addscores(txtName.Text);
            ChooseGame dne = new ChooseGame("Welcome " + txtName.Text + " !!!");
            dne.Show();
           // this.Hide();
        }

        //ss.addscores(txtName.Text);

    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void mnuExit_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }




}

}

namespace Capstone_Project { public partial class ChooseGame : Form { string namePlayer; public ChooseGame(string name) { InitializeComponent(); namePlayer = name; }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btnBlackjack_Click_1(object sender, EventArgs e)
    {
        Reach_21.Blackjack blackjack = new Reach_21.Blackjack();
        blackjack.Show();
        //this.Hide();
    }

    private void btnMadlibs_Click_1(object sender, EventArgs e)
    {
        MadLibs1.EnterWords madlibs = new MadLibs1.EnterWords();
        madlibs.Show();
        //this.Hide();
    }

    private void btnTicTacToe_Click_1(object sender, EventArgs e)
    {
        Connect4.LoginScreenConnectFour connect4 = new Connect4.LoginScreenConnectFour();
        connect4.Show();
       // this.Hide();
    }

    private void ChooseGame_Load_1(object sender, EventArgs e)
    {
        lblName1.Text = namePlayer;
    }

    private void btnHangman_Click_1(object sender, EventArgs e)
    {
        Hangman_Game.Form1 hangman = new Hangman_Game.Form1();
        hangman.Show();
        //this.Hide();
    }

    private void seeScoresToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Save_Scores scores = new Save_Scores();

    }
}

}

namespace Reach_21 { public partial class Blackjack : Form { int sumP1; int sumP2; bool bustP1, bustP2;

    public Blackjack()
    {
        InitializeComponent();
        InitAll();

    }
    bool blnGameOver = false;

        int playerTurn = 1;
        int hitCounter = 0;



        int[] cards = new int[52];
        List<int> deck = new List<int>(); 

        //int sumP1; int sumP2;
        //bool bustP1, bustP2;



    private void mnuExit_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Do you really want to quit the game?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
            this.Close();

    }

    private void mnuBegin_Click(object sender, EventArgs e)
    {



        InitAll();

    }



    private void btnDeal_Click(object sender, EventArgs e)
    {
        Random rNum = new Random();
        deck.Clear();

        for (int i = 0; i < 52; i++)
        {
            cards[i] = rNum.Next(1, 12);
        }

        deck.AddRange(cards);

        if (playerTurn == 1)
        {
            bustP1 = false;

            lblCard1p1.Text = ""; lblCard2p1.Text = ""; lblCard3p1.Text = ""; lblCard4p1.Text = ""; lblCard5p1.Text = "";
            lblCard1p2.Text = ""; lblCard2p2.Text = ""; lblCard3p2.Text = ""; lblCard4p2.Text = ""; lblCard5p2.Text = "";
            lblPointsP1.Text = "Points: ";
            lblPointsP2.Text = "Points: ";

            sumP1 = deck[0];
            sumP1 += deck[1];

            lblCard1p1.Text = deck[0].ToString();
            lblCard2p1.Text = deck[1].ToString();
            lblPointsP1.Text = "Points: " + " " + sumP1.ToString();

            Hit21(sumP1);

        }
        else
        {
            sumP2 = deck[5] + deck[6];

            bustP2 = false;
            lblCard1p2.Text = deck[5].ToString();
            lblCard2p2.Text = deck[6].ToString();
            lblPointsP2.Text = "Points: " + " " + sumP2.ToString();

            Hit21(sumP2);

        }
        btnDeal.Enabled = false;
        btnHit.Enabled = true;
        btnStand.Enabled = true;

    }

    private void btnHit_Click(object sender, EventArgs e)
    {
        hitCounter++;

        if (playerTurn == 1)
        {
            switch (hitCounter)
            {
                case 1:
                    lblCard3p1.Text = deck[2].ToString();                       
                    sumP1 += deck[2];
                    break;
                case 2:
                    lblCard4p1.Text = deck[3].ToString();
                    sumP1 += deck[3];
                    break;
                case 3:
                    lblCard5p1.Text = deck[4].ToString();
                    sumP1 += deck[4];                        
                    break;
            }

            lblPointsP1.Text = "Points: " + " " + sumP1.ToString();

            if (hitCounter == 3)
            {
                bustP1 = PlayerBust(sumP1);
                if (bustP1 == true)
                {
                    hitCounter = 0;
                    playerTurn = 2;
                    lblPointsP1.Text = "BUSTED!!!!!!!!!!!";
                    lblWinner.Text = "PLAYER 2 WINS!";
                    lblPlayer2.BackColor = Color.LawnGreen;
                    lblPlayer1.BackColor = Color.Red;
                    btnDeal.Enabled = true;
                    btnHit.Enabled = false;
                    btnStand.Enabled = false;
                    MessageBox.Show("Player 2 wins...Click 'BEGIN NEW GAME' in the menu");
                    blnGameOver = true;
                }
                hitCounter = 0;
                playerTurn = 2;
                btnDeal.Enabled = true;
                btnHit.Enabled = false;
                btnStand.Enabled = false;
            }

            else if (PlayerBust(sumP1) == true)
            {
                bustP1 = true;
                hitCounter = 0;
                playerTurn = 1;
                lblPointsP1.Text = "BUSTED!!!!!!!!!!!";
                lblWinner.Text = "PLAYER 2 WINS!";
                lblPlayer2.BackColor = Color.LawnGreen;
                lblPlayer1.BackColor = Color.Red;
                btnDeal.Enabled = true;
                btnHit.Enabled = false;
                btnStand.Enabled = false;
                MessageBox.Show("Player 2 wins...Click 'BEGIN NEW GAME' in the menu");
                blnGameOver = true;

            }

            if (Hit21(sumP1))
            {
                hitCounter = 0;
                playerTurn = 1;
                lblPointsP1.Text = "21!!!!!!!!!";
                lblWinner.Text = "PLAYER 1 WINS!";
                lblPlayer1.BackColor = Color.LawnGreen;
                lblPlayer2.BackColor = Color.Red;
                btnDeal.Enabled = true;
                btnHit.Enabled = false;
                btnStand.Enabled = false;
                MessageBox.Show("Player 1 wins...Click 'BEGIN NEW GAME' in the menu");
                blnGameOver = true;

            }                
        }
        else
        {
            switch (hitCounter)
            {
                case 1:
                    lblCard3p2.Text = deck[7].ToString();
                    sumP2 += deck[7];
                    break;
                case 2:
                    lblCard4p2.Text = deck[8].ToString();
                    sumP2 += deck[8];
                    break;
                case 3:
                    lblCard5p2.Text = deck[9].ToString();
                    sumP2 += deck[9];
                    playerTurn = 1;
                    btnDeal.Enabled = true;
                    btnHit.Enabled = false;
                    btnStand.Enabled = false;
                    break;
            }

            lblPointsP2.Text = "Points: " + " " + sumP2.ToString();

            if (hitCounter == 3)
            {
                bustP2 = true;
                if (bustP2 == true)
                {
                    hitCounter = 0;
                    playerTurn = 1;
                    lblPointsP1.Text = "BUSTED!!!!!!!!!!!";
                    lblWinner.Text = "PLAYER 1 WINS!";
                    lblPlayer1.BackColor = Color.LawnGreen;
                    lblPlayer2.BackColor = Color.Red;
                    btnDeal.Enabled = true;
                    btnHit.Enabled = false;
                    btnStand.Enabled = false;
                    MessageBox.Show("Player 1 wins...Click 'BEGIN NEW GAME' in the menu");
                    blnGameOver = true;
                }                   
                hitCounter = 0;
                playerTurn = 1;
                btnDeal.Enabled = true;
                btnHit.Enabled = false;
                btnStand.Enabled = false;
                Tokens();
            }

            else if (PlayerBust(sumP2) == true)
            {                    
                hitCounter = 0;
                playerTurn = 1;
                lblPointsP2.Text = "BUSTED!!!!!!!!!!!";
                lblWinner.Text = "PLAYER 1 WINS!";
                lblPlayer1.BackColor = Color.LawnGreen;
                lblPlayer2.BackColor = Color.Red;
                btnDeal.Enabled = true;
                btnHit.Enabled = false;
                btnStand.Enabled = false;                   
                blnGameOver = true;

                Tokens();
             }

            if (Hit21(sumP2))
            {
                hitCounter = 0;
                playerTurn = 1;
                lblPointsP2.Text = "21!!!!!!!!!";
                lblWinner.Text = "PLAYER 2 WINS!";
                lblPlayer2.BackColor = Color.LawnGreen;
                lblPlayer1.BackColor = Color.Red;
                btnDeal.Enabled = true;
                btnHit.Enabled = false;
                btnStand.Enabled = false;
                MessageBox.Show("Player 2 wins...Click 'BEGIN NEW GAME' in the menu");
                blnGameOver = true;

                Tokens();                    
            }
        }
    }

    private bool PlayerBust(int cardSum)
        {
            if (cardSum > 21 )
                return true;
            else 
                return false;
        }
        private bool Hit21(int cardSum) 
        {
            if (cardSum == 21)
                return true;
            else
                return false;
        }
        private void Tokens()
        {
            if (bustP1 == false & sumP1 == sumP2)
            {
                lblWinner.Text = "Tie. No Winner";
                lblPlayer1.BackColor = Color.DarkCyan;
                lblPlayer2.BackColor = Color.DarkCyan;
                MessageBox.Show("No one wins...Click 'BEGIN NEW GAME' in the menu");
                blnGameOver = true;

            }
            else if (bustP2 == true & bustP1 == false)
            {
                lblWinner.Text = "PLAYER 1 WINS!";
                lblPlayer1.BackColor = Color.LawnGreen;
                lblPlayer2.BackColor = Color.Red;


            }
            else if (bustP1 == true & bustP2 == false)
            {
                lblWinner.Text = "PLAYER 2 WINS!";
                lblPlayer2.BackColor = Color.LawnGreen;
                lblPlayer1.BackColor = Color.Red;
            }
            else if (bustP1 == false & bustP2 == false & sumP1 > sumP2)   
            {
                lblWinner.Text = "PLAYER 1 WINS!";
                lblPlayer1.BackColor = Color.LawnGreen;
                lblPlayer2.BackColor = Color.Red;
                MessageBox.Show("Player 1 wins...Click 'BEGIN NEW GAME' in the menu");
                blnGameOver = true;

            }
            else if (bustP1 == false & bustP1 ==false & sumP2 > sumP1)
            {                
                lblWinner.Text = "PLAYER 2 WINS!";
                lblPlayer2.BackColor = Color.LawnGreen;
                lblPlayer1.BackColor = Color.Red;
                MessageBox.Show("Player 2 wins...Click 'BEGIN NEW GAME' in the menu");
                blnGameOver = true;

            }


        }

        private void btnStand_Click(object sender, EventArgs e)
        {
            if (playerTurn == 1)
                playerTurn = 2;
            else 
            {
                playerTurn = 1;
                Tokens();
            }

            hitCounter = 0;
            btnDeal.Enabled = true;
            btnHit.Enabled = false;
            btnStand.Enabled = false;
        }

    public void InitAll()
    {
        lblCard1p1.Text = ""; lblCard2p1.Text = ""; lblCard3p1.Text = ""; lblCard4p1.Text = ""; lblCard5p1.Text = "";
        lblCard1p2.Text = ""; lblCard2p2.Text = ""; lblCard3p2.Text = ""; lblCard4p2.Text = ""; lblCard5p2.Text = "";
        lblPointsP1.Text = "Points: ";
        lblPointsP2.Text = "Points: ";

        lblPlayer1.BackColor = Color.WhiteSmoke;
        lblPlayer2.BackColor = Color.WhiteSmoke;
        lblWinner.Text = "";

        btnDeal.Enabled = true;
        btnHit.Enabled = false;
        btnStand.Enabled = false;

         playerTurn = 1;
         hitCounter = 0;

        sumP1 = 0;
        sumP2 = 0;
        bustP1 = false; bustP2 = false;
    }

    private void datesAccessedToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Capstone_Project.Save_Scores1 dateAccessed = new Capstone_Project.Save_Scores1();
        dateAccessed.Show();


    }








        }


    }

namespace Capstone_Project { public partial class Save_Scores1 : EnterName { List < string> lines = new List();

    public Save_Scores1()
    {
        InitializeComponent();
     //   addscores("dummy");
    }



    public void addscores(string un)
    {

        string basePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

        string fname = basePath + "\\Logins" + "\\PlayerScore.txt";
        File.Create(fname);

        FileStream fs = new FileStream(fname, FileMode.Append, FileAccess.Write, FileShare.None);
        StreamWriter sw = new StreamWriter(fs);
        DateTime now = DateTime.Now;

        sw.WriteLine(now.ToString());






        sw.Close();
        fs.Close();
    }


    public void openscores(string un)
    {

        string basePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        //    string fname = basePath + "\\Logins\\" + un  + "//PlayerScore.txt";
        string fname = basePath + "\\Logins"  + "//PlayerScore.txt";

        //     File.Create(fname);

       FileStream fs = new FileStream(fname, FileMode.Open, FileAccess.Read, FileShare.None);
        using (StreamReader r = new StreamReader(fs))
        {
            // 3
            // Use while != null pattern for loop
            string line;
            while ((line = r.ReadLine()) != null)
            {
                // 4
                // Insert logic here.
                // ...
                // "line" is a line in the file. Add it to our List.
                lines.Add(line);
            }
        }

        //       List<byte> bText = new List<byte>();
        //  byte[] bText = new byte[fs.Length]; //array of bytes, size of file
        //           fs.Read(bText, 0, bText);   //read file contents
        //       red = System.Text.Encoding.ASCII.GetString(bText);
        fs.Close();   //close file stream so others may use the file




    }
    public void CheckName(string playerName)
    {
        string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Logins\\";

        if (Directory.Exists(path + playerName))//returning p[layer
        {


        }
        else//new player
        {
            Directory.CreateDirectory(path + playerName);

        }


    }


    public void btnDateAccessed_Click(object sender, EventArgs e)
    {
        openscores("dummy");
        lstDates.Items.Clear();

        foreach (string s in lines)
        {
            // Console.WriteLine(s);
            lstDates.Items.Add(s);

        }
    //    lstDates.Items.Clear();

    }
}

}

Please provide me with the code to fix my program. Thanks!

Aucun commentaire:

Enregistrer un commentaire