EvilZone
Programming and Scripting => Java => : blackeagle 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.
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 :)
-
Do you use comments?
-
Do you use comments?
Do you really need them? :p
-
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
-
Reminds me of my first games I made in Python,
The code isn't perfect, but it works.
Look at this line:
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?
-
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
-
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
-
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
-
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.
-
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