TikTakToeExecise Modified

 using System;

using System.IO;

using System.Linq;

using System.Collections.Generic;


namespace CSharp_Shell

{


 public class Program

 {

  //these are members for play field

  static char[,] field =

  {

   {'1','2','3'},

   {'4','5','6'},

   {'7','8','9'}

  };

  static int turns = 0;

  //

  //Entry point of a program

  public static void Main()

  {

   //Basics/ core of the program

   int player = 2;

   int input = 0;

   bool inputCorrect = true;

   //

   //

   //Runs the program infinite

   do

   {




    //Switch between players

    #region

    //switch between field

    if (player == 2)

    {

     player = 1;

     EnterXorO('X', input);

    }

    else if (player == 1)

    {

     player = 2;

     EnterXorO('O', input);

    }

    #endregion

    //

    SetField();

    //

    //Check Winner

    #region

    char[] playerChars = { 'X', 'O' };

    //

    foreach (char playerChar in playerChars)

    {

     if ((field[0, 0] == playerChar) && (field[0, 1] == playerChar) & (field[0, 2] == playerChar)

     || (field[1, 0] == playerChar) && (field[1, 1] == playerChar) & (field[1, 2] == playerChar)

     || (field[2, 0] == playerChar) && (field[2, 1] == playerChar) & (field[2, 2] == playerChar)

     || (field[0, 0] == playerChar) && (field[1, 0] == playerChar) & (field[2, 0] == playerChar)

     || (field[0, 1] == playerChar) && (field[1, 1] == playerChar) & (field[2, 1] == playerChar)

     || (field[0, 2] == playerChar) && (field[1, 2] == playerChar) & (field[2, 2] == playerChar)

     || (field[0, 0] == playerChar) && (field[1, 1] == playerChar) & (field[2, 2] == playerChar)

     || (field[0, 2] == playerChar) && (field[1, 1] == playerChar) & (field[2, 0] == playerChar))

     {

      Console.ForegroundColor = ConsoleColor.Green;

      if (playerChar == 'O')

       Console.WriteLine("Player1 won");

      else if (playerChar == 'X')

       Console.WriteLine("Player2 won");

      //

      //

      Console.ResetColor();

      Console.WriteLine("PRESS ANY KEY TO CONTINUR");

      Console.ReadKey();

      ResetField();

      break;

     }

     else if (turns == 10)

     {

      Console.WriteLine("Draw");

      Console.ReadKey();

      ResetField();

     }

    }

    #endregion

    //

    //

    //Get input and check if input is correct

    #region

    do

    {

     bool Catch = false;

     Console.Write("Player{0} choose your field : ", player);

     try

     {

      input = Convert.ToInt16(Console.ReadLine());

     }

     catch

     {

      Console.ForegroundColor = ConsoleColor.Red;

      Console.WriteLine("Incorrect input Please enter a number");

      Console.ResetColor();

      Catch = true;

     }

     if (Catch)

     {

      inputCorrect = false;

     }

     else

     {

      inputCorrect = CheckInput(input, inputCorrect);

     }

    } while (!inputCorrect);

    #endregion

    //





   } while (true);

  }


  //Methods

  #region

  //This methods sets the structure for playfield

  public static void SetField()

  {

   Console.Clear();

   Console.WriteLine(" | | ");

   Console.WriteLine(" {0} | {1} | {2} ", field[0, 0], field[0, 1], field[0, 2]);

   Console.WriteLine(" _____|_____|_____");

   Console.WriteLine(" | | ");

   Console.WriteLine(" {0} | {1} | {2} ", field[1, 0], field[1, 1], field[1, 2]);

   Console.WriteLine(" _____|_____|_____");

   Console.WriteLine(" | | ");

   Console.WriteLine(" {0} | {1} | {2} ", field[2, 0], field[2, 1], field[2, 2]);

   Console.WriteLine(" | | ");

   turns++;

  }

  //

  //

  //

  //

  //

  //This method decide to enter x or o based on player variable and at what position

  public static void EnterXorO(char fieldign, int input)

  {

   switch (input)

   {

    case 1:

     field[0, 0] = fieldign; break;

    case 2:

     field[0, 1] = fieldign; break;

    case 3:

     field[0, 2] = fieldign; break;

    case 4:

     field[1, 0] = fieldign; break;

    case 5:

     field[1, 1] = fieldign; break;

    case 6:

     field[1, 2] = fieldign; break;

    case 7:

     field[2, 0] = fieldign; break;

    case 8:

     field[2, 1] = fieldign; break;

    case 9:

     field[2, 2] = fieldign; break;

   }

  }

  //

  //

  //

  //

  //

  //this method checks if field is already occupied

  public static bool CheckInput(int input, bool inputCorrect)

  {

   if ((input == 1) && (field[0, 0] == '1'))

    inputCorrect = true;

   else if (input == 2 && field[0, 1] == '2')

    inputCorrect = true;

   else if (input == 3 && field[0, 2] == '3')

    inputCorrect = true;

   else if (input == 4 && field[1, 0] == '4')

    inputCorrect = true;

   else if (input == 5 && field[1, 1] == '5')

    inputCorrect = true;

   else if (input == 6 && field[1, 2] == '6')

    inputCorrect = true;

   else if (input == 7 && field[2, 0] == '7')

    inputCorrect = true;

   else if (input == 8 && field[2, 1] == '8')

    inputCorrect = true;

   else if (input == 9 && field[2, 2] == '9')

    inputCorrect = true;

   else

   {

    Console.ForegroundColor = ConsoleColor.Red;

    Console.WriteLine("Field occupied enter other number");

    inputCorrect = false;

    Console.ResetColor();

   }

   return inputCorrect;

  }

  //

  //

  //

  //

  //

  //The method starts a new game

  static void ResetField()

  {

   char[,] fieldInitial =

   {

    {'1','2','3'},

    {'4','5','6'},

    {'7','8','9'}

   };

   //

   field = fieldInitial;

   SetField();

   turns = 0;

  }

  //

  //

  #endregion

 }

}

Comments

Popular Posts