Jump to content

Text-based MineSweeper

- - - - -

  • Please log in to reply
2 replies to this topic

#1
GTghost

GTghost

    Newbie

  • Members
  • PipPip
  • 19 posts
Had this compile before but not I get a whole bunch of errors, any pointers how to fix them?
import java.io.PrintStream;
import java.util.Random;
import java.util.Scanner;
public class MS2D
{
  public static int[][] map = new int[10][10];
  public static boolean[][] played = new boolean[10][10];
  public static int row_pick;
  public static int col_pick;
  public static int THRESHOLD = 8;
  public static void main(String[] paramArrayOfString)
  {
    Scanner localScanner = new Scanner(System.in);
    InitMap();
    ShowMap();
    while (true)
    {
      System.out.print("Which row and column to play (-1 to quit)? ");
      row_pick = localScanner.nextInt();
      col_pick = localScanner.nextInt();
      if ((row_pick < 0) || (row_pick > map.length - 1) || (col_pick < 0) || (col_pick > map.length - 1))
      {
        System.out.println("Thanks for playing!");
      }
      else
      {
        if (played[row_pick][col_pick] != 0)
        {
          System.out.println("That position has been played!");
          continue;
        }
        int m;
        int k = m = 0;
        int i;
        int j;
        if (map[row_pick][col_pick] == 9)
        {
          k = 1;
        }
        else
        {
          MarkPlayed(row_pick, col_pick);
          m = 1;
          for (i = 0; i < map.length; i++)
          {
            for (j = 0; j < map[0].length; j++)
            {
              if ((played[i][j] != 0) || (map[i][j] == 9)) continue; m = 0;
            }
          }
        }
        if ((k != 0) || (m != 0))
        {
          for (i = 0; i < map.length; i++)
          {
            for (j = 0; j < map[0].length; j++)
            {
              played[i][j] = 1;
            }
          }
        }
        ShowMap();
        if (k != 0)
        {
          System.out.println("Boom! Bye, bye.");
        }
        else
        {
          if (m == 0)
            continue;
          System.out.println("Good Game!");
        }
      }
    }
  }
  public static int CountMines(int paramInt1, int paramInt2)
  {
    int i = map.length;
    int j = map[0].length;
    int k = 0;
    if ((paramInt1 - 1 >= 0) && (paramInt2 - 1 >= 0) && (map[(paramInt1 - 1)][(paramInt2 - 1)] == 9)) k++;
    if ((paramInt1 - 1 >= 0) && (map[(paramInt1 - 1)][paramInt2] == 9)) k++;
    if ((paramInt1 - 1 >= 0) && (paramInt2 + 1 < j) && (map[(paramInt1 - 1)][(paramInt2 + 1)] == 9)) k++;
    if ((paramInt2 - 1 >= 0) && (map[paramInt1][(paramInt2 - 1)] == 9)) k++;
    if ((paramInt2 + 1 < j) && (map[paramInt1][(paramInt2 + 1)] == 9)) k++;
    if ((paramInt1 + 1 < i) && (paramInt2 - 1 >= 0) && (map[(paramInt1 + 1)][(paramInt2 - 1)] == 9)) k++;
    if ((paramInt1 + 1 < i) && (map[(paramInt1 + 1)][paramInt2] == 9)) k++;
    if ((paramInt1 + 1 < i) && (paramInt2 + 1 < j) && (map[(paramInt1 + 1)][(paramInt2 + 1)] == 9)) k++;
    return k;
  }
  public static void InitMap()
  {
    Random localRandom = new Random();
    int j;
    for (int i = 0; i < map.length; i++)
    {
      for (j = 0; j < map[0].length; j++)
      {
        int k = localRandom.nextInt(10);
        if (k > THRESHOLD)
        {
          map[i][j] = 9;
        }
        else
        {
          map[i][j] = 0;
        }
      }
    }
    for (i = 0; i < map.length; i++)
    {
      for (j = 0; j < map[0].length; j++)
      {
        if (map[i][j] != 0)
          continue;
        map[i][j] = CountMines(i, j);
      }
    }
  }
  public static void MarkPlayed(int paramInt1, int paramInt2)
  {
    int i = map.length;
    int j = map[0].length;
    played[paramInt1][paramInt2] = 1;
    if ((paramInt1 - 1 >= 0) && (paramInt2 - 1 >= 0) && (map[(paramInt1 - 1)][(paramInt2 - 1)] != 9)) played[(paramInt1 - 1)][(paramInt2 - 1)] = 1;
    if ((paramInt1 - 1 >= 0) && (map[(paramInt1 - 1)][paramInt2] != 9)) played[(paramInt1 - 1)][paramInt2] = 1;
    if ((paramInt1 - 1 >= 0) && (paramInt2 + 1 < j) && (map[(paramInt1 - 1)][(paramInt2 + 1)] != 9)) played[(paramInt1 - 1)][(paramInt2 + 1)] = 1;
    if ((paramInt2 - 1 >= 0) && (map[paramInt1][(paramInt2 - 1)] != 9)) played[paramInt1][(paramInt2 - 1)] = 1;
    if ((paramInt2 + 1 < j) && (map[paramInt1][(paramInt2 + 1)] != 9)) played[paramInt1][(paramInt2 + 1)] = 1;
    if ((paramInt1 + 1 < i) && (paramInt2 - 1 >= 0) && (map[(paramInt1 + 1)][(paramInt2 - 1)] != 9)) played[(paramInt1 + 1)][(paramInt2 - 1)] = 1;
    if ((paramInt1 + 1 < i) && (map[(paramInt1 + 1)][paramInt2] != 9)) played[(paramInt1 + 1)][paramInt2] = 1;
    if ((paramInt1 + 1 < i) && (paramInt2 + 1 < j) && (map[(paramInt1 + 1)][(paramInt2 + 1)] != 9)) played[(paramInt1 + 1)][(paramInt2 + 1)] = 1;
  }
  public static void ShowMap()
  {
    for (int i = 0; i < played.length; i++)
    {
      for (j = 0; j < played[0].length; j++)
      {
        if (played[i][j] != 0)
        {
          System.out.print(map[i][j] + " ");
        }
        else
        {
          System.out.print(". ");
        }
      }
      System.out.println(" " + i);
    }
    System.out.println();
    for (int j = 0; j < played[0].length; j++)
    {
      System.out.print(j + " ");
    }
    System.out.println();
  }
}



#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
And what are the errors?

#3
ttax

ttax

    Newbie

  • Members
  • Pip
  • 5 posts
As you mentioned (GThghost) there are a lot of errors. One that pops up often is your comparison
played[row_pick][col_pick] != 0
this won't work as played is an array of booleans and so you end up comparing a boolean to an int. It looks like played was originally an int array, so I'm not sure how you want to fix that. Another couple of errors stem from not initializing ints in your for loops (i.e. int i in:
    for (i = 0; i < map.length; i++)

    {

      for (j = 0; j < map[0].length; j++)

      {

        if (map[i][j] != 0)

          continue;

        map[i][j] = CountMines(i, j);

      }

    }

the last couple errors stem from trying to place ints in your boolean played array.

if ((paramInt1 - 1 >= 0) && (map[(paramInt1 - 1)][paramInt2] != 9)) played[(paramInt1 - 1)][paramInt2] = 1;

hope that helps!

EDIT: I didn't actually read the code to observe how you're using the played array, so I could be off base here, but it seems as if you want it to be an int array (this would fix a lot of issues).

Edited by ttax, 01 December 2011 - 01:02 PM.
adding more





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users