Java HangMan - 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 HangMan - Alternate between players