ich hab hier ein tic-tac-toe spiel erstellt, funktioniert soweit auch. Für Anregungen was ich besser machen kann oder Sachen die ich vermeiden sollte, wär ich wie immer sehr erfreut.

Code: Alles auswählen
from random import randint
fields = [' * ', ' * ', ' * ', ' * ', ' * ', ' * ', ' * ', ' * ', ' * ']
def position(mark, position_field, turn, cpu_level):
if fields[position_field] != ' * ' :
if turn == 'player':
print 'not possible'
player(cpu_level)
if turn == 'cpu':
cpu(cpu_level)
else:
print
fields[position_field] = mark
print ' '.join(fields[0:3])
print ' '.join(fields[3:6])
print ' '.join(fields[6:9])
print
print '-----------'
print
check_winner(turn, mark, cpu_level)
def player(cpu_level):
mark = ' X '
turn = 'player'
position_field = int(raw_input('take position: '))-1
if position_field >= 9 or position_field < 0:
print 'Position to high!'
player(cpu_level)
else:
position(mark, position_field, turn, cpu_level)
def cpu(cpu_level):
mark = ' O '
turn = 'cpu'
if cpu_level == 'l':
cpu_random(cpu_level)
elif cpu_level == 'h':
chance_1 = fields[0], fields[1], fields[2], 0, 1, 2
chance_2 = fields[3], fields[4], fields[5], 3, 4, 5
chance_3 = fields[6], fields[7], fields[8], 6, 7, 8
chance_4 = fields[0], fields[3], fields[6], 0, 3, 6
chance_5 = fields[1], fields[4], fields[7], 1, 4, 7
chance_6 = fields[2], fields[5], fields[8], 2, 5, 8
chance_7 = fields[0], fields[4], fields[8], 0, 4, 8
chance_8 = fields[2], fields[4], fields[6], 2, 4, 6
chance_9 = [fields[4], fields[2], fields[0], fields[6], fields[8], 4, 2, 0, 6, 8]
chance_10 = [fields[1], fields[3], fields[5], fields[7], 1, 3, 5, 7]
chance_list = [chance_1, chance_2, chance_3, chance_4, chance_5, chance_6, chance_7, chance_8]
# 1.Check for chance to win
for chance in chance_list:
if chance.count(' O ') == 2:
for pos in chance:
if pos == ' * ':
new_pos = chance.index(pos)+((len(chance)+1)/2)
position_field = chance[new_pos]
position(mark, position_field, turn, cpu_level)
# 2.Check to defend players win
for chance in chance_list:
if chance.count(' X ') == 2:
for pos in chance:
if pos == ' * ':
new_pos = chance.index(pos) + ((len(chance)+1)/2)
position_field = chance[new_pos]
position(mark, position_field, turn, cpu_level)
# 3.Defend player trick set cpu to corner and middle points or set chance to win
if chance_9.count(' X ') == 2:
if chance_9[0] == ' X ':
for pos in chance_9:
if pos == ' * ':
new_pos = chance_9.index(pos) + (len(chance_9)+1)/2
position_field = chance_9[new_pos]
position(mark, position_field, turn, cpu_level)
else:
for pos in chance_10:
if pos == ' * ':
new_pos = chance_10.index(pos) + ((len(chance_10)+1)/2)
position_field = chance_10[new_pos]
position(mark, position_field, turn, cpu_level)
# 4.Set at first middle point or corner point
for pos in chance_9:
if pos == ' * ':
new_pos = chance_9.index(pos) + (len(chance_9)+1)/2
position_field = chance_9[new_pos]
position(mark, position_field, turn, cpu_level)
def cpu_random(cpu_level):
mark = ' O '
turn = 'cpu'
position_field = randint(0, 8)
position(mark, position_field, turn, cpu_level)
def check_winner(turn, mark, cpu_level):
if fields[0] == fields[1] == fields[2] == mark:
winner(turn)
if fields[3] == fields[4] == fields[5] == mark:
winner(turn)
if fields[6] == fields[7] == fields[8] == mark:
winner(turn)
if fields[0] == fields[3] == fields[6] == mark:
winner(turn)
if fields[1] == fields[4] == fields[7] == mark:
winner(turn)
if fields[2] == fields[5] == fields[8] == mark:
winner(turn)
if fields[0] == fields[4] == fields[8] == mark:
winner(turn)
if fields[2] == fields[4] == fields[6] == mark:
winner(turn)
if fields.count(' * ') == 0:
turn = 'draw.'
winner(turn)
if turn == 'player':
cpu(cpu_level)
if turn == 'cpu':
player(cpu_level)
def winner(turn):
if turn == 'draw.':
print turn
print
else:
print ('The winner is %s .' % (turn))
print
new_game = raw_input('new game? type n for new game e for exit: ')
if new_game == 'n':
for pos in range(len(fields)):
fields[pos] = ' * '
main()
if new_game == 'e':
exit()
def main():
print fields
print 'Welcome to XXO!'
print
print 'CPU = O '
print 'Player = X '
print
print 'Gamefield:'
print
print ' '.join(' 1 2 3 ')
print ' '.join(' 4 5 6 ')
print ' '.join(' 7 8 9 ')
print
beginner = raw_input('Who starts? type p for player, c for cpu?: ')
cpu_level = str(raw_input('Set cpu Level. type l for low, h for hard. '))
print
if beginner == 'p':
player(cpu_level)
if beginner == 'c':
cpu(cpu_level)
if __name__ == '__main__':
main()