Here is my crappy clone of Atomas, a puzzle game where you combine small atoms into more valuable ones.
# Pytomas (an Atomas build for Python)
# Author: Qwerp-Derp
from random import randint
# Initial variables
element_table = []
atom_range = [1, 3]
specials_list = ["+"] * 6 + ["-"] * 3 + ["B", "C"]
carry_over = " "
moves = 0
score = 0
# Initialises starting list
for a in range(6):
element_table.append(randint(atom_range[0], atom_range[1]))
# Processing for Plus atom
def plus_process():
global element_table
final_atom = 0
def plus_code_block():
# Main code block for plus
global element_table, final_atom, score
temp_list_start = element_table[:user_input + 1]
temp_list_end = element_table[user_input + 1:]
final_atom = temp_list_end[0]
# Continually matches two atoms together
try:
matches = 0
while temp_list_start[-1] == temp_list_end[0]:
matches += 1
score += (temp_list_end[0] * 2) + 1
if temp_list_end[0] >= final_atom and matches != 1:
final_atom += 2
else:
final_atom += 1
temp_list_start = temp_list_start[:-1]
temp_list_end = temp_list_end[1:]
if matches == 1:
final_atom += 1
except IndexError:
pass
final_matches = []
# Matches atoms on "other side" to each other
# (Since the Atomas playing field is a loop,
# and my list is linear, this is the best I can do)
if not len(temp_list_start):
while len(temp_list_end) not in [0, 1]:
if temp_list_end[0] == temp_list_end[-1]:
final_matches.append(temp_list_end[0])
temp_list_end = temp_list_end[1:-1]
else:
break
if not len(temp_list_end):
while len(temp_list_start) not in [0, 1]:
if temp_list_start[0] == temp_list_start[-1]:
final_matches.append(temp_list_start[0])
temp_list_start = temp_list_start[1:-1]
else:
break
# Figures out the score for the "final atom"
for b in range(len(final_matches)):
if final_matches[b] >= final_atom:
final_atom += 2
else:
final_atom += 1
# Makes the final gameboard
element_table = temp_list_start + [final_atom] + temp_list_end
# Configuration of how to put the + in
if len(element_table) not in [0, 1]:
if element_table[user_input] == element_table[user_input + 1] and element_table[user_input] != "+":
plus_code_block()
else:
element_table = element_table[:user_input + 1] + ["+"] + element_table[user_input + 1:]
elif user_input == len(element_table) - 1:
if element_table[user_input] == element_table[0] and element_table[user_input] != "+":
plus_code_block()
else:
element_table.append("+")
else:
element_table.append("+")
# Processing for Minus atom
def minus_process():
global carry_over, element_table
# Grabs the atom from the gameboard
minus_atom = element_table[user_input]
del element_table[user_input]
while True:
# Gets user input for turning grabbed atom
# into a Plus atom
plus_input = raw_input("Turn to plus? [y/n]: ")
if plus_input == "y":
carry_over = "+"
break
elif plus_input == "n":
carry_over = "X" + str(minus_atom)
break
else:
print("That is not a valid input. Try again.")
# Processing for Black Plus atom
def black_plus_process():
global element_table, score
# Manipulates gameboard for processing
if element_table[user_input - 1] > element_table[user_input + 1]:
black_atom = element_table[user_input - 1] + 2
else:
black_atom = element_table[user_input + 1] + 2
element_table[user_input - 1] = black_atom
element_table[user_input + 1] = black_atom
score += (black_atom * 2) + 1
plus_process()
# Processing for Clone atom
def clone_process():
global carry_over
clone_atom = element_table[user_input]
carry_over = "X " + clone_atom
# User interface
while len(element_table) < 18:
moves += 1
if moves % 15 == 0:
atom_range = [x + 1 for x in atom_range]
# Checks if a move is "carried over"
# (From the Minus/Clone atom)
if carry_over[0] == "X":
turn_atom = int(carry_over[2:])
print("X " + str(turn_atom))
elif carry_over[0] == "+":
print("+")
else:
turn_atom = randint(atom_range[0], atom_range[1])
special_check = randint(1, 30)
# Display specials
if special_check <= 11:
print(specials_list[special_check - 1])
else:
print("X " + str(turn_atom))
# Display current state of game
print(element_table)
user_input = input("Enter # (0 to " + str(len(element_table) - 1) + "): ")
# Specials check
if special_check in [1, 2, 3, 4, 5, 6] or carry_over == "+":
plus_process()
elif special_check in [7, 8, 9]:
minus_process()
elif special_check == 10 and len(element_table) not in [0, 1]:
black_plus_process()
elif special_check == 11:
clone_process()
else:
# No special
if len(element_table) not in [0, 1]:
element_table = element_table[:user_input + 1] + [turn_atom] + element_table[user_input + 1:]
else:
element_table.append(turn_atom)
# Outside of the loop
print("Game over! Your score was: " + str(score))
Is there any code in this program that can be improved (e.g. style issues, redundant code)?
Also, I'm thinking of including all of the element names in to this as well. Is the best approach to just stick it all in, or should I put it into an external file?