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);
}
}
}
}