## News:

11 March 2016 - Forum Rules

## My strategy game

Started by InfamousKnight, September 03, 2013, 09:08:47 PM

#### InfamousKnight

I got this idea from school today. I am disappointed in the code because of the turn thing. I couldn't figure out how to switch sides every turn so I did it the bad way.

So, here is how the game is played. You have these A's that represent apples and a P that represents a poisonous apple. You could take out 1-3 apples and whoever ends up with the poisonous apple loses. So turn by turn you take 1-3 apples. There is a perfect tactic to winning this game and its a magic number but that wouldn't be any fun to tell you what it is. Unless you got some good math skills. Yeah, I played this game in Algebra 2.

Here's the code:
http://pastebin.com/huj0P3UD

The whole reason why that code is so bad is because idk how to switch sides efficiently. So is there an easier way of writing this?

#### henke37

Seems pointless to track individual cards here. And your code could use some cleanup, you have some duplicated code. Oh, and you aren't doing any input validation.

#### ETG

Look at your code. See how you're doing the same thing several times where the only difference is a number? This calls for a programming design pattern called "The Variable."

Store the player's number as an int. Use cout's ability to displany ints. I'd type you up an example, but my phone won't let me.

Also, don't ever trust user input. Figure out what the user inputed and assign an actual number to  a variable. Then you can use that in a for loop.

#### InfamousKnight

#3
The thing I'm having trouble with is keeping the sides either 1 or 2. So if player 1 entered a number then side would change to 2 and if player 2 entered a number then it would switch back to one.

I couldn't figure out any other way of doing it. I was self aware of that repetitive code. You can't just add and subtract the sides. You can't just make a condition that checks for if its 1 then make it 2 if its 2 then make it one. That would just end up with the same value every time. Making a function wouldn't make any difference either.

A side from the code, what do you think of this game? Find any glitches that are abusive? Find the perfect tactic?

#### KC

As I said several times before, use a state machine. Then seperate logic from output. And abstract data structures.

#### InfamousKnight

Quote from: KC on September 04, 2013, 03:20:52 PM
As I said several times before, use a state machine. Then seperate logic from output. And abstract data structures.

State machine? I've never heard of that. Anyways, I have computer science all year so I should get much better after the course. I'm pretty much a self taught programmer. But frankly, that doesn't work too well with me.

After 4 years of experience, I didn't learn a whole lot My first language was Batch which is a really bad start as I stated back in my How I started Programming topic.

#### KC

What schools call computer science (I don't personally acknowledge it as such) isn't likely to help you become better. You have to start doing this yourself, and do your own research. I can guarantee you that almost everyone here is self taught, so everyone can attest that.
You keep on doing the same trivial things in always the same way. That isn't likely to help you get better. Do something bigger, do it differently. But before that learn how to structure it, how to split it into independent units, and how  to use data types to your advantage. Read a few programming books.

#### LostTemplar

Quote from: InfamousKnight on September 04, 2013, 03:15:55 PM
I couldn't figure out any other way of doing it. I was self aware of that repetitive code. You can't just add and subtract the sides. You can't just make a condition that checks for if its 1 then make it 2 if its 2 then make it one. That would just end up with the same value every time. Making a function wouldn't make any difference either.

Of course you can do that:

`if(side == 1)    side = 2;else    side = 1;`

This will alternate between 1 and 2 every iteration. You could also do other fancy things to achieve this, e.g. modulo or xor.

I second the teach-yourself-notion. Programming is one of the skills you only get better at by actually doing it yourself. I majored in computer science and even there there are a lot of people who couldn't program well if their life depended on it.

#### InfamousKnight

Quote from: KC on September 04, 2013, 03:41:41 PM
What schools call computer science (I don't personally acknowledge it as such) isn't likely to help you become better. You have to start doing this yourself, and do your own research. I can guarantee you that almost everyone here is self taught, so everyone can attest that.
You keep on doing the same trivial things in always the same way. That isn't likely to help you get better. Do something bigger, do it differently. But before that learn how to structure it, how to split it into independent units, and how  to use data types to your advantage. Read a few programming books.

Maybe I should stop exposing my trivial things? I had a LOT of trouble figuring out that side thing. This isn't one my post where it is figure this out for me. I was truly lost.

And Lost Temparler(excuse if I misspelled that) I think I've tried that. Maybe it did work and I didn't realize it because I was so frustrated. But yeah, that idea did come to my mind but I think it didn't pan out. Maybe it was because I had the numbers mixed up. This happens when you don't have a plan for the program. I was reading a beginners game programming book and it said that was really important even for the simple stuff. So yes, I did read an ebook. Then again, the day I made this post was the first day of school and I was pretty stressed out.

#### BRPXQZME

we are in a horrible and deadly danger