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


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

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 View Related

Tic Tac Toe Game - Random Number Generator And Two Dimensional Arrays For Game Board

May 9, 2015

Im trying to make a tic tac toe game that you play against the computer using a random number generator and two dimensional arrays for the game board. Im not trying to make a GUI, the assignment is to have the board in the console, which I have done. I have run into a few problems with trying to get the computer player to correctly generate 2 integers and have those two integers be a place on the game board. Here is my code so far.

import java.util.Random;
import java.util.Scanner;
 public class TicTacToe {
 private static Scanner keyboard = new Scanner(System.in);
private static char[][] board = new char[3][3];
public static int row, col;
 
[Code] ....

View Replies View Related

Modern Gems - Two Dimensional Tile-management Game

Feb 20, 2014

package modern.gems;
public class Board {
// will need to add params where required ...
private Gem[][] gems;
private int[][] array;
private int rows;
private int cols;

[Code] ....

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

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

Release 1 Dimensional Array From 2 Dimensional Array

Mar 13, 2014

I have a 2x2 matrix and a 2 dimensional array.Let's say, my matrix is [a b] and array is [[1, 2], [3, 4], [5, 6],[23, 11]] .I need to multiply each 1 dimensional array in above array with the matrix.For instance,

[1, 2] multiply with [a b]
[3, 4] multiply with [a b]
[5, 6] multiply with [a b]
[23, 11] multiply with [a b]

So, each 1 dimensional array in there will be multiplied with matrix [a b] (same with matrix multiplication).how to do that multiplication in java. But I confuse how to 'release' each 1 dimensional array from the first array so I can do multiplication.How to do that in java?

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

Finding Sum Of Two Dimensional Array

Apr 1, 2014

How to declare a 2 dimensional array of interger of size 10 by 10. and then set every element of the array to 3. After this I need to add up all the element in this array and return the value.

final int ROWS = 10;
final int COLS = 10;
int[][] foo = new int[ROWS][COLS];
for (int i = 0; i < ROWS; i++){
for(int j = 0; i< COLS; i++){
foo[i][j] = 3;
}
}

This is what i have done so far(not 100% if is correct..) but how to add up all the array.

View Replies View Related

Stuck On Two Dimensional Arrays

Apr 13, 2014

I am having trouble grasping 2 dimensional arrays .I have a certain problem:

String[][] words = {
{"Red", "Blue", "Green"},
{"Orange", "Purple". "Yellow"}

[code]....

I ran the code and got OrangePurpleYellowRedBlueGreen.

View Replies View Related

Two Dimensional Array Assignment

Jun 14, 2014

I am working on the following java assignment..Write a program that randomly fills in 0s and 1s into a 4- by- 4 matrix, prints the matrix, and finds the first row and column with the most 1s. Here is a sample run of the program:

0011
0011
1101
1010

The largest row index: 2
The largest column index: 2

I have code that generates random 0s and 1s for the array, how to get the largest column and row.

import java.util.Random;
public class LargestRowColumn {
public static void main(String[] args){
//create 4x4 array matrix
int arrayMatrix[][] = new int[4][4];

[code]....

finding the row and column with the largest amount of 1s. I keep thinking well if I scan and find a one in the array, maybe I can just save the index of the row and column and then determine which index contains the most 1's after the array has been scanned.

View Replies View Related

Two Dimensional Array Of Integers

Nov 7, 2014

I am workinh with a couple of functions that deal with two dimensional (square) arrays of integers, doing things like checking equality, etc. For example, I know that I get an ArrayOutOfBoundsException in the isEqual function, but I don't know why.

public class MatrixUtils {
//This function checks if two matrices are equal
public static boolean isEqual(int A[][], int B[][]) {
for(int i=0; i<A.length; i++) {
for(int j=0; j<A.length; i++) {
if (A[i][j] != B[i][j]) return false;

[Code] ....

View Replies View Related

Comparing In A Two Dimensional Array?

May 3, 2014

I have to make a program in which users inputs a number and the program should search into a two dimensional array and print out all the values that are below the number This is my first time experimenting with 2D Arrays and how to do this program I have the array set up

String firstarray[][]=
{{"", "Store101", "Store102", "Store103", "Store104"},
{"Tennis Shoes", "102", "54", "20", "78"},
{"Sweaters", "45", "25", "35", "75"},
{"Jeans", "12", "35", "45", "65"},
{"Shorts", "54", "25", "34", "45"},
{"Jackets", "15", "35", "50", "25"}
};

View Replies View Related

Possible To Have Two Dimensional Array That Has A String And Int

Apr 8, 2014

Is it possible to have a two dimensional array that has a string and an int, so like combining these into a two dimensional instead of one:

String [] w = {"help", "me"}; and int [] n = {4,2};

obviously not what I'm really working on, but u should get the point.

View Replies View Related

Comparing Two Dimensional String Array

Mar 7, 2014

I want to compere two element of string array by each other! eventually I want to print Yes or No in matrix . SO, I start reading data from file then split them into two parts .

File file= new File(fileName);
try {
inputStream = new Scanner(file);
while (inputStream.hasNext()){
String data= inputStream.next();
String [] token =data.split(",");
System.out.println("day"+token[0] +"embloyee name:"+ token[1]) ;
}
inputStream.close();

Now I want to compere each cell from token[0] by another array :

String[] day= { "Sunday", "Monday" ................};

if the days are equal then I want print yes in front of the employee name if not then i want to print No..is this gone work with me as I imagine it to be or do I have to take few more steps to get my code going?

View Replies View Related

Declare Two Dimensional Array In Java?

Oct 28, 2014

I want to declare a 2 dimensional array in java which has 3 column and unlimited number of rows, i want to give a special name to each column . The type of first column is string second one is int and the last one is string

Column1_name
Column2_name
Column3_name
String value
Int value
String value
.
.
.
.
.
.

View Replies View Related

Students Grading - Two Dimensional Arrays

Jun 18, 2014

I have the program bellow that grades students(based on a two dimensional array with the answers) and i need to make it display the students based on the grades/scores in ascending order . I did that in two ways (using a array and a two dimensional array) but i have a hunch it can be done much more simple then i did it (but still using array object and nothing else ) .

public class GradeExam {
/** Main method */
public static void main(String args[]) {
// Students' answers to the questions
char[][] answers = {

[code]....

My first solution creating an array with the grades for sorting :

public class C8_3 {
public static void main(String args[]) {
// Students' answers to the questions
char[][] answers = {
{'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
{'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'},

[code]....

View Replies View Related

Airline Two Dimensional Boolean Array

Feb 23, 2015

I have a boolean array that looks like this

boolean seat[][] = new boolean[2][3];

I reserved one row for first class and another for economy class. This code works just as I want but my question is how can I loop through it so I don't need all the if statements? I tried it many ways. I tried something like this but I cant get it to work.

for (int row=0;row<seat.length;row++{
for (int col=0;col<seat[row].length;col++){
if (seat[0][col]==false){
seat[0][col]=true;
System.out.println("You have number 0" + row + in first class);

[Code] ......

View Replies View Related

Return Location And Value To Two-dimensional Array

Jan 14, 2015

I need to design a class named Location for locating a maximal value and its location in a two-dimensional array. The class should contain public data fields row, column, and maxValue that store the maximal value and its indices in a two dimensional array with row and column as int type and maxValue as double type.

I need to write the following method that returns the location of the largest element in a two-dimensional array:
public static location locateLargest(double[][] a)

The return value is an instance of Location. Write a test program that prompts the user to enter a two-dimensional array and displays the location of the largest element in the array.

Here is what i get when I run

Enter the number of rows and columns of the array:
3 4
Enter the array: 23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
The location of the largest element is at 00
The location of the largest element is at 01

But I need it to display this instead.

Enter the number of rows and columns of the array:
3 4
Enter the array: 23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
The location of the largest element is 45 at (1,2)

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Number of rows and columns: ");
int row = input.nextInt();
int col = input.nextInt();

[Code] ....

Enter the number of rows and columns of the array:
3 4
Enter the array: 23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
The location of the largest element is at 00
The location of the largest element is at 01

I need it to display this instead.

Enter the number of rows and columns of the array:
3 4
Enter the array: 23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
The location of the largest element is 45 at (1,2)

View Replies View Related

2D-dimensional Arrays Of Object Types

Dec 8, 2014

I need to write a class that represents and image of RGB pixels. This class uses a two-dimensional array of object type that represents R,G and B values.

When I compile the class it completes successfully. However, when I try running a "tester" class , I'm encountering errors in a few methods. I've been trying it out for 2 days now, but unsuccessfully thus far..Those method are : rotating the image by 90 degrees clockwise (and vice versa , but I just use clockwise method 3 times for that) , shifting the image sideways, and shifting the image up/down..

I've attached the whole source code for the class , and the list of errors I get when I try to run my tester. URL...

View Replies View Related

Using Two Dimensional Arrays To Make Calculations

May 10, 2014

I am having problems figuring out how to make calculations using a two dimensional array. The problem is finding the distance of a projectile given the launch velocity and angle. The equation is x = v^2*sin(2theta)/g. I understand how to implement this equation using the toRadians() and Sin() methods. What I don't understand is how to calculate the distance values with a multi dimensional array.

double [][] data = { {20, 15},
{50, 35},
{80, 45},
{100, 65},
{150, 85},
{10, 50},
{110, 8}};

My array has the velocities on the left and angles on the right. I tried using nested loops but I don't know how to access the two pieces of data at a time which I need.

View Replies View Related

2 Dimensional Array Assignment With A Loop

May 5, 2015

I have a 2 dimensional array assignment with a loop, i'm supposed find the average score of each student from a grade record and find the average score for each test. I've been trying to do the assignment all day with no progress. This is what i have so far

public class SiuTest {
public static void main(String[] args) {
String [] stu= new String [5];
int [] [] grade= new int [4][2];
String hold;

[code]....

View Replies View Related

How To Create Coordinates In Two-dimensional Grid

Mar 16, 2015

how to create coordinates in a two-dimensional grid, in the end it shall look like A5, F6 and so on. The aim is to place 3 DotComs in this grid by coincidence. To do this one uses two arrays. One array represents one DotCom, the other represents the grid's size, in this case 49 (7x7).The code ends like this:

ArrayList<String> alphaCells = new ArrayList<String>();
...
int x = 0;
int row = 0;
int column = 0;
while (x<comSize) {
grid[coords[x]] = 1; // mark master grid points as used
row = (int)(coords[x] / gridLength);
column = coords[x] % gridLength;
temp = String.valueOf(alphabet.charAt(column));
alphaCells.add(temp.concat(Integer.toString(row));
x++;
}
return alphaCells;
}

why row and column are calculated like this. Furthermore I don't understand why the column is used to generate a character, because columns are marked with numbers and rows are marked with characters.

View Replies View Related

Incompatible Types (Parsing Two-Dimensional Array)

Mar 4, 2015

What the program must do is to parse the string-declared two-dimensional array for the program to compute the student's scores in their quizzes, but I'm having difficulty in anything with Parsing so I don't know what will be the appropriate codes.

Error: incompatible types: String[][] cannot be converted to int[][]

import java.io.*;
public class Quiz3{
public static void main(String[]args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String [][]StudQuiz = new String [5][4];

[Code] ....

How am I able to compute the average of the score if the data that is needed to be computed is in the string-declared array?

View Replies View Related

Two Dimensional Array - Converting Integers To String

Apr 10, 2015

I am pretty new to Java and am just learning about two dimensional arrays. I think that I understand the concept, but I seem to be having trouble adding stuff to my array. I wanted to make an array to hold both strings and integers, but wasn't sure if I could put integers in a string array. So I thought that I would be able to convert my integers to string and then add them. This however causes an error. This is my code(yes its probably not the best):

static String [][] students = new String [14][4];
static int number = 0;
String fName, lName, fullName;
int test1, test2, test3, test4;
String a, b, c, d;

[Code] .....

View Replies View Related







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