Sticks game AI problem


1 reply [Last post]
span
span's picture
Offline
My name if Forest WiBit.Nothing on Earth could stop the coding...A Coding BeautyHalfsies!W007! I watched 10 vids!We propose a toast! To you!I found a bug so fix it!I've been here since the first tree. I am beta...burning like a watchful eye...I am a moderatorI think you're gonna need a bigger VM.
Joined: 2011-05-26
Points: 1510

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();
    }

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
cbrad
cbrad's picture
Offline
...burning like a watchful eye...I am a moderatorI've been here since the first tree. I am betaMy name if Forest WiBit.Nothing on Earth could stop the coding...A Coding BeautyDriving Ms. ChickyYou maniac! You blew it up! The compiler that is.Halfsies!W007! I watched 10 vids!I don't know when to shut up.We propose a toast! To you!I found a bug so fix it!
Joined: 2011-06-14
Points: 2785

Did you ever find an answer to this?

Where you correct?