Public Group

# Two-dimensional array, finding a unknown index

This topic is 1512 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi community, this is my first ever post, I read the guidelines so I searched as much as I could in the forums for this topic and couldn't find a post similar. So I am currently programming this island maze using two-dimensional arrays in C#. The purpose of the program is to ask the user for the size of the island (x rows, y columns) and then have a drunk guy placed near the center of the maze. Using Random method, 1-4, if 1 then the drunk guy moves up if 2 he moves left and etc... Using (+'s) char to represent the land, and (W's) to represent the water, and (B's) to represent the random placement of bridges where he will survive if he makes it to them. So far I have everything working properly except the placement of the drunk islander.

I've been using x/2, y/2 to have the (D) or drunkin guy placed in the center of the matrices or island. When I try to have my MoveDrunk method move drunk[x, y] up, left, down, right, I have no variable to associate with that random placement of the drunk guy coordinate or index. All I have found was the IndexOf() and BinarySearch() System.Array methods. But those are only one dimensional methods. I then found out there's no way to evaluate a two dimensional index? Supposedly I must now create my own algorithm to calculate that drunk guys location so I could move him (some variable)+1 right (some variable)-1 left etc...

Any ideas on how I could create this for my game or even another approach I could take?

Here's what's in my main method so you get a better idea.

Thanks,

Dustin

Console.WriteLine("All you need to know... \n1 moves drunk up, 2 moves drunk right, 3 moves drunk down, 4 moves drunk left");

Console.WriteLine("Please enter the amount of rows you want for your island, \nmust be greater than 3.");

int rows = int.Parse(input);

Console.WriteLine("Please enter the amount of columns you want for your island, \nmust be greater than 3.");

int columns = int.Parse(input);

int exaustion = 0;

//Console.WriteLine("\nPlease enter the number of bridges you prefer.");

//int bridgeCount = int.Parse(bridgeInput);

char[,] island = Dimensions(rows, columns);

Water(island, rows, columns);

Bridge(island, rows, columns);

//, bridgeCount

Land(island, rows, columns);

//InitDrunk(island, rows, columns); //Not using currently

//PlaceDrunk(island, rows, columns); //Not using currently

island[rows / 2, columns / 2] = 'D'; //Heres where I get my center placement of my drunk before passing into MoveDrunk()

MoveDrunk(island, rows, columns, exhaustion); //<-- Heres where I got to find a way to get that index

Island(island, rows, columns);

##### Share on other sites
Use another variable, int DrunkGuyX and int DrunkGuyY.  Initialize them to a random value between 0 and column-1 (or rows-1).  Then pass those variables to the MoveDrunk() function.  You'd then modify DrunkGuyX and DrunkGuyY as he moves around the island.

Good luck and have fun.

Oh, BTW, you should use the
[code]source code here [ /code ] (no spaces on close)
when entering code. It makes it look much better Edited by BeerNutts

##### Share on other sites

You are over complicating the issue ...

Create a "Drunk_Guy" class, and inside of the class you could include variables that keep track  of were he is ...

// This is written in Java - should be easy to translate to C#
public class Drunk_Guy {
int Loc_x;
int Loc_y;

public Drunk_Guy(int x, int y){ // when creating the Drunk Guy object, set location
Loc_x = x;
Loc_y = y;
}

public void SetDrunkLocation(int x, int y){ // use negative numbers for negative movement
Loc_x += x;
Loc_y += y;
}

public int GetDrunkX(){ // find out the Drunk Guy's X location
return Loc_x;
}

public int GetDrunkY(){ // find out the Drunk Guy's Y location
return Loc_y;
}

// TODO: Create more class methods related to the Drunk Guy
}

Edited by Shippou

##### Share on other sites
Okay thanks for the help! That was what I needed to know for me to get through this. I did feel like I was overhinking it a tad but a few restless nights can do that to you. I appreciate how fast I got responses as well.

##### Share on other sites

Okay, well I thought that's what I needed to get this to work but sadly my drunk guy keeps making two turns, and my bridges on the right wont appear, and my end game method is dysfunctional? I tried the approach for adding another class but with the current state of my code I feel like its to difficult to rearrange everything. I did mange to make some progress but once I added my lives, drowns, and starves my code became inoperable. Here's what I have for anyone willing to give more suggestions.

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

namespace IslandDrunk
{
class Program
{
static void Main(string[] args)
{
int lives = 0, drowns = 0, starves = 0;
Console.WriteLine("All you need to know... \n1 moves drunk up, 2 moves drunk right, 3 moves drunk down, 4 moves drunk left");
Play(lives, drowns, starves);
}
public static void Play(int lives, int drowns, int starves)
{
int exhaustion = 0;
Console.WriteLine("Please enter the amount of rows you want for your island, \nmust be greater than 3.");
int rows = int.Parse(input);

Console.WriteLine("Please enter the amount of columns you want for your island, \nmust be greater than 3.");
int columns = int.Parse(input);

Console.WriteLine("\nPlease enter the number of bridges you prefer.");
int bridgeCount = int.Parse(bridgeInput);

Console.WriteLine("You are given " + (rows * columns) / 3 + " lives based on the size of your island. \n");

Random randyX = new Random();
Random randyY = new Random();
int drunkGuyX, drunkGuyY;

drunkGuyX = randyX.Next(1, rows - 1);
drunkGuyY = randyY.Next(1, columns - 1);

char[,] island = Dimensions(rows, columns);

Water(island, rows, columns);
Bridge(island, rows, columns, bridgeCount);
Land(island, rows, columns);
island[rows / 2, columns / 2] = 'D';
MoveDrunk(island, rows, columns, lives, drowns, starves, exhaustion, drunkGuyX, drunkGuyY);
Island(island, rows, columns);
}
// Generate the dimensions from user input.
public static char[,] Dimensions(int rows, int columns)
{
char[,] island = new char[rows, columns];
return island;
}
// Create array.
public static void Island(char[,] island, int x, int y)
{
for (int i = 0; i < x; i++)
{
for (int z = 0; z < y; z++)
{
Console.Write(island[i, z]);
}
Console.WriteLine();
}
}
// Fill land in array.
public static char[,] Land(char[,] landArea, int x, int y)
{
for (int i = 1; i < x-1; i++)
{
for (int z = 1; z < y-1; z++)
{
landArea[i, z] = '+';
}
}
return landArea;
}
// Fill water in array.
public static char[,] Water(char[,] landArea, int x, int y)
{
for (int i = 0; i < x; i++)
{
for (int z = 0; z < y; z++)
{
landArea[i, z] = 'W';
}
}
return landArea;
}
// Generate random placement of bridges from user input.
public static char[,] Bridge(char[,] bridge, int x, int y, int bridgeCount)
{
int numBridges = 0;

while (numBridges < bridgeCount)
{
Random random = new Random();
int randy = random.Next(0, 4);

if (randy == 0)
{
int left = random.Next(1, x-1);
if (bridge[left, 0] != 'B')
{
bridge[left, 0] = 'B';
numBridges++;
}

}
if (randy == 1)
{
int bottom = random.Next(1, y - 1);
if (bridge[x - 1, bottom] != 'B')
{
bridge[x - 1, bottom] = 'B';
numBridges++;
}
}
if (randy == 2)
{
int right = random.Next(1, x - 1);
if (bridge[y - 1, right] != 'B')
{
bridge[y - 1, right] = 'B';
numBridges++;
}
}
if (randy == 3)
{
int top = random.Next(1, y - 1);
if (bridge[0, top] != 'B')
{
bridge[0, top] = 'B';
numBridges++;
}
}
}
return bridge;
}

public static char[,] MoveDrunk(char[,] drunk, int x, int y, int lives, int drowns, int starves, int exhaustion, int drunkGuyX, int drunkGuyY)
{
Random random = new Random();
int s = random.Next(1, 4);
if (drunk[drunkGuyX, drunkGuyY] == 'B')
{
while (starves <= ((x * y) / 3))
{
if (s == 1)
{
drunk[drunkGuyX, drunkGuyY] = '+';
drunk[drunkGuyX, drunkGuyY + 1] = 'D';
exhaustion++;
Console.WriteLine("Your drunk moved, " + s + " and your exhaustion level is, " + exhaustion + ".");
random = new Random();
Island(drunk, x, y);
return MoveDrunk(drunk, x, y, lives, drowns, starves, exhaustion, drunkGuyX, drunkGuyY);
}
else if (s == 2)
{
drunk[drunkGuyX, drunkGuyY] = '+';
drunk[drunkGuyX + 1, drunkGuyY] = 'D';
exhaustion++;

Console.WriteLine("Your drunk moved, " + s + " and your exaustion level is, " + exhaustion + ".");
random = new Random();
Island(drunk, x, y);
return MoveDrunk(drunk, x, y, lives, drowns, starves, exhaustion, drunkGuyX, drunkGuyY);
}
else if (s == 3)
{
drunk[drunkGuyX, drunkGuyY] = '+';
drunk[drunkGuyX, drunkGuyY - 1] = 'D';
exhaustion++;
Console.WriteLine("Your drunk moved, " + s + " and your exaustion level is, " + exhaustion + ".");
random = new Random();
Island(drunk, x, y);
return MoveDrunk(drunk, x, y, lives, drowns, starves, exhaustion, drunkGuyX, drunkGuyY);
}
else
{
drunk[drunkGuyX, drunkGuyY] = '+';
drunk[drunkGuyX - 1, drunkGuyY] = 'D';
exhaustion++;
Console.WriteLine("Your drunk moved, " + s + " and your exaustion level is, " + exhaustion + ".");
random = new Random();
Island(drunk, x, y);
return MoveDrunk(drunk, x, y, lives, drowns, starves, exhaustion, drunkGuyX, drunkGuyY);
}
}
if (exhaustion == ((x * y) / 3))
{
starves++;
GameOver();
}
lives++;
}
else if (drunk[drunkGuyX, drunkGuyY] == 'B')
{
drowns++;
GameOver();
}
return drunk;
}
//Ends the game when called.
public static void GameOver()
{

Console.WriteLine("This game just ended, you've reached your maximum allowed exaustion. \n Press any key to continue");

DialogResult Result = MessageBox.Show("Lets play again!",
"Message", MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation);
if (Result == DialogResult.Yes)
{
//Play();
}
else
{
System.Environment.Exit(0);
}

}

}
}



##### Share on other sites

There's a lot wrong with your code.  It's really too much to cover in one post.

You need to go over the code yourself, and find the logic errors.  A few obvious ones are, "rows" should correspond to y axis, and "columns" to x, you're setting island[rows/2, column/2] = 'D' but your Drunk guy should be at island[DrunkX, DrunkY].  The 1st line of "MoveDrunk" you check is the drunk guys is on a 'B' tile, but it will obviously have to be a 'D' tile.  Your else if after checking if he's on a 'B' tile is to check if he's on a 'B' tile!!

You really need to go over it yourself, or, if you're still stuck, try a smaller exercise 1st.  You're normal game loop should be somethig like this:

GameInitialize(); // Setup the land, water, bridges, and drunk guy spot

while (IsAlive)
{
UpdateDrunkGuy();
CheckDrunkGuyAlive();
DrawGame();
}


Good luck, and keep at it, you'll get it, and things will come together soon.

##### Share on other sites

I am pleased to note that I got full functionality to my program!!

Besides my exhaustion doesn't update for some reason and my starves doesn't increment. Thanks for the help BearNutts for pointing my obvious mistakes.

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

namespace IslandDrunk
{
class Program
{
static void Main(string[] args)
{
int lives = 0, drowns = 0, starves = 0;
Console.WriteLine("All you need to know... \n1 moves drunk up, 2 moves drunk right, 3 moves drunk down, 4 moves drunk left");
Play(lives, drowns, starves);
}
public static int NumRows()
{
int inNumRows;
String inStringValue;

while (int.TryParse(inStringValue, out inNumRows) == false)
{
Console.WriteLine("Invalid input");
Console.Write("Please re-enter an integer value between 3 and 25. \n");
}
if ((inNumRows <= 3) || (inNumRows >= 25))
{
Console.WriteLine("Invalid input");
Console.Write("Please re-enter an integer value between 3 and 25. \n");
}
return inNumRows;
}
public static int NumColumns()
{

int inNumColumns;
String inStringValue;

while (int.TryParse(inStringValue, out inNumColumns) == false)
{
Console.WriteLine("Invalid input");
Console.Write("Please re-enter an integer value between 3 and 25. \n");
}
if((inNumColumns <= 3) || (inNumColumns >= 25))
{
Console.WriteLine("Invalid input");
Console.Write("Please re-enter an integer value between 3 and 25. \n");
}
return inNumColumns;
}
public static int NumBridges()
{

int inNumBridges;
String inStringValue;

while (int.TryParse(inStringValue, out inNumBridges) == false)
{
Console.WriteLine("Invalid input");
Console.Write("Please re-enter an integer value between 1 and 25. \n");
}
if ((inNumBridges <= 0) || (inNumBridges >= 25))
{
Console.WriteLine("Invalid input");
Console.Write("Please re-enter an integer value between 1 and 25. \n");
}
return inNumBridges;
}
public static int NumExhaustion()
{
int inNumExhaustion;
String inStringValue;

while (int.TryParse(inStringValue, out inNumExhaustion) == false)
{
Console.WriteLine("Invalid input");
Console.Write("Please re-enter an integer value between 1 and 25. \n");
}
if ((inNumExhaustion <= 0) || (inNumExhaustion >= 25))
{
Console.WriteLine("Invalid input");
Console.Write("Please re-enter an integer value between 1 and 25. \n");
}
return inNumExhaustion;
}
public static void Play(int lives, int drowns, int starves)
{
Console.WriteLine("\nPlease enter the amount of rows you want for your island, \nmust be greater than 3, less than 25.");
int rows = NumRows();

Console.WriteLine("\nPlease enter the amount of columns you want for your island, \nmust be greater than 3, less than 25.");
int columns = NumColumns();

Console.WriteLine("\nPlease enter the number of bridges you prefer.");
int bridgeCount = NumBridges();

int exhaustion = NumExhaustion();

int drunkGuyX, drunkGuyY;
drunkGuyX = columns / 2;
drunkGuyY = rows / 2;

char[,] island = Dimensions(columns, rows);

Water(island, columns, rows); //Generate water.
Bridge(island, columns, rows, bridgeCount); //Generate bridges.
Land(island, columns, rows); //Generate land.
island[drunkGuyX, drunkGuyY] = 'D'; //Generates drunks location.
MoveDrunk(island, columns, rows, exhaustion, drunkGuyX, drunkGuyY, lives, drowns, starves); //Move D.
Island(island, columns, rows); //Create the island's dimensions.
}
// Generate the dimensions from user input.
public static char[,] Dimensions(int columns, int rows)
{
char[,] island = new char[columns, rows];
return island;
}
// Create array.
public static void Island(char[,] island, int x, int y)
{
for (int i = 0; i < x; i++)
{
for (int z = 0; z < y; z++)
{
Console.Write(island[i, z]);
}
Console.WriteLine();
}
}
// Fill land in array.
public static char[,] Land(char[,] landArea, int x, int y)
{
for (int i = 1; i < x - 1; i++)
{
for (int z = 1; z < y - 1; z++)
{
landArea[i, z] = '+';
}
}
return landArea;
}
// Fill water in array.
public static char[,] Water(char[,] landArea, int x, int y)
{
for (int i = 0; i < x; i++)
{
for (int z = 0; z < y; z++)
{
landArea[i, z] = 'W';
}
}
return landArea;
}
// Generate random placement of bridges from user input.
public static char[,] Bridge(char[,] bridge, int x, int y, int bridgeCount)
{
int numBridges = 0;

while (numBridges < bridgeCount)
{
Random random = new Random();
int randy = random.Next(0, 4);

if (randy == 0)
{
int left = random.Next(1, x - 1);
if (bridge[left, 0] != 'B')
{
bridge[left, 0] = 'B';
numBridges++;
}

}
if (randy == 1)
{
int bottom = random.Next(1, y - 1);
if (bridge[x - 1, bottom] != 'B')
{
bridge[x - 1, bottom] = 'B';
numBridges++;
}
}
if (randy == 2)
{
int right = random.Next(1, x - 1);
if (bridge[right, y - 1] != 'B')
{
bridge[right, y - 1] = 'B';
numBridges++;
}
}
if (randy == 3)
{
int top = random.Next(1, y - 1);
if (bridge[0, top] != 'B')
{
bridge[0, top] = 'B';
numBridges++;
}
}
}
return bridge;
}
// Move the D and update lives, drowns, and starves.
public static char[,] MoveDrunk(char[,] drunk, int x, int y, int exhaustion, int drunkGuyX, int drunkGuyY, int lives, int drowns, int starves)
{
Random random = new Random();
int s = random.Next(1, 4);
int gameCount = 0;
int exh = 0;
while (gameCount < 50)
{

if (exh < exhaustion)
{
if (s == 1)// Random 1, move up.
{
drunk[drunkGuyX, drunkGuyY] = '+';
exh++;
Console.WriteLine("Your drunk moved, " + s + " and your exhaustion level is, " + exh + ".");
drunkGuyY++;

if (drunk[drunkGuyX, drunkGuyY] == 'W')
{
drowns++;
gameCount++;
Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n");
Play(lives, drowns, starves);
}
if (drunk[drunkGuyX, drunkGuyY] == 'B')
{
lives++;
gameCount++;
Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n");
Play(lives, drowns, starves);
}
drunk[drunkGuyX, drunkGuyY] = 'D';
random = new Random();
Island(drunk, x, y);
return MoveDrunk(drunk, x, y, exhaustion, drunkGuyX, drunkGuyY, lives, drowns, starves);
}
else if (s == 2)// Random 2, move right.
{
drunk[drunkGuyX, drunkGuyY] = '+';
exh++;
Console.WriteLine("Your drunk moved, " + s + " and your exhaustion level is, " + exh + ".");
drunkGuyX++;
if (drunk[drunkGuyX, drunkGuyY] == 'B')
{
lives++;
gameCount++;
Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n");
Play(lives, drowns, starves);
}
if (drunk[drunkGuyX, drunkGuyY] == 'W')
{
drowns++;
gameCount++;
Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n");
Play(lives, drowns, starves);
}
drunk[drunkGuyX, drunkGuyY] = 'D';
random = new Random();
Island(drunk, x, y);
return MoveDrunk(drunk, x, y, exhaustion, drunkGuyX, drunkGuyY, lives, drowns, starves);
}
else if (s == 3)// Random 3, move down.
{
drunk[drunkGuyX, drunkGuyY] = '+';
exh++;
Console.WriteLine("Your drunk moved, " + s + " and your exhaustion level is, " + exh + ".");
drunkGuyY--;
if (drunk[drunkGuyX, drunkGuyY] == 'B')
{
lives++;
gameCount++;
Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n");
Play(lives, drowns, starves);
}
if (drunk[drunkGuyX, drunkGuyY] == 'W')
{
drowns++;
gameCount++;
Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n");
Play(lives, drowns, starves);
}
drunk[drunkGuyX, drunkGuyY] = 'D';
random = new Random();

Island(drunk, x, y);
return MoveDrunk(drunk, x, y, exhaustion, drunkGuyX, drunkGuyY, lives, drowns, starves);
}
else// Random 4, move left.
{
drunk[drunkGuyX, drunkGuyY] = '+';
exh++;
Console.WriteLine("Your drunk moved, " + s + " and your exhaustion level is, " + exh + ".");
drunkGuyX--;
if (drunk[drunkGuyX, drunkGuyY] == 'B')
{
lives++;
gameCount++;
Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n");
Play(lives, drowns, starves);
}
if (drunk[drunkGuyX, drunkGuyY] == 'W')
{
drowns++;
gameCount++;
Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n");
Play(lives, drowns, starves);
}
drunk[drunkGuyX, drunkGuyY] = 'D';
random = new Random();
Island(drunk, x, y);
return MoveDrunk(drunk, x, y, exhaustion, drunkGuyX, drunkGuyY, lives, drowns, starves);
}
}
// End the game with input.
if (exh == exhaustion)
{
starves++;
gameCount++;
Console.WriteLine("Game Over... \n\n" + "Results: " + "\n\nSurvived: " + lives + "\nDrowned: " + drowns + "\nStarved: " + starves + "\n");

Console.WriteLine("Do you want to keep playing? Enter n to end the game and any other key to play again.");
String inStringValue;
string endGame = inStringValue;
if (endGame == "n")
{
Console.WriteLine("This Game is about to close in 4 seconds...");
System.Environment.Exit(0);
}
else
{
Play(lives, drowns, starves);
}

}
}
return drunk;
}
}
}


Edited by jim8ean

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633703
• Total Posts
3013455
×