Advertisement
Guest User

Untitled

a guest
Feb 6th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.22 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package minesweeper;
  7. import java.util.*;
  8.  
  9.  
  10. /**
  11.  *
  12.  * @author Your Ally
  13.  */
  14. public class Minesweeper {
  15.  
  16.     static int Turns;
  17.     static boolean Won;
  18.     static String Name;
  19.     static int Winnings;
  20.     public Minesweeper(boolean won, int turns,String pName,int score){
  21.         Turns=turns;
  22.         Won=won;
  23.         Name=pName;
  24.         Winnings=score;
  25.        
  26.     }
  27.    
  28.     public static void main(String[] args) {
  29.        
  30.         Scanner inpt = new Scanner(System.in);
  31.         int pl=0,again,respd;
  32.         String ans;
  33.         String pName;
  34.         Minesweeper [] player= new Minesweeper[20];
  35.        
  36.         do{
  37.        
  38.         again=0;
  39.         respd=0;
  40.        
  41.         System.out.println("What's your name?");
  42.         pName=inpt.next();
  43.        
  44.        while(respd==0){
  45.         System.out.println(pName+", are you Ready? (Yes/No)");
  46.         ans=inpt.next();
  47.        
  48.         if(ans.charAt(0)=='y'||ans.charAt(0)=='Y'){
  49.             respd=1;
  50.            
  51.             System.out.println("");
  52.             System.out.println("Then let's begin");
  53.             System.out.println("");
  54.         }
  55.        
  56.         else{
  57.             System.out.println("");
  58.             System.out.println("It's ok, Take your time");
  59.             System.out.println("");
  60.             respd=0;          
  61.         }
  62.        
  63.        
  64.         }
  65.         Won=false;
  66.         Turns=0;    
  67.        
  68.         play(inpt);
  69.        
  70.         if (Won){
  71.             System.out.println("Congratulations "+pName+"! You won in "+Turns+" move(s)!");
  72.         }
  73.         else{
  74.             System.out.println("Ouch "+pName+"! You lost in "+Turns+" move(s)!");
  75.         }
  76.        
  77.         System.out.println("");
  78.         System.out.println("Play Again?");
  79.        
  80.         ans = inpt.next();
  81.         if(ans.charAt(0)=='y'||ans.charAt(0)=='Y'){
  82.             again=1;
  83.         }
  84.         else{
  85.             again=0;
  86.         }
  87.        
  88.         player[pl]= new Minesweeper(Won,Turns,pName,Winnings);
  89.         pl++;
  90.        
  91.         }while(again==1);
  92.        
  93.         System.out.println("Won?:\tName:\tTurns:\t:Score:");
  94.         for(int i=0;i<i;i++){
  95.             System.out.println(player[i].Won+"\t"+player[i].Name+"\t"+player[i].Turns+"\t"+player[i].Winnings);
  96.         }
  97.        
  98.     }
  99.    
  100.     public static void play(Scanner inpt){
  101.         int gamesize=2,respd,tresp,minenum=1,rrand,crand,isgoing=1,mined,c=1,r=1;
  102.         boolean win;
  103.        
  104.         respd=0;
  105.        
  106.         while (respd==0){
  107.         System.out.println("Choose the desired game size ([ ]^2): ");
  108.         gamesize=inpt.nextInt();
  109.        
  110.         if (gamesize<11&&gamesize>1){
  111.         respd=1;
  112.         }
  113.         else{
  114.             System.out.println("ERROR! Please input a valid size");
  115.             System.out.println();
  116.         }
  117.         }
  118.        
  119.         int[][] field = new int[gamesize][gamesize];
  120.         int[][] mark = new int[gamesize][gamesize];
  121.         int[][] isOpened = new int[gamesize][gamesize];
  122.        
  123.         for(int i=0;i<gamesize;i++){
  124.             for(int j=0;j<gamesize;j++){
  125.                 field[i][j]=0;
  126.                 mark[i][j]=0;
  127.                 isOpened[i][j]=0;
  128.             }
  129.         }
  130.  
  131.         respd=0;
  132.        
  133.         while (respd==0){
  134.         System.out.println("Choose the desired number of mines (1 - "+((gamesize*gamesize)-1)+"): ");
  135.         minenum=inpt.nextInt();
  136.        
  137.         if (minenum>0&&minenum<(gamesize*gamesize)){
  138.          
  139.         respd=1;
  140.         }
  141.         else{
  142.             System.out.println("ERROR! Please input a valid number");
  143.             System.out.println();
  144.         }
  145.         }
  146.        for(int i=1;i<=minenum;i++){
  147.            
  148.             mined=0;
  149.             while(mined==0){
  150.             rrand=(int)(Math.random()*gamesize)+0;
  151.             crand=(int)(Math.random()*gamesize)+0;
  152.        
  153.                 if(field[rrand][crand]!=1){
  154.                     field[rrand][crand]=1;
  155.                     mined=1;
  156.                 }
  157.             }
  158.         }
  159.  
  160.         System.out.println("Alright, Then let's begin");
  161.         System.out.println("___________________________");
  162.         System.out.println("");
  163.        
  164.         System.out.println("Welcome to Minesweeper!");
  165.         System.out.println("");
  166.        
  167.         while(isgoing==1){
  168.        
  169.         for(int i=0;i<gamesize;i++){
  170.             for(int j=0;j<gamesize;j++){
  171.                 System.out.print("[");
  172.                
  173.                  if(isOpened[i][j]==0){
  174.                     System.out.print(" ");
  175.                 }
  176.                 else{
  177.                     System.out.print(mark[i][j]);
  178.                 }
  179.                
  180.                 System.out.print("]");
  181.             }
  182.             System.out.println();
  183.         }
  184.        
  185.         tresp=0;
  186.         while(tresp==0){
  187.        
  188.         System.out.println();
  189.         respd=0;
  190.         while(respd==0){
  191.         System.out.println("Enter row of the cell you want to open (1 - "+gamesize+"): ");
  192.         r = inpt.nextInt();
  193.         if (r<=gamesize&&r>=1){
  194.             respd=1;
  195.         }
  196.         else{
  197.             System.out.println("ERROR");
  198.             System.out.println("");
  199.         }
  200.         }
  201.         for(int i=0;i<gamesize;i++){
  202.             if (r-1==i){
  203.                 System.out.print("> ");
  204.             }
  205.             else{
  206.                 System.out.print("  ");
  207.             }
  208.             for(int j=0;j<gamesize;j++){
  209.                 System.out.print("[");
  210.                
  211.                 if(isOpened[i][j]==0){
  212.                     System.out.print(" ");
  213.                 }
  214.                 else{
  215.                     System.out.print(mark[i][j]);
  216.                 }
  217.                
  218.                 System.out.print("]");
  219.             }
  220.             System.out.println();
  221.         }
  222.        
  223.         respd=0;
  224.         while(respd==0){
  225.         System.out.println("Enter the column of the cell you want to open (1 - "+gamesize+"): ");
  226.         c = inpt.nextInt();
  227.         if (c<=gamesize&&c>=1){
  228.             respd=1;
  229.         }
  230.         else{
  231.             System.out.println("ERROR");
  232.             System.out.println("");
  233.         }
  234.         }
  235.         c=c-1;
  236.         r=r-1;
  237.         if (isOpened[r][c]==0){
  238.             tresp=1;
  239.         }
  240.        
  241.         else{
  242.             System.out.println("ERROR, Restarting from the row selection");
  243.         }
  244.         }
  245.        
  246.        
  247.         mark[r][c]=cellcheck(field,r,c,gamesize);
  248.         isOpened[r][c]= 1;
  249.        
  250.         if(field[r][c]==1){
  251.            
  252.             for(int i=0;i<gamesize;i++){
  253.             for(int j=0;j<gamesize;j++){
  254.                 System.out.print("[");
  255.                
  256.                
  257.                 if(field[i][j]==1){
  258.                     System.out.print("X");
  259.                 }
  260.                 else if(isOpened[i][j]==0){
  261.                     System.out.print(" ");
  262.                 }
  263.                 else{
  264.                     System.out.print(mark[i][j]);
  265.                 }
  266.                
  267.                 System.out.print("]");
  268.             }
  269.             System.out.println();
  270.          
  271.         }
  272.            break;
  273.         }
  274.        
  275.         Turns++;
  276.            
  277.         win=isWon(minenum,gamesize);
  278.         if(win==true){
  279.             isgoing=0;
  280.             Won=win;
  281.         }
  282.        }
  283.        Winnings=(gamesize*(minenum*2))*Turns;
  284.        
  285.     }  
  286.     public static boolean isWon(int mines, int gamesize){
  287.         if((gamesize*gamesize)-mines==Turns){
  288.             return true;
  289.         }
  290.         else{
  291.            return false;
  292.         }
  293.     }
  294.     public static int cellcheck(int [][]arr,int r,int c,int gamesize){
  295.         int total=0;
  296.         int max = gamesize;
  297.         int column,row;
  298.        
  299.         row=r-1;
  300.         column=c-1;
  301.         if(isInside(column,row,max)){
  302.         total = total+arr[row][column];
  303.         }
  304.        
  305.         row=r;
  306.         column=c-1;
  307.         if(isInside(column,row,max)){
  308.         total = total+arr[row][column];
  309.         }
  310.        
  311.         row=r+1;
  312.         column=c-1;
  313.         if(isInside(column,row,max)){
  314.         total = total+arr[row][column];
  315.         }
  316.        
  317.         row=r-1;
  318.         column=c;
  319.         if(isInside(column,row,max)){
  320.         total = total+arr[row][column];
  321.         }
  322.        
  323.         row=r+1;
  324.         column=c;
  325.         if(isInside(column,row,max)){
  326.         total = total+arr[row][column];
  327.         }
  328.          
  329.         row=r-1;
  330.         column=c+1;
  331.         if(isInside(column,row,max)){
  332.         total = total+arr[row][column];
  333.         }
  334.                  
  335.         row=r;
  336.         column=c+1;
  337.         if(isInside(column,row,max)){
  338.         total = total+arr[row][column];
  339.         }
  340.                  
  341.         row=r+1;
  342.         column=c+1;
  343.         if(isInside(column,row,max)){
  344.         total = total+arr[row][column];
  345.         }
  346.        
  347.        
  348.        
  349.        
  350.        
  351.         return total;
  352.     }
  353.     public static boolean isInside(int column, int row, int max){
  354.        if(column<0||column>=max||row<0||row>=max){
  355.            return false;
  356.        }
  357.        else{
  358.            return true;
  359.        }
  360.    }
  361.  
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement