mardi 30 mai 2017

Minesweeper Java Colouring Pegs

I am making a minesweeper game where it follows the standard rules. When a mine is hit the game ends and if all spots are filled then the user wins. What I am trying to do is make the pegs have different colours according to the amount of mines it hits. I have declared a local variable to keep track of the number of surrounding mines. Then used if statements, if you find a mine, use a count++. I am not quite sure how to fix this line in particular to make it do what I want it to do. if((currentClick.getRow() == ships.get(i).getRow()) && (currentClick.getCol() == ships.get(i).getCol())){ count++; }

Here is my code so far:

import java.util.*;

public class Minesweeper2{


static int dim = userSize(); //call to userSize method is stored as static value
static Board board = new Board(dim, dim);//and immediately built to Board object of user-defined size

//this method uses Terminal to ask the user what size the board should be
public static int userSize(){

Scanner scan = new Scanner(System.in);
System.out.println("What size should the board be? (Numbers only (5-1000))");
int dim = scan.nextInt();
return dim;
}

public static void main(String[] args){

boolean unWon = true;//variable to determine if game is over
Random random = new Random();//call random method
int totalClicks = 0;
int mineHit = 0;

board.displayMessage("Welcome to Minesweeper!  White=0, Blue = 1, Green = 2, Red = 3, Yellow = 4");

//initialize each row and column interger for the ten coordinates that are red
int[] nums = new int[20];

//initialize array list and store values of ship inside
ArrayList<Coordinate> ships = new ArrayList<Coordinate>();

for(int i = 0; i < nums.length; i++){
  nums[i] = random.nextInt(10);
  if(i%2 == 1)
    ships.add(new Coordinate(nums[i-1],nums[i]));
}

Boolean isHit = false;//set boolean is hit to false

while(unWon && totalClicks < 100){
  isHit = false; // reset isHit
  Coordinate currentClick = board.getClick(); //Get the current click
  totalClicks++;//increment counter for each click

  //Check the ship coordinates to see whether it is hit
  for(Coordinate c: ships) {
    if((c.getRow() == currentClick.getRow()) && (c.getCol() == currentClick.getCol())){
      //board.putPeg("black", currentClick.getRow(), currentClick.getCol());//output red pegs
      isHit = true;//reset is hit boolean
      mineHit++;//initialize counter for each red peg

      //if all ten red pegs are found, set the game to won
      if (mineHit == 10){
        unWon = false;
        break;
      }

    }

    for (int i = 0; i < ships.size(); i++) {

    if(isHit){
      board.putPeg("black", currentClick.getRow(), currentClick.getCol());
    }

    //this needs to get more complicated
    if((currentClick.getRow() == ships.get(i).getRow()) && (currentClick.getCol() == ships.get(i).getCol())){
    count++;
    }



    else if(count==1){
      board.putPeg("blue", currentClick.getRow(), currentClick.getCol()); 
    }

    else if(count==2){
      board.putPeg("green", currentClick.getRow(), currentClick.getCol());
    }  

    else if(count==3){
      board.putPeg("red", currentClick.getRow(), currentClick.getCol());
    }  

    else if(count==4){
      board.putPeg("yellow", currentClick.getRow(), currentClick.getCol());
    }


    else{
        board.putPeg("white",currentClick.getRow(), currentClick.getCol());
      }
  }

    board.displayMessage("It has been " + totalClicks + " click(s)" + ";      White = 0, Blue = 1, Green = 2, Red = 3, Yellow = 4");

      for(int red = 0; red<10; red++){
        System.out.println(ships.get(red).getRow() + " " + ships.get(red).getCol());
      }
} 
}
}

Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire