   Yet Another Tic Tac Toe Game.. (requesting logic  help)
  Here's another question, I'm doing with Multi-Dimensional arrays.I need a way to check if the player's answer (choices 1-9, because theres 9 squares on Tic Tac Toe), is a X or O..And I'm using a switch statement.The game screen will be in console window, and look like:___|___|______|___|___   |   |I want to do everything right, so tell me if you know a better way of doing this.I believe my switch statement is much bulkier than it needs to be.   switch (answer)  { case "1":if (TTTBoard[0, 0] != "X" && TTTBoard[0, 0] != "O"){    TTTBoard[0, 0] = user.ToString();}break; case "2":if (TTTBoard[0, 1] != "X" && TTTBoard[0, 1] != "O"){    TTTBoard[0, 1] = user.ToString();}break; case "3":if (TTTBoard[0, 2] != "X" && TTTBoard[0, 2] != "O"){    TTTBoard[0, 2] = user.ToString();}break; case "4":if (TTTBoard[1, 0] != "X" && TTTBoard[1, 0] != "O"){    TTTBoard[1, 0] = user.ToString();}break; case "5":if (TTTBoard[1, 1] != "X" && TTTBoard[1, 1] != "O"){    TTTBoard[1, 1] = user.ToString();}break; case "6":if (TTTBoard[1, 2] != "X" && TTTBoard[1, 2] != "O"){    TTTBoard[1, 2] = user.ToString();}break; case "7":if (TTTBoard[2, 0] != "X" && TTTBoard[2, 0] != "O"){    TTTBoard[2, 0] = user.ToString();}break; case "8":if (TTTBoard[2, 1] != "X" && TTTBoard[2, 1] != "O"){    TTTBoard[2, 1] = user.ToString();}break; case "9":if (TTTBoard[2, 2] != "X" && TTTBoard[2, 2] != "O"){    TTTBoard[2, 2] = user.ToString();}break; default:Console.WriteLine("Your answer was not 1-9.  Try again.");break;  }This is very ugly looking.   
  If you are using a two dimensional array I see no other way to do what you want without using a switch case.If you were to use a single dimensional array though it would simplify the code a lot.  Since you already have the square labeled 1-9 I don't see why you couldn't use that to map over to indices of a single dimensional array.On this low scale you won't notice any performance differences either way you go about it.  So mostly it is just our personal preference on what you think looks better.   ;int num = answer[0] - '1';int& cell = TTTBoard[num % 3, num / 3];if (cell != "X" && cell != "O") {  cell = user.ToString();}...assuming that "answer" is a string and "answer[0]" is a char.EDIT: Ups I forgot the test "if (num > 0 && num <= 9)" and I am not sure int& works properly in C#...EDIT 2: The correct line should be object cell = TTTBoard[num % 3, num / 3]; // boxing[Edited by - Nyarlath on September 5, 2007 8:13:34 AM];  To clean up this switch statement, you simply need to find a way to map the input (1-9) to a pair of numbers (0..2, 0..2).  Let's make a simple table of values:InputRowColumn100201302410511612720821922So, looking at this table, let's see if we can find a relationship between the input and the affected row, firstly.  We can clearly see that when the input is 1, 2, or 3, the row affected is 0.  Similarly, 4, 5, and 6 lead to 1, and 7, 8, and 9 lead to 2.  How do we express this mathematically?  Integer division!  We have three divisions, so we should divide the input by three.  Since we're starting with 1, though, we should subtract 1 from the input before dividing.  So:int nRow = (nInput - 1) / 3Try a few to convince yourself.On to the column.  Organizing the above table differently, we see that inputs 1, 4, and 7 lead to column 0, 2, 5, and 8 lead to column 1, and 3, 6 and 9 lead to column 2.  Each column is affected in a rotation, repeating after 3.  This is indicative of modular arithmetic, so we'll use the modulo operator, %.  Again, we'll subtract 1 before operating:int nCol = (nInput - 1) % 3Since we now have the pertinent row and column, we can get rid of the switch case altogether, and simply do the following:if (TTTBoard[nRow, nCol] != "X" && TTTBoard[nRow, nCol] != "O")  TTTBoard[nRow, nCol] = user.ToString();Or, after initializing the board with spaces,if (TTTBoard[nRow, nCol] == " ")  TTTBoard[nRow, nCol] = user.ToString();I leave you the task of converting an input string (answer) to an integer, since it's a different task ;)You'll find this approach is typical when dealing with multi-dimensional arrays; it'll become no stranger to you with practice and use.  Best of luck!-jouley[Edit: Never could spell.][Edited by - jouley on September 8, 2007 2:20:10 PM];  Thank you everyone, you all gave wonderful intelligence feedback.Nyarlath, Surrix.Overwhelming thanks goes to Jouley, if he ever reads this.  He went beyond the call of duty to help me out by explaining the exact formula, for figuring this problem out.  So in the future, I won't be stumped on similar problems.   