mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Python: Fix Flake8 issue E712 + PEP8 variable naming
This commit is contained in:
@@ -4,11 +4,9 @@ import random
|
||||
# Class of the Game
|
||||
class NIM:
|
||||
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:
|
||||
|
||||
pile, num = command.split(",")
|
||||
@@ -26,39 +24,35 @@ class NIM:
|
||||
print("\nError, Try again\n")
|
||||
return None
|
||||
|
||||
if self._command_integrity(num, pile) == True:
|
||||
self.Piles[pile] -= num
|
||||
if self._command_integrity(num, pile):
|
||||
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():
|
||||
for k, v in self.piles.items():
|
||||
if v != 0:
|
||||
possible_pile.append(k)
|
||||
|
||||
pile = random.choice(possible_pile)
|
||||
|
||||
num = random.randint(1, self.Piles[pile])
|
||||
num = random.randint(1, self.piles[pile])
|
||||
|
||||
return pile, num
|
||||
|
||||
def _command_integrity(self, num, pile):
|
||||
|
||||
if pile <= 4 and pile >= 1:
|
||||
if num <= self.Piles[pile]:
|
||||
if num <= self.piles[pile]:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def print_pegs(self):
|
||||
|
||||
for pile, peg in self.Piles.items():
|
||||
for pile, peg in self.piles.items():
|
||||
print("Pile {} : {}".format(pile, "O " * peg))
|
||||
|
||||
def Help(self):
|
||||
|
||||
def help(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")
|
||||
@@ -72,10 +66,9 @@ class NIM:
|
||||
print("\nThe winner is defined as the one that picks the last remaning object")
|
||||
print("-" * 10)
|
||||
|
||||
def Checkforwin(self):
|
||||
|
||||
def check_for_win(self):
|
||||
sum = 0
|
||||
for k, v in self.Piles.items():
|
||||
for k, v in self.piles.items():
|
||||
sum += v
|
||||
|
||||
if sum == 0:
|
||||
@@ -85,46 +78,45 @@ class NIM:
|
||||
return False
|
||||
|
||||
|
||||
# main program
|
||||
if __name__ == "__main__":
|
||||
|
||||
def main():
|
||||
# Game initialization
|
||||
game = NIM()
|
||||
|
||||
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":
|
||||
|
||||
# Printing Game Instruction
|
||||
game.Help()
|
||||
if help.lower() == "yes":
|
||||
game.help()
|
||||
|
||||
# Start game loop
|
||||
input("\nPress Enter to start the Game:\n")
|
||||
End = False
|
||||
end = False
|
||||
while True:
|
||||
|
||||
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:
|
||||
game.remove_pegs(command)
|
||||
end = game.check_for_win()
|
||||
if end:
|
||||
print("\nPlayer Wins the Game, Congratulations!!")
|
||||
input("\nPress any key to exit")
|
||||
break
|
||||
|
||||
# Computers Move
|
||||
command = game.get_AI_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:
|
||||
game.remove_pegs(str(command[0]) + "," + str(command[1]))
|
||||
end = game.check_for_win()
|
||||
if end:
|
||||
print("\nComputer Wins the Game, Better Luck Next Time\n")
|
||||
input("Press any key to exit")
|
||||
break
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user