Question with repeating numbers
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Question with repeating numbers

  1. #1
    Join Date
    Dec 2009
    Posts
    3

    Question with repeating numbers

    I am sorry I can not think of a better title. I am writing just a simple blackjack game using Visual Web Developer 2008 Express (using C#). The problem I am running into is that even after a card has been used, it will repeat it sometimes. I am right now in the testing stages so thankfully the code is short I am also only copying the code that actually gets touch to help out more.)

    This is the Default.aspx

    <script language="javascript" type="text/javascript">
    // <!CDATA[

    //this will be my hit button. This will get one card.
    function Button1_onclick() {
    WebService.thecard(oncomplete, onerror)
    }

    function onerror(arg) {

    alert("ERROR - " + arg);
    }

    function oncomplete(arg) {
    alert(arg);
    var pathfile;
    pathfile = "C" + arg + ".png";
    document.getElementById('CallRequest').innerHTML = document.getElementById("playercard1").alt;

    if (document.getElementById("playercard1").alt == "") {
    document.getElementById("playercard1").src = pathfile;
    document.getElementById("playercard1").alt = arg;
    }
    else if (document.getElementById("playercard2").alt == "") {
    document.getElementById("playercard2").src = pathfile;
    document.getElementById("playercard2").alt = arg;
    }
    else if (document.getElementById("playercard3").alt == "") {
    document.getElementById("playercard3").src = pathfile;
    document.getElementById("playercard3").alt = arg;
    }
    else if (document.getElementById("playercard4").alt == "") {
    document.getElementById("playercard4").src = pathfile;
    document.getElementById("playercard4").alt = arg;
    }
    else if (document.getElementById("playercard5").alt == "") {
    document.getElementById("playercard5").src = pathfile;
    document.getElementById("playercard5").alt = arg;
    }
    else if (document.getElementById("playercard6").alt == "") {
    document.getElementById("playercard6").src = pathfile;
    document.getElementById("playercard6").alt = arg;
    }
    }

    //this is my reset button. This will set all cards to true when clicked and get rid of all cards on the screen
    function Button2_onclick() {
    WebService.reset(oncomplete1, onerror1)
    }

    function onerror1(arg) {
    alert("ERROR - " + arg);
    }

    function oncomplete1(arg) {



    document.getElementById("playercard1").src = "";
    document.getElementById("playercard1").alt = "";
    document.getElementById("playercard2").src = "";
    document.getElementById("playercard2").alt = "";
    document.getElementById("playercard3").src = "";
    document.getElementById("playercard3").alt = "";
    document.getElementById("playercard4").src = "";
    document.getElementById("playercard4").alt = "";
    document.getElementById("playercard5").src = "";
    document.getElementById("playercard5").alt = "";
    document.getElementById("playercard6").src = "";
    document.getElementById("playercard6").alt = "";

    }


    ***************************************************************************
    Now this next part is the WebServices page
    ***************************************************************************

    [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService {

    public WebService () {

    //Uncomment the following line if using designed components
    //InitializeComponent();
    }

    //this is when the Button1 is clicked
    [WebMethod]
    public int thecard() {
    //setting up the varibles
    bool validcard = true;
    int card;
    card = 0;


    //gets a random number between 1 - 52
    card = deal.BlackJackBrain.RandomNumber();

    //checks to see if the card has been used already
    validcard = deck.Deck.deckofcards(card);

    //if the card is already used, get a new card
    if (validcard == false)
    {
    thecard();
    }

    return card;
    }

    [WebMethod]
    public void reset()
    {
    deck.Deck.resetdeck();

    }
    }


    *************************************************************************
    Here is the blackjackbrain class.
    *************************************************************************

    namespace deal
    {
    /// <summary>
    /// This is everything that deals with giving the cards
    /// </summary>
    public class BlackJackBrain : System.Web.Services.WebService
    {

    //calculates a random number between 1 to 52
    public static int RandomNumber()
    {
    Random cardnumber = new Random();
    int thenumber = cardnumber.Next(52) + 1;
    return thenumber;
    }
    }


    **************************************************************************
    Last is the Deck class
    **************************************************************************

    namespace deck
    {
    /// <summary>
    /// This is all attributes for the deck. This will let me know
    /// if we have already used the card.
    /// When the game is over, I will call this again to reset the cards
    /// </summary>
    public class Deck : System.Web.Services.WebService
    {
    // this is the deck, I am using deckcards[1] - deckcards[52] only
    // I am leaving the rest as buffers and simplicity
    public static bool[] deckcards = new bool[53];

    // this method checks if the card has been used
    public static bool deckofcards(int x)
    {
    bool validcard = false;

    //sets the status of the card to validcard
    validcard = deckcards[x];

    // if the card has not been used, set it to false
    if (validcard == true)
    {
    deckcards[x] = false;
    }

    //returns if the card has been used or not
    return validcard;
    }

    //this sets all cards to true, showing none have been used
    public static void resetdeck()
    {
    int i = 0;

    for (i = 0; i < 53; i++)
    {
    deckcards[i] = true;
    }

    }
    }
    }


    Can someone please help me figure out why it would reuse a card? I am confused because I am re-writing this code to not use the AJAX Update Panel properties (My original copy that does use the AJAX update panel works perfect with this code and doesn't repeat any cards).

    Thank you

    Isaac_624

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Question with repeating numbers

    try change
    Code:
    //if the card is already used, get a new card
    if (validcard == false)
    {
      thecard();
    }
    into
    Code:
    if (validcard == false)  //or just simply:  if (!validcard)
    {
      return thecard();
    }
    PS, In future, please use codeTags ( [code ] your code here [/ code], but then without the spaces)

  3. #3
    Join Date
    Dec 2009
    Posts
    3

    Re: Question with repeating numbers

    Thanks for helping out. I actually found out what was wrong before the post went up. But I am running into another problem with this program, and after I can figure this out, it will be done. I am having problems with the javascript making sure that the dealer hits ONLY when it is below 17. At times, it will work fine, then all of a sudden, it will hit well beyond 17.

    Hopefully, I will post this correct for the forums. (Sorry, I am new for this forum)


    Code:
    //global varibales
    var dealercomplete = 0;
    var currentdealertotal = 0;
    var wins = 0;
    var losses = 0;
    var draws = 0;
    
    //This is the Hit Button
    function hitButton_onclick() {
        playersturn();
    }
    
    //This is the Stay Button
    function stayButton_onclick() {
        
    //I created this to see how many times I went through testing the code.  This is how I knew
    //my totals were off
        var totalnow = 0;
        totalnow = parseInt(document.getElementById("Label1").innerHTML);
        totalnow +=1;
        document.getElementById("Label1").innerHTML = totalnow;
        
    //this disables the hit and stay buttons and lets the program know it is only the dealer now
        endgame();
        dealercomplete = 1;
    
    //this flips the first card over
        document.getElementById("dealercard1").src = document.getElementById("dealercard1").alt;
        StartDealer();
    }
    
    //this is the new game button
    function newgameButton_onclick() {
        var i = 0;
    //I added the for loops to slow the game down a little, ensuring all cards get in right place    
        thereset();
        for (i = 0; i < 10000; i++) {
        }
        playersturn();
        for (i = 0; i < 10000; i++) {
        }
        dealersturn();
        for (i = 0; i < 10000; i++) {
        }
        playersturn();
        for (i = 0; i < 10000; i++) {
        }
        dealersturn();
     
    //this re-enable the buttons and disables newgamebutton
        document.getElementById("hitButton").disabled = "";
        document.getElementById("stayButton").disabled = "";
        document.getElementById("newgameButton").disabled = "disabled";
     
    }
    
    //This is to cover errors
    function onerror(arg) {
    
        alert("ERROR -  " + arg);
    }
    
    //This resets all values back to new for the C# code
    function thereset() {
        WebService.reset(resetoncomplete, onerror)
    }
    
    //this resets the images and dealercomplete
    function resetoncomplete(arg) {
    
        dealercomplete = 0;
        document.getElementById("playercard1").src = "";
        document.getElementById("playercard1").alt = "";
        document.getElementById("playercard2").src = "";
        document.getElementById("playercard2").alt = "";
        document.getElementById("playercard3").src = "";
        document.getElementById("playercard3").alt = "";
        document.getElementById("playercard4").src = "";
        document.getElementById("playercard4").alt = "";
        document.getElementById("playercard5").src = "";
        document.getElementById("playercard5").alt = "";
        document.getElementById("playercard6").src = "";
        document.getElementById("playercard6").alt = "";
        document.getElementById("playercard7").src = "";
        document.getElementById("playercard7").alt = "";
        document.getElementById("dealercard1").src = "";
        document.getElementById("dealercard1").alt = "";
        document.getElementById("dealercard2").src = "";
        document.getElementById("dealercard2").alt = "";
        document.getElementById("dealercard3").src = "";
        document.getElementById("dealercard3").alt = "";
        document.getElementById("dealercard4").src = "";
        document.getElementById("dealercard4").alt = "";
        document.getElementById("dealercard5").src = "";
        document.getElementById("dealercard5").alt = "";
        document.getElementById("dealercard6").src = "";
        document.getElementById("dealercard6").alt = "";
        document.getElementById("dealercard7").src = "";
        document.getElementById("dealercard7").alt = "";
    
    }
    
    //****************THIS IS EVERYTHING FOR THE PLAYER ONLY*****************
    
    //this is the player getting a card
    function playersturn() {
        WebService.thecard(playeroncomplete, onerror)
    }
    
    //When the return complete happens for the player, this function is called
    function playeroncomplete(card) {
        
        //if 54 is returned, that means it is a duplicate and calls for another card
        if (card == 54) {
            playersturn();
        }
    
        //if it is any other number, then complete
        //I am using the alt to see if there is a card there or not
        else {
    
            var pathfile;
            pathfile = "Pictures/C" + card + ".png";
    
            if (document.getElementById("playercard1").alt == "") {
                document.getElementById("playercard1").src = pathfile;
                document.getElementById("playercard1").alt = card;
            }
            else if (document.getElementById("playercard2").alt == "") {
                document.getElementById("playercard2").src = pathfile;
                document.getElementById("playercard2").alt = card;
            }
            else if (document.getElementById("playercard3").alt == "") {
                document.getElementById("playercard3").src = pathfile;
                document.getElementById("playercard3").alt = card;
            }
            else if (document.getElementById("playercard4").alt == "") {
                document.getElementById("playercard4").src = pathfile;
                document.getElementById("playercard4").alt = card;
            }
            else if (document.getElementById("playercard5").alt == "") {
                document.getElementById("playercard5").src = pathfile;
                document.getElementById("playercard5").alt = card;
            }
            else if (document.getElementById("playercard6").alt == "") {
                document.getElementById("playercard6").src = pathfile;
                document.getElementById("playercard6").alt = card;
            }
            else if (document.getElementById("playercard7").alt == "") {
                document.getElementById("playercard7").src = pathfile;
                document.getElementById("playercard7").alt = card;
            }
        }
        //calculate the players total and put it in the total label
        gettotal("player", card);
    }
    //calls the getthetotal which takes a string element and the card
    function gettotal(person, card) {
        WebService.getthetotal(person, card, totalcomplete, onerror);
    }
    
    // When the total comes back, put it in the player total label
    function totalcomplete(handtotal) {
        document.getElementById("playerTotalLabel").innerHTML = handtotal;
        
    //if player busted, then end the game there
         if (handtotal > 21) {
            endgame();
            winner();
        }
    }
    
    
    
    //*******************THIS IS EVERYTHING FOR DEALER*************************
    
    
    //calls the function retrieving the dealers current total
    function StartDealer() {
    
        WebService.dealertotal(dealertotalnow, onerror);
                
    }
    
    //!@#$%^ THIS IS WHERE THE ERROR IS HAPPENING!!!!!!!&^%$#
    //
    // if dealer total less than 17, get another card, if not, determine winner
    
    function dealertotalnow(dealercurrenttotalnow) {
    
        if (dealercurrenttotalnow < 17) {
            return dealersturn();
        }
        else {
            return winner();
        }
        
        
    }
    
    //dealer gets a new card
    function dealersturn() {
        WebService.thecard(dealeroncomplete, onerror)
    }
    
    //this works the same as players
    function dealeroncomplete(card) {
    
        if (card == 54) {
            dealersturn();
        }
        else {
    
            var pathfile;
            pathfile = "Pictures/C" + card + ".png";
    
            if (document.getElementById("dealercard1").alt == "") {
                document.getElementById("dealercard1").src = "Pictures/b2fv.png";
                //This is for when the card needs to be flipped up when
                //player decides to stay.
                document.getElementById("dealercard1").alt = pathfile;
            }
            else if (document.getElementById("dealercard2").alt == "") {
                document.getElementById("dealercard2").src = pathfile;
                document.getElementById("dealercard2").alt = card;
            }
            else if (document.getElementById("dealercard3").alt == "") {
                document.getElementById("dealercard3").src = pathfile;
                document.getElementById("dealercard3").alt = card;
            }
            else if (document.getElementById("dealercard4").alt == "") {
                document.getElementById("dealercard4").src = pathfile;
                document.getElementById("dealercard4").alt = card;
            }
            else if (document.getElementById("dealercard5").alt == "") {
                document.getElementById("dealercard5").src = pathfile;
                document.getElementById("dealercard5").alt = card;
            }
            else if (document.getElementById("dealercard6").alt == "") {
                document.getElementById("dealercard6").src = pathfile;
                document.getElementById("dealercard6").alt = card;
            }
            else if (document.getElementById("dealercard7").alt == "") {
                document.getElementById("dealercard7").src = pathfile;
                document.getElementById("dealercard7").alt = card;
            }
        }
    
        //calculate the players total and put it in the total label
        getdealertotal("dealer", card);
    }
    
    //calls the getthetotal which takes a string element and the card
    //this updates the dealers current hand total
    
    function getdealertotal(person, card) {
        WebService.getthetotal(person, card, dealertotalcomplete, onerror);
    }
    
    // When the total comes back, if it is only the dealer going, go back to the start of dealer
    function dealertotalcomplete(handtotal) {
    
        
    
                if (dealercomplete == 1 ) {
                   return StartDealer();
    
                } 
            
            
    }
    
    
    
    //***************This is seeing WINS, LOSSES AND DRAWS***********************
    
    //this disables the hit and stay button
    function endgame() {
        document.getElementById("hitButton").disabled = "disabled";
        document.getElementById("stayButton").disabled = "disabled";
        document.getElementById("newgameButton").disabled = "";
    }
    
    //calls the C# code and returns if player won, loss, draw
    function winner() {
        WebService.Winner(winnercomplete, onerror);
    }
    
    //determine which label to update
    function winnercomplete(decision) {
    
        //alert(decision);
        if (decision == "lost") {
            LossHand();
        }
        if (decision == "won") {
            WonHand();
        }
        if (decision == "draw") {
            DrawsHand();
        }
    }
    
    //these update the labels
    function LossHand() {
        var thetotal = 0;
        thetotal = parseInt(document.getElementById("totalLossesLabel").innerHTML);
        thetotal++;
        document.getElementById("totalLossesLabel").innerHTML = thetotal;
    }
    
    function WonHand() {
        var thetotal = 0;
        thetotal = parseInt(document.getElementById("totalWinsLabel").innerHTML);
        thetotal++; 
        document.getElementById("totalWinsLabel").innerHTML = thetotal;
    }
    
    function DrawsHand() {
        var thetotal = 0;
        thetotal = parseInt(document.getElementById("totalDrawsLabel").innerHTML);
        thetotal++;
        document.getElementById("totalDrawsLabel").innerHTML = thetotal;
    }

    I have been looking over this code now for 5 hours and I still can not figure out why Dealer is not working right. This is my first attempt working with Javascript and I am confused why the dealer is not working right.

    Thank you for your help

  4. #4
    Join Date
    Dec 2009
    Posts
    3

    Re: Question with repeating numbers

    <I have figured out why the dealer was going past 17>
    Last edited by Isaac_624; December 28th, 2009 at 04:26 PM. Reason: figured out problem

  5. #5
    Join Date
    Dec 2009
    Posts
    3

    Re: Question with repeating numbers

    Last Question for this project that I was working on.

    In this Blackjack game, lets assume that the following are true:

    Dealer has 2 cards
    Player has 4 cards (and still less than 21)
    Wins is set to 4
    Losses is set to 1
    Draws is set to 3

    If the user refreshes the page or leaves the page and comes back later (without closing the browser), is there anyway to resume the page back to this state (Dealer 2 cards, and Player 4 cards?)? If so, is there any articles that explain this? I am using Visual Web Developer 2008 Express, C# Language, Javascript. I am trying to avoid using the Microsoft .NET AJAX built in.

    I have already figured out how to get the Wins, Losses and Draws set up correctly for this example.

    Thanks

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center