Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++ to C# code conversion issue

Posted on 2016-07-25
4
Medium Priority
?
189 Views
Last Modified: 2016-08-13
I am trying to convert the following C++ code into C#.

I found the following C++ code KnightSequences

typedef enum          { _A,_B,_C,_D,_E,_F,_G,_H,_I,_J,_K,_L,_M,_N,_O,_1,_2,_3 } TKeyIdentity;
typedef std::vector<TKeyIdentity const>     TKeyPath;
typedef std::vector<TKeyPath const>         TKeyMap;

const TKeyPath keyPadRoot =
{
    _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _1, _2, _3
}

const TKeyMap keyPad =
{
    { _H, _L },         // A
    { _I, _K, _M },     // B
    { _F, _J, _L, _N }, // C
    { _G, _M, _O },     // D
    { _H, _N },         // E    
    { _C, _M, _1 },     // F  (_1) not valid for 2 moves followed by 1 move rule
    { _D, _N, _2 },     // G
    { _A, _E, _K, _O, _1, _3 }, // H
    { _B, _L, _2 },     // I
    { _C, _M, _3 },     // J  (_3) not valid for 2 moves followed by 1 move rule
    { _B, _H, _2 },     // K
    { _A, _C, _I, _3 }, // L
    { _B, _D, _F, _J }, // M
    { _C, _E, _G, _1 }, // N
    { _D, _H, _2 },     // O (oh)
    { _F, _H, _N },     // 1
    { _G, _I, _K, _O }, // 2 (_K) & (_O) not valid for 2 moves followed be 1 move rule
    { _H, _J, _L }      // 3
};
TraverseKeyPaths( TKeyPath const &keyPath, int pressesRemaining, int vowelsAllowed )
{
 foreach (TKeyIdentity pressedKey in Enum.GetValues(typeof(TKeyIdentity)))
  {
	int value =  TraverseKeyPaths( keyPad[ pressedKey ],pressesRemaining, vowelsAllowed - isVowel[pressedKey] );
  }

}

Open in new window


My C# code:

  enum TKeyIdentity { _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _1, _2, _3 };
  List<string> keyPadRoot = new List<string> { "_A", "_B", "_C", "_D", "_E", "_F", "_G", "_H", "_I", "_J", "_K", "_L", "_M", "_N", "_O", "_1", "_2", "_3" };

  string[] A = new string[] { "_H", "_L" }; //A
  string[] B = new string[] { "_I", "_K", "_M" }; //B
  .
  .
  .
  etc

 List<string> keyPadMoves = new List<string>(););

 keyPadMoves.AddRange(A);
 keyPadMoves.AddRange(B);
 .
 .
 .
etc

int TraverseKeyPaths(List<string> keyPadRoot, int pressesRemaining, int vowelsAllowed)
{
 	foreach (TKeyIdentity pressedKey in Enum.GetValues(typeof(TKeyIdentity)))
	{
		int value =  value = TraverseKeyPaths(keyPadRoot, pressesRemaining, vowels);
	}
}

Open in new window


The following C# code is not working like C++ code.
int value =   TraverseKeyPaths(keyPadRoot, pressesRemaining, vowels);

Please let me know how to pass the correct argument.
Thanks for your help.
0
Comment
Question by:HPFE455
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 41728786
I don't see what error or runtime problem you have.
I recommend that you post (or edit your OP) showing t a tiny program that can be built and run. If you cannot compile, then indicate the line and error message. If you are able to build it, but get the wrong answer, then indicate what you got and what you expect (and maybe even why you expect it).
0
 
LVL 35

Accepted Solution

by:
sarabande earned 2000 total points
ID: 41728966
actually std::vector cannot be initialized with an initialiser list in c++. so your source code would not compile (with c++). initialiser lists within {.., ..} only can be used for native type arrays or struct/class without an explicit constructor (and without class members with an explicit constructor).

the only way to init a std::vector with more than one different values is to using a c array as a helper:

static const int arr[] = { 1, 2, 3, 4, 5 };
static const int arrsize = (int)(sizeof(arr)/sizeof(arr[0]));
std::vector<int> v(&arr[0], &arr[arrsize));

Open in new window


that doesn't help for your 'table' since the lengths of 'rows' were different.

you would need to create a helper struct to come out of this:

enum TKeyIdentity { NIL=-1, _A,_B,_C,_D,_E,_F,_G,_H,_I,_J,_K,_L,_M,_N,_O,_1,_2,_3, KIM };

struct KeyPadInit
{
      int                    nkeys;
      TKeyIdentity  keys[6];   // need a fixed size of arr
};

Open in new window


then you can do (in c++)

const   KeyPadInit kpi[] =
{
    { 2, { _H, _L, NIL, NIL, NIL, NIL, } },         // A
    { 3, { _I, _K, _M, NIL, NIL, NIL,  }},     // B
    { 4, { _F, _J, _L, _N , NIL, NIL, }}, // C
    // ... and so on
};

typedef std::vector<KeyPadInit>  TKeyMap;
TKeyMap keyPad(&kpi[0], &kpi[KIM]);

Open in new window


unfortunately, i don't know whether you have similar possibilities in c#.

Sara
0
 
LVL 35

Expert Comment

by:sarabande
ID: 41748838
You probably should delete the question if there was no response from the Author.

The question was about porting of code from C++ to C#, but unfortunately the C++ code given is not valid (doesn't compile). That's why I posted code which solved the issue for C++ but is not a solution for C#.

In my opinion it makes little sense to go on without the participation of the Author.

Sara
0
 

Author Closing Comment

by:HPFE455
ID: 41755167
your comments helped to resolve the issue
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question