Sign in to follow this  

C-sharp--just started,Comment On Code?

This topic is 3110 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I started to learn c# yesterday. Its not bad, but I definitely know the way I am going about programming this "games", is not good. The problem I am have is not reading the stream correctly, or should I say efficiently. Anyway, help me improve my program,i.e comment on this code. Note: its fairly simple so I did not comment. Although if some section is query, ask please.
using System;

namespace GAME
{
    #region NUMBER GUESSING GAME
    public class Number_Guess
    {
        public static void HiLow()
        {
            int guessNum = -1;

            string gNum;

            Random rand = new Random();

            int goal = rand.Next(1, 25);

            int numOfGuess = 0;

            Console.WriteLine("Your object it to guess the random number from 1 to 25\n\n");

            do
            {

                Console.WriteLine("What will you guess   :  ");

                gNum = Console.ReadLine();

                guessNum = int.Parse(gNum);

                if (guessNum == goal)
                {
                    Console.WriteLine("You guessed correct!\n\n Press a key to continue ...");
                    string x = Console.ReadLine();
                    
                    break;
                }

                else if (guessNum > goal)
                {
                    Console.WriteLine("You guessed too high\n");
                    ++numOfGuess;

                }

                else
                {
                    Console.WriteLine("You guessed too low\n\n");
                    ++numOfGuess;
                }

            } while (guessNum != goal);


        }

    }
    #endregion
    #region TIC-TAC-TOE
    public class BOARD_GAME
    {
        const int GAME_STRT = 0;
        const int GAME_OVER = 1;
        const int RESET = 2;
        

        public static void Tic_Tac_toe()
        {

            Console.Title = "Tic-Tac-Toe";

            //Create a The main GameBoard 3x3
            char[] GameBoard = new char[9]
            {
                '1','2','3',
                '4','5','6',
                '7','8','9'
            };

            //Initialize
            char P1_ID = 'X';
            char Comp_ID = 'O';
            int GameState = GAME_STRT;
            int choice = -1;
            
            Random rand = new Random();

            Console.WriteLine("Would you like to be X or O :  ");

            P1_ID = (char)Console.Read();

            //Dummy reader
            Console.ReadLine();

            if (P1_ID == 'O' || P1_ID == 'o' || P1_ID == '0')
            { Comp_ID = 'X'; P1_ID = 'O'; }
            
            else P1_ID = 'X';


            PrintBrd(GameBoard, -1, P1_ID);
            choice = GetInput(choice);
           

            Console.Clear();
          

            do
            {
              
           
                 PrintBrd(GameBoard, choice, P1_ID);
                

                //Comp Turn                
                choice = rand.Next(1, 9);
                //Check if Valid
                while (!(ValidMove(P1_ID, Comp_ID, GameBoard, choice)))
                    choice = rand.Next(1, 9);

                PrintBrd(GameBoard, choice, Comp_ID);

                //Check winner, if any
                switch (CheckWinner(GameBoard, P1_ID, Comp_ID))
                {

                    case 'P': Console.WriteLine("\nCongrats, You beat the stupid computer\n"); GameState = GAME_OVER; goto case '-';
                    case 'C': Console.WriteLine("\nHahaha, how the Hell did you loose???\n"); GameState = GAME_OVER; goto case '-';
                    case 'D': Console.WriteLine("\nIts a Draw Game!\n\n"); GameState = GAME_OVER; goto case '-';
                    case '-':
                        char op = 'y';
                        Console.WriteLine("\nPlaye Again? (y/n) :  ");
                        op = (char)Console.Read();

                        if (op == 'y' || op == 'Y')
                        {
                            GameState = RESET; break;
                        }
                        else GameState = GAME_OVER;

                        break;

                    default: break;
                }


                if (GameState == GAME_STRT)
                {

                    //Get user Input
                    choice = GetInput(choice);
                    //Check for valid move
                    while (!(ValidMove(P1_ID, Comp_ID, GameBoard, choice)))
                        choice = GetInput(choice);

                    PrintBrd(GameBoard, choice, P1_ID);

                    //Check winner, if any
                    switch (CheckWinner(GameBoard, P1_ID, Comp_ID))
                    {

                        case 'P': Console.WriteLine("\n\nCongrats, You beat the stupid computer\n"); GameState = GAME_OVER; goto case '-';
                        case 'C': Console.WriteLine("\n\nHahaha, how the Hell did you loose???\n"); GameState = GAME_OVER; goto case '-';
                        case 'D': Console.WriteLine("\n\nIts a Draw Game!\n\n"); GameState = GAME_OVER; goto case '-';
                        case '-':
                            char op = 'y';
                            Console.WriteLine("\nPlaye Again? (y/n) :  ");
                            op = (char)Console.Read();

                            if (op == 'y' || op == 'Y')
                            {
                                GameState = RESET; break;
                            }
                            else GameState = GAME_OVER;

                            break;

                        default: break;
                    }

                }

                if (GameState == RESET)
                {
                    GameState = resetFunc(GameBoard, GameState);
                    PrintBrd(GameBoard, -1, Comp_ID);
                    Console.ReadLine();
                    choice = GetInput(choice);
                }

            } while (GameState != GAME_OVER);

        }

        #region void PrintBrdPrint(char[],int,char) //Prints Board
        static public void PrintBrd(char[] Brd, int ElemChoice, char Obj_ID)
        {
            Console.Clear();

            //Check for validity
            if ((ElemChoice < 1 || ElemChoice > 9) && ElemChoice != -1)
            {
                Console.WriteLine("INVALID ROW CHOICE! ITS BEGIN IGNORED THIS TIME\n\n");
                return;
            }

            else
            {
                if (ElemChoice != -1)
                    Brd[ElemChoice - 1] = Obj_ID;
            }

            //Print board
            for (int i = 0; i < 9; i += 3)
            {
                if (i != 0 && (i % 3 == 0))
                    Console.WriteLine('\n');

                Console.WriteLine("{0}  |  {1}  |  {2}", Brd[i], Brd[i + 1], Brd[i + 2]);
            }
        }
        #endregion
        #region ValidMove(char,char,char[],int) : Check For Valid Move
        public static bool ValidMove(char ID, char ID2, char[] GameBrd, int choice)
        {
            if (choice < 1 || choice > 9)
            {
                Console.WriteLine("\nInvalid Dimension\n");
                return false;
            }

            if (GameBrd[choice - 1] == ID ||
                GameBrd[choice - 1] == ID2)
            {
                Console.WriteLine("\nInvalid Dimension\n");
                return false;
            }

            return true;
        }
        #endregion
        #region GetInput(int) // Gets user Input
        static public int GetInput(int ch)
        {
            Console.WriteLine("\n\nWhere do you want to place your Move :  ");
            ch = Console.Read();
            ch -= '0';
            //Dummy read
            Console.ReadLine();
            return ch;
        }
        #endregion
        #region CheckWinner(char [] GameBrd, char Obj_ID,char Obj2_ID) // checks for winner
        static public char CheckWinner(char[] Gbrd, char P_ID, char C_ID)
        {

            char winner = '*';  // represent neither                       

            for (int i = 0; i < 9; i++)
            {
                if (i % 3 == 0) //check horizontal win
                {
                    if (Gbrd[i] == P_ID && Gbrd[i + 1] == P_ID && Gbrd[i + 2] == P_ID)
                    { winner = 'P'; return winner; }
                    else if (Gbrd[i] == C_ID && Gbrd[i + 1] == C_ID && Gbrd[i + 2] == C_ID)
                    { winner = 'C'; return winner; } 

                }

                if (i < 3) //check vertical win
                {
                    if (Gbrd[i] == P_ID && Gbrd[i + 3] == P_ID && Gbrd[i + 6] == P_ID)
                    { winner = 'P'; return winner; }
                    else if (Gbrd[i] == C_ID && Gbrd[i + 3] == C_ID && Gbrd[i + 6] == C_ID)
                    { winner = 'C'; return winner; }
                }

                if (i == 0 ) //check diagnol win
                {
                    if (Gbrd[i] == P_ID && Gbrd[i + 4] == P_ID && Gbrd[i + 8] == P_ID)
                    { winner = 'P'; return winner; }
                    else if (Gbrd[i] == C_ID && Gbrd[i + 4] == C_ID && Gbrd[i + 8] == C_ID)
                    { winner = 'C'; return winner; }
                }
                else if (i == 2)
                {
                    if (Gbrd[i] == P_ID && Gbrd[i + 2] == P_ID && Gbrd[i + 4] == P_ID)
                    { winner = 'P'; return winner; }
                    else if (Gbrd[i] == C_ID && Gbrd[i + 2] == C_ID && Gbrd[i + 4] == C_ID)
                    { winner = 'C'; return winner; }
                }

            }

            //Check for draw game
            int cnt = 0;

            for (int i = 0; i < 9; i++)
            {
                if (Gbrd[i] == P_ID || Gbrd[i] == C_ID)
                    ++cnt;
                else break;
            }

            if (cnt == 9)
            {
                winner = 'D'; 
                return winner;
            }
                 
            //no winner if reached
            return winner; 
           

        }
        #endregion
        #region Reset Game
       static int resetFunc(char[] brd,int state)
        {
            for (char i = '1'; i <= '9'; i++)
                brd[i-'1'] = i; 

            return (state = GAME_STRT);
        }
        #endregion
    }
    #endregion

}


class mainProg
{
    static void Main()
    {
        char p = 'y';

        string whichGame = "1";

        do
        {
            Console.Clear(); 

            Console.WriteLine("Which Game do you want to play : \n");
            Console.WriteLine("1) Number Guessing game\n");
            Console.WriteLine("2) Tic-tac-toe Game\n");

            whichGame = Console.ReadLine();

            Console.Clear();

            switch (int.Parse(whichGame))
            {
                case 1: GAME.Number_Guess.HiLow(); break;
                case 2: GAME.BOARD_GAME.Tic_Tac_toe(); break;
            }

            //Dummy read
            Console.ReadLine();

        } while (p == 'y');
    }
}


[Edited by - tnutty on June 7, 2009 10:13:26 AM]

Share this post


Link to post
Share on other sites

This topic is 3110 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this