Python? Is that one of the coolest programming languages? Yes it is -:)
So yes, I started learning Python a couple of weeks ago...a month maybe...so how did it started?
I knew Python by name for quite a long time ago...doing Ruby stuff I knew that Ruby inherit a lot from Python (from Perl as well), but I never give it too much of attention.
One day, I said to myself...you must try Python dude! It looks like a nice one...so I put myself in the journey of learning a new programming language.
Most Python people say "Python is easy to learn, it's sexy, it makes write beautiful code"...and you know what? It's damn true -:)
So, as part of my learning process I took a Tic-Tac-Toe I made on Ruby 5 years ago and ported to Python...believe or not...the code is more robust and I managed to cut to 38 less lines...nice, huh?
So here's the code...and keep in mind that I'm a Python newbie...so don't came to me with your "Oh, if you were a real Pythonist, you should thing like this"...because I'm not a Pythonist...at least not yet -;) I'm an ABAPist -:P
#TIC_TAC_TOE
#Made by Blag - 2011
game = False
coordenates = ""
player_won = ""
player_turn = "1"
line1 = ""
line2 = ""
line3 = ""
line4 = ""
line5 = ""
board_array = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
played_moves = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
def clean_lines():
line1 = " "
line2 = " "
line3 = " "
line4 = " "
line5 = " "
def initial_board():
clean_lines()
line1 = " | | "
line2 = "---+---+---"
line3 = " | | "
line4 = "---+---+---"
line5 = " | | "
print "\n"
print (line1)
print (line2)
print (line3)
print (line4)
print (line5)
print "\n"
def board(coordenates, player):
clean_lines()
if board_array[coordenates] == " ":
board_array[coordenates] = player
else:
cheat = True
Move_1 = board_array[0]
Move_2 = board_array[1]
Move_3 = board_array[2]
Move_4 = board_array[3]
Move_5 = board_array[4]
Move_6 = board_array[5]
Move_7 = board_array[6]
Move_8 = board_array[7]
Move_9 = board_array[8]
print "\n"
print " %s | %s | %s " % (Move_1, Move_2, Move_3)
print "---+---+---"
print " %s | %s | %s " % (Move_4, Move_5, Move_6)
print "---+---+--- "
print " %s | %s | %s " % (Move_7, Move_8, Move_9)
print "\n\n"
def print_winner(player):
print "Player ", player, " won\n\n"
def check_who_wins():
game = False
if (board_array[0] != " " and board_array[0] == board_array[1] and
board_array[1] == board_array[2]):
game = True
if board_array[0] == "*":
print_winner(1)
else:
print_winner(2)
if (board_array[3] != " " and board_array[3] == board_array[4] and
board_array[4] == board_array[5]):
game = True
if board_array[3] == "*":
print_winner(1)
else:
print_winner(2)
if (board_array[0] != " " and board_array[0] == board_array[3] and
board_array[3] == board_array[6]):
game = True
if board_array[0] == "*":
print_winner(1)
else:
print_winner(2)
if (board_array[1] != " " and board_array[1] == board_array[4] and
board_array[4] == board_array[7]):
game = True
if board_array[1] == "*":
print_winner(1)
else:
print_winner(2)
if (board_array[2] != " " and board_array[2] == board_array[5] and
board_array[5] == board_array[8]):
game = True
if board_array[2] == "*":
print_winner(1)
else:
print_winner(2)
if (board_array[0] != " " and board_array[0] == board_array[4] and
board_array[4] == board_array[8]):
game = True
if board_array[0] == "*":
print_winner(1)
else:
print_winner(2)
if (board_array[2] != " " and board_array[2] == board_array[4] and
board_array[4] == board_array[6]):
game = True
if board_array[2] == "*":
print_winner(1)
else:
print_winner(2)
wins = 0
for board in board_array:
if board != " ":
wins += 1
if wins == 9:
game = True
print "It's a Tie'.\n\n"
return game
def make_move(player_turn, game, coordenates):
index_coordenates = 0
if game == False:
cheat = False
if player_turn == "1":
coordenates = input("Player 1: ")
coordenates = coordenates
if coordenates <= 0 or coordenates > 9:
print "Please, only values from 1 to 9\n"
make_move(player_turn, game, coordenates)
for moves in played_moves:
if moves == coordenates:
cheat = True
if cheat == True:
cheat = False
print "That move was already made!\n"
make_move(player_turn, game, coordenates)
index_coordenates = coordenates - 1
played_moves[index_coordenates] = coordenates
player = "*"
board(index_coordenates, player)
game = check_who_wins()
if game == False:
cheat = False
player_turn = "2"
if player_turn == "2":
coordenates = input("Player 2: ")
coordenates = coordenates
if coordenates <= 0 or coordenates > 9:
print "Please, only values from 1 to 9\n"
make_move(player_turn, game, coordenates)
for moves in played_moves:
if moves == coordenates:
cheat = True
if cheat == True:
cheat = False
print "That move was already made!\n"
make_move(player_turn, game, coordenates)
index_coordenates = coordenates - 1
played_moves[index_coordenates] = coordenates
player = "O"
board(index_coordenates, player)
game = check_who_wins()
player_turn = "1"
return game
initial_board()
while(game == False):
game = make_move(player_turn, game, coordenates)
Greetings,
Blag.
No hay comentarios:
Publicar un comentario