السلام عليكم
لعبة tic tac toe على الـ Console اول برامجي بلغة البايثون وتطبيق عملي على اساسيات اللغة
المربعات مرقمة من 1 الى 9 من اعلى اليسار الى اسفل اليمين
يتم التحريك بكتابة رقم المربع من ورقم المربع الى
لا اريد التطويل عليكم
ولكن ارجو من الخبراء التقييم والتوجيه
وادعو المبتدئين امثالي للمشاركة والاستفادة
نسخة البايثون المستخدمة هي 2.7
def printView(values):# values is a list []
print '\t\t---+---+---+'
print '\t\t|%s | %s | %s |' % (values[0],values[1],values[2])
print '\t\t---+---+---+'
print '\t\t|%s | %s | %s |' % (values[3],values[4],values[5])
print '\t\t---+---+---+'
print '\t\t|%s | %s | %s |' % (values[6],values[7],values[8])
print '\t\t---+---+---+'
################################################################################
def move(moves, movefrm, moveto):
moves[moveto] = moves[movefrm]
moves[movefrm] = ' '
return moves
################################################################################
# turn X or O
# moves list of the game
def validMove(turn, moves, movefrm, moveto):
if moves[movefrm] != turn :
return False
if moves[moveto] != ' ' :
return False
return True
################################################################################
def play(moves, turn):
while True:
print 'Player %s Turn:' % turn
movefrm = input("move %s from : " % turn) - 1
moveto = input("move %s to : " % turn) - 1
if not(validMove(turn, moves, movefrm, moveto)) :
print ("Invalid Move")
continue
else :
moves = move(moves, movefrm, moveto)
return moves
################################################################################
def win(turn, moves) :
winList = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]]
turnMoves = []
count = 0
while count < len(moves) :
if turn == moves[count] :
turnMoves.append(count+1)
if len(turnMoves) == 3 :
break
count = count + 1
if turnMoves in winList :
printView(moves)
print ("Player %s Wins" % turn)
return True
else :
return False
################################################################################
def main():
moves = ['X', 'X', 'X', ' ', ' ', ' ', 'O', 'O','O']
while True: #start the game
printView(moves)
# player X Turn
moves = play(moves, 'X')
printView(moves)
if win('X', moves) :
break
# player O Turn
moves = play(moves, 'O')
if win('O', moves) :
break
if __name__ == '__main__':
main()في انتظار تعليقاتكم
تحياتي


