/// <summary> /// Give the user instructions for piece placement and return /// the 2D location of the position they select /// </summary> /// <param name="activePlayer"></param> /// <returns></returns> private int[] PiecePlacement(_2DGameLibrary.Player activePlayer) { //you need to be using the .NET framework 4.6 for this line to work (C# 6) Console.WriteLine(); Console.WriteLine($"{activePlayer.Name}, it's your turn:"); Console.WriteLine("Make your move by entering the number of the sqaure you'd like to take:"); PrintBoardMap(); Console.Write("Enter the number: "); //todo: Prevent returning a location that's already been used return(ConvertToArrayLocation(Console.ReadLine())); }
public TicTac() { board.boardArray = new char[3, 3]; players = new _2DGameLibrary.Player[2]; //Random names from http://www.behindthename.com/random/ //The names are Greek ;) players[0] = new _2DGameLibrary.Player() { Name = "Player 1: Theophania", Token = 'X' }; //using object initialization syntax players[1] = new _2DGameLibrary.Player() { Name = "Player 2: Xenon", Token = 'O' }; }
/// <summary> /// The Tic Tac Toe game loop, 2 players. Iterate player turns until the game /// is over /// </summary> private void Start() { _2DGameLibrary.Game game = new _2DGameLibrary.Game(); int indexOfCurrentPlayer = 0; activePlayer = players[indexOfCurrentPlayer]; while (!GameOver()) { Console.WriteLine("Here is the board:"); PrintBoard(); TakeTurn(activePlayer); //select the other player indexOfCurrentPlayer = (indexOfCurrentPlayer == 0) ? 1 : 0; activePlayer = players[indexOfCurrentPlayer]; //Added this slight delay for user experience. Without it it's harder to notice the board repaint //try commenting it out and check out the difference. Which do you prefer? System.Threading.Thread.Sleep(300); Console.Clear(); } }
/// <summary> /// Get and set the player's desired location on the board /// </summary> /// <param name="activePlayer"></param> private void TakeTurn(_2DGameLibrary.Player activePlayer) { int[] position = PiecePlacement(activePlayer); board.boardArray[position[0], position[1]] = activePlayer.Token; }