292 lines
7.2 KiB
Java
292 lines
7.2 KiB
Java
|
/** La classe ControllerMouseGame gere les clics lies a l'utilisateur
|
||
|
* ainsi que les regles du jeu
|
||
|
* @version 1.0
|
||
|
* @author Tanguy Domergue et Leni Boscher
|
||
|
*/
|
||
|
|
||
|
import java.awt.*;
|
||
|
import javax.swing.*;
|
||
|
import java.io.*;
|
||
|
import java.util.Random;
|
||
|
import java.lang.*;
|
||
|
import java.awt.event.*;
|
||
|
|
||
|
public class ControllerMouseGame implements MouseListener
|
||
|
{
|
||
|
|
||
|
/** Tableau d'entiers qui permet de stocker
|
||
|
* les objets d'un meme groupe
|
||
|
*/
|
||
|
private int[][] group;
|
||
|
|
||
|
private int score=0;
|
||
|
private int group_size=0;
|
||
|
private FrameJeu frame;
|
||
|
private int x,y;
|
||
|
private Score scoreS;
|
||
|
|
||
|
/** Constructeur de la classe qui cree un tableau group
|
||
|
* et qui l'initialise a -1
|
||
|
* @param fen : permet de reprendre la fenetre de jeu de FrameJeu
|
||
|
* @param scr : permet de recuperer le score de la classe Score
|
||
|
* @param x : l'indice x des cases sur lesquelles on cliquera
|
||
|
* @param y : l'indice y des cases sur lesquelles on cliquera
|
||
|
*/
|
||
|
public ControllerMouseGame(FrameJeu fen, Score scr, int x, int y)
|
||
|
{
|
||
|
this.group = new int[10][15];
|
||
|
this.frame = fen;
|
||
|
this.x = x;
|
||
|
this.y = y;
|
||
|
this.scoreS = scr;
|
||
|
for(int i=0;i<10;i++)
|
||
|
{
|
||
|
for(int j=0;j<15;j++)
|
||
|
{
|
||
|
this.group[i][j] = -1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
/** Methode qui teste si un bloc est deja
|
||
|
* dans le tableau group
|
||
|
*/
|
||
|
public boolean testInGroup(int x, int y)
|
||
|
{
|
||
|
if(group[x][y]==this.frame.getCaseTab(x,y))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/** Methode qui reunit les blocs en groupe
|
||
|
* @param x : indice x du bloc
|
||
|
* @param y : indice y du bloc
|
||
|
*/
|
||
|
public void testGroup(int x, int y)
|
||
|
{
|
||
|
|
||
|
if(x>0)
|
||
|
{
|
||
|
if((this.frame.getCaseTab(x,y) != 0) && (this.frame.getCaseTab(x,y) == this.frame.getCaseTab(x-1,y)))
|
||
|
{
|
||
|
if((testInGroup(x-1,y)))
|
||
|
{
|
||
|
this.group_size++;
|
||
|
this.group[x-1][y] = this.frame.getCaseTab(x-1,y);
|
||
|
testGroup(x-1,y);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(y>0)
|
||
|
{
|
||
|
if((this.frame.getCaseTab(x,y) != 0) && (this.frame.getCaseTab(x,y) == this.frame.getCaseTab(x,y-1)))
|
||
|
{
|
||
|
if((testInGroup(x,y-1)))
|
||
|
{
|
||
|
this.group_size++;
|
||
|
this.group[x][y-1] = this.frame.getCaseTab(x,y-1);
|
||
|
testGroup(x,y-1);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(x+1<10)
|
||
|
{
|
||
|
if((this.frame.getCaseTab(x,y) != 0) && (this.frame.getCaseTab(x,y) == this.frame.getCaseTab(x+1,y)))
|
||
|
{
|
||
|
if((testInGroup(x+1,y)))
|
||
|
{
|
||
|
this.group_size++;
|
||
|
this.group[x+1][y] = this.frame.getCaseTab(x+1,y);
|
||
|
testGroup(x+1,y);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(y+1<15)
|
||
|
{
|
||
|
if((this.frame.getCaseTab(x,y) != 0) && (this.frame.getCaseTab(x,y) == this.frame.getCaseTab(x,y+1)))
|
||
|
{
|
||
|
if((testInGroup(x,y+1)))
|
||
|
{
|
||
|
this.group_size++;
|
||
|
this.group[x][y+1] = this.frame.getCaseTab(x,y+1);
|
||
|
testGroup(x,y+1);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/** Methode qui reorganise la colonne
|
||
|
* et qui comble les vides
|
||
|
* @param j : colonne a reordonner
|
||
|
*/
|
||
|
|
||
|
public void orderColumn(int j)
|
||
|
{
|
||
|
for(int i=9;i>0;i--)
|
||
|
{
|
||
|
if(this.frame.getCaseTab(i,j)==0)
|
||
|
{
|
||
|
int c = this.frame.getCaseTab(i-1,j);
|
||
|
this.frame.setCaseTabC(i-1,j,0);
|
||
|
this.frame.setCaseTab(i-1,j,0);
|
||
|
this.frame.setCaseTabC(i,j,c);
|
||
|
this.frame.setCaseTab(i,j,c);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/** Methode qui copie une colonne de droite a gauche
|
||
|
* @param j : colonne a copier
|
||
|
*/
|
||
|
|
||
|
public void copyColumn(int j)
|
||
|
{
|
||
|
for(int i=j;i<14;i++)
|
||
|
{
|
||
|
for(int k=0;k<10;k++)
|
||
|
{
|
||
|
int c = this.frame.getCaseTab(k,i+1);
|
||
|
this.frame.setCaseTab(k,i+1,0);
|
||
|
this.frame.setCaseTabC(k,i+1,0);
|
||
|
this.frame.setCaseTab(k,i,c);
|
||
|
this.frame.setCaseTabC(k,i,c);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/** Methode qui regarde chaque colonne,
|
||
|
* si certaines sont vides, elle deplace de droite
|
||
|
* a gauche
|
||
|
*/
|
||
|
|
||
|
public void checkEmptyColumn()
|
||
|
{
|
||
|
int check = 1;
|
||
|
|
||
|
for(int i=13;i>=0;i--)
|
||
|
{
|
||
|
if(this.frame.getCaseTab(9,i) == 0)
|
||
|
{
|
||
|
copyColumn(i);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// @Override
|
||
|
public void mouseClicked(MouseEvent e){
|
||
|
|
||
|
}
|
||
|
|
||
|
// @Override
|
||
|
public void mouseEntered(MouseEvent e){
|
||
|
testGroup(this.x,this.y);
|
||
|
for(int i=0;i<10;i++){
|
||
|
for(int j=0;j<15;j++){
|
||
|
if(group[i][j] != -1)
|
||
|
{
|
||
|
this.frame.getCaseTabC(i,j).setBackground(new Color(250,188,23));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
resetGroup();
|
||
|
}
|
||
|
|
||
|
// @Override
|
||
|
public void mouseExited(MouseEvent e){
|
||
|
testGroup(this.x,this.y);
|
||
|
for(int i=0;i<10;i++){
|
||
|
for(int j=0;j<15;j++){
|
||
|
if(group[i][j] != -1)
|
||
|
{
|
||
|
this.frame.getCaseTabC(i,j).setBackground(new Color(238,238,238));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
resetGroup();
|
||
|
}
|
||
|
|
||
|
// @Override
|
||
|
public void mousePressed(MouseEvent e){
|
||
|
|
||
|
}
|
||
|
|
||
|
// @Override
|
||
|
public void mouseReleased(MouseEvent e){
|
||
|
mouseExited(e);
|
||
|
int xi,yi;
|
||
|
xi = this.x;
|
||
|
yi = this.y;
|
||
|
|
||
|
testGroup(this.x,this.y);
|
||
|
|
||
|
int i=0;
|
||
|
int cgrp=0;
|
||
|
for(i=0;i<10;i++)
|
||
|
{
|
||
|
for(int j=0;j<15;j++)
|
||
|
{
|
||
|
if(this.group[i][j]!=-1)
|
||
|
{
|
||
|
this.frame.setCaseTab(i,j,0);
|
||
|
this.frame.setCaseTabC(i,j,0);
|
||
|
cgrp++;
|
||
|
}
|
||
|
orderColumn(j);
|
||
|
}
|
||
|
}
|
||
|
checkEmptyColumn();
|
||
|
|
||
|
int s = (cgrp-2)*(cgrp-2);
|
||
|
if(cgrp>2)
|
||
|
{
|
||
|
this.scoreS.updateScore(s);
|
||
|
}
|
||
|
resetGroup();
|
||
|
this.x = xi;
|
||
|
this.y = yi;
|
||
|
|
||
|
if(endCondition()==1){
|
||
|
FrameEnd fenetre = new FrameEnd(scoreS.getScore());
|
||
|
frame.dispose();
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
/** Méthode qui remet le tableau Group
|
||
|
* à -1
|
||
|
*/
|
||
|
public void resetGroup()
|
||
|
{
|
||
|
for(int i=0;i<10;i++){
|
||
|
for(int j=0;j<15;j++){
|
||
|
this.group[i][j] = -1;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/** Méthode qui pose une condition pour la fin
|
||
|
* de la partie
|
||
|
*/
|
||
|
public int endCondition(){
|
||
|
for(int i=0;i<10;i++){
|
||
|
for(int j=0;j<15;j++){
|
||
|
testGroup(i,j);
|
||
|
for(int x=0;x<10;x++){
|
||
|
for(int y=0;y<15;y++){
|
||
|
if(group[x][y] != -1){
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return 1;
|
||
|
}
|
||
|
}
|