Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème irrésolu dans un jeu type "Morpion"

    26 novembre 2019 à 19:23:47

    Bonsoir, je suis en première et actuellement sur un projet scolaire dont le but est de réaliser un jeu "Morpion". Avec mon groupe, on a bien avancé, voire quasiment terminé, mais la partie qui détecte si la partie est terminée ou non ne fonctionne pas donc je voulais avoir des avis, merci d'avance.

    PS : les commentaires sont en anglais parce que je travaille avec une anglophone.

    #import
    import random
    import sys
    #grid declaration
    line1 =["00","01","02"]
    line2 =["10","11","12"]
    line3 =["20","21","22"]
    grid=[line1, line2, line3]
    #function to show the grid
    def showGrid() :
        print(line1[0], line1[1], line1[2])
        print(line2[0], line2[1] ,line2[2])
        print(line3[0], line3[1], line3[2])
    gameFinished = False
    #function with the whole game
    def turn() :
        showGrid()
    #The player choose what case he wants to play
        playerChoice=input("What case do you want to play ( you'll play the crosses) : ")
    #For loops, one for each line, to put the "X" at the chosen spot
        n=0
        for case in line1:
            if case == playerChoice:
                line1[n] = "X"
            n+=1
        o=0
        for case in line2:
            if case == playerChoice:
                line2[o] = "X"
            o+=1
        p=0
        for case in line3:
            if case == playerChoice:
                line3[p] = "X"
            p+=1
    #The "A.I." choose randomly a case among the "aiGrid" list
        aiGrid=["00", "01", "02", "10", "11", "12", "20", "21", "22"]
        played = False
        while played == False:
            aiChoice= random.choice(aiGrid)
            if grid[int(aiChoice[0])][int(aiChoice[1])] != "O" and grid[int(aiChoice[0])][int(aiChoice[1])] != "X":
                grid[int(aiChoice[0])][int(aiChoice[1])] = "O"
                played = True
            else:
                played = False
        return playerChoice
    #function to detect if the player wins
    def playerWin(playerChoice):
    #We analyse all the situations where the player can win, so there are nine
        if (playerChoice==grid[0][0]) and (playerChoice==grid[0][1]) and (playerChoice==grid[0][2]):
            print("Bravo ! Vous avez gagné !")
            gameFinished == True
        if (playerChoice==grid[1][0]) and (playerChoice==grid[1][1]) and (playerChoice==grid[1][2]):
            print("Bravo ! Vous avez gagné !")
            gameFinished == True
        if (playerChoice==grid[2][0]) and (playerChoice==grid[2][1]) and (playerChoice==grid[2][2]):
            print("Bravo ! Vous avez gagné !")
            gameFinished == True
        if (playerChoice==grid[0][0]) and (playerChoice==grid[1][1]) and (playerChoice==grid[2][2]):
            print("Bravo ! Vous avez gagné !")
            gameFinished == True
        if (playerChoice==grid[0][2]) and (playerChoice==grid[1][1]) and (playerChoice==grid[2][0]):
            print("Bravo ! Vous avez gagné !")
            gameFinished == True
        if (playerChoice==grid[0][0]) and (playerChoice==grid[1][0]) and (playerChoice==grid[2][0]):
            print("Bravo ! Vous avez gagné !")
            gameFinished == True
        if (playerChoice==grid[0][1]) and (playerChoice==grid[1][1]) and (playerChoice==grid[2][1]):
            print("Bravo ! Vous avez gagné !")
            gameFinished == True
        if (playerChoice==grid[0][2]) and (playerChoice==grid[1][2]) and (playerChoice==grid[2][2]):
            print("Bravo ! Vous avez gagné !")
            gameFinished == True
            
    def aiWin(playerChoice):
    #Still nine situations, but here we use " == "O" " because we don't have a variable like "playerChoice"
        if grid[int(line1[0][0])][int(line1[0][1])] == "O" and grid[int(line2[1][0])][int(line2[1][1])] == "O" and grid[int(line3[2][0])][int(line3[2][1])] == "O":
            print("Dommage ! Vous avez perdu")
            gameFinished == True
        if grid[int(line1[0][0])][int(line1[0][1])] == "O" and grid[int(line2[0][0])][int(line2[0][1])] == "O"  and grid[int(line3[0][0])][int(line3[0][1])] == "O" :
            print("Dommage ! Vous avez perdu")
            gameFinished == True
        if grid[int(line1[1][0])][int(line1[1][1])] == "O"  and grid[int(line2[1][0])][int(line2[1][1])] == "O"  and grid[int(line3[1][0])][int(line3[1][1])] == "O" :
            print("Dommage ! Vous avez perdu")
            gameFinished == True
        if grid[int(line1[2][0])][int(line1[2][1])] == "O"  and grid[int(line2[2][0])][int(line2[2][1])] == "O"  and grid[int(line3[2][0])][int(line3[2][1])] == "O" :
            print("Dommage ! Vous avez perdu")
            gameFinished == True
        if grid[int(line1[2][0])][int(line1[2][1])] == "O"  and grid[int(line2[1][0])][int(line2[1][1])] == "O"  and grid[int(line3[0][0])][int(line3[0][1])] == "O" :
            print("Dommage ! Vous avez perdu")
            gameFinished == True
        if grid[int(line1[0][0])][int(line1[0][1])] == "O"  and grid[int(line1[1][0])][int(line1[1][1])] == "O"  and grid[int(line1[2][0])][int(line1[2][1])] == "O" :
            print("Dommage ! Vous avez perdu")
            gameFinished == True
        if grid[int(line2[0][0])][int(line2[0][1])] == "O" and grid[int(line2[1][0])][int(line2[1][1])] == "O" and grid[int(line2[2][0])][int(line2[2][1])] == "O":
            print("Dommage ! Vous avez perdu")
            gameFinished == True
        if grid[int(line3[0][0])][int(line3[0][1])] == "O" and grid[int(line3[1][0])][int(line3[1][1])] == "O" and grid[int(line3[2][0])][int(line3[2][1])] == "O":
            print("Dommage ! Vous avez perdu")
            gameFinished == True
            
    while gameFinished == False:
        playerChoice=turn()
        playerWin(playerChoice)
        aiWin
    sys.exit()

    -
    Edité par *Moréo* 26 novembre 2019 à 19:25:47

    • Partager sur Facebook
    • Partager sur Twitter
      27 novembre 2019 à 9:06:09

      Il y a top de if, essaye de réduire ;)
      • Partager sur Facebook
      • Partager sur Twitter
        27 novembre 2019 à 10:23:08

        Genre plus que 3 if, avec des or dans les conditions ?
        • Partager sur Facebook
        • Partager sur Twitter
          27 novembre 2019 à 10:26:51

          Mais j'vois pas trop comment, étant donné que chaque if a des conditions différentes

          -
          Edité par *Moréo* 27 novembre 2019 à 10:29:41

          • Partager sur Facebook
          • Partager sur Twitter
            27 novembre 2019 à 10:39:41

            Un truc comme ça :

            # lignes et colonnes
            for i in range(3):
                if all(playerChoice == grid[i][j] for j in range(3)) or all(
                    playerChoice == grid[j][i] for j in range(3)
                ):
                    gameFinished = True
                    break
            # diagonales
            if (
                not gameFinished
                and all(playerChoice == grid[i][i] for i in range(3))
                or all(playerChoice == grid[i][2 - i] for i in range(3))
            ):
                gameFinished = True
            
            if gameFinished:
                print("Bravo !")
            

            au passage tes "gameFinished == True" ne servent à rien.

            tout comme implémenter quasiment la même fonction deux fois

            -
            Edité par thelinekioubeur 27 novembre 2019 à 10:44:07

            • Partager sur Facebook
            • Partager sur Twitter
              27 novembre 2019 à 11:02:11

              Bon bah soit j'edit mon post ( meme si du coup j'vois pas bien l'interet ), je disais donc que j'ai appliqué tes boucles for tout en rajoutant une deuxieme gameFinished pour le match nul plus tard, et j'ai aussi appliqué les conseils de mon prof, mais ça marche toujours pas, donc je voulais des avis

              PS : pourquoi thelinekioubeur il a le droit de poster lui :c

              #import
              import random
              import sys
              #grid declaration
              line1 =["00","01","02"]
              line2 =["10","11","12"]
              line3 =["20","21","22"]
              grid=[line1, line2, line3]
              #function to show the grid
              def showGrid() :
                  print(line1[0], line1[1], line1[2])
                  print(line2[0], line2[1] ,line2[2])
                  print(line3[0], line3[1], line3[2])
              #function with the whole game
              def turn() :
                  showGrid()
              #The player choose what case he wants to play
                  playerChoice=input("What case do you want to play ( you'll play the crosses) : ")
              #Three loops, one for each line, to put the "X" at the chosen spot
                  n=0
                  for case in line1:
                      if case == playerChoice:
                          line1[n] = "X"
                      n+=1
                  o=0
                  for case in line2:
                      if case == playerChoice:
                          line2[o] = "X"
                      o+=1
                  p=0
                  for case in line3:
                      if case == playerChoice:
                          line3[p] = "X"
                      p+=1
              #The "A.I." choose randomly a case among the "aiGrid" list
                  aiGrid=["00", "01", "02", "10", "11", "12", "20", "21", "22"]
                  played = False
                  while played == False:
                      aiChoice= random.choice(aiGrid)
                      if grid[int(aiChoice[0])][int(aiChoice[1])] != "O" and grid[int(aiChoice[0])][int(aiChoice[1])] != "X":
                          grid[int(aiChoice[0])][int(aiChoice[1])] = "O"
                          played = True
                      else:
                          played = False
              #function to detect if the player wins
              def playerWin(gameFinished):
              # lignes et colonnes
                  for i in range(3):
                      if all("X" == grid[i][j] for j in range(3)) or all(
                          "X" == grid[j][i] for j in range(3)
                      ):
                          gameFinished = True
                          break
                  # diagonales
                      if (
                          not gameFinished
                          and all("X" == grid[i][i] for i in range(3))
                          or all("X" == grid[i][2 - i] for i in range(3))):
                          gameFinished = True
                          
                  return gameFinished
                      
              def aiWin(gameFinished1):
                  #lignes et colonnes
                  for i in range(3):
                      if all("O" == grid[i][j] for j in range(3)) or all(
                          "O" == grid[j][i] for j in range(3)
                      ):
                          gameFinished = True
                          break
                  # diagonales
                      if (
                          not gameFinished1
                          and all("O" == grid[i][i] for i in range(3))
                          or all("O" == grid[i][2 - i] for i in range(3))
                      ):
                          gameFinished1 = True
                      if gameFinished1:
                          print(gameFinished1)
                  return gameFinished1
              #Main loop
              gameFinished=False
              gameFinished1=False
              while gameFinished == False or gameFinished1 == False:
                  turn()
                  gamefinished=playerWin(gameFinished)
                  gamefinished=aiWin(gameFinished1)
              sys.exit()
              

              -
              Edité par *Moréo* 27 novembre 2019 à 20:53:21

              • Partager sur Facebook
              • Partager sur Twitter
                27 novembre 2019 à 19:49:34 - Message modéré pour le motif suivant : banni pour multi-comptes


                  27 novembre 2019 à 19:54:34

                  Tu pouvais éditer ton post ...

                  • Partager sur Facebook
                  • Partager sur Twitter
                    27 novembre 2019 à 19:59:05

                    MoréoNomdefamille a écrit:

                    Bon, vu que je peux poster que toutes les 24h à la suite, j'ai fait un autre compte

                    Bonjour, NON le multi compte n'est  pas autorisé selon les règles du site. Merci d'attendre 24H avant de poster deux messages consécutif ou éditer votre précédant message.

                    Je bannis le compte  MoréoNomdefamille

                    Liens conseillés

                    -
                    Edité par AbcAbc6 27 novembre 2019 à 20:05:29

                    • Partager sur Facebook
                    • Partager sur Twitter

                    Problème irrésolu dans un jeu type "Morpion"

                    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                    • Editeur
                    • Markdown