patternpythonModerate
Script for a Civil War game
Viewed 0 times
scriptwargameforcivil
Problem
I'm new to Python, and pretty much programming/scripting. I'm just looking for someone to critique my methods and tell me if there are more efficient ways to do things.
```
#!/usr/bin/env python3
#
#
# A quick script to create a civil war game
############################
# DECLARE GLOBAL VARIABLES #
############################
### Armies = [Inf,Cav,Art] ###
global compdefmult
global compattmult
global compchoice
global userdefmult
global userattmult
global userchoice
comparmy = [100,50,10]
userarmy = [100,50,10]
##################
# IMPORT MODULES #
##################
import os
from random import randint
##########################
# DEFINE GLOBAL FUNCTIONS #
###########################
def clearscreen():
os.system('clear')
def newstart():
clearscreen()
print ('''
CCCC IIIII V V IIIII L W W W AA RRRR
C I V V I L W W W A A R R
C I VV I L W W W AAAA RRRR
CCCC IIIII VV IIIII LLLL WW WW A A R R
This is Civil War!
You will have Infantry, Calvary and Artillery to win the battles you need to turn the tide of the war.
Now, General, take your place in history!
''')
def printstatus():
print ('Your total Infantry are:' , userarmy[0])
print ('Your total Calvary are:' , userarmy[1])
print ('Your total Artillery are:' , userarmy[2])
print ('')
print ('Computers total Infantry are:' , comparmy[0])
print ('Computers total Calvary are:' , comparmy[1])
print ('Computers total Artillery are:' , comparmy[2])
print ('')
mainmenu()
####################
# MAIN BATTLE CODE #
####################
def compdigger():
global compchoice
global compdefmult
global compattmult
compchoice = randint(1,3)
if compchoice == 1:
if comparmy[0] == 0:
compdigger()
else:
compchoice = "Infantry"
compdefmult = 3
compattmult = 3
elif compchoice == 2:
```
#!/usr/bin/env python3
#
#
# A quick script to create a civil war game
############################
# DECLARE GLOBAL VARIABLES #
############################
### Armies = [Inf,Cav,Art] ###
global compdefmult
global compattmult
global compchoice
global userdefmult
global userattmult
global userchoice
comparmy = [100,50,10]
userarmy = [100,50,10]
##################
# IMPORT MODULES #
##################
import os
from random import randint
##########################
# DEFINE GLOBAL FUNCTIONS #
###########################
def clearscreen():
os.system('clear')
def newstart():
clearscreen()
print ('''
CCCC IIIII V V IIIII L W W W AA RRRR
C I V V I L W W W A A R R
C I VV I L W W W AAAA RRRR
CCCC IIIII VV IIIII LLLL WW WW A A R R
This is Civil War!
You will have Infantry, Calvary and Artillery to win the battles you need to turn the tide of the war.
Now, General, take your place in history!
''')
def printstatus():
print ('Your total Infantry are:' , userarmy[0])
print ('Your total Calvary are:' , userarmy[1])
print ('Your total Artillery are:' , userarmy[2])
print ('')
print ('Computers total Infantry are:' , comparmy[0])
print ('Computers total Calvary are:' , comparmy[1])
print ('Computers total Artillery are:' , comparmy[2])
print ('')
mainmenu()
####################
# MAIN BATTLE CODE #
####################
def compdigger():
global compchoice
global compdefmult
global compattmult
compchoice = randint(1,3)
if compchoice == 1:
if comparmy[0] == 0:
compdigger()
else:
compchoice = "Infantry"
compdefmult = 3
compattmult = 3
elif compchoice == 2:
Solution
As mentioned by @abarnert, you're using infinite recursion to "repeat forever". By default, after you've recursed more than 1000 times, your program will throw an error. The better way to do this would be to use a
You can also use a dictionary of functions containing actions rather than chaining
I'd also recommend using docstrings to document how your code works. They're very important, because they describe how your code works. Without them, code can be understandable, but can be very hard to understand. Here's an example:
Your
Finally, I'd recommend reading PEP8, Python's official style guide. It essentially tells you how your code should be formatted, and how it should look. If you want to check your code for PEP8 issues, you can use this online checker.
while condition loop. In this case, your takeaction function would become this:def take_action() -> None:
"""
Add a description of take_action here.
"""
while True:
...You can also use a dictionary of functions containing actions rather than chaining
if condition statements. Usually it's looks better. Here's a basic example of what that could look like:def my_action() -> None:
"""
Description
"""
...
actions = {
"action": my_action
}
user_input = raw_input()
if user_input in actions:
actions[user_input]()I'd also recommend using docstrings to document how your code works. They're very important, because they describe how your code works. Without them, code can be understandable, but can be very hard to understand. Here's an example:
def my_function(args) -> None:
"""
Add a description of the function here.
"""
...Your
clearscreen() function isn't very portable, and will only work on linux-based systems. I'd recommend using something like this instead:def clear_screen():
os.system("cls" if os.name == "nt" else "clear")Finally, I'd recommend reading PEP8, Python's official style guide. It essentially tells you how your code should be formatted, and how it should look. If you want to check your code for PEP8 issues, you can use this online checker.
Code Snippets
def take_action() -> None:
"""
Add a description of take_action here.
"""
while True:
...def my_action() -> None:
"""
Description
"""
...
actions = {
"action": my_action
}
user_input = raw_input()
if user_input in actions:
actions[user_input]()def my_function(args) -> None:
"""
Add a description of the function here.
"""
...def clear_screen():
os.system("cls" if os.name == "nt" else "clear")Context
StackExchange Code Review Q#91600, answer score: 11
Revisions (0)
No revisions yet.