Recursion in a MineSweeper game. So far, when a user clicks on a square and it has a bomb on it, I reveal the bomb to the user - see clicked(). I also have the ability to show the user if the squares surrounding them have any adjacent bombs - see countAdjacentSquares().
However, I am having trouble with my fillZeros() recursive method. When a user clicks on a square that has zero bombs in its surrounding squares, it not only reveals that square, but also any adjacent squares (horizontally, vertically or diagonally adjacent) that also have zero bombs in its surrounding squares.
I don't have any recursion, if I click on a square with no bombs all it does it set the square to 0, but doesn't continue to check for other empty squares.
Java Code:
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Square extends GameSquare
{
private boolean revealed = false;
private boolean squareHasBomb = false;
public static final int MINE_PROBABILITY = 10;
public int mineCount = 0;
public class MineSweeper { public static void main(String[] args) { int n = Integer.parseInt(args[0]); //columns int s = Integer.parseInt(args[1]); //rows int p = Integer.parseInt(args[2]); //max number of bombs
Every time the compiler shows me this Array Index Out Of Bounds and shows me in the witch line i have error but i just don't get it how to fixed it and why compiler printing me this mistake because there is enough space in array field[][] because i provided a n+2 X s+2 fields in array and i am using just 1 to n ,including and 1 to s ,including m, so i have not used fields from field[0][0] to field[0][s+1] and field[0][0] to field[n+1][0], field[n+1][0] to field[n+1][s+1] ,field[n+1][s+1] to field[0][s+1] ,so i suppose i can check their values and do not get out of array bounds,but compiler claims otherwise!!!
I'm having trouble filling in the blank squares on a Minesweeper game. What I am trying to is, if a user clicks a square and that square(s) does not have a bomb in it, or adjacent to it, should be set to an empty square.
Here's my recursive method below:
public void revealZeros(){ for(int x = -1; x <= 1; x++) for (int y = -1; y <= 1; y++){ Square zeroSquare = (Square)board.getSquareAt(xLocation+x, yLocation+y); if (!(zeroSquare.SquareHasBomb)){ setImage("images/empty.jpg"); } } revealZeros(); }
I am traversing around the square that the user clicks on with the use of two for-loops. Getting the square at that location and then applying an empty square image. On that square.
The function should then recurse itself and go onto the next square, if the next square does not have a bomb or does not have an adjacent bomb.
The program I'm working on is supposed to read input from a file and using recursion, print the pattern of asterisks for each value until the value is either < 0 or > 25. For example, if the value was 4, the pattern would look like this
* * * * * * * * * * * * * * * *
The values are stored in a file entitled prog3.dat which looks like this
4 3 15 26 0
I've never used recursion before and haven't been able to find anything showing how it would work with this particular type of problem. Here is what I've been able to come up with so far, but I'm having problems still which I will show following the code.
import java.util.Scanner; import java.io.*; public class Program3 { public static void main(String[] args) throws Exception { int num = 0; java.io.File file = new java.io.File("../instr/prog3.dat"); Scanner fin = new Scanner(file);
[Code] ....
Output:
Please enter an integer * * * * * * * * * * Please enter an integer * * * * * * Please enter an integer
As you can see, I don't know how to make it print the pattern like in the example and am honestly not even sure if this is recursion since I've never actually worked with recursion before.
I was asked to create a word pyramid program using recursion. I understand the concept of recursion (i.e. a method calling itself with the problem it is solving getting simpler and simpler each time) but I am having issues writing the program. Here is my code:
public static void main(String[] args) { //This program will create a word pyramid by using a recursive algorithm. //For example, the output of "DOGGY" should be: //DOGGY //OGG //G //This program will also use a recursive algorithm to accomplish this. String userWord = JOptionPane.showInputDialog(null, "Hello and welcome to the Word Pyramid program."
So I have to design a program that takes a word and reverses the order of it. The exact directions are:Write a recursive method void reverse() that reverses a sentence. For example:
[code = Java] Sentence greeting = new Sentence("Hello!"); greeting.reverse(); System.out.println(greeting.getText()); [/code] prints the string "!olleH".
Implement a recursive solution by removing the first character, reversing a sentence consisting of the remaining text, and combining the two. So basically my biggest issue is my method to actually reverse the word.
public class Sentance { private String Word; public Sentance (String aWord) { Word = aWord;
I am trying to sum up the elements of an array. When I test my code the sum is always off by one. For example if I input: 20, 40,30 it gives me 89 instead of 90.
This is what I have so far:
public static void main(String[args]){ int size = kbd.nextInt(); int [] myArray = new int [size] //user inputs the elements of array
I am trying to make a game, for some reason i have begun to get a java.lang.StackOverflowError.
I am not exactly sure how i can fix it. only removing line 14 from infopannel1 (and everything that used that class.) seems to work. im not sure what else i can do to fix it. or why its resulting in stack overflow for that matter.
I am putting in a link for the files i wrote this using bluej (several classes have no relevance, errorv2, demonstration, folderreadertest, ReadWithScanner, saveloadtest, menutest,rannum, and menutestTester. are all irrelivent to my problem.)
How to add the sum of an array with a recursion, but I don't understand how to use recursion. I just understand that it calls back the method. I am nearly done with the code.
import java.util.Scanner; class Question1{ public static void main(String[]args){ Scanner s = new Scanner(System.in); int size, sum; System.out.println("Please input how many numbers will be used"); size=s.nextInt();
public void myFunc(MyNode n, ArrayList<MyNode> path) { boolean hasChildren = false; path.add(n); int index = path.indexOf(n); ArrayList<MyNode> statefulPath = new ArrayList<MyNode>();
[Code] ....
I have similar code that I stepped through in a debugger. After running the code I found that it built the desired tree, in this case a root node H with left child L and right child P. I want list of lists to contain all paths from root to leaf. I expected [H, L] and [H, P]. I discovered that statefulPath is not stateful; after a recursive stack frame pops, statefulPath still contains n! But that stack frame just popped! I expected to see statefulPath be [H] in the debugger and it was [H, L]! So I later have a list [H,L,P] which I don't want. How do I make the statefulPath list I want for my algorithm?
I am new to java, i can develop basic java programs but am not really good at it. How should i pursue and how should i start. I know the game rules.Do i need to learn swings for the same?
i can develop basic java programs but am not really good at it.I have been given this assignment of developing UNO game in java and have been given 10 days. How should i pursue and how should i start.I know the game rules.Do i need to learn swings for the same?
I'm building this custom project in stages, and I'm having difficulty getting the health of the player and the enemy to change. Upon compiling, I'll select weapon 1, then choose action 1 (basic attack), and the health of both the player and the enemy will stay at 15 and 10.There are no errors when compiling.
import java.util.Scanner; public class CustomProject { public static void main(String[]args) { Scanner sc = new Scanner(System.in); it hp = 0; int potionsleft = 0; int choice1 = 0; int weapon = 0; int enemy1hp = 10; //int newenemy1hp = 0;
I'm working on a Java matching game and I'm having a hard time wrapping my mind around it. What I'm having trouble with is how exactly to create an array so that my squares have random values. Then how do I get it to reveal and compare those values by clicking on two of them? Here is what I have so far:
The game has two players: x and o. The players take alternate turns, with player x moving first at the beginning of each game.
Player x starts at position (1,1) while o starts at (8,8).
Each turn, a player can move like a queen in chess (in any of the eight directions) as long as her path does not cross a square already filled in or occupied. After moving, the space vacated by the player is designated as filled and cannot be moved to again. Notice that only the space that was occupied is filled, not the entire path.
The game ends when one player can no longer move, leaving the other player as the winner.
The coordinate (1 1) indicates the top left hand side of the board.
The board is specified as a list of rows. Each row is a list of entries:
- is an empty square * is a filled in square x is the current position of the x player o is the current position of the o player
The board will always be 8 by 8.
Your player function should take in the parameters as described above and return a move as a list in the format (row column) within 1 minute. If you cannot move, return (nil nil). The Tournament Referees will check for this. If you return an illegal move, the other player (and the Tournament Referees) will detect it and you will lose.
I have a question related to the code below, that I do not understand. The aim is to count all files and subdirectories in an ArrayList full of files and subdirectories. So I have to count every file and every subdirectory.
The code concerning counting files is clear to me - every time d is of the type file I have to increment n by one. However I thought that I have to do the same thing in case d is a directory, so I would have written the same code for directories.
So what does "n += ((Directory) d).countAllFiles();" mean? In my understanding the method countAllFiles() is applied again on the object Directory ( as Directory is the class that contains this method), but how is n incremented by this? I thought n should be incremented by one as we did with files.
public int countAllFiles() { int n = 0; for(SystemFile d : content) { if(d instanceof File) { n++;
How the recursion works. I tried to figure out writing down low, mid, high at each recursive call. But I seem to be making a mistake somehow. I don't understand where the values are returned to in
I just started studying recursion and I wanted to know how to create a palindrome number going up from 1 to n then back to 1 like this: "12345...n...54321".
I've done one going downwards and then upwards like this: "n...4321234...n".
Here's my code:
Java Code: import java.util.*; public class PalindromeTest { public static void downPalindrome(int n)
The program I'm working on is supposed to read input from a file and using recursion, print the pattern of asterisks for each value until the value is either < 0 or > 25.For example, if the value was 4, the pattern would look like this
* * * * * * * * * * * * * * * *
The values are stored in a file entitled prog3.dat which looks like this
4 3 15 26 0
I've never used recursion before and haven't been able to find anything showing how it would work with this particular type of problem.Here is what I've been able to come up with so far, but I'm having problems still which I will show following the code.
import java.util.Scanner; import java.io.*; public class Program3 { public static void main(String[] args) throws Exception { int num = 0; java.io.File file = new java.io.File("../instr/prog3.dat"); Scanner fin = new Scanner(file);
[code]...
It appears to be reading the file correctly, but is only printing the top half of the pattern. Also, like I said, I'm not very familiar with recursion, so am not sure if this is actually recursion?
I'm trying to create a tile based map JPanel but all I get is a white screen. I'm fairly new to the Java Swing and AWT package so I've been watching tutorials on YouTube so learn as much as I can.
I've got three classes: Window.java which includes the Main method, Panel.java which is the JPanel and Tile.java to draw all the images into an array.
Window.java:
import javax.swing.JFrame; public class Window extends JFrame { public Window() { setTitle("Project"); setSize(500, 400);
[Code] .....
I've checked through everything and still cannot find what I'm doing wrong. I did try different codes but I just got errors instead.
I have an battleship program that I would like to improve in performance. I know that using certain algorithms it can be improve however I am beginner in Java. I would like any recommendations and or hint in how to improve this game. The are 5 ships per game and the computers plays 10000. I just want to sink all ships in as many shots as possible.
I want to remove the extends Canvas from the top line and when I use JFrame to add canvas, I don't do add.(this, but instead I do add a canvas variable I make. So instead of extending I want to make a variable. But then how would I do the start, stop and run?I want to use Graphics, Canvas (Not Jpanel because I want to use BufferedImage) & JFrame. I don't want to extend Canvas, how could I use Canvas identically to shown above but instead of extending using it as a variable? or, how could I do this?