diff --git a/Praktikum/VINF_TicTacToe/src/tictactoePraktikum/TicTacToe.java b/Praktikum/VINF_TicTacToe/src/tictactoePraktikum/TicTacToe.java index df35419c8f1eebcd7d26ba947918ff31781628f2..acf39a3c429c2cf1bbde5f83ec7e0fb779137f1f 100644 --- a/Praktikum/VINF_TicTacToe/src/tictactoePraktikum/TicTacToe.java +++ b/Praktikum/VINF_TicTacToe/src/tictactoePraktikum/TicTacToe.java @@ -19,6 +19,8 @@ public class TicTacToe { private char[] spielerZeichen = new char[2]; private char[][] spielbrett = new char [3][3]; + private int anzahlZuege = 0; + public TicTacToe(TicTacToeUI ui){ @@ -35,6 +37,8 @@ public class TicTacToe { */ void resetSpiel(){ + anzahlZuege = 0; + for(char zeile[]: spielbrett ) { for (int i = 0; i < zeile.length; i++) { @@ -171,6 +175,8 @@ public class TicTacToe { do { + anzahlZuege++; + spielzustand = spielzug(aktuellerSpieler); aktuellerSpieler = getNaechstenSpieler(aktuellerSpieler); @@ -194,9 +200,9 @@ public class TicTacToe { for(int i=0; i<3; i++){ if ((spielbrett[i][0] + spielbrett[i][1] + - spielbrett[i][0] == 3*spieler) || + spielbrett[i][2] == 3*spieler) || (spielbrett[0][i] + spielbrett[1] [i] + - spielbrett[i][2] == 3*spieler)|| + spielbrett[2][i] == 3*spieler)|| (spielbrett[0][0] + spielbrett[1][1] + spielbrett[2][2] == 3*spieler) ||(spielbrett[2][0] + spielbrett[1][1] + spielbrett[0][2] == @@ -228,6 +234,10 @@ public class TicTacToe { public void setSpielbrett(char[][] spielbrett) { this.spielbrett = spielbrett; } + + public int getAnzahlZuege(){ + return anzahlZuege; + } } diff --git a/Praktikum/VINF_TicTacToe/test/tictactoePraktikum/MockUpClass.java b/Praktikum/VINF_TicTacToe/test/tictactoePraktikum/MockUpClass.java index 817835a5e6b76f4a9713e60070d96c947fead245..71fb467ec03b332947acee214314e2ce832d4409 100644 --- a/Praktikum/VINF_TicTacToe/test/tictactoePraktikum/MockUpClass.java +++ b/Praktikum/VINF_TicTacToe/test/tictactoePraktikum/MockUpClass.java @@ -1,9 +1,22 @@ package tictactoePraktikum; public class MockUpClass implements TicTacToeUI{ + + int testcase; + int index; + + String[] testcase1 = {"a", "00", "%%", "22"}; + String[] testcase2 = {"11", "01", "00", "22", "10", "20", "14", "12"}; @Override public String getZugeingabe(char[][] spielbrett, char aktuellerSpieler) { + index++; + if (testcase == 1 && index<testcase1.length){ + return testcase1[index]; + } + if (testcase == 2 && index<testcase2.length){ + return testcase2[index]; + } return null; } } diff --git a/Praktikum/VINF_TicTacToe/test/tictactoePraktikum/TicTacToeTest.java b/Praktikum/VINF_TicTacToe/test/tictactoePraktikum/TicTacToeTest.java index 95d14c9d5c583f8563603bb9c02d4823e0410933..4bfb9d0747c72c444c284bcebf388f7c54552945 100644 --- a/Praktikum/VINF_TicTacToe/test/tictactoePraktikum/TicTacToeTest.java +++ b/Praktikum/VINF_TicTacToe/test/tictactoePraktikum/TicTacToeTest.java @@ -10,13 +10,15 @@ import static org.junit.Assert.*; public class TicTacToeTest { private static TicTacToe ttt; + private static MockUpClass mockUp; /** * @throws java.lang.Exception */ @BeforeClass public static void SetUpBeforeClass() throws Exception { - ttt = new TicTacToe(new MockUpClass()); + mockUp = new MockUpClass(); + ttt = new TicTacToe(mockUp); } /** @@ -41,7 +43,7 @@ public class TicTacToeTest { @Test public void testGetNaechstenSpieler() { assertEquals(1, ttt.getNaechstenSpieler(0)); - assertEquals(2, ttt.getNaechstenSpieler(1)); + assertEquals(0, ttt.getNaechstenSpieler(1)); assertEquals(1, ttt.getNaechstenSpieler(42)); } @@ -61,36 +63,69 @@ public class TicTacToeTest { assertFalse(ttt.isGueltig(3, 0)); } - /** - * Test method for {@link tictactoePraktikum.TicTacToe#isUnentschieden()}. - */ - @Test - public void testIsUnentschieden() { - fail("Not yet implemented"); - } - /** * Test method for {@link tictactoePraktikum.TicTacToe#getSpielzustand()}. */ @Test public void testGetSpielzustand() { - fail("Not yet implemented"); + char[][] spielbrett0 = {{'X','X','X'},{' ',' ',' '},{' ',' ',' '}}; + ttt.setSpielbrett(spielbrett0); + assertEquals(1, ttt.getSpielzustand()); + char[][] spielbrett1 = {{'X',' ',' '},{'X',' ',' '},{'X',' ',' '}}; + ttt.setSpielbrett(spielbrett1); + assertEquals(1, ttt.getSpielzustand()); + char[][] spielbrett2 = {{'X',' ',' '},{' ','X',' '},{' ',' ','X'}}; + ttt.setSpielbrett(spielbrett2); + assertEquals(1, ttt.getSpielzustand()); + char[][] spielbrett3 = {{' ',' ','X'},{' ','X',' '},{'X',' ',' '}}; + ttt.setSpielbrett(spielbrett3); + assertEquals(1, ttt.getSpielzustand()); + char[][] spielbrett4 = {{'O','O','O'},{' ',' ',' '},{' ',' ',' '}}; + ttt.setSpielbrett(spielbrett4); + assertEquals(1, ttt.getSpielzustand()); + char[][] spielbrett5 = {{'O',' ',' '},{'O',' ',' '},{'O',' ',' '}}; + ttt.setSpielbrett(spielbrett5); + assertEquals(1, ttt.getSpielzustand()); + char[][] spielbrett6 = {{'O',' ',' '},{' ','O',' '},{' ',' ','O'}}; + ttt.setSpielbrett(spielbrett6); + assertEquals(1, ttt.getSpielzustand()); + char[][] spielbrett7 = {{' ',' ','O'},{' ','O',' '},{'O',' ',' '}}; + ttt.setSpielbrett(spielbrett7); + assertEquals(1, ttt.getSpielzustand()); + char[][] spielbrett8 = {{'X','X','O'},{'O','O','X'},{'X','X','O'}}; + ttt.setSpielbrett(spielbrett8); + assertEquals(2, ttt.getSpielzustand()); + char[][] spielbrett9 = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}}; + ttt.setSpielbrett(spielbrett9); + assertEquals(3, ttt.getSpielzustand()); } /** * Test method for {@link tictactoePraktikum.TicTacToe#spielzug(int)}. */ - @Test (expected=IndexOutOfBoundsException.class) + @Test public void testSpielzug() { - ttt.spielzug(3); + char[][] spielbrett1 = {{' ',' ',' '},{' ','X',' '},{' ',' ',' '}}; + char[][] spielbrett2 = {{'O',' ',' '},{' ','X',' '},{' ',' ',' '}}; + char[][] spielbrett3 = {{'O',' ',' '},{' ','X',' '},{' ',' ','O'}}; + mockUp.testcase = 1; + mockUp.index = -1; + ttt.setSpielbrett(spielbrett1); + assertEquals(4, ttt.spielzug(1)); + assertArrayEquals(spielbrett1, ttt.getSpielbrett()); + assertEquals(3, ttt.spielzug(1)); + assertArrayEquals(spielbrett2, ttt.getSpielbrett()); + assertEquals(3, ttt.spielzug(1)); + assertEquals(3, mockUp.index); + assertArrayEquals(spielbrett3, ttt.getSpielbrett()); } - + /** - * Test method for {@link tictactoePraktikum.TicTacToe#spiel(int)}. + * Test method for {@link tictactoePraktikum.TicTacToe#spielzug(int)} for an IndexOutOfBoundsException. */ - @Test - public void testSpiel() { - fail("Not yet implemented"); + @Test (expected=IndexOutOfBoundsException.class) + public void testSpielzugForIndexOutOFBoundsException() { + ttt.spielzug(3); } /** @@ -106,21 +141,18 @@ public class TicTacToeTest { ttt.setSpielbrett(spielbrettO); assertEquals('O', ttt.getGewinner()); } - - /** - * Test method for {@link tictactoePraktikum.TicTacToe#getSpielbrett()}. - */ - @Test - public void testGetSpielbrett() { - fail("Not yet implemented"); - } - + /** - * Test method for {@link tictactoePraktikum.TicTacToe#setSpielbrett(char[][])}. + * Test method for {@link tictactoePraktikum.TicTacToe#spiel()}. */ @Test - public void testSetSpielbrett() { - fail("Not yet implemented"); + public void testSpiel() { + char[][] spielbrett = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}}; + ttt.setSpielbrett(spielbrett); + mockUp.testcase = 2; + mockUp.index = -1; + ttt.spiel(0); + assertEquals('X', ttt.getGewinner()); + assertEquals(7, ttt.getAnzahlZuege()); } - }