Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 minesweeper;
- import java.util.*;
- /**
- *
- * @author Your Ally
- */
- public class Minesweeper {
- static int Turns;
- static boolean Won;
- static String Name;
- static int Winnings;
- public Minesweeper(boolean won, int turns,String pName,int score){
- Turns=turns;
- Won=won;
- Name=pName;
- Winnings=score;
- }
- public static void main(String[] args) {
- Scanner inpt = new Scanner(System.in);
- int pl=0,again,respd;
- String ans;
- String pName;
- Minesweeper [] player= new Minesweeper[20];
- do{
- again=0;
- respd=0;
- System.out.println("What's your name?");
- pName=inpt.next();
- while(respd==0){
- System.out.println(pName+", are you Ready? (Yes/No)");
- ans=inpt.next();
- if(ans.charAt(0)=='y'||ans.charAt(0)=='Y'){
- respd=1;
- System.out.println("");
- System.out.println("Then let's begin");
- System.out.println("");
- }
- else{
- System.out.println("");
- System.out.println("It's ok, Take your time");
- System.out.println("");
- respd=0;
- }
- }
- Won=false;
- Turns=0;
- play(inpt);
- if (Won){
- System.out.println("Congratulations "+pName+"! You won in "+Turns+" move(s)!");
- }
- else{
- System.out.println("Ouch "+pName+"! You lost in "+Turns+" move(s)!");
- }
- System.out.println("");
- System.out.println("Play Again?");
- ans = inpt.next();
- if(ans.charAt(0)=='y'||ans.charAt(0)=='Y'){
- again=1;
- }
- else{
- again=0;
- }
- player[pl]= new Minesweeper(Won,Turns,pName,Winnings);
- pl++;
- }while(again==1);
- System.out.println("Won?:\tName:\tTurns:\t:Score:");
- for(int i=0;i<i;i++){
- System.out.println(player[i].Won+"\t"+player[i].Name+"\t"+player[i].Turns+"\t"+player[i].Winnings);
- }
- }
- public static void play(Scanner inpt){
- int gamesize=2,respd,tresp,minenum=1,rrand,crand,isgoing=1,mined,c=1,r=1;
- boolean win;
- respd=0;
- while (respd==0){
- System.out.println("Choose the desired game size ([ ]^2): ");
- gamesize=inpt.nextInt();
- if (gamesize<11&&gamesize>1){
- respd=1;
- }
- else{
- System.out.println("ERROR! Please input a valid size");
- System.out.println();
- }
- }
- int[][] field = new int[gamesize][gamesize];
- int[][] mark = new int[gamesize][gamesize];
- int[][] isOpened = new int[gamesize][gamesize];
- for(int i=0;i<gamesize;i++){
- for(int j=0;j<gamesize;j++){
- field[i][j]=0;
- mark[i][j]=0;
- isOpened[i][j]=0;
- }
- }
- respd=0;
- while (respd==0){
- System.out.println("Choose the desired number of mines (1 - "+((gamesize*gamesize)-1)+"): ");
- minenum=inpt.nextInt();
- if (minenum>0&&minenum<(gamesize*gamesize)){
- respd=1;
- }
- else{
- System.out.println("ERROR! Please input a valid number");
- System.out.println();
- }
- }
- for(int i=1;i<=minenum;i++){
- mined=0;
- while(mined==0){
- rrand=(int)(Math.random()*gamesize)+0;
- crand=(int)(Math.random()*gamesize)+0;
- if(field[rrand][crand]!=1){
- field[rrand][crand]=1;
- mined=1;
- }
- }
- }
- System.out.println("Alright, Then let's begin");
- System.out.println("___________________________");
- System.out.println("");
- System.out.println("Welcome to Minesweeper!");
- System.out.println("");
- while(isgoing==1){
- for(int i=0;i<gamesize;i++){
- for(int j=0;j<gamesize;j++){
- System.out.print("[");
- if(isOpened[i][j]==0){
- System.out.print(" ");
- }
- else{
- System.out.print(mark[i][j]);
- }
- System.out.print("]");
- }
- System.out.println();
- }
- tresp=0;
- while(tresp==0){
- System.out.println();
- respd=0;
- while(respd==0){
- System.out.println("Enter row of the cell you want to open (1 - "+gamesize+"): ");
- r = inpt.nextInt();
- if (r<=gamesize&&r>=1){
- respd=1;
- }
- else{
- System.out.println("ERROR");
- System.out.println("");
- }
- }
- for(int i=0;i<gamesize;i++){
- if (r-1==i){
- System.out.print("> ");
- }
- else{
- System.out.print(" ");
- }
- for(int j=0;j<gamesize;j++){
- System.out.print("[");
- if(isOpened[i][j]==0){
- System.out.print(" ");
- }
- else{
- System.out.print(mark[i][j]);
- }
- System.out.print("]");
- }
- System.out.println();
- }
- respd=0;
- while(respd==0){
- System.out.println("Enter the column of the cell you want to open (1 - "+gamesize+"): ");
- c = inpt.nextInt();
- if (c<=gamesize&&c>=1){
- respd=1;
- }
- else{
- System.out.println("ERROR");
- System.out.println("");
- }
- }
- c=c-1;
- r=r-1;
- if (isOpened[r][c]==0){
- tresp=1;
- }
- else{
- System.out.println("ERROR, Restarting from the row selection");
- }
- }
- mark[r][c]=cellcheck(field,r,c,gamesize);
- isOpened[r][c]= 1;
- if(field[r][c]==1){
- for(int i=0;i<gamesize;i++){
- for(int j=0;j<gamesize;j++){
- System.out.print("[");
- if(field[i][j]==1){
- System.out.print("X");
- }
- else if(isOpened[i][j]==0){
- System.out.print(" ");
- }
- else{
- System.out.print(mark[i][j]);
- }
- System.out.print("]");
- }
- System.out.println();
- }
- break;
- }
- Turns++;
- win=isWon(minenum,gamesize);
- if(win==true){
- isgoing=0;
- Won=win;
- }
- }
- Winnings=(gamesize*(minenum*2))*Turns;
- }
- public static boolean isWon(int mines, int gamesize){
- if((gamesize*gamesize)-mines==Turns){
- return true;
- }
- else{
- return false;
- }
- }
- public static int cellcheck(int [][]arr,int r,int c,int gamesize){
- int total=0;
- int max = gamesize;
- int column,row;
- row=r-1;
- column=c-1;
- if(isInside(column,row,max)){
- total = total+arr[row][column];
- }
- row=r;
- column=c-1;
- if(isInside(column,row,max)){
- total = total+arr[row][column];
- }
- row=r+1;
- column=c-1;
- if(isInside(column,row,max)){
- total = total+arr[row][column];
- }
- row=r-1;
- column=c;
- if(isInside(column,row,max)){
- total = total+arr[row][column];
- }
- row=r+1;
- column=c;
- if(isInside(column,row,max)){
- total = total+arr[row][column];
- }
- row=r-1;
- column=c+1;
- if(isInside(column,row,max)){
- total = total+arr[row][column];
- }
- row=r;
- column=c+1;
- if(isInside(column,row,max)){
- total = total+arr[row][column];
- }
- row=r+1;
- column=c+1;
- if(isInside(column,row,max)){
- total = total+arr[row][column];
- }
- return total;
- }
- public static boolean isInside(int column, int row, int max){
- if(column<0||column>=max||row<0||row>=max){
- return false;
- }
- else{
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement