Post #10 Arcade

C# has been a wonderful programming language experience. Now I will create a program to not only show the basic features of C# but also to show off it's cool features we talked about in the previous posts. Let us discuss this program.

As the title suggests the best way I have thought of to show off C# is to make an arcade. This arcade will be a basic arcade in the sense that it will have games you can play. In addition it will also have tools that are useful for everyday life. I don't know exactly how long it will be as I will be adding whatever I can manage to get done.

Let's talk structure. The entire program will be run off of one namespace. That namespace is basically just going to be my driver. As namespaces are one of the cool things I want to show off there will be multiple namespaces. Each namespace will be it's own program. The driver will obviously have all programs but is unique because it is the main program. So far I've thought of 3 ideas for my other namespaces. 2 of which are games. These are Hangman and a binary search game which I will create. The other idea is a tool. I decided I wanted to make a basic calculator. Scopes I will pretty much show off everywhere as any code with { } has a scope.

Now I will reveal stage one of this code. I have created the main namespace which I'm using as a menu for the arcade. I have also built the hangman code in another namespace. I set up two other namespaces to give an idea of how this works. This is how far I've gotten. 

using System;

// This is the main namespace
// effectively this is my driver
namespace Arcade
{
    class Program
    {
        // main method will run everything
        static void Main(string[] args)
        {
            // boolean to test if you want to exit the loop
            Boolean playing = true;

            while(playing){
                // This is the main menu
                Console.WriteLine("Welcome to the Arcade!");
                Console.WriteLine("We have games as well as some tools.");
                Console.WriteLine("Please type one of the following things:");
                Console.WriteLine("press 1 and hit enter for playing hangman");
                Console.WriteLine("press q and hit enter to quit program");
                
                // This is creating a variable and getting keyboard input
                String x;
                x = Console.ReadLine();
                if(x == "1"){
                    // creating new hangman game
                    Hangman.Game hangman = new Hangman.Game();
                    hangman.play();
                }else if(x == "2"){
                    // creating new binary search game
                    BinarySeachGame.Game binary = new BinarySeachGame.Game();
                }else if(x == "3"){
                    // creating a calculator with basic functionality
                    Calculator.Math calculator = new Calculator.Math();
                }else if(x == "q"){
                    // This is how you exit the program
                    // This is the only way out
                    // Type q and enter and you won't be stuck in this loop
                    playing = false;
                }
            }
        }
    }
}

// new namespace to use for the hangman game
namespace Hangman
{
    // class is game
    class Game
    {
        // method is play
        public void play(){
            // menu for hangman
            Console.WriteLine("Welcome to hangman");
            Console.WriteLine("This is traditional hangman");
            Console.WriteLine("First player: type the hidden word");

            // Getting the secret word to guess
            String word;
            word = Console.ReadLine();
            word.ToCharArray();
            // creating the current word you have so far
            char[] current = new char[word.Length];
           
            // filling the current with _ to represent letters
            for(int i=0i<current.Lengthi++){
                current[i] = '_';
                Console.Write(current[i] + " ");
            }
            Console.WriteLine();

            // telling other players to guess
            Console.WriteLine("Other players guess");

            // creating variables for the game
            // these include a playing variable to control the next loop
            // creating guesses or lives you have in this case
            // wrong guesses have an array and an index
            Boolean playing = true;
            int guesses = 6;
            char[] wrongGuesses = new char[6];
            int wrong = 0;

            // This is the playing loop
            // It will continue until you win or lose
            while(playing){
                // Creates string and gets keyboard input
                // because of how this works you can type more than one letter but 
// only the first will be counted
                // converts to a char array
                String guess;
                guess = Console.ReadLine();
                guess.ToCharArray();

                // effectively checking if the guess is in the word
                Boolean inword = false;
                for(int i=0i<current.Lengthi++){
                    if(word[i] == guess[0]){
                        current[i] = guess[0];
                        inword = true;
                    }
                }

                // if it is not in the word this happens
                // changing wrong, wrongguesses, guesses
                if(inword == false){
                    guesses -= 1;
                    wrongGuesses[wrong] = guess[0];
                    wrong += 1;
                }

                // prints your current word
                for(int i=0i<current.Lengthi++){
                    Console.Write(current[i] + " ");
                }
                Console.WriteLine();

                // prints both wrong guesses and guess left
                Console.WriteLine("Wrong Ansers: ");
                for(int i=0i<wrongGuesses.Lengthi++){
                    Console.Write(wrongGuesses[i] + " ");
                }
                Console.WriteLine();
                Console.Write("guesses left: " + guesses);

                // checking for losing and printing if you lose
                if(guesses == 0){
                    Console.WriteLine("You Lose");
                    playing = false;
                }

                // checking for winning
                Boolean win = true;
                for(int i=0i<current.Lengthi++){
                    if(current[i] != word[i]){
                        win = false;
                    }
                }

                // if you win this happens
                if(win){
                    Console.WriteLine("You won");
                    playing = false;
                }
            }
        }
    }
}

// will be namespace for an interesting game I decided to make
// essentially you try to guess a number and see if you can do in less than binary 
// search
// example 1-10 it takes you seven guesses
// binary search takes 3(not real number)
// binary search wins
namespace BinarySeachGame
{
    class Game
    {
        
    }
}

// will be namespace for a calculator
// very basic +-*/%
// will probably only take two numbers(undecided)
namespace Calculator
{
    class Math
    {
        
    }
}

// There might be more than these three programs(depends what I get to)

This is the beginning of the end of the posts for C#.

I used all my books, Visual Studio Code, and all knowledge I have learned to write this post.

Resources:

Microsoft Visual C# .NET by Mickey Williams

C# collection 
Programming Basics For Absolute Beginners
A Detailed Approach To Practical Coding
Advanced Features & Programming Techniques
by Nathan Clark

The C# Programming Language Fourth Edition By Anders Hejlsberg, Mads Torgersen, Scott Wiltamuth, and Peter Golde

Comments

Popular posts from this blog

Post #11 Complete Arcade

Post #2 Hello World!