Click here to Skip to main content
15,886,030 members
Home / Discussions / C#
   

C#

 
QuestionC# checking for spaces Pin
rachel_m6-Oct-12 8:44
rachel_m6-Oct-12 8:44 
AnswerRe: C# checking for spaces Pin
Dave Kreskowiak6-Oct-12 10:06
mveDave Kreskowiak6-Oct-12 10:06 
AnswerRe: C# checking for spaces Pin
Richard Andrew x646-Oct-12 10:14
professionalRichard Andrew x646-Oct-12 10:14 
AnswerRe: C# checking for spaces Pin
DaveyM696-Oct-12 11:42
professionalDaveyM696-Oct-12 11:42 
QuestionsqlQuery Pin
Member 94545636-Oct-12 1:14
Member 94545636-Oct-12 1:14 
AnswerRe: sqlQuery Pin
Thomas Daniels6-Oct-12 2:24
mentorThomas Daniels6-Oct-12 2:24 
AnswerRe: sqlQuery Pin
OriginalGriff6-Oct-12 4:49
mveOriginalGriff6-Oct-12 4:49 
Questiontic toe game not working as expected,please help? Pin
Member 94790245-Oct-12 22:59
Member 94790245-Oct-12 22:59 
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace tictoegame
{

    public class gamestart
    {
        int functionalcount, computernumber,usernumber;
        char[] box = new char[9];
        static int j=6;
        public gamestart()
        {

            
            for(int k=0;k<9;k++)
            {
                box[k] = ' ';
            }
            functionalcount = 0;
            Random rd = new Random();

          while(true)
          {
                computernumber = rd.Next(0, 8);
                if (box[computernumber]!='x'&& box[computernumber]!='X'&&box[computernumber]!='o'&& box[computernumber]!='O')
                {
                    insertvalue(computernumber,Program.computersymbol);
                    break;
                    
                }
          }
            
            functionalcount++;
            userturn();
        }
        public void position()
        {
            int i=1;
            Console.CursorLeft=i;
            Console.CursorTop=j;
            j++;
        }
        public void userturn()
        {

            if (functionalcount != 9)
            {
                bool flag1 = checkwin(Program.usersymbol);
                if (flag1)
                {
                    position();
                    Console.WriteLine("you won");
                    Thread.Sleep(3000);
                    Environment.Exit(0);
                }
                else
                {
                    while (true)
                    {
                        position();
                        Console.WriteLine("enter the number for symbol");
                        usernumber = Convert.ToInt32(Console.ReadLine());
                        if (box[usernumber] != 'x' || box[usernumber] != 'X' && box[usernumber] != 'o' || box[usernumber] != 'O')
                        {
                            insertvalue(usernumber, Program.usersymbol);
                            functionalcount++;
                            break;
                        }
                    }
                    computerturn();

                }

            }
        }
        public void computerturn()
        {
            int count=0,i;
            if (functionalcount == 2)
            {
                Random rd = new Random();
                while (true)
                {
                    computernumber = rd.Next(0, 8);
                    if((box[computernumber] != 'x' || box[computernumber] != 'X') && (box[computernumber] != 'o' || box[computernumber] != 'O'))
                    {
                        insertvalue(computernumber,Program.computersymbol);
                        functionalcount++;
                        break;
                    }

                }
                userturn();
            }
            if(functionalcount==4||functionalcount==6)
            {
                bool flag=checkthirdposition(Program.computersymbol,ref count);
                bool flag1 = checkwin(Program.computersymbol);
                if(flag1)
                {
                    position();
                    Console.WriteLine("computer wins");
                    Thread.Sleep(3000);
                    Environment.Exit(0);
                }
                bool flag2=checkthirdposition(Program.usersymbol,ref count);
                if(flag2)
                {
                    box[count]=Program.computersymbol;
                    insertvalue(count,Program.computersymbol);
                    functionalcount++;
                    userturn();
                }
                else
                {
                    Random rd = new Random();
                while (true)
                {
                    computernumber = rd.Next(0, 8);
                    if((box[computernumber] != 'x' || box[computernumber] != 'X' )&&( box[computernumber] != 'o' || box[computernumber] != 'O'))
                    {
                        insertvalue(computernumber,Program.computersymbol);
                        functionalcount++;
                        break;
                    }

                }
                userturn();
                }
            }
            if(functionalcount==8)
            {
                int con;
                for(i=0;i<9;i++)
                {
                    if((box[i]!='x'||box[i]!='X')&&(box[i]!='o'||box[i]!='O'))
                    {
                         con=i;
                        box[con]=Program.computersymbol;
                        insertvalue(con,Program.computersymbol);
                    }
                }
                functionalcount++;
                position();
                Console.WriteLine("draw");
                userturn();

            }
                
            }
        public bool checkthirdposition(char ch, ref int x)
        {
            if (checkpos(1, 2, 0, ch) || checkpos(3, 6, 0, ch) || checkpos(4, 8, 0, ch))
            {
                if (ch == Program.computersymbol)
                {
                    insertvalue(0, ch);
                    x = 0;
                    return true;
                }
                else
                    if(ch==Program.usersymbol)
                {
                    x=0;
                    return true;
                }
            }
            if (checkpos(0,2,1, ch) || checkpos(7,4,1,ch))
            {
                if (ch == Program.computersymbol)
                {
                    insertvalue(1, ch);
                    x = 1;
                    return true;
                }
                else
                    if(ch==Program.usersymbol)
                {
                    x = 1;
                    return true;
                }

            }
            if(checkpos(0,1,2,ch) || checkpos(8,5,2,ch)||checkpos(6,4,2,ch))

            {
                if (ch == Program.computersymbol)
                {
                    insertvalue(2, ch);
                    x = 2;
                    return true;
                }
                else
                    if(ch==Program.usersymbol)
                {
                    x = 2;
                    return true;
                }

            }
              if(checkpos(0,6,3,ch) || checkpos(5,4,3,ch))
              {
                if (ch == Program.computersymbol)
                {
                    insertvalue(3, ch);
                    x = 3;
                    return true;
                }
                else
                    if(ch==Program.usersymbol)
                {
                    x = 3;
                    return true;
                }

            }
              if(checkpos(0,8,4,ch) || checkpos(2,6,4,ch)||checkpos(1,7,4,ch)||checkpos(3,5,4,ch))
              {
                if (ch == Program.computersymbol)
                {
                    insertvalue(4, ch);
                    x = 4;
                    return true;
                }
                else
                    if(ch==Program.usersymbol)
                {
                    x = 4;
                    return true;
                }

            }
             if(checkpos(2,8,5,ch) || checkpos(3,4,5,ch))
              {
                if (ch == Program.computersymbol)
                {
                    insertvalue(5, ch);
                    x = 5;
                    return true;
                }
                else
                    if(ch==Program.usersymbol)
                {
                    x = 5;
                    return true;
                }

            }
             if(checkpos(0,3,6,ch) || checkpos(2,4,6,ch)||checkpos(8,7,6,ch))
              {
                if (ch == Program.computersymbol)
                {
                    insertvalue(6, ch);
                    x = 6;
                    return true;
                }
                else
                    if(ch==Program.usersymbol)
                {
                    x = 6;
                    return true;
                }

            }
             if(checkpos(1,4,7,ch) || checkpos(6,8,7,ch))
              {
                if (ch == Program.computersymbol)
                {
                    insertvalue(7, ch);
                    x = 7;
                    return true;
                }
                else
                    if(ch==Program.usersymbol)
                {
                    x = 7;
                    return true;
                }

            }
              if(checkpos(2,5,8,ch) || checkpos(6,7,8,ch)||checkpos(0,4,8,ch))
              {
                if (ch == Program.computersymbol)
                {
                    insertvalue(8, ch);
                    x = 8;
                    return true;
                }
                else
                    if(ch==Program.usersymbol)
                {
                    x = 8;
                    return true;
                }

            }
              return false;


        }

        public bool checkpos(int i, int j, int k, char ch1)
        {
            if ((box[i] == box[j] && box[i] == ch1) && (box[k] != 'x' || box[k] != 'X' || box[k] != 'o' || box[k] != 'O'))
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        public bool checkwin(char ch)
        {
            if((box[0] == box[1] && box[1]==box[2] && box[0]==ch)||(box[3]==box[4]&&box[4]==box[5]&&box[3]==ch)||(box[6]==box[7]&&box[7]==box[8]&&box[6]==ch)||((box[0]==box[3]&&box[3]==box[6]&&box[0]==ch)||
                (box[1]==box[4]&&box[4]==box[7]&&box[1]==ch)||(box[2]==box[5]&&box[5]==box[8]&&box[2]==ch)||(box[0]==box[4]&&box[4]==box[8]&&box[0]==ch)||(box[6]==box[4]&&box[4]==box[2])&&box[6]==ch))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public void insertvalue(int x, char ch)
        {
            box[x] = ch;
            switch (x)
            {
                case 0:
                    Console.SetCursorPosition(1, 0);
                    Console.Write(ch);
                    break;
                case 1:
                    Console.SetCursorPosition(5, 0);
                    Console.Write(ch);
                    break;
                case 2:
                    Console.SetCursorPosition(9, 0);
                    Console.Write(ch);
                    break;
                case 3:
                    Console.SetCursorPosition(1,2);
                    Console.Write(ch);
                    break;
                case 4:
                    Console.SetCursorPosition(5, 2);
                    Console.Write(ch);
                    break;
                case 5:
                    Console.SetCursorPosition(9, 2);
                    Console.Write(ch);
                    break;
                case 6:
                    Console.SetCursorPosition(1,4);
                    Console.Write(ch);
                    break;
                case 7:
                    Console.SetCursorPosition(5, 4);
                    Console.Write(ch);
                    break;
                case 8:
                    Console.SetCursorPosition(9,4);
                    Console.Write(ch);
                    break;
            }
        }
    }


           
          

           
           
       



   

    class Program
    {
         static public char usersymbol;
        static public char computersymbol;
        static  void Main(string[] args)
        {
            
      
           
            
            
            Console.WriteLine(" to enter the symbol in any box number it from 1 to 9");
            picksymbol();
            Thread.Sleep(3000);
            Console.Clear();
            display();
            resets();
            
            gamestart gs = new gamestart();
            
            Console.ReadLine();
        }

        public static void display()
        {
            Console.WriteLine(" " + " " +" "+ "|" + " " + " " +" "+ "|");
            Console.WriteLine("_" + "_" + "_" + "|" + "_" + "_" + "_" + "|"+"_"+"_"+"_");
            Console.WriteLine(" " + " " + " " + "|" + " " + " " + " " + "|");
            Console.WriteLine("_" + "_" + "_" + "|" + "_" + "_" + "_" + "|" + "_" + "_" + "_");
            Console.WriteLine(" " + " " + " " + "|" + " " + " " + " " + "|");
            Console.WriteLine(" " + " " + " " + "|" + " " + " " + " " + "|");
        }

        public static void picksymbol()
        {
            while (true)
            {
                Console.WriteLine("enter the symbol-- either x or o");
                usersymbol = Convert.ToChar(Console.ReadLine());
                if (usersymbol == 'x' || usersymbol == 'X' || usersymbol == 'o' || usersymbol == 'O')
                {
                    break;
                }
            }
            if (usersymbol == 'x' || usersymbol == 'X')
            {
                Console.WriteLine(" you picked {0}", usersymbol);
                computersymbol = 'o';
                Console.WriteLine(" the computer symbol is {0}", computersymbol);
            }
            else
            {
                Console.WriteLine(" you picked {0}", usersymbol);
                computersymbol = 'x';
                Console.WriteLine(" the computer symbol is {0}", computersymbol);
            }
        }

        static void resets()
        {
            int i,j;
            for (i = 1, j = 0; j <= 4; i = i + 4)
            {
                Console.SetCursorPosition(i, j);
                Console.Write(' ');
                if (i == 9)
                {
                    i = -3;
                    j = j + 2;
                }

            }
            Console.WriteLine();
            Console.WriteLine();
            
        }



        
    }
}

AnswerRe: tic toe game not working as expected,please help? Pin
Pete O'Hanlon6-Oct-12 1:51
mvePete O'Hanlon6-Oct-12 1:51 
AnswerRe: tic toe game not working as expected,please help? Pin
John Orendt7-Oct-12 0:36
John Orendt7-Oct-12 0:36 
QuestionHow to create a message sender which will use your mobile no and credit but no use of your mobile? Pin
zain_zone5-Oct-12 22:11
zain_zone5-Oct-12 22:11 
AnswerRe: How to create a message sender which will use your mobile no and credit but no use of your mobile? Pin
Eddy Vluggen6-Oct-12 3:45
professionalEddy Vluggen6-Oct-12 3:45 
GeneralRe: How to create a message sender which will use your mobile no and credit but no use of your mobile? Pin
OriginalGriff6-Oct-12 5:00
mveOriginalGriff6-Oct-12 5:00 
QuestionNeed Help UNICODE Pin
Nazim Iqbal5-Oct-12 13:55
Nazim Iqbal5-Oct-12 13:55 
AnswerRe: Need Help UNICODE Pin
Richard MacCutchan5-Oct-12 22:29
mveRichard MacCutchan5-Oct-12 22:29 
QuestionC# search directory paths Pin
rachel_m5-Oct-12 10:43
rachel_m5-Oct-12 10:43 
AnswerRe: C# search directory paths Pin
Eddy Vluggen5-Oct-12 10:49
professionalEddy Vluggen5-Oct-12 10:49 
AnswerRe: C# search directory paths Pin
Richard MacCutchan5-Oct-12 22:28
mveRichard MacCutchan5-Oct-12 22:28 
GeneralRe: C# search directory paths Pin
rachel_m6-Oct-12 9:01
rachel_m6-Oct-12 9:01 
GeneralRe: C# search directory paths Pin
Richard MacCutchan6-Oct-12 21:19
mveRichard MacCutchan6-Oct-12 21:19 
Generalpls help learn c# need someone to guide me thru Pin
becky suinner5-Oct-12 9:42
becky suinner5-Oct-12 9:42 
GeneralRe: pls help learn c# need someone to guide me thru Pin
Eddy Vluggen5-Oct-12 9:46
professionalEddy Vluggen5-Oct-12 9:46 
GeneralRe: pls help learn c# need someone to guide me thru Pin
becky suinner5-Oct-12 9:58
becky suinner5-Oct-12 9:58 
GeneralRe: pls help learn c# need someone to guide me thru Pin
Eddy Vluggen5-Oct-12 10:03
professionalEddy Vluggen5-Oct-12 10:03 
GeneralRe: pls help learn c# need someone to guide me thru Pin
becky suinner5-Oct-12 10:01
becky suinner5-Oct-12 10:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.