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

jusqu'a projet tp 6 Q12, Missing:

parent 7a9144a2
No related branches found
No related tags found
No related merge requests found
No preview for this file type
CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png

802 B | W: | H:

CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png

675 B | W: | H:

CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png
CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png
CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png
CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png
  • 2-up
  • Swipe
  • Onion skin
CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/1.png

1.11 KiB | W: | H:

CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/1.png

514 B | W: | H:

CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/1.png
CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/1.png
CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/1.png
CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/1.png
  • 2-up
  • Swipe
  • Onion skin
CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png

712 B | W: | H:

CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png

635 B | W: | H:

CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png
CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png
CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png
CCI_Java/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png
  • 2-up
  • Swipe
  • Onion skin
package tp6.v3;
import java.awt.Color;
import java.util.ArrayList;
public class Cell {
private boolean isRevealed;
private boolean isMine;
boolean isSelected;
private boolean isSelected;
Color color;
private int row;
private int column;
private boolean active;
private int neighbors;
private int nbNeighbors;
private boolean isFlagged;
private ArrayList<Cell> neighbors;
public Cell(boolean isMine, int row, int column) {
this.isRevealed = false;
......@@ -20,6 +23,9 @@ public class Cell {
this.row = row;
this.column = column;
this.active = true;
this.isFlagged = false;
this.nbNeighbors = 0;
this.neighbors = new ArrayList<Cell>();
}
private void refreshColor() {
......@@ -32,6 +38,12 @@ public class Cell {
this.color = Color.lightGray;
}
public void addNeighbor(Cell cell) {
neighbors.add(cell);
if (cell.isMine)
nbNeighbors++;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
......@@ -57,11 +69,11 @@ public class Cell {
}
public void setNeighbors(int neighbors) {
this.neighbors = neighbors;
this.nbNeighbors = neighbors;
}
public int getNeighbors() {
return neighbors;
return nbNeighbors;
}
public boolean getIsSelected() {
......@@ -100,12 +112,25 @@ public class Cell {
@Override
public String toString() {
if(!isRevealed)
if (isFlagged)
return "F";
else if (!isRevealed)
return "C";
else if (isMine)
return "B";
return "V";
}
public void setFlagged(boolean isFlagged) {
this.isFlagged = isFlagged;
}
public boolean isFlagged() {
return this.isFlagged;
}
public boolean isSelected() {
return isSelected;
}
}
......@@ -9,26 +9,40 @@ public class Minesweeper {
private int nbColumns;
private int nbLines;
private int score;
private boolean gameWin;
private boolean gameOver;
private int nbMines;
private int nbBlanks;
private int nbRevealed;
double difficulte = 0.33;
double difficulte;
Color colorSweeper = Color.RED;
private ArrayList<ArrayList<Cell>> board;
public Minesweeper() {
this.nbColumns = 10;
this.nbLines = 10;
this.nbColumns = 3;
this.nbLines = 3;
this.difficulte = 0.1;
this.score = 0;
this.board = new ArrayList<ArrayList<Cell>>();
this.gameOver = false;
this.gameWin = false;
this.nbMines = 0;
this.nbRevealed = 0;
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);
Cell cell = new Cell(isMine, row + 1, column + 1);
this.board.get(row).add(cell);
if (isMine)
nbMines++;
}
}
this.nbBlanks = (nbLines * nbColumns) - nbMines;
setNeighbors();
}
......@@ -36,21 +50,21 @@ public class Minesweeper {
// TODO Auto-generated method stub
for (ArrayList<Cell> row : board)
for (Cell cell : row) {
cell.setNeighbors(countNeighbors(cell));
addAllNeighbors(cell);
}
}
private int countNeighbors(Cell cell) {
private void addAllNeighbors(Cell cell) {
int res = 0;
for (int row = cell.getRow() - 1; row <= (cell.getRow() + 1); row++)
for (int row = cell.getRow() - 1; row <= (cell.getRow() + 1); row++) {
for (int col = cell.getColumn() - 1; col <= (cell.getColumn() + 1); col++) {
if (isValidPositionLC(row, col) && !(row == cell.getRow()) && !(col == cell.getRow()))
// if (!(row == cell.getRow()) && !(col == cell.getRow()) && isMine(row, col))
res++;
if (isValidPositionLC(row, col) && !(row == cell.getRow() && col == cell.getColumn()))
// && isMine(row, col) && !(row == cell.getRow()) && !(col == cell.getRow())
cell.addNeighbor(getCell(row, col));
}
}
return res;
}
// public double getDifficulte() {
// return difficulte;
......@@ -85,40 +99,51 @@ public class Minesweeper {
}
public boolean isRevealed(int line, int column) {
// if (isValidPositionLC(line, column))
return board.get(line).get(column).getIsRevealed();
// return false;
if (isValidPositionLC(line, column))
return getCell(line, column).getIsRevealed();
return false;
}
public boolean isMine(int line, int column) {
// if (isValidPositionLC(line, column))
return board.get(line).get(column).getIsMine();
// return false;
if (isValidPositionLC(line, column))
return getCell(line, column).getIsMine();
return false;
}
public void reveal(int line, int column) {
boolean isIt = isMine(line, column);
if (isIt) {
gameOver = true;
for (ArrayList<Cell> row : board) {
for (Cell cell : row)
cell.reveal();
}
}
board.get(line).get(column).reveal();
getCell(line, column).reveal();
nbRevealed++;
if (nbRevealed == nbBlanks) {
gameWin = true;
}
}
public Cell getCell(int line, int column) {
return board.get(line).get(column);
if (isValidPositionLC(line, column))
return board.get(line - 1).get(column - 1);
return null;
}
public void select(int line, int column) {
board.get(line).get(column).setSelected(true);
getCell(line, column).setSelected(true);
}
// public void deselect(int line, int column) {
// board.get(line).get(column).setSelected(false);
// }
public boolean isGameOver() {
return gameOver;
}
public boolean isValidPositionLC(int line, int column) {
boolean lineCheck = line <= nbLines && line > 0;
boolean columnCheck = column <= nbColumns && column > 0;
......@@ -127,4 +152,26 @@ public class Minesweeper {
return false;
}
public boolean isOver() {
return gameOver || gameWin;
}
public boolean isWin() {
return gameWin;
}
public void flag(int line, int column) {
// TODO Auto-generated method stub
if (getCell(line, column).isFlagged())
getCell(line, column).setFlagged(false);
else {
getCell(line, column).setFlagged(true);
}
}
public boolean isFlagged(int line, int column) {
return getCell(line, column).isFlagged();
}
}
......@@ -3,6 +3,7 @@ package tp6.v3;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.util.Scanner;
......@@ -58,8 +59,8 @@ public class MinesweeperPanel extends JPanel implements MinesweeperPanelable {
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++) {
for (int idxRow = 1; idxRow <= this.minesweeper.getNbLines(); idxRow++) {
for (int idxCol = 1; idxCol <= this.minesweeper.getNbColumns(); idxCol++) {
Cell tmp = this.minesweeper.getCell(idxRow, idxCol);
// drawCell(g, idxRow, idxCol);
drawCell(g, tmp);
......@@ -72,6 +73,46 @@ public class MinesweeperPanel extends JPanel implements MinesweeperPanelable {
g.setFont(new Font("Ariel", Font.BOLD, spaceBorder - 3));
g.drawString("Score : " + this.minesweeper.getScore(), (getWidth() / 2) - 40, (spaceBorder - 3));
if (this.minesweeper.isGameOver())
drawGameOver(g);
else if (this.minesweeper.isWin())
drawWin(g);
}
private void drawWin(Graphics g) {
int fontSize = (getHeight() + getWidth()) / (2 * 3 * 2);
Font font = new Font("Ariel", Font.BOLD, fontSize);
FontMetrics metrics = g.getFontMetrics(font);
g.setColor(Color.CYAN);
g.fillRect(0, fontSize * 2, getWidth(), fontSize * 2);// fontSize * 2
String gg = "GG, U WIN!";
int xGame = (getWidth() - metrics.stringWidth(gg)) / 2;
String score = "Score : " + this.minesweeper.getScore();
int xScore = (getWidth() - metrics.stringWidth(score)) / 2;
g.setColor(Color.BLACK);
g.setFont(font);
g.drawString(gg, xGame, fontSize * 3);
g.drawString(score, xScore, (fontSize * 4) - getHeight() / 22);
}
private void drawGameOver(Graphics g) {
int fontSize = (getHeight() + getWidth()) / (2 * 3 * 2);
Font font = new Font("Ariel", Font.BOLD, fontSize);
FontMetrics metrics = g.getFontMetrics(font);
g.setColor(Color.CYAN);
g.fillRect(0, fontSize * 2, getWidth(), fontSize * 2);// fontSize * 2
String gg = "GAME OVER!";
int xGame = (getWidth() - metrics.stringWidth(gg)) / 2;
String score = "Score : " + this.minesweeper.getScore();
int xScore = (getWidth() - metrics.stringWidth(score)) / 2;
g.setColor(Color.BLACK);
g.setFont(font);
g.drawString(gg, xGame, fontSize * 3);
g.drawString(score, xScore, (fontSize * 4) - getHeight() / 22);
}
public void drawCell(Graphics g, int line, int column) {
......@@ -97,7 +138,16 @@ public class MinesweeperPanel extends JPanel implements MinesweeperPanelable {
int yTmp = getCellY(tmp.getRow());
g.drawRect(xTmp, yTmp, getCellWidth(), getCellHeight());
if (tmp.isSelected) {
if (tmp.isFlagged()) {
g.setColor(Color.DARK_GRAY);
g.fillRect(xTmp, yTmp, getCellWidth(), getCellHeight());
g.setColor(Color.WHITE);
g.drawRect(xTmp, yTmp, getCellWidth(), getCellHeight());
int sizeBomb = (getCellWidth() + getCellHeight()) / 5;
g.setColor(Color.GREEN);
g.fillOval(xTmp + (getCellWidth() / 2) - (sizeBomb / 2), yTmp + (getCellHeight() / 2) - (sizeBomb / 2),
sizeBomb, sizeBomb);
} else if (tmp.isSelected()) {
g.setColor(Color.RED);
g.fillRect(xTmp, yTmp, getCellWidth(), getCellHeight());
} else if (tmp.toString().equals("B")) {
......@@ -105,7 +155,7 @@ public class MinesweeperPanel extends JPanel implements MinesweeperPanelable {
g.fillRect(xTmp, yTmp, getCellWidth(), getCellHeight());
g.setColor(Color.WHITE);
g.drawRect(xTmp, yTmp, getCellWidth(), getCellHeight());
int sizeBomb = 25;
int sizeBomb = (getCellWidth() + getCellHeight()) / 5;
g.setColor(Color.BLACK);
g.fillOval(xTmp + (getCellWidth() / 2) - (sizeBomb / 2), yTmp + (getCellHeight() / 2) - (sizeBomb / 2),
sizeBomb, sizeBomb);
......@@ -116,47 +166,33 @@ public class MinesweeperPanel extends JPanel implements MinesweeperPanelable {
g.setColor(Color.GRAY);
g.fillRect(xTmp, yTmp, getCellWidth(), getCellHeight());
g.setColor(Color.BLACK);
g.setFont(new Font("Ariel", Font.BOLD, 35));
g.setFont(new Font("Ariel", Font.BOLD, getCellHeight() / 2));
g.drawString(String.valueOf(tmp.getNeighbors()), xTmp + 15, (yTmp + getCellHeight() - 10));
}
}
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;
return this.selCol + 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;
return this.selLine + 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()));
return (getBoardBorder() + ((column - 1) * getCellWidth()));
}
public int getCellY(int line) {
return (getBoardBorder() + (line * getCellHeight()));
return (getBoardBorder() + ((line - 1) * getCellHeight()));
}
public int getCellWidth() {
......@@ -175,9 +211,5 @@ public class MinesweeperPanel extends JPanel implements MinesweeperPanelable {
return false;
}
@Override
public void drawMinesweeper(Graphics g) {
}
}
......@@ -5,13 +5,18 @@ 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);
public int getLine(int y);
}
......@@ -3,6 +3,8 @@ package tp6.v3;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.SwingUtilities;
public class PanelMouseListener extends MouseAdapter {
MinesweeperPanel panel;
......@@ -16,12 +18,17 @@ public class PanelMouseListener extends MouseAdapter {
// System.out.println("Click Line = " + panel.minesweeper.selectedLine);
// System.out.println("Click Column = " + panel.minesweeper.selectedColumn);
if (panel.isValidPositionPx(e.getX(), e.getY())) {
if (panel.isValidPositionPx(e.getX(), e.getY()) && !panel.minesweeper.isOver()) {
int line = panel.getLine(e.getY());
int column = panel.getColumn(e.getX());
if (SwingUtilities.isLeftMouseButton(e) && !panel.minesweeper.isFlagged(line, column)) {
panel.minesweeper.reveal(line, column);
if (!panel.minesweeper.isMine(line, column))
panel.minesweeper.increaseScore();
} else if (SwingUtilities.isRightMouseButton(e)) {
panel.minesweeper.flag(line, column);
}
panel.repaint();
}
}
......@@ -32,7 +39,7 @@ public class PanelMouseListener extends MouseAdapter {
// panel.minesweeper.selectedLine = panel.getLine(e.getY());
// panel.minesweeper.selectedColumn = panel.getColumn(e.getX());
if (panel.isValidPositionPx(e.getX(), e.getY())) {
if (panel.isValidPositionPx(e.getX(), e.getY()) && !panel.minesweeper.isOver()) {
int line = panel.getLine(e.getY());
int column = panel.getColumn(e.getX());
panel.minesweeper.select(line, column);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment