Created
February 21, 2010 12:44
-
-
Save antoniopassos/310292 to your computer and use it in GitHub Desktop.
Componentes do AppJogoDeAdivinhacao
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
package net.antoniopassos.appjogodeadivinhacao.dominio; | |
/** | |
* | |
* @author Antonio Passos | |
*/ | |
public class Jogador { | |
private String nome; | |
private int palpite; | |
public Jogador(String nome) { | |
this.nome = nome; | |
this.palpite = 0; | |
} | |
public int getPalpite() { | |
return this.palpite; | |
} | |
public void darPalpite() { | |
this.palpite = (int)(Math.random() * 10); | |
} | |
public String getNome(){ | |
return this.nome; | |
} | |
} |
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
package net.antoniopassos.appjogodeadivinhacao.dominio; | |
import net.antoniopassos.appjogodeadivinhacao.visao.UI; | |
/** | |
* | |
* @author Antonio Passos | |
*/ | |
public class JogoDeAdivinhacao { | |
private Jogador jogadores[]; | |
private Jogador ganhador; | |
private int numeroSorteado; | |
private int rodada; | |
public JogoDeAdivinhacao(Jogador[] jogadores) { | |
this.jogadores = jogadores; | |
} | |
public void jogar() { | |
boolean alguemGanhou = false; | |
this.rodada = 1; | |
do { | |
this.sortearNumero(); | |
this.obterPalpites(); | |
UI ui = new UI(); | |
alguemGanhou = this.hasAcertador(); | |
if (alguemGanhou == true) { | |
ui.imprimirResultado(rodada, ganhador); | |
} else { | |
ui.imprimirResultado(rodada); | |
} | |
rodada++; | |
} while (alguemGanhou == false); | |
} | |
public void sortearNumero() { | |
this.numeroSorteado = (int) (Math.random() * 10); | |
} | |
public void obterPalpites() { | |
for (Jogador jogador : jogadores) { | |
jogador.darPalpite(); | |
} | |
} | |
public boolean hasAcertador() { | |
for (Jogador jogador : jogadores) { | |
int palpite = jogador.getPalpite(); | |
if (palpite == numeroSorteado) { | |
this.ganhador = jogador; | |
return true; | |
} | |
} | |
return false; | |
} | |
public Jogador getGanhador() { | |
return this.ganhador; | |
} | |
public int getNumeroSorteado() { | |
return this.numeroSorteado; | |
} | |
} |
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
package net.antoniopassos.appjogodeadivinhacao; | |
import net.antoniopassos.appjogodeadivinhacao.dominio.Jogador; | |
import net.antoniopassos.appjogodeadivinhacao.dominio.JogoDeAdivinhacao; | |
public class Principal { | |
public static void main(String[] args) { | |
Jogador jogador1 = new Jogador("Fulano"); | |
Jogador jogador2 = new Jogador("Beltrano"); | |
Jogador jogador3 = new Jogador("Cicrano"); | |
Jogador [] jogadores = {jogador1, jogador2, jogador3}; | |
JogoDeAdivinhacao jogo = new JogoDeAdivinhacao(jogadores); | |
jogo.jogar(); | |
} | |
} |
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
package net.antoniopassos.appjogodeadivinhacao.visao; | |
import net.antoniopassos.appjogodeadivinhacao.dominio.Jogador; | |
/** | |
* | |
* @author Antonio Passos | |
*/ | |
public class UI { | |
public void imprimirResultado(int rodada, Jogador ganhador) { | |
System.out.println("Rodada: " + rodada); | |
System.out.println("Ganhador: " + ganhador.getNome()); | |
System.out.println("Palpite dado: " + ganhador.getPalpite()); | |
} | |
public void imprimirResultado(int rodada) { | |
System.out.println("Rodada: " + rodada); | |
System.out.println("Ninguém ganhou!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment