Post #11 Complete Arcade
Hi everyone! As I said in my last post the final program is completed with what I got to. I made an arcade. It has Hangman, a Binary Search game where you see if you are faster than the computer, and a calculator that has basic operational functionality but can also probably compete with other calculators. As far as explaining the code I'm not going to do that this time because it's commented to the best of my ability and I'd like to explain that elsewhere. These posts have been fun and you are all excellent readers but unfortunately it will come to an end. Feel free to comment if you do read this. I hope these posts have given you a basic understanding of C#. The language is a lot more complex than I actually got to. Understanding the .Net Framework completely would take a lot more time than I have. Seriously I'm not sure I would be able to sleep. I will probably build more programs and learn more about that in the summer. Anyway here is the complete final program.
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 2 and hit enter for playing the binary search game");
Console.WriteLine("press 3 and hit enter for calculator");
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();
binary.play();
}else if(x == "3"){
// creating a calculator with basic functionality
Calculator.Math calculator = new Calculator.Math();
calculator.calculator();
}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=0; i<current.Length; i++){
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=0; i<current.Length; i++){
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=0; i<current.Length; i++){
Console.Write(current[i] + " ");
}
Console.WriteLine();
// prints both wrong guesses and guess left
Console.WriteLine("Wrong Ansers: ");
for(int i=0; i<wrongGuesses.Length; i++){
Console.Write(wrongGuesses[i] + " ");
}
Console.WriteLine();
Console.Write("guesses left: " + guesses);
Console.WriteLine();
// checking for losing and printing if you lose
if(guesses == 0){
Console.WriteLine("You Lose");
Console.WriteLine("the word was " + word);
playing = false;
}
// checking for winning
Boolean win = true;
for(int i=0; i<current.Length; i++){
if(current[i] != word[i]){
win = false;
}
}
// if you win this happens
if(win){
Console.WriteLine("You won");
Console.WriteLine("the word was " + word);
playing = false;
}
}
}
}
}
// namespace for the Binary Search Game
namespace BinarySeachGame
{
class Game
{
public void play(){
// menu for Binary Search game
Console.WriteLine("Welcome to the Binary Search Game");
Console.WriteLine("This is where you see if you can beat binary search to find the hidden number");
Console.WriteLine("First you will type in a max");
Console.WriteLine("Then a random number will be generated");
Console.WriteLine("You will guess first until you get the number");
Console.WriteLine("Then the computer will guess the number");
Console.WriteLine("At the end it the number of guesses will be compared to see if you beat the computer by taking less guesses");
// user must give a max
Console.WriteLine("please provide a number to be the maximum");
String m = Console.ReadLine();
int max = Convert.ToInt32(m);
int top = max;
// number generated by the max the user gave
// max plus 1 so that the random number can be the max
Random rand = new Random();
int num = rand.Next(max+1);
// player code
// sets up player count
int count = 0;
int min = 0;
// while player hasn't guessed
String g;
int guess = -1;
while(guess != num){
// player guesses
Console.WriteLine("guess the number between " + min + " and " + max);
Console.WriteLine("min and max will change to help you guess");
g = Console.ReadLine();
guess = Convert.ToInt32(g);
// checks first guess and all guesses after
if(guess == num){
Console.WriteLine("the number was " + guess);
count++;
}else if(guess > num){
Console.WriteLine("the number is less than " + guess);
count++;
max = guess;
}else if(guess < num){
Console.WriteLine("the number is greater than " + guess);
count++;
min = guess;
}
}
// computer code
// basically copy of binary search from previous code
// sets up and runs binary search with count in addition
int binaryCount = 0;
int bottom = 0;
int middle = top + bottom;
middle /= 2;
Console.WriteLine("bottom: " + bottom);
Console.WriteLine("middle: " + middle);
Console.WriteLine("top: " + top);
Console.WriteLine();
while(middle != num){
if(num < middle){
top = middle;
middle = top + bottom;
middle /= 2;
Console.WriteLine("bottom: " + bottom);
Console.WriteLine("middle: " + middle);
Console.WriteLine("top: " + top);
Console.WriteLine();
binaryCount++;
}else if(num > middle){
bottom = middle;
middle = top + bottom;
middle /= 2;
Console.WriteLine("bottom: " + bottom);
Console.WriteLine("middle: " + middle);
Console.WriteLine("top: " + top);
Console.WriteLine();
binaryCount++;
}
}
if(middle == num){
Console.WriteLine("number was " + middle);
binaryCount++;
}
if(count > binaryCount){
Console.WriteLine("You won by taking less guesses to find the number");
}else if(count < binaryCount){
Console.WriteLine("You lost by taking more guesses to find the number");
}else{
Console.WriteLine("You are the computer");
Console.WriteLine("You took the same number of guesses as the computer");
Console.WriteLine("There for a name thee Number Machine");
}
}
}
}
// namespace for calculator
namespace Calculator
{
class Math
{
public void calculator(){
Console.WriteLine("Welcome to Calculator");
Console.WriteLine("This is where you can do math");
Console.WriteLine("Basic function +-*/%");
Console.WriteLine("type in the number you would like to start with");
String n = Console.ReadLine();
int num = Convert.ToInt32(n);
int cont = 2;
while(cont == 2){
Console.WriteLine("Your number you typed was " + num);
Console.WriteLine("type 1 to continue or 2 to enter the number again");
String c = Console.ReadLine();
cont = Convert.ToInt32(c);
if(cont == 1){
Console.WriteLine("Let's get started");
}else if(cont == 2){
Console.WriteLine("type in the number you would like to start with");
n = Console.ReadLine();
num = Convert.ToInt32(n);
}
}
int con = 0;
int num2;
while(con != 6){
Console.WriteLine("current number is " + num);
Console.WriteLine("press 1 and enter to add");
Console.WriteLine("press 2 and enter to subtract");
Console.WriteLine("press 3 and enter to multiply");
Console.WriteLine("press 4 and enter to divide");
Console.WriteLine("press 5 and enter to do mod math");
Console.WriteLine("press 6 and enter to exit");
Console.WriteLine("type the operation you would like to do");
String co = Console.ReadLine();
con = Convert.ToInt32(co);
if(con == 1){
Console.WriteLine("press 1 again and enter to Add");
Console.WriteLine("press 0 and enter to go back");
co = Console.ReadLine();
con = Convert.ToInt32(co);
if(con == 1){
num2 = secondNum();
num = add(num, num2);
}else if(con == 0){
}
}else if(con == 2){
Console.WriteLine("press 2 again and enter to Subtract");
Console.WriteLine("press 0 and enter to go back");
co = Console.ReadLine();
con = Convert.ToInt32(co);
if(con == 2){
num2 = secondNum();
num = subtract(num, num2);
}else if(con == 0){
}
}else if(con == 3){
Console.WriteLine("press 3 again and enter to Multiply");
Console.WriteLine("press 0 and enter to go back");
co = Console.ReadLine();
con = Convert.ToInt32(co);
if(con == 3){
num2 = secondNum();
num = multiply(num, num2);
}else if(con == 0){
}
}else if(con == 4){
Console.WriteLine("press 4 again and enter to Divide");
Console.WriteLine("press 0 and enter to go back");
co = Console.ReadLine();
con = Convert.ToInt32(co);
if(con == 4){
num2 = secondNum();
num = divide(num, num2);
}else if(con == 0){
}
}else if(con == 5){
Console.WriteLine("press 5 again and enter to do Mod Math");
Console.WriteLine("press 0 and enter to go back");
co = Console.ReadLine();
con = Convert.ToInt32(co);
if(con == 5){
num2 = secondNum();
num = mod(num, num2);
}else if(con == 0){
}
}else if(con == 6){
Console.WriteLine("final number is " + num);
}
}
}
public int secondNum(){
Console.WriteLine("type in the second number");
String n2 = Console.ReadLine();
int num2 = Convert.ToInt32(n2);
int cont2 = 2;
while(cont2 == 2){
Console.WriteLine("Your number you typed was " + num2);
Console.WriteLine("type 1 to continue or 2 to enter the number again");
String c2 = Console.ReadLine();
cont2 = Convert.ToInt32(c2);
if(cont2 == 1){
Console.WriteLine("Let's get started");
}else if(cont2 == 2){
Console.WriteLine("type in the number you would like to start with");
n2 = Console.ReadLine();
num2 = Convert.ToInt32(n2);
}
}
return num2;
}
public int add(int num, int num2){
num = num + num2;
return num;
}
public int subtract(int num, int num2){
num = num - num2;
return num;
}
public int multiply(int num, int num2){
num = num * num2;
return num;
}
public int divide(int num, int num2){
num = num / num2;
return num;
}
public int mod(int num, int num2){
num = num % num2;
return num;
}
}
}
// There might be more than these three programs(depends what I get to)
This is the end of the posts.
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
Post a Comment