I've been thinking about trying to create a RPG game, a simple game with movement, pick up items, and open doors.
I've been thinking for a long time about the tile map engine, but i can't find anything that works :/
Basically what i'm trying to accomplish is i have an enum, like:
public enum tileSort { Dirt, Grass, Stone, Empty }
And when the engine runs through the array, which will have 0's, 1's, etc, I'm thinking about a switch statement, something like:
switch(tileSort)
{
case '0': tileList.Add(Content.Load<Texture2D>("Tiles/grass"))
}
The problem is that I have no idea on how to make this possible, all that I've been able to create is an engine that runs through and generates depending on which content you load first into the game.
I know this is confusing, as I'm not good at explaining myself.
Thanks in advance.
You can use some tools to help you:
http://jdevh.com/2012/06/01/griddy2d/
http://github.com/zachmu/tiled-xna
http://xnafantasy.wordpress.com/2008/11/22/xna-map-editor-version-30-released/
I'm sure you can find many others.
About the snippets of code you wrote, you don't want to call
Content.Load<Texture2D>("Tiles/grass")
multiple times for a single texture. Load each texture only once and print the same resource multiple times. You could have something like this:
var tileList = new List<Texture2D>();
string[] tiles = { "dirt", "grass", "stone", "empty" };
foreach (var s in tiles) {
tileList.Add(Content.Load<Texture2D>("Tiles/" + s));
}
Now each texture is loaded only once, and you can access it using tileList[index].
The next step is to print the tiles on the screen. I'll assume you have loaded your tiles into a 2 dimensional array with the tile indexes.
int[,] tileMap = {{1, 2, 0}, {0, 1, 2}, {3, 3, 1},};
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
spriteBatch.Draw(tileList[tileMap[i, j]], new Vector2D(50*i, 50*j), Color.White);
// Assuming the tiles are 50x50 pixels
This tutorial teaches what you want with more details: http://www.xnaresources.com/?page=Tutorial:TileEngineSeries:1
Related
Imagine we have 4 vertices (1,2,3,4), and I save them in array that way:
var vertices = new[] { 1, 2, 3, 4 };
var edges = new[]{Tuple.Create(1,2), Tuple.Create(2,4), Tuple.Create(4,3), Tuple.Create(4,1), Tuple.Create(3,2), Tuple.Create(3,1) };
Now it will look like this:
but what if I have more vertices, say 100? How should i generate it? will the loop be linear or not?
if i use something like this:
List<Tuple<int,int>> edges = new List<Tuple<int, int>>();
for (int i=1; i<vertices.Length-1; i++)
{
edges.Add(Tuple.Create(i, i+1));
}
I will eventually just get outline of the graph right?
I can't figure out how to make intersections, like 1 to 3, 2 to 4.
Or if i have graph that is 100 vertices in width and 50 vertices in height, or is not necessarily rectangle?
If you will need some more information i will add it.
i use something like this…I will eventually just get outline of the graph right?
Correct.
How should i generate it?
If I understand your question correctly, you just want all the possible combinations of vertexes. This is simple:
List<Tuple<int,int>> edges = new List<Tuple<int, int>>();
for (int i = 1; i < vertices.Length - 1; i++)
{
for (int j = i + 1; j < vertices.Length; j++)
{
edges.Add(Tuple.Create(i, j));
}
}
In other words, this is just a variation on the Handshake Problem.
will the loop be linear or not?
As you can see above, definitely not. It is O(n^2).
I am trying to improve myself on probabilities with a task but couldn't figure it out:
On a backgammon game, I have four (dynamic) rows of placements.
So lets say row 5,7,11,13. I am throwing dynamic number of dice. I am trying to find the probabilities of each gameplay. As an example:
I have thrown 4 dice and result is 1,3,4,6
So
I can play from the 5. row-1,3,4,6 dice
I can play from the 5. row-1,3,4 dice and from the 7. row-6 dice
I can play from the 5. row-3,1 dice and from the 11. row-6 dice and from the 13. row-4 dice
etc etc.
The dice have to be dynamic, the rows have to be dynamic, the dice can be played mixed like 1,3,4,6 or 6,1,4,3 or 3,1,6,4. And it has to calculate all the possibilities of different variations of dice spread on the rows.
Simply I am trying to calculate the possibilities of play on a backgammon with unlimited dice. The method I am trying to use it to have a resizable possible moves class and inside of this class, there are the resizable rows class. Inside the rows class, there is resizable List moves variable. I am adding one move for each variation. This is the codes for non dynamic 4 dice.
public List<int> dice;
public List<int> places;
[System.Serializable]
public class arrayData
{
public List<arrayData2> places = new List<arrayData2>();
}
public List<arrayData> pos = new List<arrayData>();
[System.Serializable]
public class arrayData2
{
public List<int> moves = new List<int> {};
}
void Start()
{
vs();
}
void vs()
{
for (int i = 0; i < places.Count; i++)
{
for (int a = 0; a < dice.Count; a++)
{
for (int b = 0; b < dice.Count; b++)
{
for (int c = 0; c < dice.Count; c++)
{
for (int d = 0; d < dice.Count; d++)
{
if (a == b || a == c || a == d || b == c || b == d || c == d)
{
continue;
}
pos.Add(new arrayData());
for (int s = 0; s < places.Count; s++)
{
pos[pos.Count - 1].places.Add(new arrayData2());
}
pos[pos.Count - 1].places[i].moves.Add(dice[a]);
pos[pos.Count - 1].places[i].moves.Add(dice[b]);
pos[pos.Count - 1].places[i].moves.Add(dice[c]);
pos[pos.Count - 1].places[i].moves.Add(dice[d]);
}
}
}
}
}
}
What I couldn't figure out:
-Firstly, I tried to make it a recursive loop but I couldn't figure out the structure
-Second, the output is giving me missing values for the variation of spread between rows: It gives such values: 5. row-1,3,4,6 dices but not such values: 5. row-3,1 dices and from the 11. row-6 dice and from the 13. row-4 dice
I know it is a big question to ask but any kind of pointing out the mistake or correct direction leading would be so appreciated.
Thanks
It might be useful to think about your moves as part of a game tree. The basic idea is that all games start in a particular state. We'll call it x. A move then changes the state of the game to x1, the next move to x2, and so on. Most moves involve a choice and, depending on the choice made, each move pushes the game into a different state. From the starting state, all possible moves form a gigantic tree. This is handy because we can use tree traversal algorithms to explore our tree. That's where the recursion comes in.
Backgammon is conceptually a little trickier than a game like chess or checkers because you have to account for the randomness of the dice as well as the behavior of your opponent. That adds even more choices during a move. In pseudo-code, I might tackle the problem like this:
function get_next_move (current_state):
dice = roll_dice()
possible_states = an empty collection
get_possible_states (dice, current_state, possible_states)
return decide_best_state (possible_states)
function get_possible_states (dice, state, possible_states):
// If we've exhausted all dice, we're done.
// Add the state we've reached to our possible outcomes.
if dice are empty:
add state to possible_states
return
for each die in dice:
dice_without_die = remove die from dice
for each next_state in get_legal_states (state, die):
// Here's the recursion. Go a level deeper in our tree.
get_possible_states (dice_without_die , next_state, possible_states)
function get_legal_states (state, die):
// Here's where you determine legal moves based on the
// die rolled and the current state of the game.
Put another way, to determine my next move I have to:
Roll the dice.
With each die rolled, generate all of its legal moves (states).
Repeat the process using a new state and without the die already used.
When there are no dice left, add the final state to the list of possible states.
Finally, determine which move is best. Keep in mind that some generated states may be duplicates. There's more than one way to move your pieces to the same locations.
The nice thing about this approach is there are no hard-coded numbers. It can handle any number of legal moves or dice. (Though I'm not sure when Backgammon would use a variable number of dice.) One detail I left out is that you need to know who's turn it is. The legal moves will depend on who's making them.
I have these arrays:
string[] Line1= data[3].ToString().Split(' ');
string[] Line2= data[4].ToString().Split(' ');
The string array contain only integer values. Data are like -20 -30 -12 0 10 20 30 and so on.
Now want to add these values which is in lineNeg1 to Devexpress Chart Control Series without loop.
As right now things are working but due to loop, system gets too slow. Code sample is here under:
for (int i = 0; i < Line1.Length; i++)
{
int y = int.Parse(Line1[i]);
SeriesPoint pt = new SeriesPoint(i, y);
chartControl1.Series[0].Points.Add(pt);
}
Is there any way that i can do something like: Add string array to series without using loop
maybe like: series[0].addrange[Line1] <- Maybe this kind of something option is available
I know the state is wrong, still just want to give an idea of what i am looking for.
You could use Linq:
int[] ints = Line1.Select(x => int.Parse(x)).ToArray();
It's still a for-loop, but now it's hidden! The compiler needs to convert the strings into ints one by one as they're fundamentally different things and stored quite differently. Strings are objects whereas integers are native types. It's not like Javascript or PHP where strings and integers get converted on the fly unfortunately. So this doesn't help you much, it's just semantic sugar.
Now, as far as adding the series goes, maybe the problem is that the chart redraws every time a point is added. Have you tried your code like this:
chartControl1.SuspendLayout();
for (int i = 0; i < Line1.Length; i++)
{
int y = int.Parse(Line1[i]);
SeriesPoint pt = new SeriesPoint(i, y);
chartControl1.Series[0].Points.Add(pt);
}
chartControl1.ResumeLayout();
I am trying to figure out how to turn the following, one-line CSV file into a 30x30 2D array.
http://pastebin.com/8NP7s7N0
I've tried looking it up myself, but I just can't seem to wrap my brain around the concept of multidimensional arrays, and I don't know how to turn a one-line file like this into an array of a specified size.
I want to be able to make an array that would look like this when printed:
0,0 = 2
0,1 = 2
All the way to 30,30.
Most of the numbers in the CSV are indeed 2's, but some are 1s. The difference is very important though. I am trying to make collision detection for a game, and this CSV file is the map. All I need left is how to create this array - leave the rest to me. :)
Thank you very much to all, have a nice day.
This should be a complete example using a 5 x 5 grid. I've tried it and seems to work as expected:
namespace ConsoleApplication1
{
using System;
class Program
{
const int MapRows = 5;
const int MapColumns = 5;
static void Main(string[] args)
{
// Create map and the raw data (from file)
var map = new int[MapRows, MapColumns];
string rawMapData = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25";
string[] splitData = rawMapData.Split(',');
int index = 0;
// Loop through data
for (int row = 0; row < MapRows; row++)
{
for (int column = 0; column < MapColumns; column++)
{
// Store in map and show some debug
map[row, column] = int.Parse(splitData[index++]);
Console.WriteLine(string.Format("{0},{1} = {2}", row, column, map[row, column]));
}
}
// Wait for user to read
Console.ReadKey();
}
}
}
Assuming your file is 900 elements first you need to read it in..
something along the lines of
line = myStreamReader.readLine().Split(',').. then in John U's example, value would be the next index in this array called line
I'll let you work out whats missing from my example :P
well, first you need to get the numbers...
var numbers = Read_File_As_String().Split(new char[',']).Select(n => int.Parse(n)).ToList();
then, you need to build your array
const int ROWS = 30;
const int COLS = 30;
var result = new int[ROWS, COLS];
for (int row = 0; row < ROWS; row++)
for (int col = 0; col < COLS; col++)
result[row, col] = numbers[(row * COLS) + col];
for(row=0;row<30;row++)
{
for(col=0;col<30;col++)
{
array[row][col] = value;
}
}
Value would need to be moved along to point to the next thing each time, but I'm sure you can figure that out.
Edited to add: If it's a map it might be easier to store it as an array in the first place.
Since you asked about the concept of multi-dimensional arrays, here are some useful ways of thinking about arrays. Please note these are analogies, meant to help you visualize them.
Think of a 1D array as a list of items (not in the programming sense of list!).
Think of a 2D array as a table (again, not in the programming sense!). In a table (like a spreadsheet) you have rows and columns, and each dimension in your array accesses one of these.
For higher dimensional arrays, it may help to think geometrically. For instance, you can think of 3D arrays as 3-dimensional points in space, and 4D arrays as 4-dimensional points in space-time.
So if you have a single CSV file, start off by conceptualizing how this would be re-structured as a table. Once you have that, you have a pretty straight-forward mapping to the array.
I'm starting out with xna, I'm pretty newbie with this, but I'm making my efforts to go on with this framework, anymay, my problem is:
that I have many .png images and dont want to make an object for any of those images so I want to put them up in a Texture2D array, I thought that this is way to do it, but looks like it's not the correct way:
Texture2D[] _rCards, _bCards, _sCards;
_bCards = new Texture2D[9];
_rCards = new Texture2D[9];
_sCards = new Texture2D[6];
for (int i = 1; i < 10; i++)
{
_bCards[i] = Content.Load<Texture2D>("Images/Common/Black/"+i);
_rCards[i] = Content.Load<Texture2D>("Images/Common/Red/"+i);
if(i<6)
_sCards[i] = Content.Load<Texture2D>("Images/Special/Card" + (i-1));
}
The file names for the texture are 1.png, 2.png, 3.png, and so on.
For the special cards are card1.png, card2.png,card3.png and so on.
I'm trying to make a blackjack game.
Can you give me an advice to load all this textures in one single texture2D array.
The IDE gives an NULLREFERENCEEXCEPTION issue or something.
Maybe the language doesnt understands the entire adress to find the textures as a string.
Indexes are 0 based...
for (int i = 1; i < 10; i++)
{
_bCards[i-1] = Content.Load("Images/Common/Black/"+i);
_rCards[i-1] = Content.Load("Images/Common/Red/"+i);
if(i<6) _sCards[i-1] = Content.Load("Images/Special/Card" + (i-1));
}
if you want to load all textures at same time you can use the sprite sheet sample:
http://create.msdn.com/en-US/education/catalog/sample/sprite_sheet
You will have an unique asset and a dictionary of source rectangles to draw the sprites...