Premier commit - Inclusion dans le système git.

This commit is contained in:
Mysaa 2021-05-23 20:41:37 +02:00
commit 1ad27dca66
8 changed files with 691 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.classpath
.project
.settings/
bin/

View File

@ -0,0 +1,87 @@
package com.bernard.bataille;
import static java.lang.Math.*;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Card {
int type;// couleur*13 + genre
//12->as,11->roi,10->dame,...,0->2
//->0,->1,->2,->3
Pos pos;
public static File backCardImage = new File("/home/mysaa/Pictures/cardBackL.png");
public static File tempCardsPNGFolder = new File("/tmp/cardsPNG/");
public Card(int type, Pos pos) {
this.type = type;
this.pos = pos;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Card [type=" + type + ", pos=" + pos + "]";
}
public void draw(Graphics2D g,int wc, int hc) {
try {
AffineTransform t = g.getTransform();
System.out.println(pos);
Image img = ((pos.flip%(2*PI)>PI/2 && pos.flip%(2*PI)<3*PI/2)?ImageIO.read(cardImage()):ImageIO.read(backCardImage)).getScaledInstance(wc, hc, 0);
t.setToTranslation(pos.x, pos.y);
t.rotate(pos.a+PI/2);
t.scale(1,abs(cos(pos.flip)));
t.translate(-abs(cos(pos.flip))*wc/2, hc/2);//recentrage
g.drawImage(img, t, null);
} catch (IOException e) {
e.printStackTrace();
}
}
public File cardImage() {
//System.err.println(type);
return new File(tempCardsPNGFolder, "card"+type + ".png");
}
public static final class Pos{
public double x,y,a;
public double flip;//-pi/2 to pi/2 : endroit
// autre : envers
public Pos(double x, double y, double flip, double a) {
this.x = x;
this.y = y;
this.flip = flip;
this.a = a;
}
@Override
public String toString() {
return "Pos [x=" + x + ", y=" + y + ", a=" + a + ", flip=" + flip + "]";
}
}
}

View File

@ -0,0 +1,5 @@
package com.bernard.bataille;
public enum CardType {
}

View File

@ -0,0 +1,67 @@
package com.bernard.bataille;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import com.bernard.bataille.Card.Pos;
public class Component implements Renderable{
int[] cards;
Pos[] startPos;
Pos[] destinations;
boolean visible;
int wc,hc;
public Component(int[] cards, Pos[] startPos, Pos[] destinations, boolean visible, int wc, int hc) {
this.cards = cards;
this.startPos = startPos;
this.destinations = destinations;
this.visible = visible;
this.wc = wc;
this.hc = hc;
for (int i = 0; i < destinations.length; i++) {
if(visible) {
this.startPos[i].flip = 0.;
this.destinations[i].flip = Math.PI;
}else {
this.startPos[i].flip = 0.;
this.destinations[i].flip = 0.;
}
}
}
public BufferedImage[] render(BufferedImage background,int frameCount) {
BufferedImage[] out = new BufferedImage[frameCount];
for (int i = 0; i < frameCount; i++) {
BufferedImage image = deepCopy(background);
Graphics2D g = image.createGraphics();
for (int j = 0; j < cards.length; j++) {
Pos actual = new Pos(
startPos[j].x + (destinations[j].x-startPos[j].x)*((double)i/(frameCount-1)),
startPos[j].y + (destinations[j].y-startPos[j].y)*((double)i/(frameCount-1)),
startPos[j].flip + (destinations[j].flip-startPos[j].flip)*((double)i/(frameCount-1)),
startPos[j].a + (destinations[j].a-startPos[j].a)*((double)i/(frameCount-1)));
Card c = new Card(cards[j], actual);
c.draw(g,wc,hc);
}
g.dispose();
out[i] = image;
}
return out;
}
public static BufferedImage deepCopy(BufferedImage bi) {
ColorModel cm = bi.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = bi.copyData(bi.getRaster().createCompatibleWritableRaster());
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
}

View File

@ -0,0 +1,224 @@
package com.bernard.bataille;
import static java.lang.Math.*;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.imageio.ImageIO;
import com.bernard.bataille.Card.Pos;
public class Componentizer {
private int playerCount;
volatile Pos[] startPoz, endPoz;
volatile Double[] directions;//angle 0-2pi, cercle trigo normal (gauche = 0, haut = pi/2)
public static File backgroundPicture = new File("/home/mysaa/Pictures/tapisPoker.jpg");
public static BufferedImage backgroundPictureImage;
private int imgw,imgh,wc,wh;
List<Renderable> renderables;
/**
* Crée un generateur de composants graphiques
* @param playerCount le nombre de joueurs
* @param wc la largeur d'une carte
* @param wh la hauteur d'une carte
*/
public Componentizer(int playerCount, int wc, int wh) {
try {
backgroundPictureImage = ImageIO.read(backgroundPicture);
} catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException("Impossible de charger l'image de fond",e);
}
imgw = backgroundPictureImage.getWidth();
imgh = backgroundPictureImage.getHeight();
System.out.println(imgw+"---"+imgh);
this.playerCount = playerCount;
renderables = new ArrayList<>();
this.wc = wc;
this.wh = wh;
pozRechnen();
}
/**
* @return the renderables
*/
public List<Renderable> getRenderables() {
return renderables;
}
/**
* @return the backgroundPictureImage
*/
public static BufferedImage getBackgroundPictureImage() {
return backgroundPictureImage;
}
private void pozRechnen() {
//TODO
directions = new Double[playerCount];
startPoz = new Pos[playerCount];
endPoz = new Pos[playerCount];
double alpha = atan((double)imgh/imgw);// L'angle limite du coin
for(int i = 0;i<playerCount;i++) {
directions[i] = (((double)i)/playerCount)*2*PI;
double t = 2*PI-directions[i];
double lradius = (wc*5)/cos(PI-(2*PI/playerCount));
endPoz[i] = new Pos((lradius+wh/2)*cos(t)+(imgw/2), (lradius+wh/2)*sin(t)+(imgh/2), Double.NaN, t);
System.out.println("(--_)"+t+"((("+endPoz[i]);
if(t>=2*PI-alpha || t<alpha) {//droite
startPoz[i] = new Pos(imgw, (imgh/2)+(imgw/2)*tan(t), Double.NaN, t);
}else if(t>=alpha && t < PI-alpha) {//haut
startPoz[i] = new Pos( (imgw/2)+(imgh/2)*tan(PI/2+t), imgh, Double.NaN, t);
}else if(t >= PI-alpha && t < PI+alpha) {//gauche
startPoz[i] = new Pos(0, (imgh/2)+(imgw/2)*tan(PI-t), Double.NaN, t);
}else {//bas
startPoz[i] = new Pos( (imgw/2)-(imgh/2)*tan(t-PI), imgh, Double.NaN, t);
}
}
System.out.println(Arrays.toString(startPoz));
System.out.println(Arrays.toString(endPoz));
System.out.println("wow");
}
public void turn(int[] cards, boolean hidden) {
renderables.add(new Component(cards, startPoz, endPoz, !hidden, wc, wh));
}
public void endOfTurn(){
renderables.add(new CardsBackGetter(startPoz, endPoz, directions));
}
public void playerDead(int playerIndex) {
removeIndex(startPoz, playerIndex);
removeIndex(endPoz, playerIndex);
removeIndex(directions, playerIndex);
}
public <T> T[] removeIndex(T[] in,int index){
Object[] result = new Object[in.length - 1];
System.arraycopy(in, 0, result, 0, index);
System.arraycopy(in, index + 1, result, index, in.length - index - 1);
return (T[]) result;
}
public static class CardsBackGetter implements Renderable{
volatile Pos[] startPoz, endPoz;
volatile Double[] directions;//angle 0-2pi, cercle trigo normal (gauche = 0, haut = pi/2)
public static final File handImage = new File("/home/mysaa/Pictures/manchette.png");
public static int wm,hm;
static {
BufferedImage bimg;
try {
bimg = ImageIO.read(handImage);
wm = bimg.getWidth();
hm = bimg.getHeight();
} catch (IOException e) {
e.printStackTrace();
}
}
public CardsBackGetter(Pos[] startPoz, Pos[] endPoz, Double[] directions) {
super();
this.startPoz = startPoz;
this.endPoz = endPoz;
this.directions = directions;
}
@Override
public BufferedImage[] render(BufferedImage background, int frameCount) {
//TODO mains qui ramassent les cartes
// utiliser backgroundPicture
int fCountP1 = frameCount/2;
int fCountP2 = frameCount-fCountP1;
BufferedImage[] out = new BufferedImage[frameCount];
for (int i = 0; i < fCountP1; i++) {
BufferedImage image = Component.deepCopy(background);
Graphics2D g = image.createGraphics();
for (int j = 0; j < directions.length; j++) {
int x = (int) (startPoz[j].x + (endPoz[j].x-startPoz[j].x)*(i/(fCountP1-1))),
y = (int) (startPoz[j].y + (endPoz[j].y-startPoz[j].y)*(i/(fCountP1-1)));
try {
AffineTransform t = g.getTransform();
Image img = ImageIO.read(handImage);
t.setToTranslation(x, y);
t.rotate(directions[j]);
t.translate(wm/2, -hm/2);//recentrage
g.drawImage(img, t, null);
} catch (IOException e) {
e.printStackTrace();
}
}
g.dispose();
out[i] = image;
}
for (int i = 0; i < fCountP2; i++) {
BufferedImage image;
Graphics2D g;
try {
image = ImageIO.read(backgroundPicture);
g = image.createGraphics();
} catch (IOException e1) {
e1.printStackTrace();
return null;
}
for (int j = 0; j < directions.length; j++) {
int x = (int) (startPoz[j].x + (endPoz[j].x-startPoz[j].x)*(1-i/(fCountP2-1))),
y = (int) (startPoz[j].y + (endPoz[j].y-startPoz[j].y)*(1-i/(fCountP2-1)));
try {
AffineTransform t = g.getTransform();
Image img = ImageIO.read(handImage);
t.setToTranslation(x, y);
t.rotate(directions[j]);
t.translate(wm/2, -hm/2);//recentrage
g.drawImage(img, t, null);
} catch (IOException e) {
e.printStackTrace();
}
}
g.dispose();
out[fCountP1 + i] = image;
}
return out;
}
}
}

View File

@ -0,0 +1,137 @@
package com.bernard.crapoyams;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CrapoyamsMotor {
public static int[][] tableDesGains = {
/*Paire*/{0,100,100,200,200,300},
/*2Paire*/{100,200,300,400,500,600},
/*Brelan*/{100,300,400,600,700,900},
/*Carré*/{200,400,600,800,1000,1200},
/*Full*/{200,500,700,1000,1200,1500},
/*Petite suite*/{5000},
/*Grande suite*/{25000},
/*Quinte*/{60000,60000,60000,60000,60000,60000}
};
public static int déFaces = 6;
public static void main(String[] args) {
int nombreLancers = 100000000;
int[] vals = new int[nombreLancers];
for (int n = 1;n < nombreLancers; n++) {
int[] dés = new int[] {
(int)(Math.random()*6+1),
(int)(Math.random()*6+1),
(int)(Math.random()*6+1),
(int)(Math.random()*6+1),
(int)(Math.random()*6+1)
};
vals[n] = gain(dés);
System.out.println(n+":"+Arrays.toString(dés)+"->"+vals[n]);
}
System.out.println("==>"+moyenne(vals));
}
public static int évaluer(int[] dés) {
double[] éspérances = new double[0b100000];
for (int i = 0; i < éspérances.length; i++)
éspérances[i] = espérance(dés, i);
List<Integer> bestMélanges = new ArrayList<>();
double perfection = highest(éspérances);
int pos;
while((pos = indexOf(éspérances, perfection)) != -1) {
éspérances[pos] = Double.NaN;
bestMélanges.add(pos);
}
System.out.println(perfection);
return bestMélanges.get(0);
}
public static double espérance(int[] dés, int mélange) {
if(mélange == 0b00000) {//Aucun mélange, donc l'espérance est le gain réel
double gain = gain(dés);
//System.out.println(Arrays.toString(dés)+" -> " + gain);
return gain;
}else {
int pos = 0;
while((mélange >> pos) % 2 == 0)pos++;
double[] espérances = new double[déFaces];
mélange -= 0b1 << pos;
int[] shuffledDés = Arrays.copyOf(dés, dés.length);
for (int tirage = 1; tirage < déFaces + 1; tirage++) {
shuffledDés[pos] = tirage;
espérances[tirage-1] = espérance(shuffledDés, mélange);
}
return moyenne(espérances);
}
}
public static final int gain(int[] dés) {
Arrays.sort(dés);
int[] counts = new int[déFaces];
Arrays.fill(counts, 0);
for (int i : dés) counts[i-1]++;
int pos = -1;
/* Traitement des cas */
if(Arrays.equals(dés, new int[] {1,2,3,4,5}))
return tableDesGains[6][0];
else if(Arrays.equals(dés, new int[] {0,1,2,3,4}))
return tableDesGains[5][0];
else if((pos = lastIndexOf(counts,5)) != -1)
return tableDesGains[7][pos];
else if((pos = lastIndexOf(counts,4)) != -1)
return tableDesGains[3][pos];
else if((pos = lastIndexOf(counts,3)) != -1)
if(lastIndexOf(counts,2) == -1)
return tableDesGains[2][pos];
else
return tableDesGains[4][pos];
else if((pos = lastIndexOf(counts,2)) != -1)
if(indexOf(counts,2) == pos)
return tableDesGains[0][pos];
else
return tableDesGains[1][pos];
else
return 0;//Aucune combinaison
}
public static final int lastIndexOf(int[] array, int element) {
for (int i = array.length -1; i >= 0; i--)
if(element == array[i])return i;
return -1;
}
public static final int indexOf(int[] array, int element) {
for (int i = 0; i < array.length; i++)
if(element == array[i])return i;
return -1;
}
public static final int indexOf(double[] array, double element) {
for (int i = 0; i < array.length; i++)
if(element == array[i])return i;
return -1;
}
public static final double moyenne(double[] valeurs) {
double moyenne = 0;
for (double d : valeurs)
moyenne += d;
return moyenne/((double)valeurs.length);
}
public static final double moyenne(int[] valeurs) {
double moyenne = 0;
for (double d : valeurs)
moyenne += d;
return moyenne/((double)valeurs.length);
}
public static final double highest(double[] valeurs) {
if(valeurs.length == 0)throw new IllegalArgumentException("valeurs ne doit pas etre vide !");
double current = valeurs[0];
for (double d : valeurs)
if(d > current)
current = d;
return current;
}
}

View File

@ -0,0 +1,159 @@
package com.bernard.bataille;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import javax.imageio.ImageIO;
public class GameManager {
List<Queue<Integer>> cartes;
List<List<Integer>> defausses;
int alivePlayers;
Random shuffler;
public static void main(String[] args) {
Componentizer c = new GameManager(2,2).game();
List<Renderable> l = c.getRenderables();
BufferedImage bg = Componentizer.getBackgroundPictureImage();
try {
for (int i = 0; i < l.size(); i++) {
System.out.println(l.get(i));
if(l.get(i) instanceof Componentizer.CardsBackGetter)continue;
BufferedImage[] imgs = l.get(i).render(bg, 10);
System.out.println(Arrays.toString(imgs));
for (int j = 0; j < imgs.length; j++) {
ImageIO.write(imgs[j], "png", new File("/tmp/bataille/","img_"+i+"_"+j+".png"));
}
bg = imgs[imgs.length-1];
}
} catch (IOException e) {
e.printStackTrace();
}
}
public GameManager(int playerCount,int packetCount) {
this.alivePlayers = playerCount;
shuffler = new Random();
cartes = new ArrayList<>();
defausses = new ArrayList<>();
for (int i = 0; i < playerCount; i++) {
cartes.add(new LinkedList<>());
defausses.add(new LinkedList<>());
}
List<Integer> pioche = new ArrayList<>();
for(int i = 0;i<13*4;i++)pioche.addAll(Collections.nCopies(packetCount, i));
Collections.shuffle(pioche, shuffler);
for (int i = 0; i < pioche.size(); i++)
cartes.get(i%playerCount).add(pioche.get(i));
}
public Componentizer game() {
Componentizer componentizer = new Componentizer(alivePlayers,50,78);
while(alivePlayers > 1) {
int[] upTable = new int[alivePlayers];
List<Integer> winners = new ArrayList<>();
Set<Integer> pot = new HashSet<>();
Integer highest = 0;
boolean flipped = false;
while(winners.size() != 1) {// Tant que bataille
if(alivePlayers == 1) break;
//Fix dead people
fixCards();
for (int i = 0; i < alivePlayers; i++) {
if(cartes.get(i).isEmpty()) {
System.out.println("Le joueur "+i+" nous a quitté ... tout le monde s'en faut de toute façon !");
componentizer.playerDead(i);
alivePlayers--;
int[] oldUpTable = upTable;
upTable = new int[alivePlayers];
System.arraycopy(oldUpTable, 0, upTable, 0, i);
System.arraycopy(oldUpTable, i+1, upTable, i, alivePlayers-i);
cartes.remove(i);
defausses.remove(i);
}
}
highest = 0;
for (int i = 0; i < upTable.length; i++) {
upTable[i] = cartes.get(i).poll();
pot.add(upTable[i]);
highest = (highest > upTable[i]%13)?highest:upTable[i]%13;
}
System.out.println(alivePlayers+" joueurs jouent : "+cardArrayToString(upTable) + (flipped?" (caché)":""));
componentizer.turn(upTable, flipped);
winners.clear();
if(!flipped)
for (int i = 0; i < upTable.length; i++)
if(upTable[i]%13==highest)winners.add(i);
flipped = !flipped;
}
if(alivePlayers > 1) {
componentizer.endOfTurn();
System.out.println("GG au joueur "+winners.get(0));
defausses.get(winners.get(0)).addAll(pot);
}
}
return componentizer;
}
public void fixCards() {
for (int i = 0; i < alivePlayers; i++) {
if(cartes.get(i).isEmpty()) {
Collections.shuffle(defausses.get(i), shuffler);
cartes.get(i).addAll(defausses.get(i));
defausses.get(i).clear();
}
}
}
public static final String cardToString(int i) {
String color = "";
switch(i/13) {
case 0:color="";break;
case 1:color="";break;
case 2:color="";break;
case 3:color="";break;
}
switch(i%13) {
case 0 : return "2"+color;
case 1 : return "3"+color;
case 2 : return "4"+color;
case 3 : return "5"+color;
case 4 : return "6"+color;
case 5 : return "7"+color;
case 6 : return "8"+color;
case 7 : return "9"+color;
case 8 : return "X"+color;
case 9 : return "V"+color;
case 10 : return "D"+color;
case 11 : return "R"+color;
case 12 : return "A"+color;
}
return "";
}
public static final String cardArrayToString(int[] cards) {
String out = "";
for (int i : cards)
out+="|"+cardToString(i);
return out.substring(1);
}
}

View File

@ -0,0 +1,7 @@
package com.bernard.bataille;
import java.awt.image.BufferedImage;
public interface Renderable {
public BufferedImage[] render(BufferedImage background,int frameCount);
}