Finding and Moving a character in a 2D Array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Finding and Moving a character in a 2D Array

  1. #1
    Join Date
    Apr 2013
    Posts
    1

    Finding and Moving a character in a 2D Array

    Hey there guys and gals,

    I had a question on how to use a method to move a character within an array.

    I'm working on a game and the board is a 10x10 array of characters. I essentially need to find the character 'P' that will always be in the middle of the board, and based on a randomly generated roll, move that character either up, down, left, or right. This is going to be looped and is to be done 50 times, as that is how many moves a player gets.

    Below is the code I have so far, and I'm lost. The game itself is called "Drunk Island". As you can see in the move method, I was thinking of reading through the entire array, finding 'P', then somehow trying to move it by adding something to the row or column of the array, but when I tried to make the rows and columns variables, Visual Studio said nope. I even tried to make row and col global variables. I'm at a wall now and have no idea what to do.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace DrunkIsland
    {
    class Program
    {
    static void Main(string[] args)
    {
    int r = 10;
    int c = 10;
    char[,] island = new char[r,c];

    Island Island = new Island(island);
    Island.buildIsland();
    Island.bridgeBuilds();
    Island.person();
    Island.printIsland();
    Console.Read();
    }
    }
    class Island
    {

    public char[,] island;

    public Island(char[,] island)
    {
    this.island = island;
    }
    public void buildIsland()
    {
    //page 415: Staggered for loop
    for (int r = 0; r < island.GetLength(0); r++)
    {
    for (int c = 0; c < island.GetLength(1); c++)
    {
    if (r == 0 || r == island.GetLength(0) - 1)
    {
    island[r, c] = 'w';
    }
    else if (c == 0 || c == island.GetLength(1) - 1)
    {
    island[r, c] = 'w';
    }
    else
    {
    island[r, c] = 'L';
    }
    }
    }
    }

    public void bridgeBuilds()
    {
    Random p = new Random();
    int ran = p.Next(0, 10);
    island[0, ran] = 'b';

    ran = p.Next(0, 10);
    island[island.GetLength(0) - 1, ran] = 'b';

    ran = p.Next(0, 10);
    island[ran, 0] = 'b';

    ran = p.Next(0, 10);
    island[ran, island.GetLength(1) - 1] = 'b';

    }

    public void printIsland()
    {
    for (int r = 0; r < island.GetLength(0); r++)
    {
    for (int c = 0; c < island.GetLength(1); c++)
    {
    Console.Write(island[r, c]);
    if (c == island.GetLength(1) - 1)
    {
    Console.WriteLine();
    }
    }
    }
    }

    public void person()
    {
    island[island.GetLength(0) / 2, island.GetLength(1) / 2] = 'P';

    }

    public void move()
    {

    Random ran = new Random();
    int move = ran.Next(1, 5);

    if (move == 1)
    {
    for (int r = 0; r < island.GetLength(0); r++)
    {
    for (int c = 0; c < island.GetLength(1); c++)
    {

    }
    }
    }
    else if (move == 2)
    {
    Console.Write(2);
    }
    else if (move == 3)
    {
    Console.Write(3);
    }
    else if (move == 4)
    {
    Console.Write(4);
    }
    }
    }
    }

  2. #2
    Join Date
    Apr 2013
    Location
    Cambridge
    Posts
    4

    Re: Finding and Moving a character in a 2D Array

    I think first of all it would be easier if you just stored the character's co-ordinates so that you didn't have to search for the 'P' every time - will save a lot of effort and probably simplify some of the logic later. Then, when you start the game, you initialise these co-ordinates to the starting location and write a 'P' to the array location for that place.

    To move the 'P', you'll have to first write an 'L' to the place in the array where the 'P' is at the moment (to erase the old 'P') and then write the 'P' to the location the player is moving to. So, assuming you've stored the player's horizontal location in the variable 'playerX' and their vertical location in 'playerY', moving to the right would work something like this:

    Code:
    island[playerY, playerX] = 'L';  // erasing old player
    playerX++;
    island[playerY, playerX] = 'P'  // writing new player
    The code for other directions would be similar. Hope this helps

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center