Author Topic: 21 Sticks game  (Read 2302 times)

0 Members and 1 Guest are viewing this topic.

Offline blackeagle

  • Serf
  • *
  • Posts: 49
  • Cookies: -2
    • View Profile
21 Sticks game
« on: June 14, 2015, 08:29:47 pm »
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: [Select]

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

}

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 :)
« Last Edit: June 14, 2015, 08:32:20 pm by blackeagle »

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: 21 Sticks game
« Reply #1 on: June 14, 2015, 10:59:38 pm »
Do you use comments?
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline HTH

  • Official EZ Slut
  • Administrator
  • Knight
  • *
  • Posts: 395
  • Cookies: 158
  • EZ Titan
    • View Profile
Re: 21 Sticks game
« Reply #2 on: June 14, 2015, 11:02:08 pm »
Do you use comments?

Do you really need them? :p

<ande> HTH is love, HTH is life
<TurboBorland> hth is the only person on this server I can say would successfully spitefuck peoples women

Offline blackeagle

  • Serf
  • *
  • Posts: 49
  • Cookies: -2
    • View Profile
Re: 21 Sticks game
« Reply #3 on: June 14, 2015, 11:23:03 pm »
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

Offline HardCodedShadow

  • /dev/null
  • *
  • Posts: 18
  • Cookies: 0
    • View Profile
Re: 21 Sticks game
« Reply #4 on: July 08, 2015, 02:26:04 am »
Reminds me of my first games I made in Python,
The code isn't perfect, but it works.

Look at this line:
Code: [Select]
if ((numberOfSticks - 2) % 3 == 0 || (numberOfSticks - 2 == 0))
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?
You may have seen me once, but you'll never forget who I am, even though I'm nobody.

Offline blackeagle

  • Serf
  • *
  • Posts: 49
  • Cookies: -2
    • View Profile
Re: 21 Sticks game
« Reply #5 on: July 14, 2015, 02:32:58 pm »
Did you think what may happen if the user entered a non-desired input?

yep i did . do i need a try and catch here ? if yes i still didn't read about it

Offline HardCodedShadow

  • /dev/null
  • *
  • Posts: 18
  • Cookies: 0
    • View Profile
Re: 21 Sticks game
« Reply #6 on: July 15, 2015, 02:03:53 am »
If you enter a value like  1.337, which is a double, it will throw an exception.
Here's a fix: http://stackoverflow.com/questions/2143797/how-to-deal-with-accidental-input-of-a-double-when-using-nextint-in-java
« Last Edit: July 15, 2015, 02:04:28 am by HardCodedShadow »
You may have seen me once, but you'll never forget who I am, even though I'm nobody.

Offline blackeagle

  • Serf
  • *
  • Posts: 49
  • Cookies: -2
    • View Profile
Re: 21 Sticks game
« Reply #7 on: July 15, 2015, 09:00:27 pm »
If you enter a value like  1.337, which is a double, it will throw an exception.
Here's a fix: http://stackoverflow.com/questions/2143797/how-to-deal-with-accidental-input-of-a-double-when-using-nextint-in-java

yep thats what i told you i'm not familiar with the try and catch yet

SOSA

  • Guest
Re: 21 Sticks game
« Reply #8 on: July 18, 2015, 03:52:21 pm »
You did a really good job for a beginner. Here's my feedback (not hating):

1. I crashed your code by entering ^. The problem was, scanner was looking for an integer not a character. When I entered a character it didn't have anything to catch the exception. You can fix this by adding a try and catch statement to your code.

try {

//your code
} catch (exception e) {

System.out.println("caught");
}

2. Just comment a little better

Overall incredible job.

Offline blackeagle

  • Serf
  • *
  • Posts: 49
  • Cookies: -2
    • View Profile
Re: 21 Sticks game
« Reply #9 on: July 20, 2015, 01:47:37 pm »
You did a really good job for a beginner. Here's my feedback (not hating):

1. I crashed your code by entering ^. The problem was, scanner was looking for an integer not a character. When I entered a character it didn't have anything to catch the exception. You can fix this by adding a try and catch statement to your code.

try {

//your code
} catch (exception e) {

System.out.println("caught");
}

2. Just comment a little better

Overall incredible job.

yep i already know