Commit a89f600f authored by Fournier Baptiste's avatar Fournier Baptiste

added grid and cell display generation in GamePanel

parent d5273894
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -3,4 +3,8 @@ package game;
public class Cell {
public int value;
public void setValue(int i) {
value = i;
}
}
\ No newline at end of file
......@@ -10,17 +10,76 @@ public class Engine {
public int ydimension;
public Engine(int gridDimension) {
// begin-user-code
cells = new Cell[gridDimension][gridDimension];
xdimension = gridDimension;
ydimension = gridDimension;
// end-user-code
}
public int getCellValue(int l, int c) {
// begin-user-code
return cells[l][c].value;
// end-user-code
}
public void rotate(){
Cell[][]tableaunew = new Cell[ydimension][xdimension];
for (int i=0; i<4; i++){
for (int j=0; j<4; j++){
tableaunew[i][j] = this.cells[3-j][i];
}
}
this.cells = 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.cells[i][j].value == 0){
Cell copie = this.cells[i][j-1];
this.cells[i][j-1] = this.cells[i][j];
this.cells[i][j] = copie;
}
}
}
}
}
public void fuseRight(){
for (int i=0; i<4; i++){
for (int j=3; j>0; j--){
if (this.cells[i][j-1].value == this.cells[i][j].value){
int value = this.cells[i][j].value;
this.cells[i][j].setValue(2 * value);
this.cells[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();
}
}
\ No newline at end of file
......@@ -3,7 +3,10 @@ package hmi;
import javax.swing.JPanel;
import java.util.Set;
import control.GameController;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class GamePanel extends JPanel {
......@@ -21,11 +24,31 @@ public class GamePanel extends JPanel {
// TODO Auto-generated constructor stub
// end-user-code
}
public void update(){
this.repaint();
}
public void paint(Graphics graph) {
// begin-user-code
// TODO Auto-generated method stub
// end-user-code
private Color colorCalc(int val){
int q = 2048/val;
if (q<255){
return new Color(255,127,80, 255-q);
}
return new Color(255,127,80,0);
}
public void paint(Graphics g){
g.setColor(Color.lightGray);
g.fillRect(10, 10, 500, 500);
//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) ));
int xoffset = 20 + i*(100+20);
int yoffset = 20 + j*(100+20);
g.fillRoundRect(xoffset, yoffset, 100, 100, 10, 10);
}
}
}
}
\ 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