master
Louison PARANT 1 year ago
parent 16cd43e3f7
commit 3e6f0b19b7

@ -0,0 +1,28 @@
package main;
public class Fibonacci {
public Integer F(int i) throws Exception{
if(i==0) {
return 0;
}
if(i==1) {
return 1;
}
if(i<0) {
throw new ArithmeticException();
}
if(i>100) {
throw new ArithmeticException();
}
{
int res=0, ret1=0, ret2=1;
for(int j=2; j<=i; j++) {
res=ret1+ret2;
ret1=ret2;
ret2=res;
}
return res;
}
}
}

@ -0,0 +1,34 @@
package main;
public class PGCD {
public Integer pgcd(int x, int y) throws Exception{
int res=0, min;
if(x>100 || x<-100 || y>100 || y<-100) {
throw new ArithmeticException();
}
if(x<0) {
x=0-x;
}
if(y<0) {
y=0-y;
}
if(x==0 && y==0) {
res=0;
}
else {
if(x<=y) {
min=x;
}
else {
min=y;
}
for (int i=1; i<=min; i++) {
if(x%i==0 && y%i==0) {
res=i;
}
}
}
return res;
}
}

@ -0,0 +1,22 @@
package main;
public class Pythagore {
public boolean P(int x, int y, int z) {
if(x<-100 || y<-100 || z<-100) {
return false;
}
if(x>100 || y>100 || z>100) {
throw new ArithmeticException();
}
if(x<=0 || y<=0 || y<=0) {
return false;
}
if((x*x)>=Integer.MAX_VALUE || (y*y)>=Integer.MAX_VALUE || (z*z)>=Integer.MAX_VALUE || (x*x)<0 || (y*y)<0 || (z*z)<0) {
return false;
}
if((x*x)+(y*y)!=(z*z) && (x*x)+(z*z)!=(y*y) && (y*y)+(z*z)!=(x*x)) {
return false;
}
return true;
}
}

@ -0,0 +1,118 @@
package main.morpion;
public class Grid {
public String[][] grid = new String[3][3];
public String lettre = "X";
public void insert(String pos) throws Exception {
if(pos=="A1") {
if(grid[0][0]=="A1") {
grid[0][0]=lettre;
}
else {
throw new Exception();
}
}
else if(pos=="A2") {
if(grid[0][1]=="A2") {
grid[0][1]=lettre;
}
else {
throw new Exception();
}
}
else if(pos=="A3") {
if(grid[0][2]=="A3") {
grid[0][2]=lettre;
}
else {
throw new Exception();
}
}
else if(pos=="B1") {
if(grid[1][0]=="B1") {
grid[1][0]=lettre;
}
else {
throw new Exception();
}
}
else if(pos=="B2") {
if(grid[1][1]=="B2") {
grid[1][1]=lettre;
}
else {
throw new Exception();
}
}
else if(pos=="B3") {
if(grid[1][2]=="B3") {
grid[1][2]=lettre;
}
else {
throw new Exception();
}
}
else if(pos=="C1") {
if(grid[2][0]=="C1") {
grid[2][0]=lettre;
}
else {
throw new Exception();
}
}
else if(pos=="C2") {
if(grid[2][1]=="C2") {
grid[2][1]=lettre;
}
else {
throw new Exception();
}
}
else if(pos=="C3") {
if(grid[2][2]=="C3") {
grid[2][2]=lettre;
}
else {
throw new Exception();
}
}
else {
throw new Exception();
}
if(lettre == "X") {
lettre = "O";
}
else {
lettre = "X";
}
}
public boolean isWin() {
if(grid[0][2]==grid[1][2] && grid[1][2]==grid[2][2]) {
return true;
}
if(grid[2][0]==grid[2][1] && grid[2][1]==grid[2][2]) {
return true;
}
if(grid[0][0]==grid[1][1] && grid[1][1]==grid[2][2]) {
return true;
}
for (Integer j=0; j<3; j++) {
if(grid[j][0]==grid[j][1] && grid[j][1]==grid[j][2]) {
return true;
}
if(grid[0][j]==grid[1][j] && grid[1][j]==grid[2][j]) {
return true;
}
}
if((grid[0][0]==grid[1][1] && grid[1][1]==grid[2][2]) || (grid[0][2]==grid[1][1] && grid[1][1]==grid[2][0])) {
return true;
}
return false;
}
public String getString(Integer x, Integer y) {
return grid[x][y];
}
}

@ -0,0 +1,64 @@
package main.morpion;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Morpion {
public Integer tour = 0;
public Grid grille = new Grid();
public List<String> list = new ArrayList<>();
public void jouer() throws Exception {
Scanner sc = new Scanner(System.in);
list.add("A1");
list.add("A2");
list.add("A3");
list.add("B1");
list.add("B2");
list.add("B3");
list.add("C1");
list.add("C2");
list.add("C3");
while(tour < 4) {
if(tour==4) {
grille.insert(list.get(0));
}
else {
afficherGrille();
System.out.println("Joueur 1, select an available slot : ");
String choice = sc.nextLine();
grille.insert(choice);
if(testWin(1)) {
System.out.println("Joueur 1 a gagné");
sc.close();
return;
}
afficherGrille();
System.out.println("Joueur 2, select an available slot : ");
choice = sc.nextLine();
grille.insert(choice);
if(testWin(2)) {
System.out.println("Joueur 2 a gagné");
sc.close();
return;
}
}
}
System.out.println("Égalité");
sc.close();
}
private boolean testWin(int i) {
if(tour < 3) {
return false;
}
return grille.isWin();
}
public void afficherGrille() {
for(Integer i=0; i<3; i++) {
System.out.println(grille.grid[i][0] + " | " + grille.grid[i][1] + " | " + grille.grid[i][2]);
}
}
}

@ -0,0 +1,46 @@
package test;
import java.util.Random;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import main.Fibonacci;
public class FibonacciTests {
public Fibonacci fi=new Fibonacci();
@Test
public void testFibonacci() {
try {
Assertions.assertEquals(0, fi.F(0));
Assertions.assertEquals(1, fi.F(1));
Assertions.assertEquals(1, fi.F(2));
Assertions.assertEquals(2, fi.F(3));
Assertions.assertEquals(3, fi.F(4));
Assertions.assertEquals(5, fi.F(5));
Assertions.assertThrows(ArithmeticException.class, ()->fi.F(-1));
Assertions.assertThrows(ArithmeticException.class, ()->fi.F(Integer.MIN_VALUE));
Assertions.assertThrows(ArithmeticException.class, ()->fi.F(Integer.MAX_VALUE));
int x = new Random().nextInt();
if(x<0) {
Assertions.assertThrows(ArithmeticException.class, ()->fi.F(x));
}
else {
int res=0, ret1=0, ret2=1;
for(int i=0; i<x; i++) {
if(i==0) {
res=0;
}
if(i==1) {
res=1;
}
else {
res=ret1+ret2;
ret1=ret2;
ret2=res;
}
Assertions.assertEquals(res, fi.F(x));
}
}
} catch (Exception e) {}
}
}

@ -0,0 +1,43 @@
package test;
import java.util.Random;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import main.PGCD;
public class PGCDTests {
public PGCD pgcd = new PGCD();
@Test
public void testPGCD() {
try {
Assertions.assertEquals(0, pgcd.pgcd(0, 0));
Assertions.assertEquals(12, pgcd.pgcd(96, 36));
Assertions.assertEquals(12, pgcd.pgcd(36, 96));
Assertions.assertEquals(2, pgcd.pgcd(4, -2));
Assertions.assertEquals(2, pgcd.pgcd(-4, 2));
Assertions.assertEquals(2, pgcd.pgcd(-2, -2));
Assertions.assertThrows(ArithmeticException.class, ()->pgcd.pgcd(Integer.MIN_VALUE, Integer.MIN_VALUE));
Assertions.assertThrows(ArithmeticException.class, ()->pgcd.pgcd(Integer.MAX_VALUE, Integer.MAX_VALUE));
int x = new Random().nextInt(), y = new Random().nextInt();
int res=0, min;
if(x==0 && y==0) {
res=0;
}
else {
if(x<=y) {
min=x;
}
else {
min=y;
}
for (int i=1; i<min; i++) {
if(x%i==0 && y%i==0) {
res=i;
}
}
}
Assertions.assertEquals(res, pgcd.pgcd(x, y));
} catch(Exception e) {}
}
}

@ -0,0 +1,46 @@
package test;
import java.util.Random;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import main.Pythagore;
public class PythagoreTests {
public Pythagore py = new Pythagore();
@Test
public void testPythagore() {
try {
Assertions.assertEquals(false, py.P(0, 0, 0));
Assertions.assertEquals(false, py.P(-2, 2, 2));
Assertions.assertEquals(false, py.P(2, -2, 2));
Assertions.assertEquals(false, py.P(2, 2, -2));
Assertions.assertEquals(false, py.P(2, 2, 2));
Assertions.assertEquals(true, py.P(3, 4, 5));
Assertions.assertEquals(true, py.P(4, 5, 3));
Assertions.assertEquals(true, py.P(5, 3, 4));
Assertions.assertEquals(false, py.P(Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE));
Assertions.assertThrows(ArithmeticException.class, ()->py.P(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE));
int x = new Random().nextInt(), y = new Random().nextInt(), z = new Random().nextInt();
boolean res = false;
if (x<0 || y<0 || y<0) {
res = false;
}
else if((x*x)>=Integer.MAX_VALUE || (y*y)>=Integer.MAX_VALUE || (z*z)>=Integer.MAX_VALUE || (x*x)<0 || (y*y)<0 || (z*z)<0) {
res = false;
}
else if((x*x)+(y*y)!=(z*z) && (x*x)+(z*z)!=(y*y) && (y*y)+(z*z)!=(x*x)) {
res = false;
}
else {
res = true;
}
Assertions.assertEquals(res, py.P(x, y, z));
} catch(Exception e) {}
}
}

@ -0,0 +1,285 @@
package test.morpion;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import main.morpion.Grid;
public class GridTests {
public Grid gr = new Grid();
@BeforeEach
public void Reset() {
gr.grid[0][0]="A1";
gr.grid[0][1]="A2";
gr.grid[0][2]="A3";
gr.grid[1][0]="B1";
gr.grid[1][1]="B2";
gr.grid[1][2]="B3";
gr.grid[2][0]="C1";
gr.grid[2][1]="C2";
gr.grid[2][2]="C3";
}
@Test
public void testGridA1() {
try {
gr.insert("A1");
} catch (Exception e) {}
Assertions.assertEquals("X", gr.getString(0, 0));
}
@Test
public void testDoubleInsertA1() {
try {
gr.insert("A1");
} catch (Exception e) {}
Assertions.assertThrows(Exception.class, ()->gr.insert("A1"));
}
@Test
public void testGridA2() {
try {
gr.insert("A2");
} catch (Exception e) {}
Assertions.assertEquals("X", gr.getString(0, 1));
}
@Test
public void testDoubleInsertA2() {
try {
gr.insert("A2");
} catch (Exception e) {}
Assertions.assertThrows(Exception.class, ()->gr.insert("A2"));
}
@Test
public void testGridA3() {
try {
gr.insert("A3");
} catch (Exception e) {}
Assertions.assertEquals("X", gr.getString(0, 2));
}
@Test
public void testDoubleInsertA3() {
try {
gr.insert("A3");
} catch (Exception e) {}
Assertions.assertThrows(Exception.class, ()->gr.insert("A3"));
}
@Test
public void testGridB1() {
try {
gr.insert("B1");
} catch (Exception e) {}
Assertions.assertEquals("X", gr.getString(1, 0));
}
@Test
public void testDoubleInsertB1() {
try {
gr.insert("B1");
} catch (Exception e) {}
Assertions.assertThrows(Exception.class, ()->gr.insert("B1"));
}
@Test
public void testGridB2() {
try {
gr.insert("B2");
} catch (Exception e) {}
Assertions.assertEquals("X", gr.getString(1, 1));
}
@Test
public void testDoubleInsertB2() {
try {
gr.insert("B2");
} catch (Exception e) {}
Assertions.assertThrows(Exception.class, ()->gr.insert("B2"));
}
@Test
public void testGridB3() {
try {
gr.insert("B3");
} catch (Exception e) {}
Assertions.assertEquals("X", gr.getString(1, 2));
}
@Test
public void testDoubleInsertB3() {
try {
gr.insert("B3");
} catch (Exception e) {}
Assertions.assertThrows(Exception.class, ()->gr.insert("B3"));
}
@Test
public void testGridC1() {
try {
gr.insert("C1");
} catch (Exception e) {}
Assertions.assertEquals("X", gr.getString(2, 0));
}
@Test
public void testDoubleInsertC1() {
try {
gr.insert("C1");
} catch (Exception e) {}
Assertions.assertThrows(Exception.class, ()->gr.insert("C1"));
}
@Test
public void testGridC2() {
try {
gr.insert("C2");
} catch (Exception e) {}
Assertions.assertEquals("X", gr.getString(2, 1));
}
@Test
public void testDoubleInsertC2() {
try {
gr.insert("C2");
} catch (Exception e) {}
Assertions.assertThrows(Exception.class, ()->gr.insert("C2"));
}
@Test
public void testGridC3() {
try {
gr.insert("C3");
} catch (Exception e) {}
Assertions.assertEquals("X", gr.getString(2, 2));
}
@Test
public void testDoubleInsertC3() {
try {
gr.insert("C3");
} catch (Exception e) {}
Assertions.assertThrows(Exception.class, ()->gr.insert("C3"));
}
@Test
public void testGridLetterO() {
gr.lettre="O";
try {
gr.insert("C3");
} catch (Exception e) {}
Assertions.assertEquals("O", gr.getString(2, 2));
}
@Test
public void testOutOfGrid() {
Assertions.assertThrows(Exception.class, ()->gr.insert("D7"));
}
@Test
public void testWinVertical() {
try {
gr.insert("A3");
gr.insert("A2");
gr.insert("B3");
gr.insert("B2");
gr.insert("C3");
} catch (Exception e) {}
Assertions.assertEquals(true, gr.isWin());
}
@Test
public void testWinHorizontal() {
try {
gr.insert("C1");
gr.insert("B1");
gr.insert("C2");
gr.insert("B2");
gr.insert("C3");
} catch (Exception e) {}
Assertions.assertEquals(true, gr.isWin());
}
@Test
public void testWinDiagonal() {
try {
gr.insert("A1");
gr.insert("A2");
gr.insert("B2");
gr.insert("B3");
gr.insert("C3");
} catch (Exception e) {}
Assertions.assertEquals(true, gr.isWin());
}
@Test
public void testLose() {
try {
gr.insert("A1");
gr.insert("A2");
} catch (Exception e) {}
Assertions.assertEquals(false, gr.isWin());
}
@Test
public void testRandom() {
boolean res=false;
List<String> list = new ArrayList<>();
Random rand = new Random();
for (Integer j=0; j<=9; j++) {
Integer x=rand.nextInt(3);
Integer y=rand.nextInt(3);
if (x==0 && y ==0) {
list.add("A1");
}
if (x==0 && y ==1) {
list.add("A2");
}
if (x==0 && y ==2) {
list.add("A3");
}
if (x==1 && y ==0) {
list.add("B1");
}
if (x==1 && y ==1) {
list.add("B2");
}
if (x==1 && y ==2) {
list.add("B3");
}
if (x==2 && y ==0) {
list.add("C1");
}
if (x==2 && y ==1) {
list.add("C2");
}
if (x==2 && y ==2) {
list.add("C3");
}
}
try {
for (String i : list){
gr.insert(i);
}
for (Integer j=0; j<3; j++) {
if(gr.grid[j][0]==gr.grid[j][1] && gr.grid[j][1]==gr.grid[j][2]) {
res=true;
}
if(gr.grid[0][j]==gr.grid[1][j] && gr.grid[1][j]==gr.grid[2][j]) {
res=true;
}
}
if((gr.grid[0][0]==gr.grid[1][1] && gr.grid[1][1]==gr.grid[2][2]) || (gr.grid[0][2]==gr.grid[1][1] && gr.grid[1][1]==gr.grid[2][0])) {
res=true;
}
} catch (Exception e) {}
Assertions.assertEquals(res, gr.isWin());
}
}
Loading…
Cancel
Save