0

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++; 
Atb
  • 49
  • 1
  • 6

3 Answers3

2

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
  • 348,265
  • 75
  • 913
  • 977
  • @James: I do too. Sometimes I make multiple functions. Someone else apparently doesn't like functions. – GManNickG Oct 23 '10 at 22:35
  • @GMan, maybe one of these terrible subroutine zealots? – Péter Török Oct 23 '10 at 22:51
  • 3
    @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 Oct 23 '10 at 22:56
  • @sbi, I start to have the feeling I should've added that smiley to my previous comment after all :-( – Péter Török Oct 23 '10 at 23:09
  • @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? – Benjamin Lindley Oct 23 '10 at 23:19
  • @sbi: That's a great way of putting it. – GManNickG Oct 24 '10 at 04:56
  • @Péter: My comment started with "me, too", after all. `:)` – sbi Oct 24 '10 at 08:11
  • @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 Oct 24 '10 at 08:11
1
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
  • 130,161
  • 59
  • 324
  • 434
1

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
  • 114,404
  • 31
  • 268
  • 329
  • 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 Oct 23 '10 at 22:37
  • 3
    @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. – GManNickG Oct 23 '10 at 22:39
  • 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 Oct 23 '10 at 22:41
  • I did study a lot from the book, but it's just that I think C++ is hard :/ – Atb Oct 23 '10 at 22:44
  • 1
    @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 Oct 23 '10 at 22:49