Skip to content

Instantly share code, notes, and snippets.

@LucasAlfare
Created April 5, 2016 23:19
Show Gist options
  • Save LucasAlfare/5366c7c7d3787abf73139255c07225a9 to your computer and use it in GitHub Desktop.
Save LucasAlfare/5366c7c7d3787abf73139255c07225a9 to your computer and use it in GitHub Desktop.
Esboço de um Campo Minado em Java.
package com;
import com.minesweeper.TabGui;
import com.minesweeper.Tabuleiro;
/**
* Created by lucas on 01/04/16.
*/
public class MainClass {
public static void main(String[] args) {
/**
* inicializaçao de tabuleiro com tamanho
* de linhas e colunas igual a 3
*/
Tabuleiro tabuleiro = new Tabuleiro(3);
/**
* inicializaçao de gui para testes
*/
TabGui tabGui = new TabGui(tabuleiro);
tabGui.setVisible(true);
}
}
package com.minesweeper;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Created by lucas on 02/04/16.
*/
public class TabGui extends JFrame {
private CButton[][] slots;
private Tabuleiro tabuleiro;
/**
* @param tabuleiro tabuleiro contendo todas as variáveis essenciais.
*/
public TabGui(Tabuleiro tabuleiro){
this.tabuleiro = tabuleiro;
slots = new CButton[this.tabuleiro.tabSize][this.tabuleiro.tabSize];
setSize((this.tabuleiro.tabSize*100)/2,((this.tabuleiro.tabSize*100)/2) - 50);
setDefaultCloseOperation(3);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
for (int x = 0; x < this.tabuleiro.tabSize; x++){
for (int y = 0; y < this.tabuleiro.tabSize; y++){
slots[x][y] = new CButton(x,y);
this.add(slots[x][y]);
}
}
}
private class CButton extends JButton implements ActionListener{
private int x ,y;
/**
* @param x coordenada x na matriz do botão atual;
* @param y coordenada y na matriz do botão atual.
*/
public CButton(int x, int y){
this.x = x;
this.y = y;
this.setText("?");
this.setFocusable(false);
this.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
this.setEnabled(false);
refreshButtons();
System.out.println("Coordenadas do clique: ["+x+","+y+"]");
if (tabuleiro.getContentFrom(x,y) == Tabuleiro.MINE){
System.out.println("PERDEU");
}
}
public void refreshButtons(){
for (int i = 0; i < tabuleiro.tabSize; i++){
for (int j = 0; j < tabuleiro.tabSize; j++){
if (tabuleiro.hints[x][y] != 9){
slots[x][y].setText(String.valueOf(tabuleiro.hints[x][y]));
break;
}
if (tabuleiro.hints[x][y] == 9){
slots[x][y].setText("@");
break;
}
}
}
}
}
}
package com.minesweeper;
import java.util.ArrayList;
import java.util.Random;
/**
* Created by lucas on 01/04/16.
*/
public class Tabuleiro {
public int[][] tabContent, hints;
public int tabSize;
public static int MINE = 1, NONE = 0;
public Tabuleiro(int tabSize) {
this.tabSize = tabSize;
setupTab(tabSize);
//Provavelmente mudarei como e quando as bombas serao definidas...
setMinesRandom(tabSize);
setupHints();
}
/**
* @param tabSize tamanho para inicialização da matriz de conteúdo (quadraticamente).
*/
private void setupTab(int tabSize) {
tabContent = new int[tabSize][tabSize];
for (int i = 0; i < tabSize; i++) {
for (int j = 0; j < tabSize; j++) {
tabContent[i][j] = NONE;
}
}
}
/**
* @param type tipo do conteúdo a substituir pelo existente;
* @param x coordenada x do item a ser substituido;
* @param y coordenada y do item a ser substituido.
*/
private void setSlotContent(int type, int x, int y) {
try {
tabContent[x][y] = type;
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("Tentou mudar o valor de um indice que nao esta definido (== null) no array tabContent!");
e.printStackTrace();
}
}
/**
* @param numMines número de minas para serem adicionadas no tabuleiro principal.
*/
private void setMinesRandom(int numMines) {
Random r = new Random(System.nanoTime());
for (int counter = 0; counter < numMines; counter++) {
int x = r.nextInt((tabSize)), y = r.nextInt((tabSize));
if (tabContent[x][y] != 1) setSlotContent(MINE, x, y);
else counter--;
}
}
/**
* @param x coordenada x para ser analisada;
* @param y coordenada y para ser analisada;
* @return conteúdo presente nas coordenas @param x e @param y.
*/
public int getContentFrom(int x, int y) {
try {
return tabContent[x][y] == MINE ? MINE : NONE;
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("Tentou acessar o valor de um indice que nao esta definido (== null) no array tabContent!");
e.printStackTrace();
return -1;
}
}
/**
* @param x coordenada x para analisar;
* @param y coordenada y para analisar;
* @return coordenadas ao norte de @param x e @param y.
*/
private int[] N(int x, int y) {
return (x > 0 && x <= (tabSize - 1)) ? new int[]{x - 1, y} : null;
}
/**
* @param x coordenada x para analisar;
* @param y coordenada y para analisar;
* @return coordenadas ao sul de @param x e @param y.
*/
private int[] S(int x, int y) {
return (x >= 0 && x < (tabSize - 1)) ? new int[]{x + 1, y} : null;
}
/**
* @param x coordenada x para analisar;
* @param y coordenada y para analisar;
* @return coordenadas a leste de @param x e @param y.
*/
private int[] E(int x, int y) {
return (y >= 0 && y < (tabSize - 1)) ? new int[]{x, y + 1} : null;
}
/**
* @param x coordenada x para analisar;
* @param y coordenada y para analisar;
* @return coordenadas a oeste de @param x e @param y.
*/
private int[] W(int x, int y) {
return (y > 0 && y <= (tabSize - 1)) ? new int[]{x, y - 1} : null;
}
/**
* @param x coordenada x para analisar;
* @param y coordenada y para analisar;
* @return coordenadas a nordeste de @param x e @param y.
*/
private int[] NE(int x, int y) {
int[] n = N(x, y);
if (n != null) {
int[] e = E(n[0], n[1]);
if (e != null) {
return e;
}
return null;
}
return null;
}
/**
* @param x coordenada x para analisar;
* @param y coordenada y para analisar;
* @return coordenadas a noroeste de @param x e @param y.
*/
private int[] NW(int x, int y) {
int[] n = N(x, y);
if (n != null) {
int[] w = W(n[0], n[1]);
if (w != null) {
return w;
}
return null;
}
return null;
}
/**
* @param x coordenada x para analisar;
* @param y coordenada y para analisar;
* @return coordenadas ao sudeste de @param x e @param y.
*/
private int[] SE(int x, int y) {
int[] s = S(x, y);
if (s != null) {
int[] e = E(s[0], s[1]);
if (e != null) {
return e;
}
return null;
}
return null;
}
/**
* @param x coordenada x para analisar;
* @param y coordenada y para analisar;
* @return coordenadas ao sudoeste de @param x e @param y.
*/
private int[] SW(int x, int y) {
int[] s = S(x, y);
if (s != null) {
int[] w = W(s[0], s[1]);
if (w != null) {
return w;
}
return null;
}
return null;
}
/**
* @param x coordenada x para analisar;
* @param y coordenada y para analisar;
* @return todas as cordenas existentes ao redor de @param x e @param y.
*/
private final ArrayList<int[]> getSurroundingsFrom(int x, int y) {
ArrayList<int[]> r = new ArrayList<>();
r.add(N(x, y));;;;;r.add(NE(x, y));
r.add(S(x, y));;;;;r.add(NW(x, y));
r.add(E(x, y));;;;;r.add(SE(x, y));
r.add(W(x, y));;;;;r.add(SW(x, y));
return r;
}
/**
* Define as dicas.
*/
public void setupHints() {
hints = new int[tabSize][tabSize];
for (int x = 0; x < tabSize; x++) {
for (int y = 0; y < tabSize; y++) {
hints[x][y] = 9;
if (getContentFrom(x, y) == NONE && getContentFrom(x, y) != -1) {
int slotMinesCounter = 0;
for (int[] surroudings : getSurroundingsFrom(x, y)) {
if (surroudings != null)
if (getContentFrom(surroudings[0], surroudings[1]) == MINE && getContentFrom(x, y) != -1)
slotMinesCounter++;
}
hints[x][y] = slotMinesCounter;
}
}
}
//printHints();
}
public void printHints() {
String r = "";
for (int i = 0; i < tabSize; i++) {
for (int j = 0; j < tabSize; j++) {
if (hints[i][j] != 9) r += hints[i][j] + " ";
else r += "W ";
}
r += "\n";
}
System.out.println(r);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment