Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7437115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:20:55+00:00 2026-05-29T10:20:55+00:00

I am creating a game using a 10×10 2D array. The player starts at

  • 0

I am creating a game using a 10×10 2D array. The player starts at the top left hand corner indicated as “P” and the objective is to get the player to avoid obstacles to get to the treasure indicated as “T” located in the lower right corner.

How would I go about making the player move about the grid using commands Up/Down/Left/Right?

Would I use a for loop to count through the elements in the array to designate the move?

Here is what I have so far:

import java.util.Scanner;
import java.util.Random;

public class Adventure {

public static void main(String[] args) {
    char grid[][]= new char[10][10];
    Scanner move = new Scanner(System.in);
    System.out.println("Here is the current game board:");
    System.out.println("-------------------------------");

    for(int i=0; i<grid.length; i++) {          
        for(int j=0; j<grid.length; j++) {
            double random = Math.random();
            if(random <=.05) {
                grid[i][j]='*';
            }
            else if(random > .06 && random <= .15) {
                grid[i][j]='X';
            }
            else {
                grid[i][j]='.';
            }
            grid[0][0]='P';
            grid[9][9]='T';
            System.out.print(grid[i][j]);
        }
        System.out.println("");         
    }           
        System.out.print("Enter your move (U/D/L/R)>");
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:20:56+00:00Added an answer on May 29, 2026 at 10:20 am

    you should keep track of the current position of the player and just update those variables.
    initial values would be (0,0) as you said.

    int px = 0;
    int py = 0;
    

    when a move is made, update the variables accordingly:

    grid[px][py] = <empty cell>;
    switch (move) {
      case 'u': py += 1; break;
      case 'r': px += 1; break;
      ...
    }
    grid[px][py] = 'P';
    

    of course you shouldn’t just updated the values “blindly”, you should insert some validation logic to follow the rules of the game:

    if (grid[px][py] != <obstacle> )
       // update player coordinates...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a graphical roguelike game using Java. In the game, I'm painting a
I'm creating a game using c# and I would like an online highscore board.
i'm looking at creating a web game using html5 canvas and javascript. i was
I'm creating a game for the iPad using Phonegap, which means I'm using JavaScript/
I'm creating a game engine using wxWidgets and OpenGL. I'm trying to set up
I am creating a game web site using PHP and I want to just
I am currently working on creating an import-based pipeline for my indie game using
I am creating my first game ever using pygame and I've found that in
I'm creating a game in Flash CS4 and using Actionscript 3.0. I'm adding all
I'm creating a simple 3D game on Windows 7 in C++ using the free

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.