A simple but engaging pencil game in Java.
In this stage, the program prints two lines: one line with several vertical bar symbols representing pencils
(for example, |||
or |||||||
) and one Your turn!
string.
Even though both pencils are acceptable, you can choose to get a large pencil, or a small one, by providing a
boolean value for the argument largePencil
of the getPencil()
function
Example:
|||
Your turn!
Expanding the previous stage by getting a few detailed needed for the game initialization.
Example
How many pencils would you like to use:
> 20
Who will be the first (John, Jack):
> Jack
||||||||||||||||||||
Jack is going first!
Let's change the rules of game. Players take turns taking X
pencils until none of them remain.
Run PencilGame.java
Example 1
How many pencils would you like to use:
> 5
Who will be the first (John, Jack):
> John
|||||
John's turn:
> 2
|||
Jack's turn:
> 1
||
John's turn:
> 2
Example 2:
How many pencils would you like to use:
> 15
Who will be the first (John, Jack):
> John
|||||||||||||||
John's turn:
> 8
|||||||
Jack's turn:
> 7
In this stage, a new level of control over the game has been added. The program checks the input. If it is incorrect, the reason is printed.
Also, the allowed amount of pencils which can be taken is limited; Let's say that players can remove no more than 3 pencils at a time.
Example 1: the chosen first player is not in the list
How many pencils would you like to use:
> 5
Who will be the first (John, Jack):
> Fatma
Choose between 'John' and 'Jack'
> John
|||||
John's turn!
>
Example 2: John is the winner of the game
How many pencils would you like to use:
> 5
Who will be the first (John, Jack):
> John
|||||
John's turn!
> 3
||
Jack's turn!
> 3
Too many pencils were taken
> 2
John won!
After playing a couple of games, both
players found out that if there are 2
, 3
, or 4
pencils left on the table, the current player
automatically wins. It happens because a player can take 1
, 2
, or 3
pencils
and leave the other player with only one. The other player has nothing left
but to take the last pencil and lose the game.
The same thing happens when there are 6
, 7``, or 8
pencils left on the table.
It will eventually repeat all over again.
So, if the player chooses Jack as the first player, after that input, the bot's move should be printed.
the program is expanded. The solution for last stage, can be executed for any initial number of pencils. The second player (Jack), is the bot this time.
For each iteration, the program checks whose turn is now. If it is the bot, instead of requiring input from the second player, one line that contains the bot's move (1, 2 or 3) that follows the winning strategy, is printed.
Example 1:
How many pencils would you like to use:
> 10
Who will be the first (John, Jack):
> Jack
||||||||||
Jack's turn:
1
|||||||||
John's turn!
> 2
|||||||
Jack's turn:
2
|||||
John's turn!
> 1
||||
Jack's turn:
3
|
John's turn!
> 1
Jack won!
Example 2:
How many pencils would you like to use:
> 6
Who will be the first (John, Jack):
> John
||||||
John's turn!
> 1
|||||
Jack's turn:
2
|||
John's turn!
> 2
|
Jack's turn:
1
John won!