Partage
  • Partager sur Facebook
  • Partager sur Twitter

Java & HTML: Tic Tac Toe

Anonyme
    26 septembre 2011 à 10:30:53

    Bonjour, tout d'abord je suis désolé si le message que je viens de poster n'est pas dans la bonne section de ce forum (j'hésitais entre deux sous-catégories ^^)

    Mais bon, je vous expose mon problème. Je suis novice dans la programmation (HTML & autre) et je souhaiterais intégré à mon site web un jeu de Tic Tac toe (ou morpion pour ceux qui préfère). Donc j'ai fait mes petites recherches sur le web grâce à mon ami google et j'ai trouvé un code java de cette application:

    import java.util.Scanner;
    
    public class TicTacToe{
    
            //keeps track of the turns to state a cat's game
            public static int catsGame=0;
    
            //all of the characters
            private static char r1c1;
            private static char r1c2;
            private static char r1c3;
            private static char r2c1;
            private static char r2c2;
            private static char r2c3;
            private static char r3c1;
            private static char r3c2;
            private static char r3c3;
            
            //draws the board
            private static void drawBoard() {
                    System.out.println(r1c1 + "|" + r1c2 + "|" + r1c3);
                    System.out.println(r2c1 + "|" + r2c2 + "|" + r2c3);
                    System.out.println(r3c1 + "|" + r3c2 + "|" + r3c3);
            }
            //gives the user an example
            private static void exampleBoard() {
            System.out.println(" 7 | 8 | 9 ");
            System.out.println(" 4 | 5 | 6 ");
            System.out.println(" 1 | 2 | 3 \n\n");
            }
            //gets the input from the user
            public static void getInput(int playerNumber){
                    Scanner kb = new Scanner(System.in);
                    System.out.print("Where do you want the Piece?:  ");
                    int location = kb.nextInt();
                    char piece;
                    if (playerNumber == 1)
                            piece = 'X';
                    else
                            piece = 'O';
            
                            
                    if (location == 7)
                            if (r1c1 == ' ')
                                    r1c1 = piece;
                            else getInput(playerNumber);
                    if (location == 8)
                            if (r1c2 == ' ')
                            r1c2 = piece;
                            else getInput(playerNumber);
                    if (location == 9)
                            if (r1c3 == ' ')
                            r1c3 = piece;
                            else getInput(playerNumber);
                    if (location == 4)
                            if (r2c1 == ' ')
                            r2c1 = piece;
                            else getInput(playerNumber);
                    if (location == 5)
                            if (r2c2 == ' ')
                            r2c2 = piece;
                            else getInput(playerNumber);
                    if (location == 6)
                            if (r2c3 == ' ')
                            r2c3 = piece;
                            else getInput(playerNumber);
                    if (location == 1)
                            if (r3c1 == ' ')
                            r3c1 = piece;
                            else getInput(playerNumber);
                    if (location == 2)
                            if (r3c2 == ' ')
                            r3c2 = piece;
                            else getInput(playerNumber);
                    if (location == 3)
                            if (r3c3 == ' ')
                            r3c3 = piece;
                            else getInput(playerNumber);
            }
            
                    
            //Checks to see if anyone won        
            private static char checkWinner() {
                    if (r1c1 == r1c2 && r1c2 == r1c3 && r1c1 != ' ')
                            return r1c1;
                    else if (r2c1 == r2c2 && r2c2 == r2c3 && r2c1 != ' ')
                        return r2c1;
                    else if (r3c1 == r3c2 && r3c2 == r3c3 && r3c1 != ' ')
                        return r3c1;
                    else if (r1c1 == r2c2 && r2c2 == r3c3 && r1c1 != ' ')
                        return r1c1;
                    else if (r1c3 == r2c2 && r2c2 == r3c1 && r1c3 != ' ')
                        return r1c3;
                    else if (r1c1 == r2c1 && r2c1 == r3c1 && r1c1 != ' ')
                        return r1c1;
                    else if (r1c3 == r2c3 && r2c3 == r3c3 && r2c3 != ' ')
                        return r1c3;
            else if (r1c2 == r2c2 && r2c2 == r3c2 && r1c2 != ' ')
                            return r1c2;
                    else
                            return 'P';
                    
        }
            
                    
        //starts a new board
            private static void newGame() {
                    r1c1 = ' ';
                    r1c2 = ' ';
                    r1c3 = ' ';
                    r2c1 = ' ';
                    r2c2 = ' ';
                    r2c3 = ' ';
                    r3c1 = ' ';
                    r3c2 = ' ';
                    r3c3 = ' ';
            }
            //Main Method
            public static void main(String args[]) {
                    r1c1 = ' ';
                    r1c2 = ' ';
                    r1c3 = ' ';
                    r2c1 = ' ';
                    r2c2 = ' ';
                    r2c3 = ' ';
                    r3c1 = ' ';
                    r3c2 = ' ';
                    r3c3 = ' ';
                    
                    int playerNumber =1;
                    
                
                    
                    Scanner kb = new Scanner(System.in);
                    do{
                            catsGame = 0;
                            newGame();
                            while(checkWinner()=='P'){
                                    
                                    exampleBoard();
                                    drawBoard();
                                    checkWinner();
                                    playerNumber *= -1;
                                    getInput(playerNumber);
                                    catsGame ++;
                                    System.out.println(catsGame);
                                    if (catsGame == 9){
                                            break;
                                    }
                            
                            }
                             exampleBoard();
                             drawBoard();
                            if (checkWinner()=='X')
                                                    System.out.println("X Wins!!!!!");
                            else if (checkWinner()=='O')
                                    System.out.println("O Wins!!!!!");
                            else 
                                    System.out.println("Cat's Game!");
                            
                            System.out.println("Would you like to play again? (Y/N)");
                    } while (kb.next().toLowerCase().charAt(0) == 'y');
                    
            }
    }


    Et là je suis face à un problème de taille, comme insérer ce code sachant que j'aimerais que l'utilisateur n'ai pas a télécharger de fichier (l'application se lancerais automatiquement quand on arrive sur une page X)? J'avais essayé de le convertir en XHTML avec Java2html sans résultat.

    Par avance, merci pour tous ceux qui voudront bien répondre à mon appel à l'aide ;-)

    Cordialement,

    Magiik0Rel

    PS: si vous ne connaissez pas la marche à suivre exacte, merci de poster le lien vers un tutoriel du site et j'essayerais de l'insérer selon leurs conseils.
    • Partager sur Facebook
    • Partager sur Twitter
      26 septembre 2011 à 10:44:27

      Attention !
      Erreur 1000 -> Vous avez confondu deux langages ! JAVA != Javascript !!

      Tu insères le javascript dans une page web (si j'ai bien compris) avec <script type="text/javascript" src="adresse du script">
      • Partager sur Facebook
      • Partager sur Twitter
      Anonyme
        26 septembre 2011 à 10:51:22

        Merci pour ta réponse (très) rapide ;-)
        Donc au final, sa donnerais quoi ?

        import java.util.Scanner;
        
        <erreur></erreur><script type=public class TicTacToe{
        
                //keeps track of the turns to state a cat's game
                public static int catsGame=0;
        
                //all of the characters
                private static char r1c1;
                private static char r1c2;
                private static char r1c3;
                private static char r2c1;
                private static char r2c2;
                private static char r2c3;
                private static char r3c1;
                private static char r3c2;
                private static char r3c3;
                
                //draws the board
                private static void drawBoard() {
                        System.out.println(r1c1 + "|" + r1c2 + "|" + r1c3);
                        System.out.println(r2c1 + "|" + r2c2 + "|" + r2c3);
                        System.out.println(r3c1 + "|" + r3c2 + "|" + r3c3);
                }
                //gives the user an example
                private static void exampleBoard() {
                System.out.println(" 7 | 8 | 9 ");
                System.out.println(" 4 | 5 | 6 ");
                System.out.println(" 1 | 2 | 3 \n\n");
                }
                //gets the input from the user
                public static void getInput(int playerNumber){
                        Scanner kb = new Scanner(System.in);
                        System.out.print("Where do you want the Piece?:  ");
                        int location = kb.nextInt();
                        char piece;
                        if (playerNumber == 1)
                                piece = 'X';
                        else
                                piece = 'O';
                
                                
                        if (location == 7)
                                if (r1c1 == ' ')
                                        r1c1 = piece;
                                else getInput(playerNumber);
                        if (location == 8)
                                if (r1c2 == ' ')
                                r1c2 = piece;
                                else getInput(playerNumber);
                        if (location == 9)
                                if (r1c3 == ' ')
                                r1c3 = piece;
                                else getInput(playerNumber);
                        if (location == 4)
                                if (r2c1 == ' ')
                                r2c1 = piece;
                                else getInput(playerNumber);
                        if (location == 5)
                                if (r2c2 == ' ')
                                r2c2 = piece;
                                else getInput(playerNumber);
                        if (location == 6)
                                if (r2c3 == ' ')
                                r2c3 = piece;
                                else getInput(playerNumber);
                        if (location == 1)
                                if (r3c1 == ' ')
                                r3c1 = piece;
                                else getInput(playerNumber);
                        if (location == 2)
                                if (r3c2 == ' ')
                                r3c2 = piece;
                                else getInput(playerNumber);
                        if (location == 3)
                                if (r3c3 == ' ')
                                r3c3 = piece;
                                else getInput(playerNumber);
                }
                
                        
                //Checks to see if anyone won        
                private static char checkWinner() {
                        if (r1c1 == r1c2 && r1c2 == r1c3 && r1c1 != ' ')
                                return r1c1;
                        else if (r2c1 == r2c2 && r2c2 == r2c3 && r2c1 != ' ')
                            return r2c1;
                        else if (r3c1 == r3c2 && r3c2 == r3c3 && r3c1 != ' ')
                            return r3c1;
                        else if (r1c1 == r2c2 && r2c2 == r3c3 && r1c1 != ' ')
                            return r1c1;
                        else if (r1c3 == r2c2 && r2c2 == r3c1 && r1c3 != ' ')
                            return r1c3;
                        else if (r1c1 == r2c1 && r2c1 == r3c1 && r1c1 != ' ')
                            return r1c1;
                        else if (r1c3 == r2c3 && r2c3 == r3c3 && r2c3 != ' ')
                            return r1c3;
                else if (r1c2 == r2c2 && r2c2 == r3c2 && r1c2 != ' ')
                                return r1c2;
                        else
                                return 'P';
                        
            }
                
                        
            //starts a new board
                private static void newGame() {
                        r1c1 = ' ';
                        r1c2 = ' ';
                        r1c3 = ' ';
                        r2c1 = ' ';
                        r2c2 = ' ';
                        r2c3 = ' ';
                        r3c1 = ' ';
                        r3c2 = ' ';
                        r3c3 = ' ';
                }
                //Main Method
                public static void main(String args[]) {
                        r1c1 = ' ';
                        r1c2 = ' ';
                        r1c3 = ' ';
                        r2c1 = ' ';
                        r2c2 = ' ';
                        r2c3 = ' ';
                        r3c1 = ' ';
                        r3c2 = ' ';
                        r3c3 = ' ';
                        
                        int playerNumber =1;
                        
                    
                        
                        Scanner kb = new Scanner(System.in);
                        do{
                                catsGame = 0;
                                newGame();
                                while(checkWinner()=='P'){
                                        
                                        exampleBoard();
                                        drawBoard();
                                        checkWinner();
                                        playerNumber *= -1;
                                        getInput(playerNumber);
                                        catsGame ++;
                                        System.out.println(catsGame);
                                        if (catsGame == 9){
                                                break;
                                        }
                                
                                }
                                 exampleBoard();
                                 drawBoard();
                                if (checkWinner()=='X')
                                                        System.out.println("X Wins!!!!!");
                                else if (checkWinner()=='O')
                                        System.out.println("O Wins!!!!!");
                                else 
                                        System.out.println("Cat's Game!");
                                
                                System.out.println("Would you like to play again? (Y/N)");
                        } while (kb.next().toLowerCase().charAt(0) == 'y');
                        
                }
        }
        


        ? (bizarre car il me met erreur à l1 ;)

        Et donc, je n'ai pas besoin de passer par du HTML (pas de conversion?) ?
        • Partager sur Facebook
        • Partager sur Twitter
          26 septembre 2011 à 10:54:16

          Euh......... comme il l'a dit, tu fais du Java.
          Hors le Javascript n'est pas du tout le même langage. T'es pas dans la bonne section.
          • Partager sur Facebook
          • Partager sur Twitter
          Anonyme
            26 septembre 2011 à 11:01:02

            Entre tous vos messages, je suis pommé ^^'

            Ca, c'est du JAVA ou du JAVASCRIPT? (java si j'ai bien compris ...)
            • Partager sur Facebook
            • Partager sur Twitter
              26 septembre 2011 à 11:05:42

              Oui, ton code est du Java. Et les messages de remontees sont inutiles et déplacés. :D
              • Partager sur Facebook
              • Partager sur Twitter
              Anonyme
                26 septembre 2011 à 11:11:44

                Ok merci, c'est plus clair ainsi ;-)
                Je vais donc reposter mon message dans la section adéquate ... (à moins qu'on ne puisse déplace ce sujet ^^)
                • Partager sur Facebook
                • Partager sur Twitter
                Anonyme
                  26 septembre 2011 à 12:50:33

                  Voilà, donc merci de me l'avoir déplacé !

                  Je réiterre ma question ici: comment insérer ce code sur un site web (aucun téléchargement)? faut-il passer par un logiciel comme eclipse? ...
                  • Partager sur Facebook
                  • Partager sur Twitter
                  Anonyme
                    26 septembre 2011 à 14:00:29

                    Merci mais apparemment ton lien oblige l'utilisateur à télécharger le fichier. Or, je recherche un truc de ce style là : http://tpe.drux.fr/morpion/
                    • Partager sur Facebook
                    • Partager sur Twitter
                      26 septembre 2011 à 14:20:11

                      Pas si tu utilises OBJECT ou APPLET ;)
                      • Partager sur Facebook
                      • Partager sur Twitter
                      Anonyme
                        26 septembre 2011 à 16:28:20

                        D'accord, pourrais-tu juste copier mon code et coller APPLET/OBJECT pour savoir exactement où je dois les insérer !
                        • Partager sur Facebook
                        • Partager sur Twitter
                          26 septembre 2011 à 16:55:59

                          Je vais pas te faire tout ton boulot !!! C'est très bien expliqué dans mon lien, à toi de te débrouiller un peu :euh:
                          • Partager sur Facebook
                          • Partager sur Twitter
                          Anonyme
                            26 septembre 2011 à 17:32:39

                            Merci de ton aide, sa fait plaisir ;-)

                            EDIT:

                            J'ai tenté comme indiqué pour avoir :

                            charger : classe tictactoe.class introuvable.
                            java.lang.ClassNotFoundException: tictactoe.class
                            	at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
                            	at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
                            	at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
                            	at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
                            	at java.lang.ClassLoader.loadClass(Unknown Source)
                            	at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
                            	at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
                            	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
                            	at java.lang.Thread.run(Unknown Source)
                            Exception : java.lang.ClassNotFoundException: tictactoe.class
                            

                            Donc j'ai compris que le fichier .class était iexistant sur mon ordinateur, ce qui est vrai étant donné que je n'ai que le code. Comment créé un fichier de ce type?
                            • Partager sur Facebook
                            • Partager sur Twitter
                              26 septembre 2011 à 18:03:57

                              Oulolo !!!! Tu gardes ton fichier java comme il est et tu l'intègres comme dit dans le lien que je t'ai passé. C'est super simple... Après, si tu connais pas le XHTML ou le CSS, c'est pas la peine d'essayer de faire un site, va déjà les apprendre...
                              • Partager sur Facebook
                              • Partager sur Twitter

                              Java & HTML: Tic Tac Toe

                              × 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