Thursday, 5 September 2013

Tic Tac Toe ( console ) game

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tictactest
{
    class Tictactest
    {

        int row,col;
        char player1;
    char player2;
char[,] arr = new char[3, 3];  

 int choice()
 {
  Console.WriteLine("Enter player 1 choice(O/X)");
  player1 = char.Parse(Console.ReadLine());
        Console.WriteLine("Enter player 2 choice(O/X)");
        player2 = char.Parse(Console.ReadLine());
    return 0;
 }

 int inputplayer1()
    {
     Console.WriteLine("player 1 turn");
      Console.WriteLine("enter the row ");
      row=Int32.Parse(Console.ReadLine());
      Console.WriteLine("enter the column ");
      col=Int32.Parse(Console.ReadLine());
   arr[row,col]=player1;
    
   return 0;
    }

 int inputplayer2()
    {
     Console.WriteLine("player 2 turn");
      Console.WriteLine("enter the row ");
      row = Int32.Parse(Console.ReadLine());
      Console.WriteLine("enter the column ");
      col = Int32.Parse(Console.ReadLine());
   arr[row,col]=player2;
    
   return 0;
    }

 int checkplayer( )
 {
     if (arr[0, 0] == 'o' && arr[1, 1] == 'o' && arr[2, 2] == 'o' ||
arr[0, 2] == 'o' && arr[1, 1] == 'o' && arr[2, 0] == 'o' ||
arr[0, 0] == 'o' && arr[0, 1] == 'o' && arr[0, 2] == 'o' ||
arr[1, 0] == 'o' && arr[1, 1] == 'o' && arr[1, 2] == 'o' ||
arr[2, 0] == 'o' && arr[2, 1] == 'o' && arr[2, 2] == 'o' ||
arr[0, 0] == 'o' && arr[1, 0] == 'o' && arr[2, 0] == 'o' ||
arr[0, 1] == 'o' && arr[1, 1] == 'o' && arr[2, 1] == 'o' ||
arr[0, 2] == 'o' && arr[1, 2] == 'o' && arr[2, 2] == 'o')
  {
  Console.WriteLine("BINGO :) Player 1 won ");

  Environment.Exit(0);

  }
  else
  {
      if (arr[0, 0] == 'x' && arr[1, 1] == 'x' && arr[2, 2] == 'x' ||
      arr[0, 2] == 'x' && arr[1, 1] == 'x' && arr[2, 0] == 'x' ||
      arr[0, 0] == 'x' && arr[0, 1] == 'x' && arr[0, 2] == 'x' ||
      arr[1, 0] == 'x' && arr[1, 1] == 'x' && arr[1, 2] == 'x' ||
      arr[2, 0] == 'x' && arr[2, 1] == 'x' && arr[2, 2] == 'x' ||
      arr[0, 0] == 'x' && arr[1, 0] == 'x' && arr[2, 0] == 'x' ||
      arr[0, 1] == 'x' && arr[1, 1] == 'x' && arr[2, 1] == 'x' ||
                arr[0, 2] == 'x' && arr[1, 2] == 'x' && arr[2, 2] == 'x')
      {
          Console.WriteLine(" BINGO :) Player 2 won ");
        
          Environment.Exit(0);
      } 
 
  }

 return 0;
 }


 void display()
 {
 for(row=0;row<3;row++)
 {
 for(col=0;col<3;col++)
 {
 Console.Write(arr[row,col]);
    
 }
 Console.WriteLine("\n");
 }
 }


        static void Main(string[] args)
        {

            Tictactest tic=new Tictactest();
        tic.choice();

      
    tic.inputplayer1();
    Console.Clear();
            tic.display();
   
    tic.inputplayer2();
    Console.Clear();
            tic.display();
   
tic.inputplayer1();
Console.Clear();
            tic.display();
  
            tic.inputplayer2();
    Console.Clear();
            tic.display();

            tic.checkplayer();
            tic.inputplayer1();
         Console.Clear();
            tic.display();
            tic.checkplayer();
            tic.inputplayer2();
            Console.Clear();
            tic.display();
            tic.checkplayer();
            tic.inputplayer1();
            Console.Clear();
            tic.display();
            tic.checkplayer();      
            tic.inputplayer2();         
            Console.Clear();
            tic.display();
            tic.checkplayer();
            tic.inputplayer1();        
            Console.Clear();
            tic.display();

            tic.checkplayer();
                      tic.inputplayer2();
            Console.Clear();
            tic.display();

    }
    }

        }
  

No comments:

Post a Comment