Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MINY MAIN
- package javaapplication16;
- import java.util.Scanner;
- /* hledani min */
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- HraciPole a = new HraciPole(10,10,10);
- a.Start();
- while(a.GameOver == false && a.Finish == false) {
- a.VypisPrubeznehoPole();
- SOPLN("Zadejte souradnice: radek sloupec");
- int m = sc.nextInt();
- int n = sc.nextInt();
- a.Hadam(m, n);
- }
- if (a.GameOver == true) {
- SOPLN(" ");
- a.VypisKonecnehoPole();
- SOPLN(" ");
- SOPLN("Slapli jste na minu!");
- SOPLN("!!! Game Over !!!");
- SOPLN(" ");
- }
- else {
- SOPLN(" ");
- a.VypisKonecnehoPole();
- SOPLN(" ");
- SOPLN("Blahopřeji !! Našli jste všechny miny !!");
- }
- }
- public static void SOPLN(String s) {
- System.out.println(s);
- }
- public static void SOP(String s) {
- System.out.print(s);
- }
- }
- ---------------------------------------------------------------------------------
- CLASS HRACIPOLE
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication16;
- /**
- *
- * @author Roman Belda
- */
- public class HraciPole {
- int m;
- int n;
- int PocetMin;
- int[][] HraciPole;
- int[][] HadaciPole;
- boolean GameOver = false;
- boolean Finish = false;
- HraciPole(int m, int n, int PocetMin) {
- this.m = m;
- this.n = n;
- this.PocetMin = PocetMin;
- this.HraciPole = new int[m][n];
- this.HadaciPole = new int[m][n];
- }
- public void Start() {
- GenerovaniMin();
- SpocitejPoleMin();
- }
- public void Hadam(int m, int n) {
- for (int i=0; i<this.m; i++){
- for (int j=0; j<this.n; j++) {
- if (this.HraciPole[i][j] == -1 && i == (m-1) && j == (n-1)) this.GameOver = true;
- else {
- if (this.HraciPole[i][j] == 0) this.HadaciPole[i][j] = 1;
- else {
- this.HadaciPole[m-1][n-1] = 1;
- }
- }
- }
- }
- }
- private void GenerovaniMin() {
- int minaM = (int) (Math.random()*(this.m-1))+1; // osetreni aby mina nemela adresu mimo hraci pole
- int minaN = (int) (Math.random()*(this.n-1))+1;
- for (int i=1; i<this.m; i++){
- for (int j=1; j<this.n; j++) {
- if (i == minaM && j == minaN) {
- if (this.HraciPole[i][j] == -1){ // osetreni aby nebyli 2 mini na sobe
- GenerovaniMin();
- }
- this.HraciPole[i][j] = -1;
- }
- }
- }
- this.PocetMin--;
- if (this.PocetMin>0) {
- GenerovaniMin();
- }
- }
- private void SpocitejPoleMin() {
- for (int i=0; i<this.m; i++){
- for (int j=0; j<this.n; j++) {
- if (this.HraciPole[i][j] == -1){
- if((i-1)>=0 && (j-1)>=0) if(this.HraciPole[i-1][j-1] != -1) this.HraciPole[i-1][j-1]++;
- if((i-1)>=0) if(this.HraciPole[i-1][j] != -1) this.HraciPole[i-1][j]++;
- if((i-1)>=0 && (j+1)<this.n) if(this.HraciPole[i-1][j+1] != -1) this.HraciPole[i-1][j+1]++;
- if((j-1)>=0) if(this.HraciPole[i][j-1] != -1) this.HraciPole[i][j-1]++;
- if((j+1)<this.n) if(this.HraciPole[i][j+1] != -1) this.HraciPole[i][j+1]++;
- if((i+1)<this.m && (j-1)>=0) if(this.HraciPole[i+1][j-1] != -1) this.HraciPole[i+1][j-1]++;
- if((i+1)<this.m) if(this.HraciPole[i+1][j] != -1) this.HraciPole[i+1][j]++;
- if((i+1)<this.m && (j+1)<this.n) if(this.HraciPole[i+1][j+1] != -1) this.HraciPole[i+1][j+1]++;
- }
- }
- }
- }
- public void VypisKonecnehoPole() {
- for (int i=0; i<this.m; i++){
- for (int j=0; j<this.n; j++) {
- if (this.HraciPole[i][j] == -1) System.out.print("• ");
- else if (this.HraciPole[i][j] == 0) System.out.print("* ");
- else System.out.print(this.HraciPole[i][j]+" ");
- }
- System.out.println(" ");
- }
- }
- public void VypisPrubeznehoPole() {
- for (int i=0; i<this.m; i++){
- for (int j=0; j<this.n; j++) {
- if (this.HadaciPole[i][j] == 1) System.out.print(this.HraciPole[i][j]+" ");
- else System.out.print("* ");
- /*
- if (this.HraciPole[i][j] == -1) System.out.print("• ");
- else if (this.HraciPole[i][j] == 0) System.out.print("* ");
- else System.out.print(this.HraciPole[i][j]+" ");*/
- }
- System.out.println(" ");
- }
- }
- }
Add Comment
Please, Sign In to add comment