Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
Finisco parte Java
Browse files Browse the repository at this point in the history
  • Loading branch information
VitalyOlegovic committed Jan 17, 2018
1 parent da8f825 commit 8f4f19b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
37 changes: 19 additions & 18 deletions java/src/main/java/lambdaroma/LambdaGardenDue.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public String toString() {
}
}

static final Cella CELLA_ZERO = new Cella(0,0);

public Character aggrega(Character a, Character b){
if(a=='X' && b=='X')return 'X';
if(a=='O' && b=='O')return 'O';
Expand Down Expand Up @@ -63,14 +65,6 @@ Optional<Character> estrai(Character[][] partita, Cella cella){

}

public Character[] generaVettoreGen(Character[][] partita, Cella inizio, UnaryOperator<Cella> successore){
return Stream.iterate(inizio,successore)
.limit(partita.length * partita.length)
.map(cella -> estrai(partita,cella))
.filter(opt -> opt.isPresent())
.map(opt -> opt.get())
.toArray(size->new Character[size]);
}

public Character[] generaVettore(Character[][] partita, Cella inizio, UnaryOperator<Cella> successore){
return Stream.iterate(inizio,successore)
Expand All @@ -79,45 +73,52 @@ public Character[] generaVettore(Character[][] partita, Cella inizio, UnaryOpera
.toArray(size->new Character[size]);
}

public Stream<Character> generaStream(Character[][] partita, Cella inizio, UnaryOperator<Cella> successore){
return Stream.iterate(inizio,successore)
.limit(partita.length)
.map(cella -> partita[cella.x][cella.y]);
}

public Character aggregaVincitore(Character a, Character b){
if(a=='X' && b==' ') return 'X';
if(a==' ' && b=='X') return 'X';
if(a=='O' && b==' ') return 'O';
if(a==' ' && b=='O') return 'O';
if(a==' ' && b==' ') return ' ';
throw new IllegalArgumentException();
throw new IllegalArgumentException(a + " " + b);
}

public Character trovaVincitoreRighe(Character[][] partita){
Optional<Character> opt = Arrays.stream(partita)
.map(riga -> Arrays.stream(riga))
.map(stream->stream.reduce(this::aggrega))
.map(optional->optional.get())
.reduce(this::aggrega);
.reduce(this::aggregaVincitore);
return opt.get();
}

public Character trovaVincitore(Character[][] partita, UnaryOperator<Cella> successore){
Optional<Character> opt = Stream.iterate(new Cella(0,0),this::cellaSuccessivaRighe)
public Character trovaVincitoreColonne(Character[][] partita){
Optional<Character> opt = Stream.iterate(CELLA_ZERO,this::cellaSuccessivaRighe)
.limit(partita.length)
.map(cella -> generaVettore(partita,cella, this::cellaSuccessivaColonne))
.map(riga -> Arrays.stream(riga))
.map(stream->stream.reduce(this::aggrega))
.map(optional->optional.get())
.reduce(this::aggrega);
.reduce(this::aggregaVincitore);
return opt.get();
}

public Character trovaVincitoreColonne(Character[][] partita){
return trovaVincitore(partita, this::cellaSuccessivaColonne);
}

public Character trovaVincitoreDiagonaleMaggiore(Character[][] partita){
return trovaVincitore(partita, this::cellaSuccessivaDiagonaleMaggiore);
Optional<Character> opt = generaStream(partita,CELLA_ZERO, this::cellaSuccessivaDiagonaleMaggiore)
.reduce(this::aggrega);
return opt.get();
}

public Character trovaVincitoreDiagonaleMinore(Character[][] partita){
return trovaVincitore(partita, this::cellaSuccessivaDiagonaleMinore);
Optional<Character> opt = generaStream(partita,new Cella(partita.length-1,0), this::cellaSuccessivaDiagonaleMinore)
.reduce(this::aggrega);
return opt.get();
}

public Character vincitoreMatrice(Character[][] partita){
Expand Down
37 changes: 37 additions & 0 deletions java/src/test/java/lambdaroma/LambdaGardenDueTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lambdaroma;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import lambdaroma.LambdaGardenDue.Cella;
Expand Down Expand Up @@ -64,4 +65,40 @@ void verificaParteB(){
assertEquals(new Cella(1,1), lambdaGardenDue.cellaSuccessivaDiagonaleMinore(new Cella(2,0)));
}

@Test
void verificaParteC(){
Character[] rigaA = {' ','O','X'};
assertArrayEquals(rigaA, lambdaGardenDue.generaVettore(matriceA,new Cella(0,0), lambdaGardenDue::cellaSuccessivaRighe));
Character[] rigaB = {' ',' ','X'};
assertArrayEquals(rigaB, lambdaGardenDue.generaVettore(matriceA,new Cella(0,0), lambdaGardenDue::cellaSuccessivaColonne));
Character[] rigaC = {' ','X',' '};
assertArrayEquals(rigaC, lambdaGardenDue.generaVettore(matriceA,new Cella(0,0), lambdaGardenDue::cellaSuccessivaDiagonaleMaggiore));
Character[] rigaD = {'X','X','X'};
assertArrayEquals(rigaD, lambdaGardenDue.generaVettore(matriceA,new Cella(2,0), lambdaGardenDue::cellaSuccessivaDiagonaleMinore));
}

@Test
void verificaParteD1(){
assertEquals(Character.valueOf('X'), lambdaGardenDue.vincitoreMatrice(matriceA));
}

@Test
void verificaParteD2(){
assertEquals(Character.valueOf(' '), lambdaGardenDue.vincitoreMatrice(matriceB));
}

@Test
void verificaParteD3(){
assertEquals(Character.valueOf('O'), lambdaGardenDue.vincitoreMatrice(matriceC));
}

@Test
void verificaParteD4(){
assertEquals(Character.valueOf('X'), lambdaGardenDue.vincitoreMatrice(matriceD));
}

@Test
void verificaParteE(){
assertEquals(Character.valueOf('X'), lambdaGardenDue.vincitoreMatrice(matrice10x10));
}
}

0 comments on commit 8f4f19b

Please sign in to comment.