Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Java Game - Alternate between players

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Game - Alternate between players

    Hey guys. I'm new to the forum and this is my first post so be nice!

    So i'm developing a HangMan game in Java using TCP Sockets for 2 players to play against each other. I'm down to the last problem which is figuring out the correct sequence of the game. One of the players invites another for a game if he says yes than the one who invited sends the other player the message in this format: -----; mistakes; nrHits

    When the player who's guessing the word finds it out or makes wrong guesses in a number superior to the maximum permitted by the level, then the roles switch and the one who started the game is now in the position of "invited" and has to guess a word. This goes on until each of the players sends 5 words for the other to guess, 10 games are played in total. The the socket is closed.

    I can't figure out the mechanics of it. Please help me! Here's what i have so far:

    package HangMan;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.lang.String;
    import java.net.Socket;
    import java.util.ArrayList;
    import javax.swing.JOptionPane;
     
    public class Game implements Runnable {
     
        static boolean listening, end = false;
        Player player;
        String opponentNickname, difficulty, theme, word, opponentIp, msg, request, tempp, attempt;
        BufferedReader in;
        PrintWriter out;
        String[] fields;
        ArrayList usedLetters = new ArrayList();
        private char[] _word;
        private char[] guess_word;
        private int mistakes, mistakesAllowed, times, myScore, scoreOtherplayer, gamesCounter = 0;
        private Socket connection;
     
        public Game(Socket connection, Player player, String difficulty, BufferedReader in, PrintWriter out) {
            this.difficulty = difficulty;
            switch (difficulty) {
                case "0":
                    setMistakesAllowed(10);
                    break;
                case "1":
                    setMistakesAllowed(7);
                    break;
                case "2":
                    setMistakesAllowed(5);
                    break;
                default:
                    System.out.println("Erro");
                    break;
            }
     
            this.connection = connection;
            this.player = player;
            this.in = in;
            this.out = out;
        }
     
        @Override
        public void run() {
            while (gamesCounter <= 5) {
                gamesCounter++;
                if (player.isChallenger()) {
                    playersTurn();
                    opponentsTurn();
                    playersTurn();
                    opponentsTurn();
                    playersTurn();
                } else {
                    opponentsTurn();
                    playersTurn();
                    opponentsTurn();
                    playersTurn();
                    opponentsTurn();
                }
            }
        }
     
        public void checkLetter(String letter) {
            times = 0;
    //        letter = letter.toUpperCase();
            if (usedLetters.contains(letter)) {
                //JOptionPane.showMessageDialog(null, "Já tentou essa letra!");
                System.out.println("Player already tried the letter");
                drawWord(false);
            } else {
                // Armazena a letra nova na lista de tentativas.
                usedLetters.add(letter);
                if (word.contains(letter)) {
                    // Acertou
                    times++;
                    drawWord(false);
                } else {
                    System.out.println("Player lost ");
                    mistakes++;
                    drawWord(false);
                }
            }
        }
     
        public void drawWord(boolean showWholeWord) {
            String tempText = new String();
            boolean oneMissingFlag = false;
            for (int n = 1; n <= word.length(); n++) {//""shuffles" the word banana = - - - - - -
                if ((showWholeWord) || (usedLetters.contains(word.substring(n - 1, n)))) {
                    tempText = tempText + word.substring(n - 1, n); //The player lost and the whole word is shown
                } else {
                    System.out.println("Hit");
                    tempText = tempText + "-"; //Player hits and new String is formed 
                    oneMissingFlag = true; //Flag which indicates if only one letter is ye to be guesses
                }
            }
     
            if (!oneMissingFlag) {
                end = true;
                if (mistakes < mistakesAllowed) {//verifica se o numero de tentativas é menor que 6; se for, o usuario ganhou
                    System.out.println("The player won");
                    scoreOtherplayer++;
                    player.setIsChallenger(false);
                    end = true;
                } else {
                    System.out.println("The player lost");
                    player.setIsChallenger(false);
                    end = true;
                }
            }
     
            out.println(tempText + "; " + mistakes + "; " + times);
            out.flush();
        }
     
        private void setMistakesAllowed(int i) {
            this.mistakesAllowed = i;
        }
     
        public void opponentsTurn() {
            while (mistakes < mistakesAllowed || end == false) {
                try {
                    msg = in.readLine();
                    fields = msg.split("; ");
                    System.out.println(msg);
                    attempt = JOptionPane.showInputDialog("Your guess:");
                    out.println(attempt);
                    out.flush();
                } catch (IOException ex) {
                    System.out.println("Error: " + ex);
                }
            }
        }
     
        public void playersTurn() {
            this.word = JOptionPane.showInputDialog(null, "Insert the word for the other player to guess!");
            drawWord(false);
            out.flush();
            while (mistakes < mistakesAllowed || end == false) {
                try {
                    msg = in.readLine();
                    System.out.println(msg);
                    checkLetter(msg);
                    out.flush();
                } catch (IOException ex) {
                    System.out.println("Erro: " + ex);
                }
            }
     
        }
    }

    Please. I know this must be simple but i really can't figure it out. Thanks in advance.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Game - Alternate between players

    Do you have code to send and receive the messages?
    How often are messages sent and by whom?
    Make a list of each message that is sent showing from whom/to whom
    add what the receiver does and what he sends back to the sender.
    The list should show all the important events that take place.
    Then go over your list, double checking and adding things you have forgotten.
    And then do it again until there are no more things to be added to the list.
    Then post it and we'll have a look at it.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Game - Alternate between players

    Quote Originally Posted by Norm View Post
    Do you have code to send and receive the messages?
    How often are messages sent and by whom?
    Make a list of each message that is sent showing from whom/to whom
    add what the receiver does and what he sends back to the sender.
    The list should show all the important events that take place.
    Then go over your list, double checking and adding things you have forgotten.
    And then do it again until there are no more things to be added to the list.
    Then post it and we'll have a look at it.
    Hey Norm. Thank you for the quick response. The applications is fully working except for the "multiplayer" aspect of the game. I can play the first game well but it's the "reversal" of the roles that's not working, the challenger becomes challenged and vice-versa. I've already coded the most part and what each role does is well defined it's how to change from one role to another that's bugging me!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Game - Alternate between players

    to change from one role to another that's bugging me!
    What are the things to be done in each role? Can they be isolated to methods? One for each role. Then have a master method that calls the method for the role to be played.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Game - Alternate between players

    Quote Originally Posted by Norm View Post
    What are the things to be done in each role? Can they be isolated to methods? One for each role. Then have a master method that calls the method for the role to be played.
    Yes. They are in the code in the code i posted along with my first post. The challengers method is playersTurn() and the other one is opponentsTurn(). The master method is the threads run method. Basically the challenger provides the word and checks the challenged attempts to discover it. The challenged only has to input letters.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Game - Alternate between players

    If you have already designed and coded the app, what happens when you execute it?

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Game - Alternate between players

    The first game goes well but then i can't make the challenged become the challenger. What happens is that the challenger in the first place keeps getting asked to input a letter for the next game.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Game - Alternate between players

    i can't make the challenged become the challenger.
    Sounds like you need to go back to the design stage as I suggested in post #2.

  9. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Java Game - Alternate between players

    Java HangMan - Alternate between players

    Duplicate post!
    Improving the world one idiot at a time!

  10. The Following User Says Thank You to Junky For This Useful Post:

    Tjstretch (November 22nd, 2011)

Similar Threads

  1. java game, both players move for player 2 but not player 1
    By ajakking789 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 21st, 2011, 12:52 PM
  2. Help With Java Game Code
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 26th, 2010, 04:07 PM
  3. Need help for my java game
    By blunderblitz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2010, 05:32 AM
  4. Java hangman game help...
    By AnotherNoob in forum AWT / Java Swing
    Replies: 16
    Last Post: December 4th, 2009, 11:17 PM
  5. Help with 1st Java applet game!
    By Sneak in forum Java Applets
    Replies: 0
    Last Post: November 28th, 2009, 11:20 AM

Tags for this Thread