tags:

views:

95

answers:

3

I want this code to represent symbols and not numbers (A, O, X)? Can someone give me a simple code to make the numbers into symbols? Thanks

int game[3][3]; 
int x, y; 
int lines = 0; 

// select a random grid 
srand(time(0)); 
for(x = 0; x < 3; x++) { 
for(y = 0; y < 3; y++) { 
game[x][y] = rand() % 3; 
cout << game[x][y]; 
if (y == 2) 
cout << '\n'; 

} 
} 

for (y = 0; y < 2; y++) 
if (game[0][y] == game[1][y] && game[0][y] == game[2][y]) 
lines++; 
+1  A: 
chat c;
switch(game[x][y])
{
   case 0:
      c = 'A';
      break;
   case 1:
      c = 'O';
      break;
   case 2:
      c = 'X';
      break;
}

or

char c;
if(game[x][y] == 0)
    c = 'A';
else if(game[x][y] == 1)
   c = 'O';
else 
   c = 'X';
Armen Tsirunyan
+1  A: 

Use another array, filled with the desired character values, then index that array with the generated random number:

char chars[] = { 'A', 'O', 'X' };
... 
for(x = 0; x < 3; x++) { 
  for(y = 0; y < 3; y++) { 
    game[x][y] = chars[rand() % 3];
    ...
  } 
}
Péter Török
Thank you for helping me out. But you seem to know a lot of c++. Can I e-mail you? Im a beginner in c++ and I'm trying to make a game, and I've tried for days now and I'm almost done with my code. There is only one problem left. Can you please look at my code and help me out? I will be very thankfull. Because I don't understand how to solve it on my own :S:S
Atb
@Atb: Do you have [a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? You need to learn C++ *before* you use it to make a game. Grab one, spend a while learning C++, *then* make a game. This isn't very difficult C++, you're going to learn bad habits and have trouble moving on if you guess it the whole time. I really doubt @Peter wants to devote all his time teaching you C++, especially when the material is already made. We want to help, but we aren't full-time teachers.
GMan
Im a beginner in c++ and I'm allready done with a code. It's just that I don't know how to solve one problem. It's only one problem left. You would understand if you saw my whole code.
Atb
I did study a lot from the book, but it's just that I think C++ is hard :/
Atb
@Atb, if you can simplify the representation of the problem in code so that it fits within a separate SO post (that's a few dozen lines of code to me), I am sure there will be people willing to chew through it (including myself, if I happen to be online). If you can't simplify it, you most likely don't understand the problem well enough.
Péter Török
+2  A: 

You can use a lookup table:

char convert_number_to_letter(unsigned number)
{
    static const char characters[] = "AOX";
    if (number >= sizeof(characters) - 1)
        return '\0'; // or other error handling
    return characters[number];
}
James McNellis
+1 simply for putting it in a function.
GMan
@GMan: I like functions.
James McNellis
@James: I do too. Sometimes I make multiple functions. Someone else apparently doesn't like functions.
GMan
@GMan, maybe one of these terrible subroutine zealots?
Péter Török
@Péter: Me, too. Every time you feel like you need to add a comment to your code to make it clearer, put the code you wanted to comment into a function instead, and turn the gist of your comment into the function's name.
sbi
@sbi, I start to have the feeling I should've added that smiley to my previous comment after all :-(
Péter Török
@sbi: I like that. I find I generally do that anyway, but I've never heard the principle stated so succinctly. Did you come up with that?
PigBen
@sbi: That's a great way of putting it.
GMan
@Péter: My comment started with "me, too", after all. `:)`
sbi
@PigBen: I don't know. Honestly. I doubt, though. I'm sure you will find something like that in the annals of comp.lang.c++.moderated.
sbi