Commit 7ef0ea5a authored by Baptiste Fournier's avatar Baptiste Fournier

initial commit

parent 7baa9ff8
File added
public class Cell {
int valeur;
Cell (int valeurReelle){
this.valeur=valeurReelle;
}
public String toString(){
return this.valeur + "";
}
public int getValue(){
return this.valeur;
}
public void setValue( int val){
this.valeur = val;
}
}
import java.util.ArrayList;
//import java.util.Arrays;
import java.util.Scanner;
public class Engine {
int hauteur = 4;
int largeur = 4;
Cell[][]tableau = new Cell[hauteur][largeur];
Engine(){
for (int i=0; i<4; i++ ){
for (int j=0; j<4; j++){
Cell a = new Cell(0);
this.tableau[i][j] = a;
}
}
this.addNewCell();
this.addNewCell();
}
Engine(int[][] tab){
for (int i=0; i<4; i++ ){
for (int j=0; j<4; j++){
Cell a = new Cell(tab[i][j]);
this.tableau[i][j] = a;
}
}
}
public void printBoard(){
for (int i=0; i<4; i++){
String affiche = "";
for (int j = 0; j<4; j++){
affiche = affiche + this.tableau[i][j].toString() + " ";
}
System.out.println(affiche);
}
}
public ArrayList<Cell> getEmptyCells(){
ArrayList<Cell> vide = new ArrayList<Cell>();
for (int i=0; i<4; i++){
for (int j=0; j<4; j++){
if (this.tableau[i][j].getValue()==0){
vide.add(this.tableau[i][j]);
}
}
}
return vide;
}
public void addNewCell(){
double rnd = Math.random();
int value = 4;
if (rnd<0.8){
value=2;
}
ArrayList<Cell> vide1= this.getEmptyCells();
double taille = vide1.size();
if (taille > 0){
double rnd2= Math.random();
double indicefloat = rnd2 * (taille-1);
int indice = (int) indicefloat;
vide1.get(indice).setValue(value);
}
}
public void rotate(){
Cell[][]tableaunew = new Cell[hauteur][largeur];
for (int i=0; i<4; i++){
for (int j=0; j<4; j++){
tableaunew[i][j] = this.tableau[3-j][i];
}
}
this.tableau = tableaunew;
}
public void moveRight(){
for (int n = 0; n < 3; n++){
for (int i=0; i<4; i++){
for (int j=3; j>0; j--){
if (this.tableau[i][j].getValue() == 0){
Cell copie = this.tableau[i][j-1];
this.tableau[i][j-1] = this.tableau[i][j];
this.tableau[i][j] = copie;
}
}
}
}
}
public void fuseRight(){
for (int i=0; i<4; i++){
for (int j=3; j>0; j--){
if (this.tableau[i][j-1].getValue() == this.tableau[i][j].getValue()){
int value = this.tableau[i][j].getValue();
this.tableau[i][j].setValue(2 * value);
this.tableau[i][j-1].setValue(0);
}
}}
}
public void right(){
this.moveRight();
this.fuseRight();
this.moveRight();
}
public void up(){
this.rotate();
this.right();
this.rotate();this.rotate();this.rotate();
}
public void left(){
this.rotate();this.rotate();
this.right();
this.rotate();this.rotate();
}
public void down(){
rotate();rotate();rotate();
right();
rotate();
}
public void oneStep(){
System.out.println("Choisir un mouvement");
Scanner sin = new Scanner(System.in);
int val = sin.nextInt();
// on utilise les flches du pav numrique, qui correspondent 8,4,6 et 2
if (val == 8){
this.up();
this.addNewCell();
}
if (val == 4){
this.left();
this.addNewCell();
}
if (val == 6){
this.right();
this.addNewCell();
}
if (val == 2){
this.down();
this.addNewCell();
}
}
public Boolean isOver(){
ArrayList<Cell> vide = this.getEmptyCells();
if (vide.size() != 0){
return false;
}
else{
for (int i=0; i<4; i++){
for (int j=0; j<3; j++){
if (this.tableau[i][j].getValue()== this.tableau[i][j+1].getValue()){
return false;
}
}
}
for (int i=0; i<4; i++){
for (int j=0; j<3; j++){
if (this.tableau[j][i].getValue()== this.tableau[j+1][i].getValue()){
return false;
}
}
}
return true;
}
}
public int score(){
int max =0;
for (int i=0; i<4; i++){
for (int j=0; j<4; j++){
if (this.tableau[i][j].getValue() > max){
max = this.tableau[i][j].getValue();
}
}
}
return max;
}
public void play(){
this.printBoard();
while (! isOver()){
this.oneStep();
this.printBoard();
System.out.println(" ");
}
System.out.println("Vous avez perdu, votre score est :" + this.score());
}
public void nextRandomStep(){
double rnd = Math.random();
if ( rnd<=0.25){
this.right();
}
if (rnd >0.75){
this.up();
}
if (rnd > 0.25 && rnd <=0.5){
this.down();
}
if (rnd > 0.5 && rnd <=0.75){
this.left();
}
this.addNewCell();
}
public void automaticPlay(){
this.printBoard();
while (! isOver()){
this.nextRandomStep();
this.printBoard();
System.out.println(" ");
}
System.out.println("Vous avez perdu, votre score est :" + this.score());
}
}
import java.util.Scanner;
public class Launcher {
public static void main(String[] args) {
int[][] vals = {{1,0,0,2},{0,0,2,0},{0,1,0,1},{1,1,1,1}};
Engine mainEngine = new Engine(vals);
System.out.println("Choisir un mode de jeu");
System.out.println("1 = automatique");
System.out.println("0 = manuel");
Scanner sin = new Scanner(System.in);
int val = sin.nextInt();
if (val == 1){
mainEngine.automaticPlay();
} else{
mainEngine.play();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment