Created
November 20, 2015 14:06
-
-
Save Pranz/e4f5e1ead2c8ed6a3b7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace Bingo | |
{ | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
Console.WriteLine ("Bingo!\n\n" + | |
"Detta program tillåter dig att spela bingo. Tryck retur för att börja spela"); | |
Console.ReadLine (); | |
List<uint> score = new List<uint> (); | |
bool keepRunning = true; | |
while (keepRunning) { | |
uint currentScore = runBingo (); | |
score.Add (currentScore); | |
Console.WriteLine ("Din poäng var: " + currentScore.ToString ()); | |
Console.WriteLine ("Spela igen? [y/n]"); | |
keepRunning = promptForYesNo (); | |
} | |
// Skriv ut resultaten | |
Console.WriteLine("Dina poäng var:"); | |
foreach (uint number in score) { | |
Console.WriteLine("\t" + number.ToString()); | |
} | |
} | |
public static uint runBingo() { | |
uint[] bingoCard = new uint[10]; | |
uint[] keyCard = new uint[7]; | |
Console.WriteLine ("Nu ska du skriva in 10 tal mellan 1 och 25.\n" + | |
"Talen får inte upprepas"); | |
// Fyll bingobrickan med tal | |
uint i = 0; | |
while (i < 10) { | |
Console.WriteLine ("Skriv in tal nummer " + (i + 1).ToString ()); | |
bool didParse = uint.TryParse(Console.ReadLine(), out bingoCard[i]); | |
if (!didParse) { | |
Console.WriteLine ("Du skrev inte in ett giltigt tal"); | |
} else if (bingoCard [i] == 0 || bingoCard [i] > 25) { | |
Console.WriteLine ("Du får bara välja tal mellan 1 och 25"); | |
} | |
else if (arrayContainsPreviousNumber(bingoCard, i)) { | |
Console.WriteLine("Du har redan valt det talet"); | |
} | |
else { | |
i++; | |
} | |
} | |
// Generera slumpade tal | |
Random rng = new Random(); | |
i = 0; | |
while(i < 7) { | |
keyCard [i] = (uint)rng.Next (1,26); | |
if (!arrayContainsPreviousNumber (keyCard, i)) { | |
i++; | |
} | |
} | |
Console.WriteLine ("\nNu kommer vi dra kulorna. Tryck på retur för att fortsätta.\n"); | |
Console.ReadLine(); | |
// Poängsätt | |
uint currentScore = 0; | |
foreach (uint key in keyCard) { | |
bool currentKeyIsMatched = false; | |
foreach (uint number in bingoCard) { | |
if (key == number) { | |
currentScore++; | |
currentKeyIsMatched = true; | |
} | |
} | |
char suffix = currentKeyIsMatched ? | |
'\u2713': //Unicodesymbol för avbockning | |
'\u2717'; //Unicodesymbol för kryss | |
Console.Write ("Slumpar fram ett nytt tal"); | |
writeDots(3); | |
Console.WriteLine (" " + key.ToString () + "! \t" + suffix.ToString ()); | |
System.Threading.Thread.Sleep (1000); // för att användaren ska hinna läsa resultatet. | |
} | |
return currentScore; | |
} | |
public static bool promptForYesNo() { | |
String answer = Console.ReadLine().ToLower(); | |
if (answer.Length > 0) { | |
if (answer[0] == 'y') { | |
return true; | |
} | |
if (answer [0] == 'n') { | |
return false; | |
} | |
} | |
Console.WriteLine ("Förstod inte ditt svar. Använd bara [y/n]"); | |
return promptForYesNo (); | |
} | |
public static bool arrayContainsPreviousNumber(uint[] arr, uint last_index) { | |
for (int i = 0; i < last_index; i++) { | |
if (arr [i] == arr [last_index]) { | |
return true; | |
} | |
} | |
return false; | |
} | |
public static void writeDots(uint count) { | |
// Skriver ut punkter med jämna mellanrunm för dramatisk effekt | |
if (count != 0) { | |
System.Threading.Thread.Sleep (300); | |
Console.Write ("."); | |
writeDots (count - 1); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment