What is wrong with this code? (B+)
Community Forums/General Help/What is wrong with this code? (B+)
| ||
I was trying to work out the odds of correctly predicting a game similar to Play Your Cards Right. 13 cards are dealt from a standard pack of 52. 1 is shown, and 9 other follow it face down. 3 more are held in reserve below main bunch. Player predicts whether next card will be higher or lower than current card, with the option to change the card with one from the reserve pile of 3 cards, as long as they haven't all been used. One mistake and the game is lost. I wanted to know what the odds were of getting 9 predictions correct, getting the last card turned over successfully. I have run with Debug ON, not the syntax that's wrong, but it keeps crashing with a message saying "blitzcc.exe has stopped working" What's wrong? Global gp Dim card(13) Dim qc(13) Graphics 800,600,32,2 Text 400,300,"Press a Key to start...",1,1 Flip WaitKey() SeedRnd(MilliSecs()) Cls Flip Text 400,300,"Press Esc to STOP",1,1 Flip While Not KeyHit(1) .newdeal Deal() If VerifyDeal()=False Then Goto newdeal WriteResult(PlayIt$()) Cls Flip Text 400,300,"Games Played ="+gp,1,1 Flip Wend End Function Deal() For n=1 To 13 card(n)=Rnd(1,13) Next End Function Function VerifyDeal() Dim qc(13) For n=1 To 13 qc(n)=0 Next For n=1 To 13 qc(n)=qc(n)+1 Next result=True For n=1 To 13 If qc(n)>4 Then result=False Next Return result End Function Function PlayIt$() result=True changecard=11 For n=1 To 9 .again If card(n)>4 And card(n)<10 And changecard<14 Then card(n)=card(changecard) changecard=changecard+1 Goto again EndIf If card(n)<8 And card(n+1)>card(n) Then Goto OK If card(n)>7 And card(n+1)<card(n) Then Goto OK result=False .OK If result=False Then Exit Next Return result ;Once I get this code to actually work, I'd like to change this 'result' to equal n, and change the way it's Written so I can work out odds of getting each step correct. End Function Function WriteResult(result) mf1=ReadFile("PYCRdata.txt") win=ReadLine(mf1) lose=ReadLine(mf1) totalplayed=ReadLine(mf1) totalplayed=totalplayed+1 CloseFile(mf1) gp=totalplayed If result=True Then win=win+1 If result=False Then lose=lose+1 odds#=(win+lose)/win mf2=WriteFile("PYCRdata.txt") WriteLine(mf2,win) WriteLine(mf2,lose) WriteLine(mf2,totalplayed) WriteLine(mf2,"Odds of winning tending towards "+Str$(odds)) End Function |
| ||
The error I get is 'invalid stream handle' on trying to read from mf1 because the file does not exist the first time through. |
| ||
Ah, I had that too, but I created a file called "PYCRdata.txt" with simple 4 lines all containing 0. This could be it... maybe its a divide by zero fault. I'll go back to Windows desktop (never did get Blitz to work in Linux even using Wine). Rewrite the file to contain 1's and try again. EDIT Did this and the program ran for two 'games' then crashed the same way... Must be an endless loop or something. I'll look again, but sometimes fresh eyes on the job see errors that I'd never see. |
| ||
It crashes in WriteResult() for me, even after disabling the reads & writes. Probably divide by zero, as it doesnt happen with all 1s. Also, you never close the file after writing to it. |
| ||
Thanks grable! Adding a Closefile(mf2) was all it needed. Now to write the killer casino game! EDIT. Works now. It's coming in at around 13 or 14 to 1 which is what I had thought it would be. |
| ||
Happy to help, I know how sucky it is to go code blind ;) |