Question

How to replace elements in a 2D array? Okay so for my program, I am trying...

How to replace elements in a 2D array?

Okay so for my program, I am trying to create a fish tank. My program is to generate 4 different FISH: ><))'> in a tank of tilde (~) characters. The tank is a 2D array of 8 rows and 32 columns so it would look like ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ for one of the eight rows (before I generate the random positions of the fish in the rank). Then one row could look like ~~~~~~~~~~><))'>~~~~~~~~~~~~~~~~ after I generate random positions for my fish. It should also hide characters of the fishes body if it is more to the left like such )'>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ where there is only three parts of the fish, 2 body characters and the head character.The random position coordinate I have generated for each of the four fishes starts on the head of the fish (>) which means I have been able to successfully generate four > in the tank, which looks like ~~~~~~~~~~~~~~~~>~~~~~~~~~~~~~~~ when ran. But my problem is how I can get the rest of the fishes body after the head. I know I am going to be working backwards from the > character and replace the tilde characters with characters from the fishes body. I don't need the direct answer, rather some steps that would explain it and some suggestions. It would be a big help. Here is some of the code below.

import java.util.Random;

public class Main {

   public static void main(String[] args) {

       char [][] tilde = new char [8][32];

       fillTank(tilde, '~');

       int fish [][] = generateRandomPositions(4, 8, 32);

       for (int i = 0; i < fish.length; ++i) {

           placeFishInTank(tilde, fish[i][0], fish[i][1]); // METHOD I AM ON...

       }

       renderTank(tilde);

   }

public static void placeFishInTank(char [][] positionInTank, int x, int y) //METHOD TO PLACE THE HEAD AND BODY

   {

       positionInTank[x][y] = '>'; //PLACING THE HEAD

  

   //   for (int i = 0; i < positionInTank.length; i++) {

   //       for (int j = 0; i < positionInTank[x].length; j++)

   //           if (positionInTank[][] >= 0) {

   //               String.charAt(i, j);

   //           }

   //   }

//OR I WAS THINKING OF DOING SOMETHING LIKE

       //if (!positionInTank[row].length - 1 = 0) {

       //add on next character '\''

   //   }

      

       //if (!positionInTank[row] - 2 = 0) {

      //add on next character ')'

   //   }

      

       //if (!positionInTank[row] - 3 = 0) {

       //add on next character ')'

   //   }

      

       //if (!positionInTank[row] - 4 = 0) {

       //add on next character '<'

   //   }

      

       //if (!positionInTank[row] - 5 = 0) {

       //add on next character '>'

   //   }

      

   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

set fish[] = {> ,< ,), ), ', >} // place fish characters in an array

set head = 5 // head as the last index

loop if x > 0 or head > 0 // loop till reached end of the maze i.e. x > 0 or complete fish is drawn i.e. head > 0

start

place fish[head] at position[x, y]

head = head - 1;

x = x - 1;

end

Add a comment
Know the answer?
Add Answer to:
How to replace elements in a 2D array? Okay so for my program, I am trying...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner...

    Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...

  • Hello, I am trying to write this program and have received a "Segmentation Fault" error but...

    Hello, I am trying to write this program and have received a "Segmentation Fault" error but cannot seem to figure out where. I haven't been able to find it and have been looking for quite a while. The goal is to basically create a version of Conway's Game of Life. I am able to generate a table if the input values for rows and columns are equal, but if they are not I receive a segmentation fault and cannot continue...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • I am writing a program to play the game "Game of Life". I am hoping someone...

    I am writing a program to play the game "Game of Life". I am hoping someone can help me edit my sad program so that it will print the first board as well as the next generation. The first board is working just fine. It is the next generation that is not working and i'm not sure what I am doing wrong. Thanks in advance. public class Life {    public char FirstBoard[][];    public Life()    { FirstBoard =...

  • I am failing one of the tests cases for the trimArray function. The test that my...

    I am failing one of the tests cases for the trimArray function. The test that my function is failing is testing that when you get a toTrim array of  " ", it should return "" (an array of no characters). How do I incorporate that functionality into the function and where will it go in the C++ code? Here's my function: // The function trimArray() is given a pointer to a character array that // is NULL terminated called toTrim. This...

  • Hello, I am currently taking a Foundations of Programming course where we are learning the basics of Java. I am struggli...

    Hello, I am currently taking a Foundations of Programming course where we are learning the basics of Java. I am struggling with a part of a question assigned by the professor. He gave us this code to fill in: import java.util.Arrays; import java.util.Random; public class RobotGo {     public static Random r = new Random(58);     public static void main(String[] args) {         char[][] currentBoard = createRandomObstacle(createBoard(10, 20), 100);         displayBoard(startNavigation(currentBoard))     }     public static int[] getRandomCoordinate(int maxY, int maxX) {         int[] coor = new...

  • I am trying to understand how to manipulate the Gregorian Calendar so that I can convert...

    I am trying to understand how to manipulate the Gregorian Calendar so that I can convert my answer into a string format. What I'm hoping to do is to do is return string to a different file to add to a string in separate file (Object-oriented exercise between files), but once I can convert this into a string I can convert this file on my own. I added the System.out.println() to show what the results should look like. In other...

  • I am trying to add a string command to my code. what am i doing wrong?...

    I am trying to add a string command to my code. what am i doing wrong? #define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <stdio.h> #include <string.h> #include <math.h> int main(void) {    int i, j;    int rowA, colA, rowB, colB;    int A[10][10], B[10][10];    int sum[10][10];    char str1[10];    printf("This is a matrix calculator\n");    //read in size from user MATRIX A    printf("Enter in matrix A....\n");    printf("\t#row = ");    scanf("%d", &rowA);    printf("\t#col = ");   ...

  • I need help modifying this program. How would I make sure that the methods is being...

    I need help modifying this program. How would I make sure that the methods is being called and checked in my main method? Here is what the program needs to run as: GDVEGTA GVCEKST The LCS has length 4 The LCS is GVET This is the error that I'm getting: The LCS has length 4 // I got this right The LCS is   //the backtrace is not being called for some reason c++ code: the cpp class: /** * calculate...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT