Related
I created a static class to keep all my "level" arrays in (I'm writing a game in monogame, using XNA).
The problem is that i can't use the class anywhere, it just won't show up in intellisense, and returns an error The name 'Rooms' does not exist in the current context. Any idea what may cause this?
namespace Marked_V0._2
{
public static class Rooms
{
static public int[,] Level1 = new int[,]
{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1 }
};
}
}
Error:
CS1002 ; Expected
CS1001 Identifier expected
CS0103 The name 'Rooms' does not exist in the current context.
The namespace for Rooms most likely does not match the namespace of the rest of the project. You can either change the namespace or include the extra namespace with using Marked_V0._2.
I'm essentially trying to do the Game Of Life. All seems well, for the most part it works as I want. But for some reason when I go to print it out and have it play, it prints two incomplete parts of the array side by side. Half of the cells, about every other, on on the right, the other half are on the left. I have no idea what I'm doing wrong, I've used similar methods to print 2d arrays before with no trouble, so this is really baffling me. Code below.
class Program
{
static void Main(string[] args)
{
GameOfLife myGame = new GameOfLife();
myGame.Start();
}
}
class GameOfLife
{
private int[,] grid = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
private int[,] tempGrid;
public GameOfLife()
{
tempGrid = new int[grid.GetLength(0), grid.GetLength(1)];
}
public void Start()
{
Setup();
do
{
Thread.Sleep(30);
UpdateGrid();
CopyTempGridToGrid();
PrintGrid();
} while (!Console.KeyAvailable);
Console.WriteLine();
}
private void UpdateGrid()
{
for(int r = 0; r < grid.GetLength(0); r++)
{
for(int c = 0; c < grid.GetLength(1); c++)
{
tempGrid[r, c] = UpdateCell(r, c);
}
}
}
private void CopyTempGridToGrid()
{
Array.Copy(tempGrid, grid, tempGrid.Length);
}
private void PrintGrid()
{
Console.CursorTop = 2;
for (int r = 0; r < grid.GetLength(0); r++)
{
for (int c = 0; c < grid.GetLength(1); c++)
{
if (grid[r, c] == 0)
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write(".");
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(".");
}
}
}
}
private int GetNeighbors(int row, int col)
{
int neighbors = 0;
try
{
if (grid[row - 1, col - 1] == 1)
neighbors++;
if (grid[row - 1, col] == 1)
neighbors++;
if (grid[row - 1, col + 1] == 1)
neighbors++;
if (grid[row, col - 1] == 1)
neighbors++;
if (grid[row, col + 1] == 1)
neighbors++;
if (grid[row + 1, col - 1] == 1)
neighbors++;
if (grid[row + 1, col] == 1)
neighbors++;
if (grid[row + 1, col + 1] == 1)
neighbors++;
}
catch (IndexOutOfRangeException) { }
return neighbors;
}
private int UpdateCell(int row, int col)
{
int n = GetNeighbors(row, col);
if (n <= 1)
return 0;
if (n == 2)
return grid[row, col];
if (n == 3)
return 1;
else
return 0;
}
private void Setup()
{
Console.Clear();
Console.CursorVisible = false;
Console.WriteLine("Press any key to stop");
PrintGrid();
}
}
Although the loops within PrintGrid() have inner and an outer nested loops, they are not outputting a newline after each row. I'd try a Console.Writeln("") between the inner and outer loops.
I made a tile level system that uses a multidimensinal array, for example:
new int[,]{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},
Every specific number represents a block(tile).
This is just in code. I want to code a system that can load a level(an multidimensinal int array)
That array needs to be converted from a string. How do I do that?
public static int[,] getLvl(string lvlName = "")
{
string readed = "";
using (StreamReader read = new StreamReader(path))
{
readed = read.ReadToEnd();
}
return null; //What to put here?!?!?!
}
EDIT: I do not have yet a format for the file to read. So you can be flexible in that.
I don't think you really need to or should bother trying to serialize it into XML or some other format, since your data storage is so simple.
One easy way is to just store your array in a text file as comma separated values. So one level might have:
0,0,0,1
0,1,1,0
0,1,1,3
3,3,4,1
The String.Split() method is really useful for parsing something simple like this. It allows you to split a string into an array of substrings based on a certain delimiting character.
Step by step:
First you can use var RowArray = MyString.Split('\n') (the newline character) to split your string into an array of rows. This leaves you with the array:
[0]: "0,0,0,1"
[1]: "0,1,1,0"
[2]: "0,1,1,3"
[3]: "3,3,4,1"
You can sort of see what Split() does here and why that's useful for your case. You can in turn run split each row on ',' leaving with you an array of arrays, which you can very easily convert to exactly the 2D array you're looking for.
The one pitfall here is somewhere, depending on your design needs, one invariant might have to be that in the file all rows will be of the same length. Or if you can't guarantee that, you'll have to write some code so that when turning the below text into an array from an array of rows, you make the width equal to the longest row and fill in blanks with 0s or some other method.
0,0,0,1,6,4
0,1,1,0
0,1,1,3,2,6,3,7,1
3,3,4,1,2,4
The shortest method is using linq, for this format:
0,0,0,0,0
1,0,1,0,1
....
You can use this sentence:
int[][] Data = File.ReadAllLines( path )
.Select( s => s.Trim())
.Where( s => !string.IsNullOrempty(s))
.Select( s => s.Split( ',' )
.Select( token => int.Parse( token ) )
.ToArray( ) )
.ToArray( );
var DataToOneDim = Data.SelectMany( a => a).ToArray();
var Result = new int[Data[0].Length, Data.Length];
Buffer.BlockCopy( DataToOneDim, 0, Result, 0, DataToOneDim.Length );
I believe there are a few other posts regarding this, but you can use either XML and parse through each possible dimension or serialize everything at once using the SoapFormatter class. Below is a link to a similar question with some examples:
Convert Multi-Dimensional Array to String and Back
I make it for you fast you can make it better do not Forget the vote :_)
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
var array =
"{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,} , {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,}";
string input = array;
string pattern = #"{|}";
var index = 0;
var results = new int[100,100];
foreach (var result in Regex.Split(input, pattern))
{
var sp = result.Split(',');
if (sp.Length <4)
continue;
for (int i = 0; i < sp.Count(); i++)
{
if (!string.IsNullOrEmpty(sp[i]))
results[index,i] = Convert.ToInt32(sp[i]);
}
index++;
}
}
}
}
I've got a mysterious compile error with the following test case:
[TestCase(new byte[259], new byte[] { 0, 0, 0, 0, 255 })]
public void EncodeTest(byte[] source, byte[] expected) {
...
}
error CS0182: An attribute argument must be a constant expression,
typeof expression or array creation expression of an attribute parameter type
If I replace the first argument of the attribute as follows:
[TestCase(new byte[259] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0 },
new byte[] { 0, 0, 0, 0, 255 })]
public void EncodeTest(byte[] source, byte[] expected) {
...
}
it compiles just fine. Why?
Update: Let me clarify the problem. If you write code like the following:
int[] arr = new int[259];
Console.Write(arr[0]); // 0
no compile error occurs since the compiler (or CLR?) implicitly initializes every element with the default value.
In your first construction, you simply declared an array of 259 bytes, but it's uninitialized. Sure, the CLR will automatically initialize intrinsic values (actually, this is an array, so it's not even intrinsic, it's an object--the byte[] keyword (and operator) is just syntactic sugar to make writing and reading code easier. In fact, if you'd written Array<System.Byte> someArray;, someArray would be null until you initialized it, e.g. someArray = new Array<System.Byte>(259);.
Attributes, as the error states, requires constants (or something that can be made constant) as a parameter.
A declared array is just that, a declaration. Is there anything in it? You haven't said so, but the compiler can't assume you want a zero-filled array. You must tell it explicitly what value to use. Attributes are evaluated at compile-time and adorn/decorate code constructs providing meta-data (or additional functionality to provide) to various code constructs you've used them with.
I'm trying to port a desencrypt function i have in Delphi to C#.
in delphi i use the function Move to get data into memory using a pointer, so i tried to use the marshal.copy function with the same results but sometimes i get an acces violation exception and sometimes blank spaces.
leave attached the code of the function.
Any ideas? Thanks a lot!
public string prDecode(string s)
{
byte[] ArrayMap = new byte[256] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59,60,
61, 0, 0, 0, 0, 0, 0, 0, 0, 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,
0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int I;
char[] ArrayS = s.ToCharArray(0, s.Length);
string resultado = "";
char[] ArrayR = resultado.ToCharArray(0, resultado.Length);
switch (s.Length)
{
case 2:
I = ArrayMap[ArrayS[0]] + (ArrayMap[ArrayS[1]] << 6);
ArrayR = new char[1];
Marshal.Copy((IntPtr)I, ArrayR, 0, ArrayR.Length);
break;
case 3:
I = ArrayMap[ArrayS[0]] + (ArrayMap[ArrayS[1]] << 6) +
(ArrayMap[ArrayS[2]] << 12);
ArrayR = new char[2];
Marshal.Copy((IntPtr)I, ArrayR, 0, ArrayR.Length);
break;
case 4:
I = ArrayMap[ArrayS[0]] + (ArrayMap[ArrayS[1]] << 6) +
(ArrayMap[ArrayS[2]] << 12) + (ArrayMap[ArrayS[3]] << 18);
ArrayR = new char[3];
Marshal.Copy((IntPtr)I, ArrayR, 0, ArrayR.Length);
break;
}
string clave = new string (ArrayR);
return clave;
}
I think the function you're looking for is Array.Copy or Buffer.BlockCopy rather than Marshal.Copy. Marshal.Copy is for copying data to and from unmanaged memory.
There's no need to use Marshal unless you are accessing unmanaged memory for legacy code. Managed code has no need for pointers & using managed methods will be much quicker.
What you're looking for I think, is Array.Copy to copy between arrays using managed code.