Commit afae96df authored by Fournier Baptiste's avatar Fournier Baptiste

first functionnal version

parent a89f600f
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
import game.*;
import hmi.*;
import control.*;
public class Launcher {
public static void main(String... args) {
Engine Eng = new Engine(4);
GameController Control = new GameController(Eng);
System.out.println(Control.getCellValue(2, 2));
GameWindow MainFrame = new GameWindow(Control);
}
}
\ No newline at end of file
......@@ -24,14 +24,21 @@ public class GameController {
public void handleUp(){
engine.up();
engine.addNewCell();
}
public void handleDown(){
engine.down();
engine.addNewCell();
}
public void handleLeft(){
engine.left();
engine.addNewCell();
}
public void handleRight(){
engine.right();
engine.addNewCell();
}
}
\ No newline at end of file
......@@ -3,7 +3,10 @@ package game;
public class Cell {
public int value;
public Cell(int i){
value = i;
}
public void setValue(int i) {
value = i;
}
......
package game;
import java.util.ArrayList;
public class Engine {
private Cell[][] cells = null;
......@@ -11,10 +13,71 @@ public class Engine {
public Engine(int gridDimension) {
cells = new Cell[gridDimension][gridDimension];
xdimension = gridDimension;
ydimension = gridDimension;
for (int i=0; i<ydimension; i++ ){
for (int j=0; j<xdimension; j++){
Cell a = new Cell(0);
this.cells[i][j] = a;
}
}
}
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.cells[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.cells[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.cells[i][j].value==0){
vide.add(this.cells[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 int getCellValue(int l, int c) {
return cells[l][c].value;
}
......
......@@ -20,9 +20,7 @@ public class GamePanel extends JPanel {
public GamePanel(GameController control) {
// begin-user-code
// TODO Auto-generated constructor stub
// end-user-code
gameController = control;
}
public void update(){
......@@ -30,11 +28,11 @@ public class GamePanel extends JPanel {
}
private Color colorCalc(int val){
int q = 2048/val;
if (q<255){
return new Color(255,127,80, 255-q);
int quo = val/2048*255+1;
if (quo<255){
return new Color(255,127,quo);
}
return new Color(255,127,80,0);
else return new Color(255,127,255);
}
public void paint(Graphics g){
......@@ -43,10 +41,13 @@ public class GamePanel extends JPanel {
//4 cases de 100, 5 intervalles de 20
for (int i=0; i<4; i++ ){
for (int j=0; j<4; j++){
g.setColor(colorCalc( gameController.getCellValue(i, j) ));
g.setColor(colorCalc(gameController.getCellValue(i, j)));
System.out.println(colorCalc(gameController.getCellValue(i, j)));
int xoffset = 20 + i*(100+20);
int yoffset = 20 + j*(100+20);
g.fillRoundRect(xoffset, yoffset, 100, 100, 10, 10);
g.fillRoundRect(xoffset, yoffset, 100, 100, 20, 20);
g.setColor(Color.black);
g.drawString(String.valueOf(gameController.getCellValue(i, j)), xoffset + 40, yoffset+40);
}
}
}
......
......@@ -30,21 +30,20 @@ public class GameWindow extends JFrame implements KeyListener {
}
public void keyTyped(KeyEvent e) {
int keyCode = e.getKeyCode();
switch( keyCode ) {
case KeyEvent.VK_UP:
gameController.handleUp();
break;
case KeyEvent.VK_DOWN:
gameController.handleDown();
break;
case KeyEvent.VK_LEFT:
gameController.handleLeft();
break;
case KeyEvent.VK_RIGHT :
gameController.handleRight();
break;
}
char pressedKey = e.getKeyChar();
if (pressedKey == 'z'){
gameController.handleLeft();
}
else if (pressedKey == 'q'){
gameController.handleUp();
}
else if (pressedKey == 's'){
gameController.handleRight();
}
else if (pressedKey == 'd'){
gameController.handleDown();
}
gamePanel.update();
}
public void keyPressed(KeyEvent e) {
......
package jeu2048;
public class Launcher {
public void main(String... args) {
// begin-user-code
// TODO Auto-generated method stub
// end-user-code
}
}
\ No newline at end of file
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