NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pt.ipleiria.estg.es1.minesfinder;

import java.util.Random;

/**
*
* @author Figueiredo
*/
public class CampoMinado {

public static final int VAZIO = 0;

public static final int TAPADO = 9;
public static final int DUVIDA = 10;
public static final int MARCADO = 11;
public static final int REBENTADO = 12;

private boolean[][] minas;
private int[][] estado;
private int largura;
private int altura;
private int numMinas;
private boolean primeiraJogada;
private boolean jogoTerminado;
private boolean jogadorDerrotado;
private Random gerador;
private long instanteInicioJogo;
private long duracaoJogo;

public CampoMinado(int largura, int altura, int numMinas) {
this.largura = largura;
this.altura = altura;
this.numMinas = numMinas;
minas = new boolean[largura][altura]; // Valores começam a false
estado = new int[largura][altura]; // Valores começam a 0
primeiraJogada = true;
jogoTerminado = false;
jogadorDerrotado = false;
gerador = new Random();

for (int x = 0; x < largura; x++) {
for (int y = 0; y < altura; y++) {
estado[x][y] = TAPADO;
}
}

}

public int getEstadoQuadricula(int x, int y) {
return estado[x][y];
}

public boolean hasMina(int x, int y) {
return minas[x][y];
}

public int getLargura() {
return largura;
}

public int getAltura() {
return altura;
}

public boolean isJogoTerminado() {

return jogoTerminado;
}

public boolean isJogadorDerrotado() {

return jogadorDerrotado;
}

public int getNumMinas() {
return numMinas;
}



private void revelarQuadriculasVizinhas(int x, int y) {
for (int coluna = Math.max(0, x - 1); coluna < Math.min(largura, x + 2); coluna++) {
for (int linha = Math.max(0, y - 1); linha < Math.min(altura, y + 2); linha++) {
if (!minas[coluna][linha]) {
estado[x][y] = VAZIO;
}
}
}

}

public void revelarQuadricula(int x, int y) {
if (estado[x][y] == TAPADO && !jogoTerminado) {
if (primeiraJogada) {
primeiraJogada = false;
colocarMinas(x, y);
instanteInicioJogo = System.currentTimeMillis();

}
}
if (this.hasMina(x, y)) {
estado[x][y] = REBENTADO;
System.out.println("bomba");
jogoTerminado = true;
jogadorDerrotado = true;
duracaoJogo = System.currentTimeMillis() - instanteInicioJogo;

}
if (!this.hasMina(x, y)) {


if (this.contarMinasAVolta(x, y) > 0) {
System.out.println("juca");
estado[x][y] = contarMinasAVolta(x, y);

}else{
estado[x][y] = VAZIO;
System.out.println("vazio");
}
this.revelarQuadriculasVizinhas(x, y);
}
if (verificarVitoria()) {
jogoTerminado = true;
jogadorDerrotado = false;
duracaoJogo = System.currentTimeMillis() - instanteInicioJogo;
}

}

private void colocarMinas(int exceptoX, int exceptoY) {
for (int i = 0; i < numMinas; i++) {
int x = 0;
int y = 0;
do {
x = gerador.nextInt(largura);
y = gerador.nextInt(altura);
} while (minas[x][y] || (x == exceptoX && y == exceptoY));
minas[x][y] = true;
}
}

private int contarMinasAVolta(int x, int y) {
int resultado = 0;
for (int coluna = Math.max(0, x - 1); coluna < Math.min(largura, x + 2); coluna++) {
for (int linha = Math.max(0, y - 1); linha < Math.min(altura, y + 2); linha++) {
if (minas[coluna][linha]) {
resultado++;
}
}
}
return resultado - (minas[x][y] ? 1 : 0);
}

private boolean verificarVitoria() {
boolean vitoria = true;
for (int x = 0; x < largura; x++) {
for (int y = 0; y < altura; y++) {
if (!minas[x][y]) {
vitoria = vitoria && estado[x][y] >= 0 && estado[x][y] < 9;
}
}
}
return vitoria;
}

public void marcarComoTendoMina(int x, int y) {

if (estado[x][y] == TAPADO || estado[x][y] == DUVIDA) {
estado[x][y] = MARCADO;
}

}

public void marcarComoSuspeita(int x, int y) {

if (estado[x][y] == TAPADO || estado[x][y] == MARCADO) {
estado[x][y] = DUVIDA;
}

}

public long getDuracaoJogo() {
if (primeiraJogada) // O jogo ainda não começou
{
return 0;
}
if (!jogoTerminado) // O jogo ainda não terminou
{
return System.currentTimeMillis() - instanteInicioJogo;
}
return duracaoJogo;
}

}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.