mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 07:10:42 -08:00
MAINT: Apply pre-commit
Remove byte-order-marker pre-commit check as there would be many adjustments necessary
This commit is contained in:
@@ -1,126 +1,130 @@
|
||||
import random
|
||||
|
||||
#Class of the Game
|
||||
|
||||
# Class of the Game
|
||||
class NIM:
|
||||
def __init__(self):
|
||||
|
||||
def __init__(self):
|
||||
self.Piles = {1: 7, 2: 5, 3: 3, 4: 1}
|
||||
|
||||
self.Piles = {
|
||||
1 : 7,
|
||||
2 : 5,
|
||||
3 : 3,
|
||||
4 : 1
|
||||
}
|
||||
def Remove_pegs(self, command):
|
||||
|
||||
def Remove_pegs(self, command):
|
||||
try:
|
||||
|
||||
try:
|
||||
pile, num = command.split(",")
|
||||
num = int(num)
|
||||
pile = int(pile)
|
||||
|
||||
pile, num = command.split(',')
|
||||
num = int(num)
|
||||
pile = int(pile)
|
||||
except Exception as e:
|
||||
|
||||
except Exception as e:
|
||||
if "not enough values" in str(e):
|
||||
print(
|
||||
'\nNot a valid command. Your command should be in the form of "1,3", Try Again\n'
|
||||
)
|
||||
|
||||
if 'not enough values' in str(e):
|
||||
print('\nNot a valid command. Your command should be in the form of "1,3", Try Again\n')
|
||||
else:
|
||||
print("\nError, Try again\n")
|
||||
return None
|
||||
|
||||
else:
|
||||
print('\nError, Try again\n')
|
||||
return None
|
||||
if self._command_integrity(num, pile) == True:
|
||||
self.Piles[pile] -= num
|
||||
else:
|
||||
print("\nInvalid value of either Peg or Pile\n")
|
||||
|
||||
if self._command_integrity(num, pile) == True:
|
||||
self.Piles[pile] -= num
|
||||
else:
|
||||
print('\nInvalid value of either Peg or Pile\n')
|
||||
def get_AI_move(self):
|
||||
|
||||
def get_AI_move(self):
|
||||
|
||||
possible_pile = []
|
||||
for k,v in self.Piles.items():
|
||||
if v != 0:
|
||||
possible_pile.append(k)
|
||||
possible_pile = []
|
||||
for k, v in self.Piles.items():
|
||||
if v != 0:
|
||||
possible_pile.append(k)
|
||||
|
||||
pile = random.choice(possible_pile)
|
||||
pile = random.choice(possible_pile)
|
||||
|
||||
num = random.randint(1,self.Piles[pile])
|
||||
num = random.randint(1, self.Piles[pile])
|
||||
|
||||
return pile, num
|
||||
return pile, num
|
||||
|
||||
def _command_integrity(self, num, pile):
|
||||
def _command_integrity(self, num, pile):
|
||||
|
||||
if pile <= 4 and pile >= 1:
|
||||
if num <= self.Piles[pile]:
|
||||
return True
|
||||
|
||||
return False
|
||||
if pile <= 4 and pile >= 1:
|
||||
if num <= self.Piles[pile]:
|
||||
return True
|
||||
|
||||
def print_pegs(self):
|
||||
return False
|
||||
|
||||
for pile, peg in self.Piles.items():
|
||||
print('Pile {} : {}'.format(pile, 'O '*peg))
|
||||
|
||||
def Help(self):
|
||||
def print_pegs(self):
|
||||
|
||||
print('-'*10)
|
||||
print('\nThe Game is player with a number of Piles of Objects("O" == one peg)')
|
||||
print('\nThe Piles are arranged as given below(Tradional NIM)\n')
|
||||
self.print_pegs()
|
||||
print('\nAny Number of of Objects are removed one pile by "YOU" and the machine alternatively')
|
||||
print('\nOn your turn, you may take all the objects that remain in any pile')
|
||||
print('but you must take ATLEAST one object')
|
||||
print('\nAnd you may take objects from only one pile on a single turn.')
|
||||
print('\nThe winner is defined as the one that picks the last remaning object')
|
||||
print('-'*10)
|
||||
for pile, peg in self.Piles.items():
|
||||
print("Pile {} : {}".format(pile, "O " * peg))
|
||||
|
||||
def Checkforwin(self):
|
||||
def Help(self):
|
||||
|
||||
sum = 0
|
||||
for k,v in self.Piles.items():
|
||||
sum += v
|
||||
|
||||
if sum == 0:
|
||||
return True
|
||||
|
||||
else:
|
||||
return False
|
||||
print("-" * 10)
|
||||
print('\nThe Game is player with a number of Piles of Objects("O" == one peg)')
|
||||
print("\nThe Piles are arranged as given below(Tradional NIM)\n")
|
||||
self.print_pegs()
|
||||
print(
|
||||
'\nAny Number of of Objects are removed one pile by "YOU" and the machine alternatively'
|
||||
)
|
||||
print("\nOn your turn, you may take all the objects that remain in any pile")
|
||||
print("but you must take ATLEAST one object")
|
||||
print("\nAnd you may take objects from only one pile on a single turn.")
|
||||
print("\nThe winner is defined as the one that picks the last remaning object")
|
||||
print("-" * 10)
|
||||
|
||||
#main program
|
||||
def Checkforwin(self):
|
||||
|
||||
sum = 0
|
||||
for k, v in self.Piles.items():
|
||||
sum += v
|
||||
|
||||
if sum == 0:
|
||||
return True
|
||||
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
# main program
|
||||
if __name__ == "__main__":
|
||||
|
||||
#Game initialization
|
||||
game = NIM()
|
||||
# Game initialization
|
||||
game = NIM()
|
||||
|
||||
print('Hello, This is a game of NIM')
|
||||
help = input('Do You Need Instruction (YES or NO): ')
|
||||
print("Hello, This is a game of NIM")
|
||||
help = input("Do You Need Instruction (YES or NO): ")
|
||||
|
||||
if help == 'yes' or help == 'YES' or help == 'Yes':
|
||||
if help == "yes" or help == "YES" or help == "Yes":
|
||||
|
||||
#Printing Game Instruction
|
||||
game.Help()
|
||||
# Printing Game Instruction
|
||||
game.Help()
|
||||
|
||||
#Start game loop
|
||||
input('\nPress Enter to start the Game:\n')
|
||||
End = False
|
||||
while True:
|
||||
# Start game loop
|
||||
input("\nPress Enter to start the Game:\n")
|
||||
End = False
|
||||
while True:
|
||||
|
||||
game.print_pegs()
|
||||
game.print_pegs()
|
||||
|
||||
#Players Move
|
||||
command = input('\nYOUR MOVE - Number of PILE, Number of Object? ')
|
||||
game.Remove_pegs(command)
|
||||
End = game.Checkforwin()
|
||||
if End == True:
|
||||
print('\nPlayer Wins the Game, Congratulations!!')
|
||||
input('\nPress any key to exit')
|
||||
break
|
||||
|
||||
#Computers Move
|
||||
command = game.get_AI_move()
|
||||
print('\nA.I MOVE - A.I Removed {} pegs from Pile {}'.format(command[1],command[0]))
|
||||
game.Remove_pegs(str(command[0]) +',' + str(command[1]))
|
||||
End = game.Checkforwin()
|
||||
if End == True:
|
||||
print('\nComputer Wins the Game, Better Luck Next Time\n')
|
||||
input('Press any key to exit')
|
||||
break
|
||||
# Players Move
|
||||
command = input("\nYOUR MOVE - Number of PILE, Number of Object? ")
|
||||
game.Remove_pegs(command)
|
||||
End = game.Checkforwin()
|
||||
if End == True:
|
||||
print("\nPlayer Wins the Game, Congratulations!!")
|
||||
input("\nPress any key to exit")
|
||||
break
|
||||
|
||||
# Computers Move
|
||||
command = game.get_AI_move()
|
||||
print(
|
||||
"\nA.I MOVE - A.I Removed {} pegs from Pile {}".format(
|
||||
command[1], command[0]
|
||||
)
|
||||
)
|
||||
game.Remove_pegs(str(command[0]) + "," + str(command[1]))
|
||||
End = game.Checkforwin()
|
||||
if End == True:
|
||||
print("\nComputer Wins the Game, Better Luck Next Time\n")
|
||||
input("Press any key to exit")
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user