Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
319 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,102 @@ | ||
import java.util.ArrayList; | ||
import java.util.Objects; | ||
|
||
public class Jogador { | ||
|
||
public static final String SENHA_PADRAO = "1234"; | ||
public static final int PONTUACAO_INICIAL = 1000; | ||
|
||
private String username; | ||
private String senha; | ||
private int pontuacao; | ||
|
||
private boolean online; | ||
private boolean jogando; | ||
|
||
private ArrayList<Partida> minhasPartidas; | ||
|
||
public Jogador(String username) { | ||
this(username, SENHA_PADRAO); | ||
} | ||
|
||
public Jogador(String username, String senha) { | ||
this.username = username; | ||
this.senha = senha; | ||
this.pontuacao = PONTUACAO_INICIAL; | ||
this.online = false; | ||
this.jogando = false; | ||
this.minhasPartidas = new ArrayList<>(); | ||
} | ||
|
||
public void adicionarPartidaJogada(Partida partida) { | ||
this.minhasPartidas.add(partida); | ||
|
||
switch (partida.getResultado()) { | ||
case Partida.VITORIA_JOGADOR_1: | ||
if (partida.getJogador1().equals(this)) { | ||
this.pontuacao += JogoOnline.PONTOS_VITORIA; | ||
} else { | ||
this.pontuacao += JogoOnline.PONTOS_DERROTA; | ||
} | ||
break; | ||
|
||
case Partida.VITORIA_JOGADOR_2: | ||
if (partida.getJogador2().equals(this)) { | ||
this.pontuacao += JogoOnline.PONTOS_VITORIA; | ||
} else { | ||
this.pontuacao += JogoOnline.PONTOS_DERROTA; | ||
} | ||
break; | ||
|
||
case Partida.EMPATE: | ||
this.pontuacao += JogoOnline.PONTOS_EMPATE; | ||
} | ||
} | ||
|
||
public int getPontuacao() { | ||
return pontuacao; | ||
} | ||
|
||
public String getUsername() { | ||
return this.username; | ||
} | ||
|
||
public String getSenha() { | ||
return this.senha; | ||
} | ||
|
||
public void setSenha(String senha) { | ||
// ToDo: validar senha segunda as regras do sistema para senhas permitidas | ||
|
||
this.senha = senha; | ||
} | ||
|
||
public boolean isOnline() { | ||
return online; | ||
} | ||
|
||
public void setOnline(boolean online) { | ||
this.online = online; | ||
} | ||
|
||
public boolean isJogando() { | ||
return jogando; | ||
} | ||
|
||
public void setJogando(boolean jogando) { | ||
this.jogando = jogando; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Jogador jogador = (Jogador) o; | ||
return Objects.equals(username, jogador.username); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(username); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,175 @@ | ||
import java.util.ArrayList; | ||
import java.util.Random; | ||
|
||
public class JogoOnline { | ||
|
||
public static final int PONTOS_VITORIA = 1; | ||
public static final int PONTOS_EMPATE = 0; | ||
public static final int PONTOS_DERROTA = -1; | ||
|
||
private Random random; | ||
|
||
private ArrayList<Jogador> jogadores; | ||
|
||
public JogoOnline() { | ||
this.random = new Random(); | ||
this.jogadores = new ArrayList<>(); | ||
} | ||
|
||
/** | ||
* Cadastra um novo jogador. Se já existir, não cadastra outro igual. | ||
* | ||
* @param username | ||
* @param senha | ||
* | ||
* @return O novo jogador cadastrado. | ||
* Ou null, caso o cadastro não tenha sido feito. | ||
*/ | ||
public Jogador cadastrarJogador(String username, String senha) { | ||
if (encontrarJogador(username) != null) { | ||
return null; // nada a fazer, pois o jogador já existe! | ||
} | ||
|
||
// jogador não existe, será cadastrado | ||
Jogador novoJogador = new Jogador(username); | ||
novoJogador.setSenha(senha); | ||
|
||
this.jogadores.add(novoJogador); | ||
|
||
return novoJogador; | ||
} | ||
|
||
private Jogador encontrarJogador(String username) { | ||
for (Jogador jogador : this.jogadores) { | ||
if (jogador.getUsername().equals(username)) { | ||
return jogador; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public boolean fazerLogin(String username, String senha) { | ||
// for (int i = 0; i < this.jogadores.size(); i++) { | ||
// Jogador jogador = this.jogadores.get(i); | ||
// if (jogador.getUsername().equals(username)) { | ||
// if (jogador.getSenha().equals(senha)) { | ||
// jogador.setOnline(true); | ||
// return true; | ||
// } | ||
// break; | ||
// } | ||
// } | ||
// return false; | ||
// | ||
// // outro jeito... | ||
// | ||
// for (Jogador jogador : this.jogadores) { // for each | ||
// if (jogador.getUsername().equals(username)) { | ||
// if (jogador.getSenha().equals(senha)) { | ||
// jogador.setOnline(true); | ||
// return true; | ||
// } | ||
// break; | ||
// } | ||
// } | ||
// return false; | ||
|
||
// melhor jeito (uma vez que já temos um método para localizar o jogador)... | ||
|
||
Jogador jogador = encontrarJogador(username); | ||
if (jogador != null) { | ||
if (jogador.getSenha().equals(senha)) { | ||
jogador.setOnline(true); | ||
return true; | ||
} | ||
} | ||
return false; | ||
|
||
} | ||
|
||
public void fazerLogout(String username) { | ||
Jogador jogador = encontrarJogador(username); | ||
if (jogador != null) { | ||
jogador.setOnline(false); | ||
} | ||
} | ||
|
||
/** | ||
* Inicia uma partida. | ||
* | ||
* @param jogador1 O primeiro jogador | ||
* @param jogador2 O segundo jogador | ||
* | ||
* @return O novo objeto Partida criado; | ||
* ou null caso um dos jogadores esteja offline ou já envolvido em outra partida | ||
*/ | ||
public Partida iniciarPartida(Jogador jogador1, Jogador jogador2) { | ||
if (jogador1 == null || jogador2 == null || | ||
!jogador1.isOnline() || jogador1.isJogando() || | ||
!jogador2.isOnline() || jogador2.isJogando()) { | ||
return null; | ||
} | ||
|
||
Partida novaPartida = new Partida(jogador1, jogador2); | ||
jogador1.setJogando(true); | ||
jogador2.setJogando(true); | ||
|
||
novaPartida.setResultado(Partida.PARTIDA_EM_ANDAMENTO); | ||
|
||
return novaPartida; | ||
} | ||
|
||
public void encerrarPartida(Partida partida, int resultado) { | ||
if (partida.getResultado() != Partida.PARTIDA_EM_ANDAMENTO) { | ||
// a partida não está em andamento, não posso atribuir resultado! | ||
return; // ToDo lançar uma exceção!!! | ||
} | ||
|
||
if (resultado != Partida.EMPATE && | ||
resultado != Partida.VITORIA_JOGADOR_1 && | ||
resultado != Partida.VITORIA_JOGADOR_2) { | ||
// resultado inválido, não faz nada! | ||
return; // ToDo lançar uma excecão!!! | ||
} | ||
|
||
partida.setResultado(resultado); | ||
|
||
Jogador jogador1 = partida.getJogador1(); | ||
Jogador jogador2 = partida.getJogador2(); | ||
|
||
jogador1.adicionarPartidaJogada(partida); | ||
jogador2.adicionarPartidaJogada(partida); | ||
|
||
jogador1.setJogando(false); | ||
jogador2.setJogando(false); | ||
} | ||
|
||
/** | ||
* Escolhe um adversário aleatório que esteja online e não esteja jogando, | ||
* distinto do jogador solicitante. | ||
* | ||
* @param solicitante o jogador solicitante | ||
* | ||
* @return O adversário escolhido, se encontrar algum; | ||
* ou null, caso não encontre nenhum que atenda às condições | ||
*/ | ||
public Jogador escolherAdversario(Jogador solicitante) { | ||
int posicaoInicial = this.random.nextInt(this.jogadores.size()); | ||
|
||
int posicaoCorrente = posicaoInicial; | ||
do { | ||
Jogador adversario = this.jogadores.get(posicaoCorrente); | ||
if (adversario.isOnline() && | ||
!adversario.isJogando() && | ||
!adversario.equals(solicitante)) { | ||
return adversario; | ||
} | ||
posicaoCorrente++; | ||
if (posicaoCorrente == this.jogadores.size()) { | ||
posicaoCorrente = 0; | ||
} | ||
} while (posicaoCorrente != posicaoInicial); | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,47 @@ | ||
public class Partida { | ||
|
||
// ToDo usar enums!!!! (muito melhor) | ||
public static final int VITORIA_JOGADOR_1 = 1; | ||
public static final int VITORIA_JOGADOR_2 = 2; | ||
public static final int EMPATE = 0; | ||
public static final int PARTIDA_EM_ANDAMENTO = -1; | ||
public static final int PARTIDA_NAO_INICIADA = -2; | ||
|
||
private final Jogador jogador1; | ||
|
||
private final Jogador jogador2; | ||
|
||
private int resultado; | ||
|
||
public Partida(Jogador jogador1, Jogador jogador2) { | ||
this.jogador1 = jogador1; | ||
this.jogador2 = jogador2; | ||
this.resultado = PARTIDA_NAO_INICIADA; | ||
} | ||
|
||
public Jogador getJogador1() { | ||
return jogador1; | ||
} | ||
|
||
public Jogador getJogador2() { | ||
return jogador2; | ||
} | ||
|
||
public void setResultado(int resultado) { | ||
this.resultado = resultado; | ||
} | ||
|
||
/** | ||
* Retorna o resultado da partida. | ||
* | ||
* @return -1 para partida em andamento | ||
* 0 para empate | ||
* 1 para vitória do jogador 1 | ||
* 2 para vitória do jogador 2 | ||
* | ||
* ToDo: usar enum!!!! (seria muito melhor) | ||
*/ | ||
public int getResultado() { | ||
return this.resultado; | ||
} | ||
} |