-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hangman.cs
89 lines (75 loc) · 2.91 KB
/
Hangman.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SOURCE: https://www.sanfoundry.com/csharp-programs-gaming-hangman/
// AUTHOR: Manish Bhojasia, a technology veteran with 20+ years @ Cisco &
// Wipro, [along with consultancies at IBM, Brocade, Quantum, etc.]
// is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and
// SAN Architect and is passionate about competency development....
// He lives in Bangalore ....
// FILENAME: Hangman.cs
// PURPOSE: C# Program to Create a Hangman Game
// STUDENT: James Hiegel
// DATE: 07 May 2019
// STYLE MODIFICATIONS:
// Extracted displaying the contents of the dictionary into a method.
// Added writelines to output text on console describing what is happening
// FUNCTIONAL MODIFICATIONS:
// Changed program so that it exits once all characters are guessed
/*
* C# Program to Create a HangMan Game
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace JJH
{
class Hangman
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to Hangman!!!!!!!!!!");
// creates array of words
string[] listwords = new string[10];
listwords[0] = "sheep";
listwords[1] = "goat";
listwords[2] = "computer";
listwords[3] = "america";
listwords[4] = "watermelon";
listwords[5] = "icecream";
listwords[6] = "jasmine";
listwords[7] = "pineapple";
listwords[8] = "orange";
listwords[9] = "mango";
// randomly picks a word from the array
Random randGen = new Random();
var idx = randGen.Next(0, 9);
string mysteryWord = listwords[idx];
// creates an array equal the the length of the word to guess
// and fills it with stars
char[] guess = new char[mysteryWord.Length];
for (int p = 0; p < mysteryWord.Length; p++)
guess[p] = '*';
Console.WriteLine(guess);
Console.Write("Please enter your guess: ");
int correct = 0;
// loops until all characters are guessed
while (correct < mysteryWord.Length)
{
// reads the character entered by the user
char.TryParse(Console.ReadLine(), out char playerGuess);
// iterates over the star array
for (int j = 0; j < mysteryWord.Length; j++)
{
// at each index, checks if user entered character exisits
// if so it replaces the star with the character
if (playerGuess == mysteryWord[j])
{
guess[j] = playerGuess;
// increments correct count
correct++;
}
}
Console.WriteLine(guess);
}
}
}
}