Sticks game AI problem
Hey Buddies!
I'm working on a Java lab in school where we are writing a "Sticks game". You know the game where you have a pile of sticks and then you are allowed to take 1 or 2 sticks out of the pile and the person who takes the last stick/sticks wins?
Anyway, when programming the ComputerPlayer AI I've run into a small problem. I'm not sure if the problem is that I don't understand the assignment or if I just can't figure out the algoritm to use. Here is the description of the method move() which should contain the simple algoritm.
public int move()
Makes the computerplayers move by taking sticks from the pile. For some number of sticks left, there is a winning strategy. This strategy should be used in a general way. The solution may not be based on enumerating special cases. The method returns the number of sticks left in the pile.
Ok, so that should be pretty straight-forward no? Well, the way I see it is that the winning strategy is to take only one stick if there is either four or one left. In all other cases two sticks should be subtracted. Simple enough. The question now becomes though, does an if-statement checking for these conditions count as an enumeration of special cases or could it be considered general?
Here is the method as I've written it:
/**
* Makes the move for the computerplayer
*/
public int move()
{
int num;
//TODO create AI
if(sticks.sticksLeft() == 4 || sticks.sticksLeft() == 1)
{
num = 1;
}
else
{
num = 2;
}
sticks.take(num);
return sticks.sticksLeft();
}
Forum Rules/Features
Welcome to the WiBit.net forums! Check out our terms and features:
Hot Topic(s)
Annoy your friends
Is this something you think someone else would enjoy? Is it not for you, but do you know someone who is down with this type of content? Well share away:
Tweet
The Blog
Don't Let XML Make You a Cheater
This blog is a reminder that cheating in software development can get you into big trouble. Sometimes developers get really really lazy, OR are pressured to write something using overly simplified data structures. Almost every time this happens you are bitten in the butt! Sometimes the problems show up immediately and other times it may take months or years (especially in integrated systems).
- 1 of 78
- ››


















Did you ever find an answer to this?
Where you correct?