Computer Science Canada Black Jack trouble |
Author: | jamestur [ Fri Jan 15, 2010 7:30 pm ] |
Post subject: | Black Jack trouble |
Need help, my program has multiple problems, First problem is that i can continue to hit as long as i want, also in the event the dealer busts and player total is less than or equal to 21 chip total is not changed, and the dealers final total remains the same throughout the program, and dealer can go over and still win. P.S : I'm not very good at Turing, nor am I familiar with much terms beyond a grade 10 level. Here's what I have so far, %Beginning of Program var pt, dt, pt2, dt2, num1, num2, chips, wager : int var hit : string chips := 1000 put "You have ", chips, " chips, how much do you want to wager?" loop loop % loop pt := Rand.Int (1, 11) pt2 := Rand.Int (1, 11) dt := Rand.Int (1, 11) dt2 := Rand.Int (1, 11) pt3 := Rand.Int (1, 11) num1 := pt + pt2 num2 := dt + dt2 % put "You have ", chips, " chips, how much do you want to wager?" loop get wager if wager > chips then put "You don't have that much chips." elsif wager < 1 then put "Error try again." elsif not wager > chips then exit end if end loop put "Dealer shows a ", dt put "You have a ", pt, " and a ", pt2, " your total is ", num1, ", would you like to hit?" loop loop loop get hit if hit = "y" then num1 := num1 + Rand.Int (1, 11) put "Total is now ", num1 end if if hit = "n" then exit end if end loop exit end loop loop if num2 <= 16 then num2 := num2 + Rand.Int (1, 11) put "He hits, and his total is now ", num2 exit when num2 >= 17 elsif num2 > 16 then exit end if end loop if num1 < num2 and num2 < 22 and num1 >= 22 then put "You lose!" chips := chips - wager exit elsif num1 > num2 and num1 <= 21 then chips := chips + wager put "You win!" exit end if if hit = "n" then put "Dealers total is ", num2 if num1 < num2 and num1 <= 22 and num2 <= 21 then put "" put "You lose!" chips := chips - wager elsif num1 > num2 and num1 <= 21 then chips := chips + wager put "You win!" exit end if exit end if end loop % loop % if num2 <= 16 then % num2 := num2 + Rand.Int (1, 11) % put "He hits, and his total is now ", num2 % end if % end loop put "You have ", chips, " chips, how much do you want to wager?" end loop end loop %end of program FYI, I am using Turing 4.0.5 , just in case it matters. Variables : num1 is the players current card count, num2 is the dealers. wager is amount of chips wagered self explanatory I guess. chips is the current player chip total. pt dt pt2 dt2 are the starting cards for the player and dealer . hit is the yes or no question on whether the player wants another card , also self explanatory. |
Author: | TerranceN [ Fri Jan 15, 2010 8:21 pm ] |
Post subject: | RE:Black Jack trouble |
This is just some boolean logic errors, and a lot of abuse of loops. I suggest writing what your program has to do, step by step on paper, not in actual code, but in words. This will help you see why you need or do not need some of those loops, and figure out what has to be true in order to tell if dealer went over, etc. I removed some unnecessary loops and code (the 'if hit = "n" then' part after the 'you lose' loop) and changed some boolean logic, and got this to work, so you are very close. |
Author: | jamestur [ Sat Jan 16, 2010 2:07 pm ] |
Post subject: | Re: Black Jack trouble |
Thanks for the help, I removed lots of unnecessary code and the program is working great. ![]() |