Skip to content
Snippets Groups Projects
Commit 39e5e2bd authored by Oscar-git97's avatar Oscar-git97
Browse files

tp6 Q8 finit

parent 9aa59690
No related branches found
No related tags found
No related merge requests found
No preview for this file type
INDEX VERSION 1.134+C:\DevCCI\Eclipse\CCI_Java\.metadata\.plugins\org.eclipse.jdt.core
1415636810.index
1865797976.index
3437664616.index
package tp6.v3;
import java.awt.Color;
public class Cell {
private boolean isRevealed;
private boolean isMine;
boolean isSelected;
Color color;
private int row;
private int column;
private boolean active;
public Cell(boolean isMine, int row, int column) {
this.isRevealed = false;
this.isSelected = false;
this.color = Color.GRAY;
this.isMine = isMine;
this.row = row;
this.column = column;
this.active = true;
}
private void refreshColor() {
if (this.isSelected)
this.color = Color.RED;
else if (!this.isRevealed)
this.color = Color.DARK_GRAY;
else if (!this.isRevealed)
this.color = Color.lightGray;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
public void setRevealed(boolean status) {
this.isRevealed = status;
this.active = false;
if (status && !this.isMine)
this.color = Color.DARK_GRAY;
}
public void reveal() {
this.isRevealed = true;
}
public boolean getIsRevealed() {
return isRevealed;
}
public void setMine(boolean isMine) {
this.isMine = isMine;
}
public boolean getIsSelected() {
return isSelected;
}
public Color getColor() {
if (!isRevealed)
refreshColor();
return color;
}
public void setColor(Color color) {
this.color = color;
}
public boolean isRevealed() {
return isRevealed;
}
public boolean getIsMine() {
return isMine;
}
public void setActive(boolean active) {
this.active = active;
}
public int getColumn() {
return column;
}
public int getRow() {
return row;
}
@Override
public String toString() {
if(!isRevealed)
return "C";
else if (isMine)
return "B";
return "V";
}
}
package tp6.v3;
public class MainGui {
public static void main(String[] args) {
new MinesweeperFrame();
}
}
package tp6.v3;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Minesweeper {
private int nbColumns;
private int nbLines;
private int score;
double difficulte = 0.33;
Color colorSweeper = Color.RED;
private ArrayList<ArrayList<Cell>> board;
public Minesweeper() {
this.nbColumns = 10;
this.nbLines = 10;
this.score = 0;
this.board = new ArrayList<ArrayList<Cell>>();
for (int row = 0; row < this.nbLines; row++) {
this.board.add(new ArrayList<Cell>());
for (int column = 0; column < this.nbColumns; column++) {
boolean isMine = ((int) (Math.random() + difficulte)) == 1;
Cell cell = new Cell(isMine, row, column);
this.board.get(row).add(cell);
}
}
}
public double getDifficulte() {
return difficulte;
}
public void setDifficulte(double difficulte) {
this.difficulte = difficulte;
}
public Color getColorSweeper() {
return colorSweeper;
}
public void setColorSweeper(Color color) {
this.colorSweeper = color;
}
public int getNbColumns() {
return nbColumns;
}
public int getNbLines() {
return nbLines;
}
public int getScore() {
return score;
}
public void increaseScore() {
score++;
}
public boolean isRevealed(int line, int column) {
// if (isValidPositionLC(line, column))
return board.get(line).get(column).getIsRevealed();
// return false;
}
public boolean isMine(int line, int column) {
// if (isValidPositionLC(line, column))
return board.get(line).get(column).getIsMine();
// return false;
}
public void reveal(int line, int column) {
boolean isIt = isMine(line, column);
if(isIt) {
for(ArrayList<Cell> row : board) {
for(Cell cell : row)
cell.reveal();
}
}
board.get(line).get(column).reveal();
}
public Cell getCell(int line, int column) {
return board.get(line).get(column);
}
public void select(int line, int column) {
board.get(line).get(column).setSelected(true);
}
public void deselect(int line, int column) {
board.get(line).get(column).setSelected(false);
}
public boolean isValidPositionLC(int line, int column) {
boolean lineCheck = line <= nbLines && line > 0;
boolean columnCheck = column <= nbColumns && column > 0;
if (lineCheck && columnCheck)
return true;
return false;
}
}
package tp6.v3;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.WindowConstants;
@SuppressWarnings("serial")
public class MinesweeperFrame extends JFrame {
Minesweeper minesweeper;
MinesweeperPanel minesweeperPanel;
static JMenuBar mb;
static JMenu menu, size, difficulte, color;
// static JMenuItem size, difficulte, color;
static JMenuItem size1, size2, size3, diff1, diff2, diff3, color1, color2, color3 ;
static JFrame f;
public MinesweeperFrame() {
mb = new JMenuBar();
menu = new JMenu("menu");
size = new JMenu("size in pixel");
difficulte = new JMenu("Mines/no mines");
color = new JMenu("Color");
size1 = new JMenuItem("500");
size2 = new JMenuItem("700");
size3 = new JMenuItem("1000");
size.add(size1);
size.add(size2);
size.add(size3);
diff1 = new JMenuItem("50/50");
diff2 = new JMenuItem("25/75");
diff3 = new JMenuItem("75/25");
difficulte.add(diff1);
difficulte.add(diff2);
difficulte.add(diff3);
color1 = new JMenuItem("red");
color2 = new JMenuItem("blue");
color3 = new JMenuItem("black");
color.add(color1);
color.add(color2);
color.add(color3);
menu.add(size);
menu.add(difficulte);
menu.add(color);
mb.add(menu);
setJMenuBar(mb);
this.minesweeper = new Minesweeper();
this.minesweeperPanel = new MinesweeperPanel(minesweeper);
add(minesweeperPanel);
pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
}
\ No newline at end of file
package tp6.v3;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MinesweeperPanel extends JPanel implements MinesweeperPanelable {
Minesweeper minesweeper;
int spaceHor;
int spaceVert;
int nbColumns;
int nbRows;
int selCol;
int selLine;
int carreWidth;
int carreHeight;
int spaceBorder;
static JMenuBar mb;
static JMenu menu;
static JMenuItem size, difficulte, color;
static JFrame f;
public MinesweeperPanel(Minesweeper minesweeper) {
this.minesweeper = minesweeper;
setPreferredSize(new Dimension(500, 500));
addMouseListener(new PanelMouseListener(this));
addMouseMotionListener(new PanelMouseListener(this));
spaceBorder = 20;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.nbColumns = this.minesweeper.getNbColumns();
this.nbRows = this.minesweeper.getNbLines();
this.carreWidth = (getWidth() - (getBoardBorder() * 2)) - ((getWidth() - (getBoardBorder() * 2)) % nbColumns);
this.carreHeight = (getHeight() - (getBoardBorder() * 2)) - ((getHeight() - (getBoardBorder() * 2)) % nbRows);
this.spaceHor = carreWidth / nbColumns;
this.spaceVert = carreHeight / nbRows;
int lineEndH = carreHeight + getBoardBorder();
int lineEndV = carreWidth + getBoardBorder();
for (int idxRow = 0; idxRow < this.minesweeper.getNbLines(); idxRow++) {
for (int idxCol = 0; idxCol < this.minesweeper.getNbColumns(); idxCol++) {
Cell tmp = this.minesweeper.getCell(idxRow, idxCol);
// drawCell(g, idxRow, idxCol);
drawCell(g, tmp);
tmp.setSelected(false);
}
}
// show score
g.setColor(Color.darkGray);
g.setFont(new Font("Ariel", Font.BOLD, spaceBorder - 3));
g.drawString("Score : " + this.minesweeper.getScore(), (getWidth() / 2) - 40, (spaceBorder - 3));
}
public void drawCell(Graphics g, int line, int column) {
g.setColor(Color.BLACK);
g.drawRect(getCellX(column), getCellY(line), getCellWidth(), getCellHeight());
Cell tmp = this.minesweeper.getCell(line, column);
g.setColor(tmp.getColor());
g.fillRect(getCellX(column), getCellY(line), getCellWidth(), getCellHeight());
if (tmp.getIsRevealed() && tmp.getIsMine()) {
int sizeBomb = 25;
g.setColor(Color.BLACK);
g.fillOval(getCellX(column) + (getCellWidth() / 2) - (sizeBomb / 2),
getCellX(line) + (getCellHeight() / 2) - (sizeBomb / 2), sizeBomb, sizeBomb);
}
}
public void drawCell(Graphics g, Cell tmp) {
g.setColor(Color.BLACK);
int xTmp = getCellX(tmp.getColumn());
int yTmp = getCellY(tmp.getRow());
g.drawRect(xTmp, yTmp, getCellWidth(), getCellHeight());
if (tmp.isSelected) {
g.setColor(Color.RED);
g.fillRect(xTmp, yTmp, getCellWidth(), getCellHeight());
} else if (tmp.toString().equals("B")) {
g.setColor(Color.RED);
g.fillRect(xTmp, yTmp, getCellWidth(), getCellHeight());
g.setColor(Color.WHITE);
g.drawRect(xTmp, yTmp, getCellWidth(), getCellHeight());
int sizeBomb = 25;
g.setColor(Color.BLACK);
g.fillOval(xTmp + (getCellWidth() / 2) - (sizeBomb / 2),
yTmp + (getCellHeight() / 2) - (sizeBomb / 2), sizeBomb, sizeBomb);
} else if (tmp.toString().equals("C")) {
g.setColor(Color.DARK_GRAY);
g.fillRect(xTmp, yTmp, getCellWidth(), getCellHeight());
} else {
g.setColor(Color.GRAY);
g.fillRect(xTmp, yTmp, getCellWidth(), getCellHeight());
}
}
public int getColumn(int x) {
boolean condX = x > getBoardBorder() && x < (getBoardBorder() + (this.nbColumns * getCellWidth()));
if (condX) {
int posCarre = x - getBoardBorder();
this.selCol = posCarre / getCellWidth();
return this.selCol;
}
return -1;
}
public int getLine(int y) {
boolean condY = y > getBoardBorder() && y < (getBoardBorder() + (this.nbRows * getCellHeight()));
if (condY) {
int posCarre = y - getBoardBorder();
this.selLine = posCarre / spaceVert;
return this.selLine;
}
return -1;
}
public int getBoardBorder() {
return this.spaceBorder;
}
// boolean isMine(int line, int column) {
// return this.minesweeper.isMine(line, column);
// }
public int getCellX(int column) {
return (getBoardBorder() + (column * getCellWidth()));
}
public int getCellY(int line) {
return (getBoardBorder() + (line * getCellHeight()));
}
public int getCellWidth() {
return this.spaceHor;
}
public int getCellHeight() {
return this.spaceVert;
}
public boolean isValidPositionPx(int x, int y) {
boolean condX = x > getBoardBorder() && x < (getBoardBorder() + (this.nbColumns * getCellWidth()));
boolean condY = y > getBoardBorder() && y < (getBoardBorder() + (this.nbRows * getCellHeight()));
if (condX && condY)
return true;
return false;
}
@Override
public void drawMinesweeper(Graphics g) {
}
}
package tp6.v3;
import java.awt.Graphics;
public interface MinesweeperPanelable {
public int getBoardBorder();
public int getCellX(int column);
public int getCellY(int line);
public int getCellWidth();
public int getCellHeight();
public void drawCell(Graphics g, int line, int column);
public int getColumn(int x);
public int getLine(int y);
void drawMinesweeper(Graphics g);
}
package tp6.v3;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class PanelMouseListener extends MouseAdapter {
MinesweeperPanel panel;
public PanelMouseListener(MinesweeperPanel panel) {
this.panel = panel;
}
@Override
public void mouseClicked(MouseEvent e) {
// System.out.println("Click Line = " + panel.minesweeper.selectedLine);
// System.out.println("Click Column = " + panel.minesweeper.selectedColumn);
if (panel.isValidPositionPx(e.getX(), e.getY())) {
int line = panel.getLine(e.getY());
int column = panel.getColumn(e.getX());
panel.minesweeper.reveal(line, column);
if (!panel.minesweeper.isMine(line, column))
panel.minesweeper.increaseScore();
panel.repaint();
}
}
@Override
public void mouseMoved(MouseEvent e) {
// panel.minesweeper.selectedLine = panel.getLine(e.getY());
// panel.minesweeper.selectedColumn = panel.getColumn(e.getX());
if (panel.isValidPositionPx(e.getX(), e.getY())) {
int line = panel.getLine(e.getY());
int column = panel.getColumn(e.getX());
panel.minesweeper.select(line, column);
panel.repaint();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment