How To Improve Battleship Game Java

Dec 3, 2014

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.

Main class

package solution;
import battleship.BattleShip;
import java.awt.Point;
import java.util.List;

[Code] .....

View Replies


ADVERTISEMENT

Error In Battleship Game

Jun 8, 2014

I'm trying to make a simple battleship game, but when I make a ship with the addShip method (it is suppoused to initialize the board with the numbers 1,2,3 or 4 depending on the ship type, at the given coordinates) it dosen't initialize the field with any numbers, although the method returns true. The place the ship is suppoused to be made stays initialized with ".",

package battleship;

public class Sea {
private int width;
private int height;
private String[][] field;

[code].....

View Replies View Related

One Dimensional Battleship Game

Oct 24, 2014

I'm working on a project and I can't seem to get it working. It is a battleship game, in a one dimensional array. This is as much as I got. I can't get the 'river' to display when I run the program. How I might get it to work?

import java.util.*;
import java.util.Random;
public class Battleship
{
static Scanner input = new Scanner(System.in);
public static int promptForInt(String prompt)

[Code] ....

View Replies View Related

How To Improve File Reading Efficiency And Its Data Insertion In Java

Feb 8, 2014

We have an autosys job running in our production on daily basis. It calls a shell script which in turn calls a java servlet. This servlet reads these files and inserts the data into two different tables and then does some processing. Java version is 1.6 & application server is WAS7 and database is oracel-11g.

We get several issues with this process like it takes time, goes out of memory etc etc. Below are the details of the way we have coded this process.

1. When we read the file using BufferedReader, do we really get a lot of strings created in the memory as returned by readLine() method of BufferedReader? These files contain 4-5Lacs of line. All the records are separated by newline character. Is there a better way to read files in java to achieve efficiency? I couldnt find any provided the fact that all the record lines in the file are of variable length.

2. When we insert the data then we are doing a batch process with statement/prepared statement. We are making one batch containing all the records of the file. Does it really matter to break the batch size to have better performance?

3. If the tables has no indexes defined nor any other constraints and all the columns are VARCHAR type, then which operation will be faster:- inserting a new row or updating an existing row based upon some matching condition?

View Replies View Related

Android App Battleship Board

Nov 11, 2014

I want to make the board for the game, I want it to be a two-dimensional array of buttons.I know how to make a array but is there a class of buttons that I need to use? and how do I connect it to the xml?I am using eclipse.

View Replies View Related

How Does Encapsulation Improve Maintainability

Jan 29, 2014

I understand making things private and using getters and setters to prevent corrupt data/valdiation. I am having trouble picturing a scenario where I can change my code without breaking others. What can I change without breaking existing code? One example I can think of is I can change an instance variable name since the getter for it will return it regardless. I am looking for a more vivid example...

View Replies View Related

Design And Implement GUI Front End For Computerized Version Of Battleship

Feb 21, 2014

Code for implementing a simple computerised version of Battleships has been supplied. Examine the code ensuring that you understand it. The game is played against the computer.

A number of ships (default is 6) are place randomly on a 4 x 4 grid. A ship occupies one square only. The player then guesses the position of a ship. If he/she guesses correctly the ship is HIT and sinks. If the guess is incorrect the player loses a life. The default number of lives is 7. If the location has been guessed already, no lives are lost.

When the game starts, a 4 x 4 grid should be displayed.

The user should be allowed to enter/select a cell location. If the selected cell contains a ship, the ship will be destroyed. If not the player loses a life. The cell should display an appropriate picture, icon or message informing the user of its current state.

The number of lives and number of ships remaining should be displayed.

When the game ends an appropriate message should be shown informing the player whether he/she has won or lost. The positions of the ships should also be shown.

The purpose of this assignment is that students demonstrate an understanding of the Java Swing library and that he/she can navigate and use the API effectively.

Your finished GUI should be both user-friendly and aesthetically pleasing.

Ideally, the user should have the option to play another game, to quit or give up mid-game. You may also wish to include other options such as different levels of difficulty, etc.

View Replies View Related

How To Improve JSP Page Load Performance

Feb 18, 2015

How to improve jsp page load performance and i have use sessions in jsp page.i tried using with include tag rather than using jsp:include. also

View Replies View Related

JavaFX 2.0 :: How To Improve Application Startup

Aug 26, 2014

I have a UI that uses fx:include to include a handful of nodes in a StackPane.  So far I have less than 10 panes and I can already notice a delay of ~3 seconds (on an older machine) when the initial scene is built.  It's especially noticeable because I'm using a pre-loader with a progress bar.  The progress bar runs smoothly until the pre-loader calls start() on my application.  After that, the scene is built on the application thread, so the progress bar doesn't get any more updates.  It looks like the progress bar freezes until the main scene is built and shown.
 
I was hoping I could build the main scene on the JavaFX launcher thread, but that doesn't work.  I tried it and, not surprisingly, get an exception for not being on the application thread.  What are the options, if any, for making an application's start up feel a bit smoother?

View Replies View Related

Improve Code To Output A Word With Most Frequent Letter?

Jan 31, 2015

The code is supposed to output a word from a sentence(from a sample file) with most frequently occurring letter in that word.

Example input: "Therefore, it is this word."

output: therefore

It currently uses 3 loops nested loops, is there a way to improve this?

public static String findWord(String file) throws IOException {
BufferedReader in = new BufferedReader(new FileReader(file));
String text, correctWord = null;
text = in.readLine();
StringTokenizer tokens = new StringTokenizer(text.toLowerCase(), " ,.?"); //split string into tokens of words only
int count = 0, numLetter = 0;

[code]....

View Replies View Related

Developing UNO Game In Java

May 11, 2014

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?

View Replies View Related

Developing UNO Game In Java?

May 11, 2014

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?

View Replies View Related

Java Game Creation

Jan 26, 2015

I am creating a simple tiled minecraft like game in java, but i dont know how to make the game loop.Here is the source:

package net.pltformgame.main;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class main extends JFrame implements KeyListener {

[code]...

I really dont know how i make the jump or the game loop.

View Replies View Related

Java Game Health

Jan 20, 2015

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;

[code]....

View Replies View Related

Java Matching Game

Apr 14, 2015

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:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

[code]....

View Replies View Related

Coding AI Game Through Java

Apr 10, 2015

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.

Additionally if your time expires you will lose.

View Replies View Related

Tiled Based Map Game In Java

May 10, 2014

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.

View Replies View Related

Make A Connect 4 Game With Java

Nov 17, 2014

I have to make the connect 4 game be connect 3. Ive edited a code but I the math is over my head. These loop methods check the ways someone can win.

for (int j=0;j<7;j+=2)//need to change
{
if ((f[i][j+1] != " ")
&& (f[i][j+3] != " ")
&& (f[i][j+5] != " ")
&& (f[i][j+7] != " ")
&& ((f[i][j+1] == f[i][j+3])
&& (f[i][j+3] == f[i][j+5])
&& (f[i][j+5] == f[i][j+7])))
//end of loop

[code]....

View Replies View Related

Java Game Export Error

May 8, 2014

So I am creating a game in Java, the game works fine when I run it in Eclipse but when I export it it does not draw anything on the screen.

I added a try and catch for nullpointerexceptions but the game does not throw any nullpointerexceptions.

What is the problem?

The game seems to be finding all the images & resources since it does not throw any NullPointerExceptions...

View Replies View Related

Java 2D Game - Use Canvas Without Extending It

Apr 10, 2014

Java Code:

public class Game extends Canvas implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;

public Game() {
setMinimumSize(new Dimension(width, height));
setMaximumSize(new Dimension(width, height));
setPreferredSize(new Dimension(width, height));
}

[code]...

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?

View Replies View Related

Java Memory Matching Game

Feb 13, 2015

"A common memory matching game played by young children is to start with a deck of cards that contain identical pairs. For example, given six cards in the deck, two might be labeled

1, two might be labeled
2 and two might be labeled
3. The cards are shuffled and placed face down on the table.

The player then selects two cards that are face down, turns them face up, and if they match they are left face up. If the two cards do not match they are returned to their original position face down. The game continues in this fashion until all cards are face up.Write a program that plays the memory matching game. Use sixteen cards that are laid out in a 4x4 square and are labeled with pairs of numbers from 1 to 8. Your program should allow the player to specify the cards that she would like to select through a coordinate system.For example in the following layout:

1 2 3 4
--------------------
1 | 8 * * *
2 | * * * *
3 | * 8 * *
4 | * * * *

All of the cards are face down except for the pair 8 which has been located at coordinates (1,1) and (2,3). To hide the cards that have been temporarily placed face up, output a large number of newlines to force the old board off the screen.Use 2D array for the arrangement of cards and another 2D array that indicates if a card is face up or face down.Or, a more elegant solution is to create a single 2D array where each element is an object that stores both the cards value and face.Write a function that shuffles the cards in the array by repeatedly selecting two cards at random and swapping them.

Note:To generate a random number x, where 0<= x <1, use x=Math.random();.For example, multiplying y six and converting to an integer results in an integer that is from 0 to 5."I have been thinking about the algorithm and design of the question for a few hours.

View Replies View Related

CheckerBoard Won't Appear - Creating A Game In Java

Mar 8, 2015

Anyway I am creating a game for my A2 coursework, most commonly known as Checkers. I have completed my code and everything works as I had planned except that the CheckerBoard itself as well as the checkerpieces do not appear to be showing.

The section of were my board should be present is just a black space. Although my board does not appear to be displaying, all of the actions I perform on it such as clicking certain section produces the planned response, and although I've checked through my code I cannot work out what I've done wrong.

CheckerBoard content = new CheckerBoard(); // Sets the CheckerBoard values into the content to be used in the next line
application.setContentPane(content); // Container holds the values together, Content pane of the CheckerBoard
application.pack(); // Use preferred size of content to set size of application.
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
application.setLocation( (screensize.width - application.getWidth())/2,
(screensize.height - application.getHeight())/2 );

[Code] ....

I have noticed is that within the BoardComplete(), if you change the setBackground(Color.BLACK) to WHITE, it will change the colour of the section to white, but the checkboard is still unshown.

View Replies View Related

Implementing A Card Game In JAVA

Apr 19, 2015

I am facing with a JAVA code needed for my assignment.We are implementing a Card Game.. There is a Card Class which i have not included in the code below .We have a Dec class that has various arrays..

privateCard[] masterPack >> Is a master Pack that is initialized once and has all the 52 cards.

private Card[] cards >> Is the array that has the card values from masterPack array.

My problem is :

After I called the Deck object D1, it goes to the Deck COnstrucutor OK.It then populates the values of masterPack as per the allowmasterPack method.Then it stores the values of masterPack into cards array in the init method..I get an exception when line below is executed..

cards[pack * 52 + k]= masterPack[k];

I get a java.lang.NullPointerException error..

public class debug
{
public static void main(String[] args)
{
int pack = 1;
Dec D1;
D1 = new Dec(pack);

[oode]...

View Replies View Related

Creating A Guessing Game In Java

Nov 30, 2014

I am creating a Guessing game program in java code. I am having an issue with the guess class and main/tester class running. The instructions for the game are The computer generates a random # and the user must guess that number in 7 or fewer guesses. If the guesses exceed 7, then the game is over and the user is asked if they want to 'play again?'These are things I need to incorporate into my code:

If Statement

A Loop of some kind
At least three imported methods
At least two methods you create
Obtains input from the user
At least two instance variables
At least two local variables
Some form of concatenation
At least two calculations

import java.util.Random;
public class Guess
{
int computersNumber; // A random number picked by the computer.
int usersGuess = 0; // A number entered by user as a guess.
int guessCount = 0; // Number of guesses the user has made.

[code]...

View Replies View Related

Java Recursion In Minesweeper Game

Nov 16, 2014

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;

[Code] ....

View Replies View Related

Opoly Game - Goal Of The Game Is To Reach Or Exceed Reward Value 1000

Mar 12, 2015

Opoly works this way: The board is a circular track of variable length (the user determines the length when the game app runs). There is only one player, who begins the game at position 0.

Thus, if the board length is 20, then the board locations start at position 0 and end at position 19. The player starts with a reward of 100, and the goal of the game is to reach or exceed reward value 1000. When this reward value is reached or exceeded, the game is over. When the game ends, your program should report the number of turns the player has taken, and the final reward amount attained.

In Opoly the game piece advances via a spinner - a device that takes on one of the values 1, 2, 3, 4, 5 at random, with each of the five spin values equally likely.

Although the board is circular, you should draw the state of the board as a single "line", using an 'o' to represent the current player position, and * represent all other positions. Thus if the board size is 10, then this board drawing:

**o******

means that the player is at location 2 on the board.

Here are the other Opoly game rules:

If your board piece lands on a board cell that is evenly divisible by 7, your reward doubles.

If you land on the final board cell, you must go back 3 spaces. Thus if the board size is 20, the last position is position 19, and if you land there, you should go back to position 16. (If the position of the last cell is evenly divisible by 7, no extra points are added, but if the new piece location, 3 places back, IS evenly divisible by 7, then extra points ARE added).

If you make it all the way around the board, you get 100 points. Note that if you land exactly on location 0, you first receive 100 extra points (for making it all the around), and then your score is doubled, since 0 is evenly divisible by 7,

Every tenth move (that is, every tenth spin of the spinner, move numbers 10,20,30,... etc.), reduces the reward by 50 points. This penalty is applied up front, as soon as the 10th or 20th or 30th move is made, even if other actions at that instant also apply. Notice that with this rule it's possible for the reward amount to become negative.

Here is the driver class for the game:

import java.util.*;
public class OpolyDriver{
public static void main(String[] args){
System.out.println("Enter an int > 3 - the size of the board");
Scanner s = new Scanner(System.in);
int boardSize = s.nextInt();

[Code] ....

heres the methods:

REQUIRED CODE STRUCTURE: Your Opoly class must include the following methods (in addition to the Opoly constructor) and must implement the method calls as specified:

playGame - The top-level method that controls the game. No return value, no parameters. Must call drawBoard, displayReport, spinAndMove, isGameOver.

spinAndMove - spins the spinner and then advances the piece according to the rules of the game. No return value, no parameters. Must call spin and move.

spin - generates an integer value from 1 to 5 at random- all equally likely. Returns an integer, no parameters.

move - advances the piece according to the rules of the game. No return value, takes an integer parameter that is a value from 1 to 5.

isGameOver - checks if game termination condition has been met. Returns true if game is over, false otherwise. No parameters.

drawBoard - draws the board using *'s and an o to mark the current board position. Following each board display you should also report the current reward. No return value, no parameters.

displayReport - reports the end of the game, and gives the number of rounds of play, and the final reward. No return value, no parameters.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved