public
Created

Morse code game. Meant to look like a TI-83 thats why screen is so small.

  • Download Gist
gistfile1.cs
C#
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
using System;
using System.Threading;
using System.Linq;
using System.Collections.Generic;
 
public class SimplePool
{
private int direction;
private int position = 4;
private int score = 0;
private char currLetter = 'A';
private char incorrectLetter = ' ';
private List<List<char>> board = new List<List<char>>();
 
public void ReadKeyThread()
{
while(true){
switch(Console.ReadKey(true).Key){
case ConsoleKey.A:
if (direction < 3 ) Interlocked.Increment(ref direction);
break;
case ConsoleKey.D:
if (direction > -3) Interlocked.Decrement(ref direction);
break;
}
}
}
public void TimerEvent()
{
position -= direction;
if ( direction > 0 ) Interlocked.Decrement(ref direction);
if ( direction < 0 ) Interlocked.Increment(ref direction);
if ( position < 0 ) position = 0;
if ( position > 5*2) position = 5*2;
Dictionary<char,string> morse = new Dictionary<char,string>(){
{'A',".- "}, {'B',"-..."}, {'C',"-.-."}, {'D',"-.. "},
{'E',". "}, {'F',"..-."}, {'G',"--. "}, {'H',"...."},
{'I',".. "}, {'J',".---"}, {'K',"-.- "}, {'L',".-.."},
{'M',"-- "}, {'N',"-. "}, {'O',"--- "}, {'P',".--."},
{'Q',"--.-"}, {'R',".-. "}, {'S',"... "}, {'T',"- "},
{'U',"..- "}, {'V',"...-"}, {'W',".-- "}, {'X',"-..-"},
{'Y',"-.--"}, {'Z',"--.."}, {'1',".----"}, {'2',"..---"},
{'3',"...--"}, {'4',"....-"}, {'5',"....."}, {'6',"-...."},
{'7',"--..."}, {'8',"---.."}, {'9',"----."}, {'0',"-----"},
{' '," "}, {'\n',"\n"}, {'\r',"\r"}, {'\0'," "}};
 
Console.Clear();
board.ForEach( bl =>
{
bl.ForEach( c => Console.Write( morse[c] + ' '));
Console.WriteLine();
});
Console.WriteLine(new string(' ',position) + "____");
Console.Write("Morse: " + currLetter);
Console.Write(" Pts: " + score );
//if ( incorrectLetter != ' ' ) Console.WriteLine( incorrectLetter + ": " + morse[incorrectLetter] );
}
 
public void ShiftDown()
{
if ( board[board.Count-1][0] != ' ' ){
if ( board[board.Count-1][((position+1)/5)] == currLetter){
score++;
incorrectLetter = ' ';
}
else incorrectLetter = currLetter;
NewTurn();
}
List<char> newLine = new List<char>((new string(' ',3)).ToCharArray());
board.RemoveAt(board.Count-1);
board.Insert(0,newLine);
}
 
public void NewTurn()
{
Random rand = new Random();
currLetter = (char)('A' + rand.Next(26));
List<char> newLine = new List<char>();
newLine.Add(currLetter);
for( int i = 0; i < 2; ++i) newLine.Add((char)('A'+rand.Next(26)));
board.Clear();
board.Add(new List<char>(newLine.OrderBy( a => rand.Next())));
newLine.Clear();
for(int i = 0; i < 5; ++i) board.Add(new List<char>((new string(' ',3)).ToCharArray()));
 
}
 
public static void Main(string[] args)
{
SimplePool sp = new SimplePool();
Console.SetWindowSize(16,8);
sp.NewTurn();
Console.WriteLine("Lambdas and threads");
Thread th = new Thread(sp.ReadKeyThread);
th.Start();
int loops = 0;
while(true){
Thread.Sleep(100);
sp.TimerEvent();
loops++;
if ( loops > 5 )
{
loops = 0;
sp.ShiftDown();
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.