0 Replies - 493 Views - Last Post: 27 August 2011 - 04:51 PM Rate Topic: -----

#1 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,906
  • Joined: 30-April 10

Cookies not working in coin flip game

Posted 27 August 2011 - 04:51 PM

I have a game that I am doing for my asp.net class and I cant get my cookies to work

I dont know if its not geting a value or not setting a value its just not working.

Can someone take a look and tell me what I am doing wrong?
Game
 <div id="head2">
            <asp:Label ID="coinHead" runat="server" CssClass="coinHeadLbl" 
                Text="Coin Toss Game"></asp:Label>
        </div>
        <div id="money">
            <asp:Label ID="bankLbl" runat="server" BackColor="Black" CssClass="bankLbl" 
                Text="Bank:" Font-Bold="True" ForeColor="White" Width="50px"></asp:Label>
            $<asp:Label ID="bankAmount" runat="server" CssClass="bankAmountLbl" Text="100"></asp:Label>
            <asp:Label ID="betLbl" runat="server" BackColor="Black" CssClass="betLbl" 
                Text="Bet:" Font-Bold="True" ForeColor="White" Width="50px"></asp:Label>
            $<asp:TextBox ID="betAmount" runat="server" CssClass="betAmount">0</asp:TextBox>
        </div>
        <div id="coinImg">
            <asp:Image ID="coin" runat="server" 
                ImageUrl="~/Images/coingame/heads_tails.gif" />
        </div>
        <div id="gameControls">
            <asp:Label ID="controlsLbl" runat="server" CssClass="controlsLbl" 
                Text="Make Your Selection"></asp:Label>
            <br />
            <div id="rdoButtons">
            <asp:RadioButton ID="heads" runat="server" Text="Heads" 
                    GroupName="select" Checked="True" />
            <br />
            <asp:RadioButton ID="tails" runat="server" Text="Tails"
                    GroupName="select" />
            </div>
            <asp:Button ID="playBtn" runat="server" CssClass="playBtn" 
                Font-Names="Book Antiqua" onclick="playBtn_Click" Text="Play" 
                ToolTip="Click to play" Width="76px" />
            <br />
            <asp:Label ID="statusDisplay" runat="server" Text="Did you win or lose?" 
                Font-Bold="True" Font-Size="Large"></asp:Label>
        </div>


Code behind C#
 public partial class _Default : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Response.Cookies["gameData"]["bank"] == null)
            {
                HttpCookie gameData = new HttpCookie("gameData");
                gameData.Values["bank"] = "100";
                Response.Cookies.Add(gameData);
            }
            else
            {
                if (Request.Cookies["gameData"]["bank"] != null)
                    bankAmount.Text = Response.Cookies["bank"].Value;
            }
        }
        protected void playBtn_Click(object sender, EventArgs e)
        {
            int bank = int.Parse(bankAmount.Text);

            int bet = int.Parse(betAmount.Text);

            string choice = "";

            if (heads.Checked == true)
            {
                choice = "heads";
            }
            if (tails.Checked == true)
            {
                choice = "tails";
            }

            Random randNum = new Random();
            int flip = randNum.Next(0, 2);

                string coinToss = "";

                if (flip == 0)
                {
                    coinToss = "heads";
                    coin.ImageUrl = "~/Images/coingame/heads.gif";
                }

                if (flip == 1)
                {
                    coinToss = "tails";
                    coin.ImageUrl = "~/Images/coingame/tails.gif";
                }

                if (coinToss == choice)
                {
                    statusDisplay.Text = "You Win!";

                    bank += bet;

                    bankAmount.Text = bank.ToString();
                    if(Request.Cookies["gameData"] != null)
                    {
                    Response.Cookies["bank"].Value = bank.ToString();
                    }
                }

                if (coinToss != choice)
                {
                    statusDisplay.Text = "Sorry, You Lose!";

                    bank -= bet;

                    bankAmount.Text = bank.ToString();
                    Response.Cookies["bank"].Value = bank.ToString();
                }
        }

    }


Is This A Good Question/Topic? 0
  • +

Page 1 of 1