SkyNet Number Bot

Overview

This is your final project for Getting Started with C#. The goal of this project is to create a game where the computer guesses your number.

Designing

When developing a program there are two general philosophies that we follow to turn an idea into reality, WATERFALL and AGILE. Watch the videos below to learn more.

Waterfall

Waterfall Process

Waterfall is a design philosphy that complements large static (non-changing) projects. It focuses on a top-down sequentail order, that requires all previous steps to be completed before moving on.

  • Analysis – Look at the requirements for the program or application. Research the different methodogies of meeting these requirements and decided on the best approach for the project.
  • Design – Design the programs structure, usually using flow charts. This helps you identify any possible logical errors in the overall structure of your program.
  • Implement (Code) – You start coding the program and features for the project.
  • Testing – You test the program in a variety of senarios, in this step you attempt to break your program to see how durable it is.
  • Maintenance – Once a program is released you need to maintain it, in this module we will never reach this step, but in production programs this is very important to ensure that your users have a smooth, bug-free experience.

Agile

Agile Process

Agile is a design process that focuses on fast interactions of the development. They are quick to adapt to users’ needs and have a generally short development time compared to Waterfall. Agile works on the idea of sprints, where the team gets together and develops the product in a short time looking for the minimum viable product. Generally, in an Agile product’s lifecycle, multiple sprints will happen before the product is released and through its maintenance cycle.

The Flowchart (Waterfall: Design)

Flow charts or Unified Modeling Langauge (UML) diagrams are a great way to get a top-down overview of structure and flow of your program. These diagrams can help identify issues and can act as a blueprint when developing your program. For more information on UML click the following link.

The Basics of the Code (Waterfall: Implement)

The Variables

class Program {

	static int min = 1;
	static int max = 100;
	static int currentNumber;
	static int numberOfGuess = 0;
	
	static void Main(string[] args) {
		// ...
	}
	
}
  • Line 3 – The min integer variable is the minimum possible value the number being guessed could be.
  • Line 4 – The max integer variable is the maximum possible value the number being guessed could be.
  • Line 5 – currentNumber is the variable that stores the randomly generated number that the computer will use to guess the users number. Remember that we use the Random class to generated random numbers in C#.
  • Line 6 – numberOfGuess is the number we increment (increase by 1) each time the computer makes a guess. We can use this int variable to help determine if the computer or user wins.

Method: StartDialogue()

Think of this like your main menu. Use it to confirm the user wants to play the game and/or give some basic information about the game.

class Program {

	//...
	
	static void StartDialogue() {
		string reponse;
		Console.WriteLine("Do you want to play a game?");
		
		reponse = Console.ReadLine();
		reponse = reponse.ToLower();
		
		if (reponse == "yes") {
			Console.WriteLine("Great let's get started!");
			StartGame();
		} else if(reponse == "no") {
			Console.WriteLine("Oh.... too bad... bye... ");
			System.Threading.Thread.Sleep(1500);
			return;
		} else {
			Console.WriteLine("A simple [yes] or [no] will do.");
			StartDialogue();
		}
		
	}
	
	
}
  • Line 6 – We declare the reposne variable of the string type to store the users reponses.
  • Line 10 – The reponse.ToLower() method makes all the characters in the string lower-case, this means we do not have to worry about case sensitivy when validating our responses.
  • Line 14 – The StartGame() method is invoked (called). We have yet to construct this method, but it will be used to reset all the varaibles and start a fresh game.
  • Line 17 – The System.Threading.Thread.Sleep(1500) is a builtin method of the console application that will wait for a specific amount of time. In this case it will wait for 1500ms or 1.5 seconds.
  • Line 18 – The keyword return is used to return a value and exit the method, in this case, because we are in a console application we return to the method that invoked StartDialogue() which will be our Main method. Since the Main method has not more code to execute it will close the console application.
  • Line 21 – We invoke the method StartDialogue() (which is the method we are currently in) to restate the instructions because the user entered a response that does not align with our accepted responses.

Method: StartGame()

The StartGame() method is used to reset the variables and give some basic instructions. It is Invoked each time a new game is started.

class Program {

	//...
	
	static void StartGame() {
		Console.WriteLine("Think of a number between " + min + " and " + max + ".");
		System.Threading.Thread.Sleep(500);
		Console.WriteLine("          ...do you have it?");
		System.Threading.Thread.Sleep(1500);
		Console.WriteLine("                   ...good let's go!");
		
		GuessLoop();
	}
	
	
}
  • Not we are just using this method as a basis for instructions, it is a good idea to seperate your methods based on their functions, making sure not to overload a single method (give it too many functions).

Method: GuessLoop()

The GuessLoop() is the most vital method of the program.

class Program {

	//...
	
	static void GuessLoop() {
		numberOfGuess++;
		Random r = new Random();
		currentNumber = r.Next(min, max);
		
		string reposnse;
		System.Threading.Thread.Sleep(1000);
		
		Console.WriteLine("         ...is your number " + currentNumber + "?");
		Console.WriteLine("Tell me if your number is [higher] or [lower]...");
		System.Threading.Thread.Sleep(50);
		Console.WriteLine("           ...or did I [win]?");
		
		reponse = console.ReadLine();
		reponse = reponse.ToLower();
		
		if (reponse == "higher") {
			Console.WriteLine("Oh, of course, it is higher...");
			min = currentNumber + 1;
			GuessLoop();
		} else if (reponse == "lower") {
			Console.WriteLine("Make sense, lower... got it.");
			max = currentNumber - 1;
			GuessLoop();
		} else if (reponse == "win") {
			Win();
		} else {
			Console.WriteLine("It is pretty simple, type [higher], [lower], or [win]. Let us try again.");
			numberOfGuess--;
			GuessLoop();
		}
	}
	
	
}
  • Line 6 – We incremenet the numberOfGuess (increase its value by 1) to keep track of the number of guesses the computer has made.
  • Line 8 – At this point the r.Random() should be getting familiar, but the two variables inside, min and max may need a little explaining. The min is the minimum number the user’s number code be, while the max is the maximum number the user’s number could be. What we are saying with by Invoking this method is, select a random number between the minimum possible value and the maximum possible value the user’s number could be.
  • Line 23 – This is one of the hardest concepts to understand. But since the computer guess a number and the player said that number is higher we know that the current number and every number below it could never be the user’s number. In code, we represent that by setting the min variable to the current computer guess (currentNumber) plus one or min = currentNumber + 1;
  • Line 27 – Much like line 23, this can be a hard concept to understand. In this senario the user has said that their number is lower than the current guess by the computer. This means we know that the current number and all numbers above it can not be the user’s number. We represent this in code by setting the max variable to the current guess minus one or max = currentNumber – 1;
  • Line 30 – When the computer wins we Invoke a the Win method, which has yet to be constructed.
  • Line 33 – If the user enters an unknown reponse we need to restate the instructions again, but since at the start of the GuessLoop() we increment the numberofGuess by 1, we need to decrement it here to make sure we only count actual responses.

Method: Win()

The Win() method is used to let the user know the computer has won the game and reset the variables if the user wants to start a new game.

class Program {

	//...
	
	static void Win() {
		Console.WriteLine("Some text to let the user know the computer has won.");

		min = 1;
		max = 100;
		numberOfGuess = 0;
		
		StartDialogue();
	}
	
	
}
  • Line 6 – Add some text to let the user know they have lost and the computer has won. Be creative!
  • Line 8 – Reset the min variable for the next game.
  • Line 9 – Reset the max variable for the next game.
  • Line 10 – Reset the numberOfGuess variable for the next game.
  • Line 12 – Start a new game. You could add check to make sure the user wants to play the game.

Taking It Further

Congratulations on completing your first major project. At this point, I would recommend making this project your own; you are allowed to research new features and implement them. A large part of learning a new coding language is playing around with code, exploring built-in methods, and trying new things. If you get stuck, you can look up your specific issue using Google or developer-to-developer services like StackOverflow. Of course, feel free to ask your teacher as well. Next, we will be starting Unity development. Do not forget to check your finish project for bugs, see below (Waterfall: Testing stage) for more information.

Quality Control (Waterfall: Testing)

This is your first major project in computer science. It is essential to understand that your final deliverable will be marked to an entry-level software developer’s standards, excluding possible error scenarios (and how to handle errors) that we have yet to cover. Before handing it in, check the following elements.

  • Syntax – Make sure all your {} are correctly spaced and organized. In C#, this is not to help the compiler but to make sure the code is easy to understand for all programmers.
  • Syntax Errors – Never hand in a product with syntax errors. If a program has a syntax error, it will not run or throws an error when you attempt to run it. Often they are shown in your IDE (Visual Studio) as red lines.
  • Logical Errors – Checking for logical errors can be very hard. For this reason, it is a good idea to have a plan (the flow chart) for more extensive or new projects. Still, you should have tested your product enough to know that it works as intended.
  • Comments – Comments in code are amazing; they help you remember what your thought process was when you come back to a project after some time; also they help communicate the methodology of your code to other programmers (or your teacher marking the assignment *HINT*).
  • Spelling and Grammar – As crazy as it sounds, you need to be grammatically correct and have clear instructions for your users. Sometimes products fail, not because they are poorly coded, but the users struggle to understand how to use it. Check your strings and instructions carefully to make sure they are easy to understand. Having another person test your product can also be very useful.

Deliverable

Once you have completed your final project hand in the .CS file in Google Classroom. As with all deliverables it is expected that you watched the companion video, if you have forgotten the instructions for the deliverable you have find them at the end of the video or click here.

Assignments in this module are handed in via Google Classroom. Make sure you are signed into your PLSD account and click the link below:

Mark as Complete
chevron_right