oy...10000000 conditional statements...

Walleye

Limp Gawd
Joined
Jan 13, 2004
Messages
432
anyone know how to do this without resorting to the ugliness that is a bunch of if statements?

i need to compare 4 ints. 3 in 1 situation, and 4 in the second situation.

maybe i should explain the prompt a little better....

this is a game of the cardgame War... i'm writing the computer's AI as opposed to randomly drawing a card from his 3 hand deck.

if he goes first, then he picks highest card. (what if the 2 top cards are equal?)
if he goes second, he picks the lowest card that is still higher than the opponent's played card. (what if the opponent's played card is equal to his? another if statement..)
if he has no cards that can beat his opponent, then he picks his lowest card (again, what if the 2 bottom cards are equal?)

i have little under 6 hours to finish this... i have it working with all other prompt inputs working than this. i have it working if the computer draws a random card...

what i really need is an idea on how to write member functions "lowestOfThree" and "highestOfThree"

as for the advanced AI part, i suppose a really crappy solution would be to stick em into an array of type card with size of 4, and then organize em lowest to highest, then output the one just above the player-played card. if none are above it, then output the bottom card.... that takes care of the "lowestOfThree" idea, but doesnt take care of the "highestofthree" idea.... i still need to figure out how to play that...
 
Sounds like a school project.

Perhaps you might want to try writing it and asking if you have specific coding problems.
 
Uh, write a lot of functions?

Seriously, though, do ask more specific questions. You didn't even say what language you're doing this in.
 
Minishark said:
Uh, write a lot of functions?

Seriously, though, do ask more specific questions. You didn't even say what language you're doing this in.

doh, sorry, C++.

ya, it's a school project.

basically, it's game War. each player has 3 cards, and play till deck is exhausted. as they play, they get more cards.

human player is allowed to choose whatever cards they want. currently, machine player draws a random card of his 3 cards.


i figured out how to do this...except one item. i need to figure out how to sort an array of ints into lowest to highest. i'm googling for an example...

basically what i'm gonna do is use 2 arrays...1 of 3 cards for the highestOf idea, and one of 4 cards for the comp AI part.

3 cards array is pretty strait forward. arrange array into 3 cards lowest to highest, and return the top card of the array.

4 cards is slightly more tricky. arrange all the cards into lowest to highest, but set the one player card to another variable so it doesnt get touched. then find the player card in the array, and return player card's position + 1. if player card's position is 3, then return to 0.

the problem is, i dont know how to arrange cards in order.
 
A couple pointers:

2 arrays is wasteful. There's no reason to put the player card in the same data set as the comp cards.

Googling "sort algorithms" should return examples, but the sort that you'll be programming are really very straightforward. Grab three cards and sort them methodically, there's a 95%+ chance you'll use one of the three basic sorts. Work through it a few times to understand what you're doing and code that.
 
Whatsisname said:
Putting the card game "War" and AI together does not compute.
must be a different version. The one I know is "flip over the top card" -- I guess that's yours as well -- but it's still pretty easy to figure out what he's trying to write.
 
umm... there are multitudes of sorting algorithms

the easiest to code is bubble sort imo
 
lomn75 said:
must be a different version. The one I know is "flip over the top card" -- I guess that's yours as well -- but it's still pretty easy to figure out what he's trying to write.


yeah, that's how we always played.... but i dont' know where he's getting 3 piles from? each player only has 1. at least in my version...
 
woooooot... i finished...

least i hope so. got some wierd compile error at the end, told it screw you, deleted the make file and all the other crap, imported all the files into another new project. no compile errors, and it works perfect :D


EDIT:

unfortunately, it kicked my ass at War...
 
berky said:
umm... there are multitudes of sorting algorithms

the easiest to code is bubble sort imo

Depends on the language

in c/c++ i'd say quicksort is the easiest to write, since it's in the library for you, all you need to do is write a generic comparison function.

For lisp i'd say quicksort would be the fastest, since lisp is terrible at doing iterative and imperative actions, a bubble sort is going to be quite a bit longer in lisp than a quicksort (quicksort is 5 lines in lisp IIRC)

In the case of only 3 cards though, I'd rather just use a "custom" min-max type function, will save a lot of coding and headaches, since you always know that the size of the dataset is going to be extremely small.
 
Back
Top