Programming and Scripting > Java

21 Sticks game

(1/2) > >>

blackeagle:
The rules of the game are simple. You start with 21 sticks, and two players take turns either taking one or two sticks. The player who takes the last stick loses.


--- Code: ---
import java.util.Scanner;

public class TwentyOneSticks {

    public static boolean whoStart(String choice) {
        int ran = (int) (Math.random() * 2 + 1);
        String ht = "";
        switch (ran) {
            case 1:
                ht = "head";
                break;
            case 2:
                ht = "tails";
        }
        if (ht.equals(choice.toLowerCase())) {
            System.out.println("you start first");
            return true;
        } else {
            System.out.println("computer start first");
            return false;
        }
    }

    public static int playerTurn(int numberOfSticks) {
        System.out.println("there are " + numberOfSticks + "sticks");
        System.out.println("how many sticks you wanna take 1 or 2");
        Scanner in = new Scanner(System.in);
        int sticksToTake = in.nextInt();
        while ((sticksToTake != 1) && (sticksToTake != 2)) {
            System.out.println("you can only take 1 or 2 sticks");
            System.out.println("how many sticks you wanna take");
            sticksToTake = in.nextInt();
        }
        numberOfSticks -= sticksToTake;
        return numberOfSticks;
    }

    public static int computerTurn(int numberOfSticks) {
        int sticksToTake;
        System.out.println("there are " + numberOfSticks + "sticks");
        if ((numberOfSticks - 2) % 3 == 0 || (numberOfSticks - 2 == 0)) {
            sticksToTake = 1;
            numberOfSticks -= sticksToTake;
        } else {
            sticksToTake = 2;
            numberOfSticks -= sticksToTake;
        }
        System.out.println("computer took " + sticksToTake + " stick");
        return numberOfSticks;

    }

    public static boolean checkWinner(int turn, int numberOfSticks) {
        if ((turn == 1) && (numberOfSticks <= 0)) {
            System.out.println("player lost");
            return true;
        }
        if ((turn == 2) && (numberOfSticks <= 0)) {
            System.out.println("player won");
            return true;
        }
        return false;

    }

    public static void main(String args[]) {
        int turn;
        int numberOfSticks = 21;
        Scanner in = new Scanner(System.in);
        System.out.println("choose head or tails to see who starts first");
        String choice = in.next();
        if (whoStart(choice) == true) {
            do {
                turn = 1;
                numberOfSticks = playerTurn(numberOfSticks);
                if (checkWinner(turn, numberOfSticks) == true) {
                    break;
                };

                turn = 2;
                numberOfSticks = computerTurn(numberOfSticks);
                checkWinner(turn, numberOfSticks);
            } while (numberOfSticks > 0);
        } else {
            do {
                turn = 2;
                numberOfSticks = computerTurn(numberOfSticks);
                if (checkWinner(turn, numberOfSticks) == true) {
                    break;
                };
                turn = 1;
                numberOfSticks = playerTurn(numberOfSticks);
                checkWinner(turn, numberOfSticks);
            } while (numberOfSticks > 0);
        }
    }

}

--- End code ---

feel free to comment on my code any place i could make better let me know  yep i know i still have a lot to learn but i managed to get here by taking advices from good programmers so theres no shame to ask you to check on my code
thx in advance :)

proxx:
Do you use comments?

HTH:

--- Quote from: proxx on June 14, 2015, 10:59:38 pm ---Do you use comments?

--- End quote ---

Do you really need them? :p

blackeagle:
nop i don't !! but yes i should start using comments just to get used to it !!
it'll make things easier for any1 reading my code

HardCodedShadow:
Reminds me of my first games I made in Python,
The code isn't perfect, but it works.

Look at this line:

--- Code: ---if ((numberOfSticks - 2) % 3 == 0 || (numberOfSticks - 2 == 0))
--- End code ---

The second condition is useless, because 0 % 3 is still equal 0.
Anyway good job, try to use different classes in future projects. (For short programs like these they are not necessary)

Also, if it may assure you, I don't use comments as well.
If your variables and functions are properly named, I don't need them to read the code.

Did you think what may happen if the user entered a non-desired input?

Navigation

[0] Message Index

[#] Next page

Go to full version