Skip to content

@duncanspumpkin /gist:5425314
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Morse code game. Meant to look like a TI-83 thats why screen is so small.
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();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.