The game begins with the first player choosing either 1, 2, or 3. Then, the second player chooses either 1, 2 or 3 and adds that number to the number chosen by the first player to establish a total. The game continues by alternating between players, each choosing to add either 1, 2 or 3 to the total. The game is over when the total reaches 21 or higher. The player who adds the number to the total that causes the game to end is the loser.
PROCEDURE main()
{
}
main()
PROCEDURE main()
{
total <- 0
REPEAT WHILE(total <= 21)
{
# players take turns
}
# game over
}
main()
PROCEDURE main()
{
total <- 0
player <- 1
REPEAT WHILE(total <= 21)
{
# player's turn
IF(player = 1)
{
player = 2
}
ELSE
{
player = 1
}
}
# game over
}
main()
PROCEDURE read_input(player)
{
DISPLAY("Enter 1, 2, or 3 for player " + player + ": ")
x <- INPUT()
REPEAT WHILE(x != "1" AND x != "2" AND x != "3")
{
DISPLAY("Invalid Input!\n")
DISPLAY("Enter 1, 2, or 3 for player " + player + ": ")
x <- INPUT()
}
RETURN NUMBER(x)
}
PROCEDURE read_input(player)
{
DISPLAY("Enter 1, 2, or 3 for player " + player + ": ")
x <- INPUT()
REPEAT WHILE(x != "1" AND x != "2" AND x != "3")
{
DISPLAY("Invalid Input!\n")
DISPLAY("Enter 1, 2, or 3 for player " + player + ": ")
x <- INPUT()
}
RETURN NUMBER(x)
}
PROCEDURE main()
{
total <- 0
player <- 1
REPEAT WHILE(total <= 21)
{
# player's turn
total <- total + read_input(player)
IF(player = 1)
{
player = 2
}
ELSE
{
player = 1
}
}
# game over
DISPLAY("Player " + player + " wins!")
}
main()
Input | Player | Sum/Error |
---|---|---|
"1" |
1 | 1 |
"2" |
2 | 3 |
"3" |
1 | 6 |
"0" |
2 | error |
"4" |
2 | error |
"1" |
2 | 7 |
"-1" |
1 | error |
"5" |
1 | error |
"2" |
1 | 9 |
"lizard" |
2 | error |
"(*&#$&*^@" |
2 | error |
"3" |
2 | 12 |
"invalid" |
1 | error |
"3" |
1 | 15 |
"broken" |
2 | error |
"2" |
2 | 17 |
" " |
1 | error |
"1" |
1 | 18 |
"3" |
2 | 21 |
Player 1 Wins |
PROCEDURE read_input(player)
{
DISPLAY("Enter 1, 2, or 3 for player " + player + ": ")
x <- INPUT()
REPEAT WHILE(x != "1" AND x != "2" AND x != "3")
{
DISPLAY("Invalid Input!\n")
DISPLAY("Enter 1, 2, or 3 for player " + player + ": ")
x <- INPUT()
}
RETURN NUMBER(x)
}
PROCEDURE main()
{
total <- 0
player <- 1
REPEAT WHILE(total <= 21)
{
# player's turn
total <- total + read_input(player)
IF(player = 1)
{
player = 2
}
ELSE
{
player = 1
}
}
# game over
DISPLAY("Player " + player + " wins!")
}