   Tic Tac Toe - C# (text-based)
  I have some experience with C++ on and off and have recently started learning C#.  This is the first game (that is not guess the number) I have ever made so it might be all over the place a little bit (probably should have written it out before just programming it).  Even though I've been into programming off and on for the past couple of years I consider myself a newbie.  I've gotten some advice on this already and was looking for any additional suggestions about the design or layout.  I'm fairly new to object-oriented programming and am self-taught so I'm not really sure when I'm doing something wrong (I understand that just because it works, that doesn't mean it's good).  Any advice or suggestions would be appreciated!using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TicTacToe{    public class Player    {   private string playerPiece = "n";   //METHODS //MovePlayer takes a multi-dimensional array board, and a string piece that are both ref    public void MovePlayer(ref string[,] board, ref string piece)   {  playerPiece = piece;  int rows = 3;  int columns = 3;  bool turnOver = false;  bool validMove = false;  int availabilityCounter = 0;  int playerMoveCounter = 0;  string playerMove;  do  { //Reset counters availabilityCounter = 0; playerMoveCounter = 0; for (int i = 0; i < rows; i++) {for (int j = 0; j < columns; j++){    ///Check the board to see if any moves are available    if ((board[i, j] == "o") || (board[i, j] == "x"))    {   availabilityCounter++;   if (availabilityCounter == 9)   {  turnOver = true;   }    }} }   if (turnOver == false) {    Console.WriteLine("\n\nPLEASE CHOOSE A NUMBER FOR YOUR NEXT MOVE: \n\n");    playerMove = Console.ReadLine();do{    //See if the player's move is equal to a string 1-9 and increase the playerMove counter if it is found    //If the playerMove counter = 1 then the move has been found,  if not then please ask the player to enter a valid move    for (int r = 1; r <= 9; r++)    {   if (playerMove == Convert.ToString(r))   {  playerMoveCounter++;   }    }    //Make sure the player has entered a space on the board    //if they haven't make them choose again    if (playerMoveCounter != 1)    {   Console.WriteLine("\nThat is not a valid choice please enter a new number...\n");   playerMove = Console.ReadLine();    }} while (playerMoveCounter != 1);//This loop searchs through the board looking for player's desired move while his/her turn is not overfor (int i = 0; i < rows; i++){    for (int j = 0; j < columns; j++)    {   //Find the player's desired move, see if the spot is occupied, if not, place the player piece   if ((board[i, j] == playerMove) & (board[i, j] != "o") & (board[i, j] != "x"))   {  board[i, j] = playerPiece;  turnOver = true;  validMove = true;   }    }   }//If the move is not available because the space is occupied inform the playerif (validMove == false){    Console.WriteLine("THAT MOVE IS NOT AVAILABLE!\n");}  }  }while((turnOver == false) & (validMove == false));   }//End move player    }//End player class    public class Computer    {   private string computerPiece = "n";   //Methods   //MoveComputer takes a multidimensional array board refernce and a string piece reference   public void MoveComputer(ref string[,] board, ref string cPiece)   {  //create an instance of the random class  Random RandomClass = new Random();  computerPiece = cPiece;  bool turnOver = false;  int count = 0;  //AI  //The computer will make a move based on random numbers  do  {int randomOne = RandomClass.Next(0, 3); int randomTwo = RandomClass.Next(0, 3); //End turn when the computer places a piece if ((board[randomOne, randomTwo] != "x") & (board[randomOne, randomTwo] != "o")) {board[randomOne, randomTwo] = cPiece;turnOver = true; } //End turn if no spots available else {count++;//if the count is greater than 9 then there are no spaces available (only 9 places on the board)if (count > 9){    turnOver = true;} }   } while ((turnOver == false));   }//End MoveComputer    }//End Computer class    public class Board    {   //Creates a game board with pre-set values   private string[,] gameBoard =   {  {"1","2","3"}, {"4","5","6"}, {"7","8","9"}   };   //Methods   //DisplayBoard just displays the updated game board   public void DisplayBoard(ref string[,] board)   {  Console.WriteLine("\n{0} {1} {2}\n", board[0, 0], board[0, 1], board[0, 2]);  Console.WriteLine("{0} {1} {2}\n", board[1, 0], board[1, 1], board[1, 2]);  Console.WriteLine("{0} {1} {2}\n\n", board[2, 0], board[2, 1], board[2, 2]);   }   //CheckBoard accepts a board and checks it to see who has won, then returns a string that describes who won   public string CheckBoard(ref string[,] board)   { string gameOver = "n"; //Conditions for winning the game  string[,] winningCombos =  { {board[0,0], board[0,1], board[0,2]}, {board[1,0], board[1,1], board[1,2]}, {board[2,0], board[2,1], board[2,2]}, {board[0,0], board[1,0], board[2,0]}, {board[0,1], board[1,1], board[2,1]}, {board[0,2], board[1,2], board[2,2]}, {board[0,0], board[1,1], board[2,2]}, {board[0,2], board[1,1], board[2,0]}   };  int totalRows = 8;    //Check the Board to see if anyone has won  for ( int i = 0; i < totalRows; i++)  {  //Check all the winning rows to see if x's won if ((winningCombos[i, 0] == "x") & (winningCombos[i,1] == "x") & (winningCombos[i,2] == "x")) {gameOver = "X"; } //Check all the winning rows to see if o's have won if ((winningCombos[i, 0] == "o") & (winningCombos[i, 1] == "o") & (winningCombos[i, 2] == "o") & (gameOver != "X")) {gameOver = "O"; }  }  //return the status of the game  return gameOver;   }//End CheckBoard   //accessor method for retrieving the board   public string[,] GetBoard()   {  return gameBoard;   }    }//End Board class    public class GameLoop    {   public void Run()   {  string userResponse = "";  string gameStatus = "n";  do  { Console.WriteLine("\n\nWELCOME TO ANEXA'S TIC TAC TOE GAME!!");  //Create a new Player, a new Board, and a new Computer Player newPlayer = new Player(); Computer newComputer = new Computer(); Board newBoard = new Board(); //the variable turn will helps to determine when the game is a tie int turn = 0; //Use the Board accessor method to get the newly created board and store it in newGameBoard string[,] newGameBoard = newBoard.GetBoard();  Console.WriteLine("\nWould you like to be X's or O's?"); string userXorO = Console.ReadLine(); //If the user does not enter x or o then keep asking until they do do {if ((userXorO != "x") & (userXorO != "o")){    Console.WriteLine("You must type lower case x or lower case o to continue: ");    userXorO = Console.ReadLine();} } while ((userXorO != "x") & (userXorO != "o")); //display the updated newGameBoard newBoard.DisplayBoard(ref newGameBoard);  do {string playerGamePiece = userXorO;//If the player chooses x then they will always move firstif (userXorO == "x"){    string computerPiece = "o";   //Get the player move    newPlayer.MovePlayer(ref newGameBoard, ref playerGamePiece);    //Get the status of the game after the player's move    gameStatus = newBoard.CheckBoard(ref newGameBoard);    //Display the updated board after the move    newBoard.DisplayBoard(ref newGameBoard);    //increase the turn count so we can tell when there's a tie    turn++;    //same logic as player movenewComputer.MoveComputer(ref newGameBoard, ref computerPiece);    gameStatus = newBoard.CheckBoard(ref newGameBoard);    newBoard.DisplayBoard(ref newGameBoard);    turn++;}    //if the player chooses o then they will always move after the computerelse if (userXorO == "o"){    string computerPiece = "x";    //same as above    newComputer.MoveComputer(ref newGameBoard, ref computerPiece);    gameStatus = newBoard.CheckBoard(ref newGameBoard);    newBoard.DisplayBoard(ref newGameBoard);    turn++;    //same as above    newPlayer.MovePlayer(ref newGameBoard, ref playerGamePiece);    gameStatus = newBoard.CheckBoard(ref newGameBoard);    newBoard.DisplayBoard(ref newGameBoard);    turn++;}//if the gameStatus has X as the winner, and the player's piece is X, then congratulate themif ((gameStatus == "X") & (userXorO == "x")){    Console.WriteLine("YOU WON!\n");    Console.WriteLine("Would you like to play again? (y or n)");    userResponse = Console.ReadLine();}//if the game status has X as the winner and the player piece is o, then tell them they lostelse if ((gameStatus == "X") & (userXorO == "o")){    Console.WriteLine("YOU LET THE COMPUTER BEAT YOU?\n");    Console.WriteLine("Would you like to play again? ( y or n )");    userResponse = Console.ReadLine();}//Same logic as aboveif ((gameStatus == "O") & (userXorO == "x")){    Console.WriteLine("YOU LET THE COMPUTER BEAT YOU?\n");    Console.WriteLine("Would you like to play again? ( y or n )");    userResponse = Console.ReadLine();}    else if ((gameStatus == "O") & (userXorO == "o")){    Console.WriteLine("YOU WON!\n");    Console.WriteLine("Would you like to play again? (y or n)");    userResponse = Console.ReadLine();}//if we have gone through all 9 turns and the game status does not show x or o as a winner then there is a tie, inform the playerif ((turn > 9) & (gameStatus != "X") & (gameStatus != "O")){    Console.WriteLine("IT'S A TIE!!!\n");    Console.WriteLine("WOULD YOU LIKE TO PLAY AGAIN? (y or n)\n\n");    userResponse = Console.ReadLine();    gameStatus = "T";} //continue to get player moves while the there is no winner and no tie } while ((gameStatus != "X") & (gameStatus != "O") & (gameStatus != "T"));    //If the player chooses to quit then end the overall game loop.  If the player chooses to continue playing start at the top of the outer do, while loop  } while (userResponse != "n");   }//Run   static void Main()   {  //Create an instance of the gameloop class and run it  GameLoop g = new GameLoop();  g.Run();   }    }//End GameLoop class}  
  You are making your logic too complicated, in general. You need to fix these things before you attempt to address whether you are "doing OOP properly".1) Don't compare to boolean literals. "If it is raining" means the same thing as "if it is true that it is raining", and is clearer.2) If two things are mutually exclusive, you don't need to check them both.3) To check if a value is within a numeric range, you don't need to check against every element of that range; just check if it's - well, "in range". I.e., at least as much at the bottom of the range, and no bigger than the top.4) Don't "count things up" if the maximum possible value for the count is 1.Also, your AI logic has a bug: because the "candidate" location is random each time, it could select the same location more than once. Just because it chooses an occupied square 9 times doesn't mean that all 9 squares are occupied.To fix this, make a struct that represents a coordinate for a move, then make a container that contains all coordinates representing legal moves (i.e. the board is blank at the specified coordinate), and then choose randomly from the contents of the container.But here are two quick points about how to do OOP:1) Don't pass a string[,] parameter to the member functions of Board. There is already a perfectly usable string[,] lying around: the gameBoard member.Remember, structs and classes define data types. Every instance has its own data, just like every int in your program can have its own value. When you call a method, you are calling it on an instance of the struct or class; that is, you are telling that specific object to do some work, possibly involving its own data.2) Don't add 'Board' to the names of Board methods; it doesn't add any information. You do not emptygarbagecan your garbage can; you empty it.   ;  wow zahlman, thanks for all the advice!  Like i said i had no idea that much stuff was wrong lol.  I did think that i had the computer check the spot to if it's occupied before it moved. i'll have to go back through this and try to make it more efficient and clear. I'm glad i posted this before moving on, i'll be sure to run it by you as i fix it.*edit: i just realized what you meant about the computer AI. My newbieness is definitely showing haha.   ;  I think for your win condition check instead of typeing all that: string[,] winningCombos =  { {board[0,0], board[0,1], board[0,2]}, {board[1,0], board[1,1], board[1,2]}, {board[2,0], board[2,1], board[2,2]}, {board[0,0], board[1,0], board[2,0]}, {board[0,1], board[1,1], board[2,1]}, {board[0,2], board[1,2], board[2,2]}, {board[0,0], board[1,1], board[2,2]}, {board[0,2], board[1,1], board[2,0]}   };You can simplify it and do it in math... You know each column and row can have a max of 3 "points" per player. Check in a loop if any row or column adds up to 3.   ;  Hey Zahlman,I fixed the AI bug by adding the same check for available moves I used in the Player class. I also fixed all the Method names so that they were not repetitive. I think I understand what you mean about the gameBoard member but am at a loss about how to apply your first and second suggestion *edit* could you let me know where this is an issue specifically? *I don't expect you to just give me answers or anything).  I've only been programming in C# for about 2 weeks now so I know that my fundamentals are pretty weak.  (I also haven't figured out how to check for a range with strings).To kubapl,  My problem ends up being that I still need that list of winning combos because I need to have all three spaces occupied at the same point in time.  So if I use a counter of some sort or a loop I think it would take about the same amount of effort as what I already have.  I also need it to check for the diagonal wins as well.  However, like I said I'm pretty new, so if you were thinking of a different implementation of that idea, let me know!*Sorry it took me awhile to respond... had finals this week!*edit:  I believe I fixed what you were talking about with using gameBoard instead of passing in an additional instance call to the same thing.Here is the updated version:[source = "csharp"] using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TicTacToe{    public class Player    {   private string playerPiece = "n"; //METHODS   //Move takes a multi-dimensional array board, and a string piece that are both ref    public void Move(ref string[,] board, ref string piece)   {  playerPiece = piece;  int rows = 3;  int columns = 3;  bool turnOver = false;  bool validMove = false;  string playerMove;  do  { //Reset counters int availabilityCounter = 0; int playerMoveValid = 0; ///Check the board to see if any moves are available for (int i = 0; i < rows; i++) {for (int j = 0; j < columns; j++){   if ((board[i, j] == "o") || (board[i, j] == "x"))    {   availabilityCounter++;   if (availabilityCounter == 9)   {  turnOver = true;   }    }} } if (turnOver == false) {Console.WriteLine("\n\nPLEASE CHOOSE A NUMBER FOR YOUR NEXT MOVE: \n\n");playerMove = Console.ReadLine();do{    //See if the player's move is equal to a string 1-9 and increase the playerMove counter if it is found    //If the playerMove counter = 1 then the move has been found,  if not then please ask the player to enter a valid move    for (int r = 1; r <= 9; r++)    {   if (playerMove == Convert.ToString(r))   {  playerMoveValid++;   }    }    //Make sure the player has entered a space on the board    //if they haven't make them choose again    if (playerMoveValid != 1)    {   Console.WriteLine("\nThat is not a valid choice please enter a new number...\n");   playerMove = Console.ReadLine();    }} while (playerMoveValid != 1);//This loop searchs through the board looking for player's desired move while his/her turn is not overfor (int i = 0; i < rows; i++){    for (int j = 0; j < columns; j++)    {   //Find the player's desired move, see if the spot is occupied, if not, place the player piece   if ((board[i, j] == playerMove) & (board[i, j] != "o") & (board[i, j] != "x"))   {  board[i, j] = playerPiece;  turnOver = true;  validMove = true;   }    }}//If the move is not available because the space is occupied inform the playerif (validMove == false){    Console.WriteLine("THAT MOVE IS NOT AVAILABLE!\n");} }  } while ((turnOver == false) & (validMove == false));   }//End move player    }//End player class    public class Computer    {   private string computerPiece = "n";   //METHODS   //Move takes a multidimensional array board refernce and a string piece reference   public void Move(ref string[,] board, ref string cPiece)   {  //create an instance of the random class  Random RandomClass = new Random();  computerPiece = cPiece;  bool turnOver = false;  int rows = 3;  int columns = 3;  //AI  //The computer will make a move based on random numbers  do  { int availabilityCounter = 0; ///Check the board to see if any moves are available for (int i = 0; i < rows; i++) {for (int j = 0; j < columns; j++){   if ((board[i, j] == "o") || (board[i, j] == "x"))    {   availabilityCounter++;   if (availabilityCounter == 9)   {  turnOver = true;   }    }} } if (turnOver == false) {int randomOne = RandomClass.Next(0, 3);int randomTwo = RandomClass.Next(0, 3);//End turn when the computer places a pieceif ((board[randomOne, randomTwo] != "x") & (board[randomOne, randomTwo] != "o")){    board[randomOne, randomTwo] = cPiece;    turnOver = true;} }  } while ((turnOver == false));   }//End Move    }//End Computerpublic class Board    {   //Creates a game board with pre-set values   private string[,] gameBoard =   {  {"1","2","3"}, {"4","5","6"}, {"7","8","9"}   };   //Methods   //Display just displays the updated game board   public void Display()   {  Console.WriteLine("\n{0} {1} {2}\n", gameBoard[0, 0], gameBoard[0, 1], gameBoard[0, 2]);  Console.WriteLine("{0} {1} {2}\n", gameBoard[1, 0], gameBoard[1, 1], gameBoard[1, 2]);  Console.WriteLine("{0} {1} {2}\n\n", gameBoard[2, 0], gameBoard[2, 1], gameBoard[2, 2]);   }   //CheckBoard accepts a board and checks it to see who has won, then returns a string that describes who won   public string Check()//use gameBoard member here instead!   {  string gameOver = "n";  //Conditions for winning the game  string[,] winningCombos =  { {gameBoard[0,0], gameBoard[0,1], gameBoard[0,2]}, {gameBoard[1,0], gameBoard[1,1], gameBoard[1,2]}, {gameBoard[2,0], gameBoard[2,1], gameBoard[2,2]}, {gameBoard[0,0], gameBoard[1,0], gameBoard[2,0]}, {gameBoard[0,1], gameBoard[1,1], gameBoard[2,1]}, {gameBoard[0,2], gameBoard[1,2], gameBoard[2,2]}, {gameBoard[0,0], gameBoard[1,1], gameBoard[2,2]}, {gameBoard[0,2], gameBoard[1,1], gameBoard[2,0]}   };  int totalCombos = 8;  //Check the Board to see if anyone has won  for (int i = 0; i < totalCombos; i++)  { //Check all the winning rows to see if x's won if ((winningCombos[i, 0] == "x") & (winningCombos[i, 1] == "x") & (winningCombos[i, 2] == "x")) {gameOver = "X"; } //Check all the winning rows to see if o's have won if ((winningCombos[i, 0] == "o") & (winningCombos[i, 1] == "o") & (winningCombos[i, 2] == "o") & (gameOver != "X")) {gameOver = "O"; }  }  //return the status of the game  return gameOver;   }//End Check   //accessor method for retrieving the board   public string[,] Get()   {  return gameBoard;   }    }//End Boardpublic class GameLoop    {   public void Run()   {  string userResponse = "";  string gameStatus = "n";  do  { Console.WriteLine("\n\nWELCOME TO ANEXA'S TIC TAC TOE GAME!!"); //Create a new Player, a new Board, and a new Computer Player newPlayer = new Player(); Computer newComputer = new Computer(); Board newBoard = new Board(); //the variable turn will helps to determine when the game is a tie int turn = 0; //Use the Board accessor method to get the newly created board and store it in newGameBoard string[,] newGameBoard = newBoard.Get(); Console.WriteLine("\nWould you like to be X's or O's?"); string userXorO = Console.ReadLine(); //If the user does not enter x or o then keep asking until they do do {if ((userXorO != "x") & (userXorO != "o")){    Console.WriteLine("You must type lower case x or lower case o to continue: ");    userXorO = Console.ReadLine();} } while ((userXorO != "x") & (userXorO != "o")); //display the updated newGameBoard newBoard.Display(); do {string playerGamePiece = userXorO;//If the player chooses x then they will always move firstif (userXorO == "x"){    string computerPiece = "o";    //Get the player move    newPlayer.Move(ref newGameBoard, ref playerGamePiece);    //Get the status of the game after the player's move    gameStatus = newBoard.Check();    //Display the updated board after the move    newBoard.Display();    //increase the turn count so we can tell when there's a tie    turn++;    //same logic as player movenewComputer.Move(ref newGameBoard, ref computerPiece);    gameStatus = newBoard.Check();    newBoard.Display();    turn++;}//if the player chooses o then they will always move after the computerelse if (userXorO == "o"){    string computerPiece = "x";    //same as above    newComputer.Move(ref newGameBoard, ref computerPiece);    gameStatus = newBoard.Check();    newBoard.Display();    turn++;    //same as above    newPlayer.Move(ref newGameBoard, ref playerGamePiece);    gameStatus = newBoard.Check();    newBoard.Display();    turn++;}//if the gameStatus has X as the winner, and the player's piece is X, then congratulate themif ((gameStatus == "X") & (userXorO == "x")){    Console.WriteLine("YOU WON!\n");    Console.WriteLine("Would you like to play again? (y or n)");    userResponse = Console.ReadLine();}//if the game status has X as the winner and the player piece is o, then tell them they lostelse if ((gameStatus == "X") & (userXorO == "o")){    Console.WriteLine("YOU LET THE COMPUTER BEAT YOU?\n");    Console.WriteLine("Would you like to play again? ( y or n )");    userResponse = Console.ReadLine();}//Same logic as aboveif ((gameStatus == "O") & (userXorO == "x")){    Console.WriteLine("YOU LET THE COMPUTER BEAT YOU?\n");    Console.WriteLine("Would you like to play again? ( y or n )");    userResponse = Console.ReadLine();}else if ((gameStatus == "O") & (userXorO == "o")){    Console.WriteLine("YOU WON!\n");    Console.WriteLine("Would you like to play again? (y or n)");    userResponse = Console.ReadLine();}//if we have gone through all 9 turns and the game status does not show x or o as a winner then there is a tie, inform the playerif ((turn > 9) & (gameStatus != "X") & (gameStatus != "O")){    Console.WriteLine("IT'S A TIE!!!\n");    Console.WriteLine("WOULD YOU LIKE TO PLAY AGAIN? (y or n)\n\n");    userResponse = Console.ReadLine();    gameStatus = "T";}//continue to get player moves while the there is no winner and no tie } while ((gameStatus != "X") & (gameStatus != "O") & (gameStatus != "T")); //If the player chooses to quit then end the overall game loop.  If the player chooses to continue playing start at the top of the outer do, while loop  } while (userResponse != "n");   }//Run   static void Main()   {  //Create an instance of the gameloop class and run it  GameLoop g = new GameLoop();  g.Run();   }    }//End GameLoop };  Can someone explain what is meant by 'not comparing to boolean literals' in the suggestions in Zahlman's reply?   ;  The following all mean the same thing:bool x = SoomBoolean();//if x is trueif(x == true) {do stuff}if(x != false){do stuff}if(x)    {do stuff}//if x is falseif(x == false){do stuff}if(x != true) {do stuff}if(!x)   {do stuff};  Ahhh I see.... Thanks!   ;  A literal is a bit of code that describes a specific value. These are often classified by their type; so for example 123 is an integer literal, 'q' is a character literal, and "hi mom" is a string literal.There are two boolean literals: true and false. It's pointless to compare to them, because it's simply redundant: the thing that you're comparing is already a boolean value (or at least convertible to one).Many people do recommend, however, that you compare, for example, to integer literals instead of relying on an implicit conversion: thus if (x == 0) instead of if (!x) where x is an int.Also, on the rare occasion that you need a constant value for an if-condition (maybe to temporarily disable some code) or looping condition (maybe you have a loop with complex exit conditions, where it's easier to loop infinitely and break out), do use a boolean literal and not an integer literal: thus while (true), not while (1). It's more expressive. (And please don't write for (;;). That's just ugly.)   ;  Thanks for the explanation :)  I think that I finally fixed everything you were talking about (Zahlman).  However, I still wasn't sure how to check against a range of values when using a string so I haven't fixed that yet (still using a for loop to check against each value to see if the playerMove is valid).  At the beginning of your first post Zahlman you said I was making my logic too complicated in general and that I should worry about that before OOP.  I was just wondering if your suggestions were what you meant by that or if there was more I needed to fix or re-arrange. I wanted to make sure that this tic tac toe game was done correctly before moving onto Blackjack which is what I want to make next.using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TicTacToe{    public class Player    {   private string playerPiece = "n"; //METHODS   //Move takes a multi-dimensional array board, and a string piece that are both ref    public void Move(ref string[,] board, ref string piece)   {  playerPiece = piece;  int rows = 3;  int columns = 3;  bool turnOver = false;  bool validMove = false;  string playerMove;  do  { //Reset counters int availabilityCounter = 0; bool playerMoveValid = false; ///Check the board to see if any moves are available for (int i = 0; i < rows; i++) {for (int j = 0; j < columns; j++){   if ((board[i, j] == "o") || (board[i, j] == "x"))    {   availabilityCounter++;   if (availabilityCounter == 9)   {  turnOver = true;   }    }} } if (!turnOver) {Console.WriteLine("\n\nPLEASE CHOOSE A NUMBER FOR YOUR NEXT MOVE: \n\n");playerMove = Console.ReadLine();do{    //See if the player's move is equal to a string 1-9 and increase the playerMove counter if it is found    //If the playerMove counter = 1 then the move has been found,  if not then please ask the player to enter a valid move    for (int r = 1; r <= 9; r++)    {   if (playerMove == Convert.ToString(r))   {  playerMoveValid = true;   }    }    //Make sure the player has entered a space on the board    //if they haven't make them choose again    if (!playerMoveValid)    {   Console.WriteLine("\nThat is not a valid choice please enter a new number...\n");   playerMove = Console.ReadLine();    }} while (!playerMoveValid);//This loop searchs through the board looking for player's desired move while his/her turn is not overfor (int i = 0; i < rows; i++){    for (int j = 0; j < columns; j++)    {   //Find the player's desired move, see if the spot is occupied, if not, place the player piece   if ((board[i, j] == playerMove) & (board[i, j] != "o") & (board[i, j] != "x"))   {  board[i, j] = playerPiece;  turnOver = true;  validMove = true;   }    }}//If the move is not available because the space is occupied inform the playerif (!validMove){    Console.WriteLine("THAT MOVE IS NOT AVAILABLE!\n");} }  //Updated so that while statement does check for mutually exclusive events.  turnOver will never be true when validMove is false so I only need to check one of them.  } while ((!turnOver));   }//End move player    }//End player class    public class Computer    {   private string computerPiece = "n";   //METHODS   //Move takes a multidimensional array board refernce and a string piece reference   public void Move(ref string[,] board, ref string cPiece)   {  //create an instance of the random class  Random RandomClass = new Random();  computerPiece = cPiece;  bool turnOver = false;  int rows = 3;  int columns = 3;  //AI  //The computer will make a move based on random numbers  do  { int availabilityCounter = 0; ///Check the board to see if any moves are available for (int i = 0; i < rows; i++) {for (int j = 0; j < columns; j++){   if ((board[i, j] == "o") || (board[i, j] == "x"))    {   availabilityCounter++;   if (availabilityCounter == 9)   {  turnOver = true;   }    }} } if (!turnOver) {int randomOne = RandomClass.Next(0, 3);int randomTwo = RandomClass.Next(0, 3);//End turn when the computer places a pieceif ((board[randomOne, randomTwo] != "x") & (board[randomOne, randomTwo] != "o")){    board[randomOne, randomTwo] = cPiece;    turnOver = true;} }  } while ((!turnOver));   }//End Move    }//End Computerpublic class Board    {   //Creates a game board with pre-set values   private string[,] gameBoard =   {  {"1","2","3"}, {"4","5","6"}, {"7","8","9"}   };   //Methods   //Display just displays the updated game board   public void Display()   {  Console.WriteLine("\n{0} {1} {2}\n", gameBoard[0, 0], gameBoard[0, 1], gameBoard[0, 2]);  Console.WriteLine("{0} {1} {2}\n", gameBoard[1, 0], gameBoard[1, 1], gameBoard[1, 2]);  Console.WriteLine("{0} {1} {2}\n\n", gameBoard[2, 0], gameBoard[2, 1], gameBoard[2, 2]);   }   //CheckBoard accepts a board and checks it to see who has won, then returns a string that describes who won   public string Check()//use gameBoard member here instead!   {  string gameOver = "n";  //Conditions for winning the game  string[,] winningCombos =  { {gameBoard[0,0], gameBoard[0,1], gameBoard[0,2]}, {gameBoard[1,0], gameBoard[1,1], gameBoard[1,2]}, {gameBoard[2,0], gameBoard[2,1], gameBoard[2,2]}, {gameBoard[0,0], gameBoard[1,0], gameBoard[2,0]}, {gameBoard[0,1], gameBoard[1,1], gameBoard[2,1]}, {gameBoard[0,2], gameBoard[1,2], gameBoard[2,2]}, {gameBoard[0,0], gameBoard[1,1], gameBoard[2,2]}, {gameBoard[0,2], gameBoard[1,1], gameBoard[2,0]}   };  int totalCombos = 8;  //Check the Board to see if anyone has won  for (int i = 0; i < totalCombos; i++)  { //Check all the winning rows to see if x's won if ((winningCombos[i, 0] == "x") & (winningCombos[i, 1] == "x") & (winningCombos[i, 2] == "x")) {gameOver = "X"; } //Check all the winning rows to see if o's have won if ((winningCombos[i, 0] == "o") & (winningCombos[i, 1] == "o") & (winningCombos[i, 2] == "o") & (gameOver != "X")) {gameOver = "O"; }  }  //return the status of the game  return gameOver;   }//End Check   //accessor method for retrieving the board   public string[,] Get()   {  return gameBoard;   }    }//End Boardpublic class GameLoop    {   public void Run()   {  string userResponse = "";  string gameStatus = "n";  do  { Console.WriteLine("\n\nWELCOME TO ANEXA'S TIC TAC TOE GAME!!"); //Create a new Player, a new Board, and a new Computer Player newPlayer = new Player(); Computer newComputer = new Computer(); Board newBoard = new Board(); //the variable turn will helps to determine when the game is a tie int turn = 0; //Use the Board accessor method to get the newly created board and store it in newGameBoard string[,] newGameBoard = newBoard.Get(); Console.WriteLine("\nWould you like to be X's or O's?"); string userXorO = Console.ReadLine(); //If the user does not enter x or o then keep asking until they do do {if ((userXorO != "x") & (userXorO != "o")){    Console.WriteLine("You must type lower case x or lower case o to continue: ");    userXorO = Console.ReadLine();} } while ((userXorO != "x") & (userXorO != "o")); //display the updated newGameBoard newBoard.Display(); do {string playerGamePiece = userXorO;//If the player chooses x then they will always move firstif (userXorO == "x"){    string computerPiece = "o";    //Get the player move    newPlayer.Move(ref newGameBoard, ref playerGamePiece);    //Get the status of the game after the player's move    gameStatus = newBoard.Check();    //Display the updated board after the move    newBoard.Display();    //increase the turn count so we can tell when there's a tie    turn++;    //same logic as player movenewComputer.Move(ref newGameBoard, ref computerPiece);    gameStatus = newBoard.Check();    newBoard.Display();    turn++;}//if the player chooses o then they will always move after the computerelse if (userXorO == "o"){    string computerPiece = "x";    //same as above    newComputer.Move(ref newGameBoard, ref computerPiece);    gameStatus = newBoard.Check();    newBoard.Display();    turn++;    //same as above    newPlayer.Move(ref newGameBoard, ref playerGamePiece);    gameStatus = newBoard.Check();    newBoard.Display();    turn++;}//if the gameStatus has X as the winner, and the player's piece is X, then congratulate themif ((gameStatus == "X") & (userXorO == "x")){    Console.WriteLine("YOU WON!\n");    Console.WriteLine("Would you like to play again? (y or n)");    userResponse = Console.ReadLine();}//if the game status has X as the winner and the player piece is o, then tell them they lostelse if ((gameStatus == "X") & (userXorO == "o")){    Console.WriteLine("YOU LET THE COMPUTER BEAT YOU?\n");    Console.WriteLine("Would you like to play again? ( y or n )");    userResponse = Console.ReadLine();}//Same logic as aboveif ((gameStatus == "O") & (userXorO == "x")){    Console.WriteLine("YOU LET THE COMPUTER BEAT YOU?\n");    Console.WriteLine("Would you like to play again? ( y or n )");    userResponse = Console.ReadLine();}else if ((gameStatus == "O") & (userXorO == "o")){    Console.WriteLine("YOU WON!\n");    Console.WriteLine("Would you like to play again? (y or n)");    userResponse = Console.ReadLine();}//if we have gone through all 9 turns and the game status does not show x or o as a winner then there is a tie, inform the playerif ((turn > 9) & (gameStatus != "X") & (gameStatus != "O")){    Console.WriteLine("IT'S A TIE!!!\n");    Console.WriteLine("WOULD YOU LIKE TO PLAY AGAIN? (y or n)\n\n");    userResponse = Console.ReadLine();    gameStatus = "T";}//continue to get player moves while the there is no winner and no tie } while ((gameStatus != "X") & (gameStatus != "O") & (gameStatus != "T")); //If the player chooses to quit then end the overall game loop.  If the player chooses to continue playing start at the top of the outer do, while loop  } while (userResponse != "n");   }//Run   static void Main()   {  //Create an instance of the gameloop class and run it  GameLoop g = new GameLoop();  g.Run();   }    }//End GameLoop }