0

So for our computer science class, we had to write a program that randomly generates cards, i decided to do mine in batch, because im a massive noob XD I was confident that i could do it as im quite experienced with it. Even though batch isn't by any means a good 'language' if your going to call it that. I was able to fix most of the problems by myself with some hard work. I am however, still having some issues i don't know how to resolve.

My biggest issues i don't know how to fix are...

  • Text not being displayed properly.
  • Numbers (i use for the base of the AI and card generation) sometimes not being defined properly in variables.
  • The point system just refuses to work not matter what i do.
  • It sometimes randomly just flat out decides to crash on me if i skip a 'TIMEOUT' or a 'PAUSE'.
  • Some 'IF' statements not being executed properly even though there exactly the same as the other ones.

I'm sorry if this question is too broad, but i really didn't know quite how to summarize it.

Here is a link to my card game: http://pastebin.com/t2S3yWk5

Here is our question:

1) Create a program that will generate two random numbers - one that maps to a suit (hearts,diamonds, clubs or spades) and one that maps to a card (Ace, 2, 3, ..... Jack, Queen, King)

*Mine is slightly different, it generates two different suits based on two random numbers.*

2) Create a program that will generate a random card and then ask the user to go Higher,Lower or Quit. If the player chooses Higher the next card must be of a higher value or theplayer is out. Likewise for Lower.

3)Extending the previous program, the user will select a trump suit at the start of the game. If the card is a trump suit card then the game continues regardless of the card’s value. The program will keep score and will save the score to a highscore file. The user will also be able to display the current highscore file.

I would like to try and do this stuff (listed above) myself. I just need help trying to fix my existing program.

I hope that if your reading this you could give me some advise or provide solutions to some of my problems. Thanks in advance! :3

Will11600
  • 3
  • 6
  • You're going to have to add specific problems to your question, because right now it's too broad. What should your code be doing, and what is it actually doing? – SomethingDark Feb 27 '16 at 02:32
  • @Will11600 – Are you really required to create this in batch language? Or was it your choice? – miroxlav Feb 27 '16 at 10:36
  • Mirolav, i know, i was going to add a comment on that to my post. We can do it in any language we wanted. I know batch is really not the best language for this. But i don't have experience with Python or Java and i was confident i could do this in batch. I've also come very far on it and i wouldn't like to start again. – Will11600 Feb 27 '16 at 10:54
  • I meant what _specific_ problems are you encountering. For example, you say the text doesn't display correctly. What's wrong with it? _Which_ `if` statements fail? – SomethingDark Feb 27 '16 at 14:07
  • IF statements from line 119-132 don't all execute properly, during the game, you decide to play a card that will loose to your opponent or equal that of your opponent. Whats supposed to happen is, the game will take the card value of the player and compare it to the ai's card. If the ai's is higher it will say a mocking message then set the ai's points to + 2. Except for some reason i cant fathom. This doesn't happen, however when the player plays the winning card, it works fine. Because these IF statements also control the points, that also doesn't work. – Will11600 Feb 27 '16 at 16:21
  • @SomethingDark Another issue i have is with card generation, i don't understand why, but you don't always get 2 cards. You often only get one. I've tried to fix this before by adding code to check if those variables are defined, but it did nothing. Maybe you could try executing it yourself to see where the problems are. – Will11600 Feb 27 '16 at 16:31
  • I know you said you didn't want to start over, but your code does not meet any of the three requirements given by the question; I can practically guarantee that you will fail the project, even if you do fix all of the problems in your code. It may be worth using a language that you are more comfortable in to complete the assignment, because you're going to have to completely start over anyway. – SomethingDark Feb 27 '16 at 23:52
  • @SomethingDark I actually spoke to my Computer Science teacher about that, he said that it was okay. I only need to add in all of the other missing features. I cannot progress further because of these problems. – Will11600 Feb 28 '16 at 01:12
  • I'm convinced that it probably would have been easier for you to learn enough basics of Java or C# for this assignment than to deal with all the oddball stuff that Windows' batch language makes you jump through to get anything of reasonable complexity done. Kudos to SomethingDark for giving some pretty impressive detailed help. – Michael Burr Feb 28 '16 at 05:31

1 Answers1

1

Good news; nothing is super wrong with your code, it's just a bunch of little things that seem like a lot. Some off-by-one errors and a missing variable that I can only assume got replicated from copying and pasting.

Text not being displayed properly

I assume this is the "Access denied" errors that your code produces instead of the AI's comments. This is due to the fact that > and < are used for output redirection, so the emoticons you are adding are trying to create a file in a place you don't have access to. Either get rid of them (recommended) or use ^ to escape them (^>:)).

Numbers (I use for the base of the AI and card generation) sometimes not being defined properly in variables

%random% %% 5 results in one of the numbers in the set 0, 1, 2, 3, 4. You currently do not have an if statement for what should happen if 0 is randomly selected. Add an if statement or have the code go back to the top of the section if a 0 is selected.

The point system just refuses to work no matter what I do

You're going to kick yourself...

Your set statements are missing the assignment portion. SET /A %p1p% + 2 should be SET /A p1p=%p1p% + 2 and so forth (or set /a p1p+=2 if you think that looks better).

If sometimes randomly just flat out decides to crash on me if I skip a 'TIMEOUT' or 'PAUSE'

I couldn't replicate that, but the code seemed to work fine when I removed those statements.

Some 'IF' statements not being executed properly even though they're exactly the same as the other ones

Your comment indicated lines 119-132, which include the if statements that assign points. See above for why those aren't working.

Some other recommendations for your code

Your variable names should be more descriptive. For example, ctog doesn't tell me anything about what that variable should be; I can look at the code to see what it does, but without any context, that could be doing anything.

You should add the /i flag to the if statements that check which card you put down so that C1 and c1 get treated the same. On a related note, you should add a check for when the player enters something other than C1 or C2. You can even use a choice command like you did earlier.

:pvic is missing an exit command, so you automatically play again if you win. Combined with the fact that you only check if lt is equal to 2, not greater than or equal to 2, there's no way to stop playing if you win. Also on the subject of end game conditions, there's no if statement for if you tie the computer.

cp1 and num1 are effectively the same variable, there's no reason to have both (same with cp2/num2, ap1/num3, and ap2/num4).

You need some kind of goto at the end of :pc1 so that :pc2 doesn't automatically run after :pc1 finishes.

SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • Thank you so much for your help. My program now is fully working except for one thing, on lines 137-140 i attempt to un set the variables so that the player cant use the card he used again. It dosn't work. I put it as this *IF %ch% == ch1 SET %card1%=* whats wrong with this? Im going to try it without the spacing between the *==*. Thank you so much for your help. – Will11600 Feb 28 '16 at 15:34
  • @Will11600 - You're effectively telling the computer to unset a variable called "Jack" or whatever the value or card1 is. To actually clear the card1 variable, remove the `%` signs like this: `IF %ch% == ch1 SET card1=` – SomethingDark Feb 28 '16 at 15:58
  • Also, if my answer solved your problem, please click the check mark next to my answer. – SomethingDark Feb 28 '16 at 15:59
  • Thank you, i clicked the tick. Ive managed to progress much further now. Now my game has sound! :O using wscripts and midi files for that arcade feel. – Will11600 Feb 28 '16 at 16:51
  • Not sure if i can still post on here, but i made some drastic changes to card generation and the card variables aren't being set. I get a frequent 'Missing Operator' message in the program. Here is the snippet of code im talking about. http://pastebin.com/aHz8Bcnd, i don't understand what im doing wrong, please help. – Will11600 Mar 09 '16 at 18:03
  • @Will11600 - You're doing math with variables that don't exist, specifically the Threes. You need to correct the variable names. – SomethingDark Mar 09 '16 at 18:29
  • You have yet again made me feel stupid! XD I must of forgotten to change them, as the code went through many different alterations. Thank you! – Will11600 Mar 10 '16 at 13:47
  • @Will11600 - You can't open a text editor, press `CTRL+F`, and search for "Three"? Lines 81, 108, 135, and 162. – SomethingDark Mar 10 '16 at 16:35
  • Apologies, i read your initial reply wrong. I fixed it but i'm still getting that 'missing operator' dialogue and the variables still aren't being set. Is there anything else? – Will11600 Mar 10 '16 at 16:47
  • You've also got a couple of instances of `%ct%=ct=%ct% + 1`. For future investigation, open a command prompt, type `script.txt >>log.txt 2>&1` and run that about 10 times. Then open log.txt and search for "Missing operator" and you'll see roughly where the script is when it gets the error. – SomethingDark Mar 10 '16 at 16:53
  • I did what you said, i cannot however find any 'missing operator' in the logs. I'm still not able to echo the variables. – Will11600 Mar 10 '16 at 21:55
  • @Will11600 - You're not seeing any Missing Operator errors because they've been fixed. You're not finding the variables (I'm guessing the %card% variables?) because %t% is not getting incremented properly. Take another look at log.txt; %cft% is reaching 4 once it gets to the end (as it makes perfect sense to do) so %t% never gets incremented. Change `card%t%` to `card%cft%`. – SomethingDark Mar 10 '16 at 22:31
  • Im really sorry but im still not getting the variables. I have seen 'missing operator', after checking it over thoroughly. I have had a long good look at my code and i really can't find anything that may be causing this issue. Here is an updated version, with all the fixes. http://pastebin.com/TMEzC47t I would fix it myself but i have no idea whats wrong. – Will11600 Mar 11 '16 at 13:57
  • Ive managed to get it to work. For some reason 'SETLOCAL' and 'ENDLOCAL' is preventing the varibles from being set. :/ Sorry to bother you! – Will11600 Mar 11 '16 at 14:46