- #1
shivajikobardan
- 674
- 54
- Homework Statement
- Designing HTML+CSS for 23 toothpicks game
- Relevant Equations
- None
The project is a game of 23 toothpicks.
The game of “23” is a two-player game that begins with a pile of 23 toothpicks. Players take turns, withdrawing either 1,2, or 3 toothpicks at a time. The player to withdraw the last toothpick loses the game. Write a human vs. computer program that plays “23”. The human should always move first. When it is the computer’s turn, it should play according the following rules:
a. If there are more than 4 toothpicks left, the computer should withdraw 4 – X toothpicks, where X is the number of toothpicks the human withdrew on the previous turn.
b. If there are 2 to 4 toothpicks left, the computer should withdraw enough toothpicks to lave 1.
c. If there is 1 toothpick left, the computer has to take it and loses.
When the human player enters the number of toothpicks to withdraw, the program should perform input validation. Make sure the entered number is between 1 and 3 and that the player is not trying to withdraw more toothpicks than exist in the pile.
I have written the code for it using console.log. But I want to convert it into a full fledged application just for the kick and dopamine.
I'm unable to plan CSS for this. Can you suggest me some ideas? Like how should I design my css? I mean something like how should the UI look?
The game of “23” is a two-player game that begins with a pile of 23 toothpicks. Players take turns, withdrawing either 1,2, or 3 toothpicks at a time. The player to withdraw the last toothpick loses the game. Write a human vs. computer program that plays “23”. The human should always move first. When it is the computer’s turn, it should play according the following rules:
a. If there are more than 4 toothpicks left, the computer should withdraw 4 – X toothpicks, where X is the number of toothpicks the human withdrew on the previous turn.
b. If there are 2 to 4 toothpicks left, the computer should withdraw enough toothpicks to lave 1.
c. If there is 1 toothpick left, the computer has to take it and loses.
When the human player enters the number of toothpicks to withdraw, the program should perform input validation. Make sure the entered number is between 1 and 3 and that the player is not trying to withdraw more toothpicks than exist in the pile.
I have written the code for it using console.log. But I want to convert it into a full fledged application just for the kick and dopamine.
JavaScript:
let total_toothpicks = 23;
let pass_toothpick_to_computer;
let counter = 0;
let currentPlayer = "human";
function switchPlayer(currentPlayer) {
return (currentPlayer = currentPlayer === "human" ? "computer" : "human"); //added a return statement here
}
function playGame(currentPlayer) {
if (currentPlayer === "human") {
counter++;
console.log(counter + "human iteration");
let user_choosed_toothpicks = Number(
prompt("Pick 1,2 or 3 toothpicks at a time")
);
total_toothpicks -= user_choosed_toothpicks;
console.log("Human choosed" + user_choosed_toothpicks + "toothpicks");
console.log("total number of toothpicks=", total_toothpicks);
pass_toothpick_to_computer = user_choosed_toothpicks;
if (total_toothpicks === 0) {
console.log("Human loses the game");
}
currentPlayer = "computer";
}
if (currentPlayer === "computer") {
counter++;
console.log(counter + "computer iteration");
if (total_toothpicks > 4) {
total_toothpicks = total_toothpicks - 4 + pass_toothpick_to_computer;
console.log(
"Computer takes" + Number(4 - pass_toothpick_to_computer) + "toothpicks"
);
console.log("total number of toothpicks=", total_toothpicks);
} else if (total_toothpicks >= 2 && total_toothpicks <= 4) {
switch (total_toothpicks) {
case 2:
total_toothpicks -= 1;
console.log("Computer takes 1 toothpicks");
console.log("total number of toothpicks=", total_toothpicks);
break;
case 3:
total_toothpicks -= 2;
console.log("Computer takes 2 toothpicks");
console.log("total number of toothpicks=", total_toothpicks);
break;
case 4:
total_toothpicks -= 3;
console.log("Computer takes 3 toothpicks");
console.log("total number of toothpicks=", total_toothpicks);
break;
default:
break;
}
if (total_toothpicks === 0) {
total_toothpicks -= 1;
console.log("Computer loses the game");
}
}
currentPlayer = "human";
}
}
while (total_toothpicks > 0) {
playGame(currentPlayer);
}