forked from ashwanijha04/Python-mini-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nim_improved.py
58 lines (49 loc) · 1.42 KB
/
nim_improved.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Game of Nim
def welcomeMessage():
print\
"""
This is a 2 player game.
The computer-human based game will be made soon.
There are a total of 13 sticks.
Each player choses minimum 1 and maximum 4 sticks in one turn.
The player who picks up the last stick wins.
All the best to both players.
"""
def get_players():
player1 = raw_input("What is player 1's name?")
player2 = raw_input("What is player 2's name?")
return player1, player2
def gameLogic():
totalSticks = 13
numberOfSticksPicked = 0
turn = 1
while totalSticks:
print"Number of sticks remaining: ", totalSticks
print "Player", turn, "'s turn."
numberOfSticksPicked = int(raw_input("Pick minimum 1 and maximum 4 sticks.\n"))
if numberOfSticksPicked > totalSticks\
or numberOfSticksPicked > 4\
or numberOfSticksPicked < 1:
print"That is not allowed. Try picking again."
continue
totalSticks = totalSticks - numberOfSticksPicked
if turn == 1:
turn = 2
elif turn == 2:
turn = 1
if turn == 1:
return 1
else:
return 0
def winMessage(turn,player1,player2):
if turn:
print player2 + " won."
else:
print player1 + " won."
def main():
welcomeMessage()
player1, player2 = get_players()
turn = gameLogic()
winMessage(turn,player1,player2)
#Starting the program
main()