Java Game - Alternate between players
Hey guys. I'm new to the forum and this is my first post so be nice! :rolleyes:
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:
Code :
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.
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.
Re: Java Game - Alternate between players
Quote:
Originally Posted by
Norm
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!
Re: Java Game - Alternate between players
Quote:
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.
Re: Java Game - Alternate between players
Quote:
Originally Posted by
Norm
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.
Re: Java Game - Alternate between players
If you have already designed and coded the app, what happens when you execute it?
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.
Re: Java Game - Alternate between players
Quote:
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.
Re: Java Game - Alternate between players