TP 8 : Correction

Tic Tac Toe

Voici le code complet du jeu :


  • ######################
    # Jeu de Tic Tac Toe #
    # #
    # version 1.0 #
    ######################

    def init() :
    """ Initialisation de la grille """
    grille = {}
    i = 1
    while (i <= 3) :
    j = 1
    while (j <= 3) :
    grille[(i, j)] = ' '
    j += 1
    i += 1
    return grille

    def affiche(grille) :
    """ Affichage de la grille """
    i = 1
    print('-------')
    while (i <= 3) :
    j = 1
    print('|', end='')
    while (j <= 3) :
    print(grille[(i, j)] + '|', end ='')
    j += 1
    print()
    print('-------')
    i+= 1

    def gagne_vertical(grille, pion, x, y) :
    """ Teste si le joueur 'pion' a un alignement vertical en posant un pion en (x, y) """
    if (x == 1) :
    return ((grille[(2, y)] == pion) and (grille[(3, y)] == pion))
    if (x == 2) :
    return ((grille[(1, y)] == pion) and (grille[(3, y)] == pion))
    if (x == 3) :
    return ((grille[(1, y)] == pion) and (grille[(2, y)] == pion))

    def gagne_horizontal(grille, pion, x, y) :
    """ Teste si le joueur 'pion' a un alignement horizontal en posant un pion en (x, y) """
    if (y == 1) :
    return ((grille[(x, 2)] == pion) and (grille[(x, 3)] == pion))
    if (y == 2) :
    return ((grille[(x, 1)] == pion) and (grille[(x, 3)] == pion))
    if (y == 3) :
    return ((grille[(x, 1)] == pion) and (grille[(x, 2)] == pion))

    def gagne_diagonale(grille, pion, x, y) :
    """ Teste si le joueur 'pion' a un alignement en diagonale en posant un pion en (x, y) """
    if ((x == 1) and (y == 1)) :
    return ((grille[(2, 2)] == pion) and (grille[(3, 3)] == pion))
    if ((x == 3) and (y == 1)) :
    return ((grille[(2, 2)] == pion) and (grille[(1, 3)] == pion))
    if ((x == 1) and (y == 3)) :
    return ((grille[(2, 2)] == pion) and (grille[(3, 1)] == pion))
    if ((x == 3) and (y == 3)) :
    return ((grille[(2, 2)] == pion) and (grille[(1, 1)] == pion))
    if ((x == 2) and (y == 2)) :
    return (((grille[(1, 1)] == pion) and (grille[(3, 3)] == pion)) or ((grille[(3, 1)] == pion) and (grille[(1, 3)] == pion)))

    def gagne(grille, pion, x, y) :
    """ Teste si le joueur 'pion' a un alignement quelconque en posant un pion en (x, y) """
    return ((gagne_vertical(grille, pion, x, y)) or (gagne_horizontal(grille, pion, x, y)) or (gagne_diagonale(grille, pion, x, y)))

    def libre(grille, x, y) :
    """ Teste si la case (x, y) ne contient pas de pion """
    return (grille[(x, y)] == ' ')

    def plus_de_place(grille) :
    """ Teste s'il reste une case libre sur la grille """
    i = 1
    while (i <= 3) :
    j = 1
    while (j <= 3) :
    if (grille[(i, j)] == ' ') :
    return False
    j += 1
    i +=1
    return True

    def prochain_joueur(pion) :
    """ Renvoie le prochain joueur """
    if (pion == 'X') :
    return 'O'
    else :
    return 'X'

    def place_pion(grille, pion, x, y) :
    """ Place le 'piion' en (x, y) """
    if (libre(grille, x, y)) :
    if (gagne(grille, pion, x, y)) :
    print('Le joueur', pion, 'gagne la partie!')
    exit()
    grille[(x, y)] = pion
    if (plus_de_place(grille)) :
    print('Match nul!')
    exit()
    return prochain_joueur(pion)
    else :
    print('Placement Impossible !')
    return pion

    def jouer(pion) :
    """ Demande au joueur de saisir la case (x, y) où poser son pion """
    print('Joueur', pion, 'c\'est à vous!')
    x = 4
    while ((x < 1) or (x > 3)) :
    try :
    x = int(input('x (1 <= x <= 3) = ? '))
    except :
    print('Erreur de saisie')
    y = 4
    while ((y < 1) or (y > 3)) :
    try :
    y = int(input('y (1 <= y <= 3) = ? '))
    except :
    print('Erreur de saisie!')
    return [x, y]

    #####################################
    # Programme principal

    grille = init()
    affiche(grille)
    pion = 'X'
    while 1 :
    case = jouer(pion)
    pion = place_pion(grille, pion, case[0], case[1])
    affiche(grille)