Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Making Minesweeper in Ready To Program.
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
iamkyle015




PostPosted: Wed Nov 04, 2009 12:34 pm   Post subject: Making Minesweeper in Ready To Program.

Java:

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

public class Minesweeper
{
    public static void main (String[] args)
    {
        JFrame j = new JFrame ();
        j.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        j.getContentPane ().add (new MinesweeperPanel (), BorderLayout.CENTER);
        j.setSize (1025, 745);
        j.setVisible (true);


    }
}

class MinesweeperPanel extends JPanel implements MouseListener
{
    TroysGrid2 grid;
    int rows, columns;
    int[] [] game;
    boolean[] [] clicked;
    Mine m;
    Font font1;
    boolean lose, win;
    public MinesweeperPanel ()
    {

        setBackground (Color.black);
        font1 = new Font ("Arial", 1, 77);
        rows = 3;
        columns = 4;
        game = new int [rows + 2] [columns + 2];
        clicked = new boolean [rows + 2] [columns + 2];

        initMinesweeper ();

        debug ();

        m = new Mine ();
        grid = new TroysGrid2 ();
        grid.setDimensions (rows, columns);
        grid.setCellSize (70);
        grid.setPosition (55, 55);
        grid.setLineThickness (5);
        grid.setFillColor (Color.gray);
        grid.setColor (Color.white);


        addMouseListener (this);
    }



    public void paintComponent (Graphics g)
    {
        super.paintComponent (g);

        grid.draw (g);
        for (int r = 1 ; r <= rows ; r++)
        {
            for (int c = 1 ; c <= columns ; c++)
            {
                if (clicked [r] [c] == true)
                {
                    if (game [r] [c] == 9) //if position has a 9 its a mine
                    {
                        m.setColor (Color.red);
                        m.setPosition (grid.getCellX (c), grid.getCellY (r));
                        m.setHeight (70);
                        m.draw (g);
                    }
                    else
                    {
                        g.setColor (Color.black);
                        g.setFont (font1);
                        g.drawString ("" + game [r] [c], grid.getCellX (c), grid.getCellBottomY (r));
                    }
                }
            }
        }


        if (lose == false)
        {
            System.out.println ("win");
        }


        if (win == false)
        {
            System.out.println ("lose");
        }


        if (win == true)
        {
            Font f = new Font ("Arial", 1, 88);
            g.setFont (f);
            g.setColor (Color.blue);
            g.drawString ("You Win", 0, 88);
        }


        if (lose == true)
        {
            Font f = new Font ("Arial", 1, 88);
            g.setFont (f);
            g.setColor (Color.green);
            g.drawString ("You Lose", 0, 88);
        }


        requestFocus ();
    }




    // To fulfill our obligations as a MouseListner, we implement the following...
    public void mouseClicked (MouseEvent e)
    {
        if (lose == false && win == false)
        {
            int mRow = grid.getRow (e.getY ());
            int mCol = grid.getColumn (e.getX ());

            clicked [mRow] [mCol] = true;

            //determine if we lose
            if (game [mRow] [mCol] == 9)
            {
                lose = true;
            }



            //determine if we win
            int count = 0;
            for (int r = 1 ; r <= rows ; r++)
            {
                for (int c = 1 ; c <= columns ; c++)
                {
                    if (clicked [r] [c] == true)
                    {
                        count++;
                    }
                }
            }
            if (count == 9)
            {
                win = true;
            }
        }


        debug ();
        repaint ();
    }


    public void mouseEntered (MouseEvent e)
    {

    }


    public void mousePressed (MouseEvent e)
    {

    }


    public void mouseReleased (MouseEvent e)
    {

    }


    public void mouseExited (MouseEvent e)
    {

    }


    public void debug ()
    {

        System.out.println ("Contents of the game array:");
        for (int r = 1 ; r <= rows ; r++)
        {
            for (int c = 1 ; c <= columns ; c++)
            {
                System.out.print (game [r] [c] + " ");
            }
            System.out.println ();
        }


        System.out.println ();
        System.out.println ("Contents of the clicked array:");
        for (int r = 1 ; r <= rows ; r++)
        {
            for (int c = 1 ; c <= columns ; c++)
            {
                System.out.print (clicked [r] [c] + " ");
            }
            System.out.println ();
        }


        System.out.println ();
    }


    public void initMinesweeper ()
    {
    }

in the initMinesweeper i have to do these things:
// 1. Assign mines to positions in the grid
// 1.0 repeat these steps for desired number of mines
// 1.1 random row & column

// 1.2 if the cell does not already contain a mine
// 1.3 assign mine to the cell in game array


// 2. Count the number of mines adjourning each cell and store that number
// 2.1 for all cells
// 2.2 if the cell does not contain a mine
// 2.3 count the number of mines in adjourning cells
// 2.4 store the count in the cell in the game array

can you help me a little because im stuck and dont know what to do?

Mod edit: Code tags are cool, mkay?
Sponsor
Sponsor
Sponsor
sponsor
Euphoracle




PostPosted: Wed Nov 04, 2009 1:45 pm   Post subject: RE:Making Minesweeper in Ready To Program.

What concept do you need help with in particular and what do you have thus far?

We don't want all the code as we'll help you with single concepts, assuming you've made an attempt already, and not specifics regarding this particular situation.
S_Grimm




PostPosted: Wed Nov 04, 2009 4:47 pm   Post subject: RE:Making Minesweeper in Ready To Program.

1) code tags

2) what specific problems are you encountering?

3) code tags

4) get rid of ready to program and compile with command line. (JDK) or use another IDE like eclipse. but RTP is very outdated.

5) *I dont think i stressed this enough* code tags!!!
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: