Search code examples
windowsbatch-filetext-based

Windows batch card game not working properly, don't know how to fix


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


Solution

  • 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.