Created
June 1, 2012 18:26
-
-
Save ccalmendra/2854194 to your computer and use it in GitHub Desktop.
Dojo Quixadá - 27/07/2011
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
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertTrue; | |
import org.junit.Test; | |
public class JogoDaVelhaTest { | |
@Test | |
public void aoCriarJogoQuadroEstaVazio() { | |
JogoVelha jogo = new JogoVelha(); | |
String quadroVazio = "_________"; | |
assertEquals(quadroVazio, jogo.getQuadro()); | |
} | |
@Test | |
public void TestesetQuadro() { | |
JogoVelha jogo = new JogoVelha(); | |
String tabuleiro = "XOOO_____"; | |
jogo.setQuadro(tabuleiro); | |
assertEquals(tabuleiro, jogo.getQuadro()); | |
} | |
@Test | |
public void TesteJogada() { | |
JogoVelha jogo = new JogoVelha(); | |
jogo.setJogada(0, 0, 'X'); | |
assertEquals("X________", jogo.getQuadro()); | |
} | |
@Test | |
public void TesteColuna() { | |
JogoVelha jogo = new JogoVelha(); | |
jogo.setJogada(0, 0, 'X'); | |
jogo.setJogada(1, 0, 'X'); | |
jogo.setJogada(2, 0, 'X'); | |
assertEquals(true, jogo.ExisteGanhador()); | |
} | |
@Test | |
public void TesteDiagonal() { | |
JogoVelha jogo = new JogoVelha(); | |
jogo.setJogada(0, 0, 'X'); | |
jogo.setJogada(1, 1, 'X'); | |
jogo.setJogada(2, 2, 'X'); | |
assertTrue(jogo.ExisteGanhador()); | |
} | |
@Test | |
public void TesteDiagonal2() { | |
JogoVelha jogo = new JogoVelha(); | |
jogo.setJogada(0, 2, 'X'); | |
jogo.setJogada(1, 1, 'X'); | |
jogo.setJogada(2, 0, 'X'); | |
assertTrue(jogo.ExisteGanhador()); | |
} | |
} |
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
public class JogoVelha { | |
private char[][] quadro; | |
public JogoVelha() { | |
quadro = new char[3][3]; | |
for (int i = 0; i < 3; i++) | |
for (int j = 0; j < 3; j++) | |
quadro[i][j] = '_'; | |
} | |
public String getQuadro() { | |
char[] tabuleiro; | |
tabuleiro = new char[9]; | |
for (int i = 0; i < 3; i++) | |
for (int j = 0; j < 3; j++) | |
tabuleiro[3 * i + j] = quadro[i][j]; | |
return new String(tabuleiro); | |
} | |
public void setQuadro(String tabuleiro) { | |
for (int i = 0; i < 3; i++) | |
for (int j = 0; j < 3; j++) | |
quadro[i][j] = tabuleiro.charAt(3 * i + j); | |
} | |
public void setJogada(int x, int y, char a) { | |
if (quadro[x][y] == '_') | |
quadro[x][y] = a; | |
} | |
public boolean ExisteGanhador() { | |
if (quadro[0][0] == quadro[1][1] && quadro[1][1] == quadro[2][2]) { | |
return true; | |
} | |
if (quadro[0][2] == quadro[1][1] && quadro[1][1] == quadro[2][0]) { | |
return true; | |
} | |
for (int i = 0; i < 3; i++) { | |
if (quadro[i][0] == quadro[i][1] && quadro[i][1] == quadro[i][2]) | |
return true; | |
if (quadro[0][i] == quadro[1][i] && quadro[1][i] == quadro[2][i]) | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Participantes:
Roberto Cabral, Leonara Medeiros, José Fernandes, Felipe Alves, Pedro Henrique, Paulo Filipe, Lina Raylly, Danilo Nunes, Antônio Cleginaldo, Paulo Ivo, Letícia Mara, Antônio Alessandro, Lucas Ismaily, Rodrigo Girão, Fábio Dias, Wladimir Tavares, Camilo Almendra