Sei sulla pagina 1di 8

Part B:

using System;
using
System.Collections.Generic;
using System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace Assignmen2014_15
{
class Program
{
const int MAXNUMBERS = 3;
static void Main(string[] args)
{
int[] theNumbers = new int[MAXNUMBERS];
int chosenOption = 0; bool quit = false;
InitialiseNumbers(theNumbers);
while (quit == false)
{
DisplayHeader();
DisplayMenu();
chosenOption = ReadNumber("Please choose an option: ");
quit = ProcessMenu(chosenOption, theNumbers);
Console.Clear();
}
}
static void InitialiseNumbers(int[] numbers)
{
for (int index = 0; index < MAXNUMBERS; index++)
{
numbers[index] = 0;
}
}
static void DisplayHeader()
{

WriteText("***************************************************************************
****", 0, 0); // Top left hand corner of screen is x = 0, y = 0;
WriteText("* This application is designed to allow you to choose numbers
*", 0, 1); // Next line down is x = 0, y = 1, etc WriteText("* and finds the biggest
and the smallest value *", 0, 2);

WriteText("***************************************************************************
****", 0, 3);
}
static void DisplayMenu()
{
WriteText("Select an option", 20, 8); // Display menu at at x = 20, y = 8
WriteText("1. Enter the numbers", 20, 9);
WriteText("2.Find the smallest", 20, 10);
WriteText("3. Find the largest", 20, 11);
WriteText("4. Display all numbers", 20, 12);
WriteText("5. Quit", 20, 13);
}
static void WriteText(string text, int x, int y)
{
Console.CursorLeft = x;
Console.CursorTop = y;
Console.Write(text);
}
static int ReadNumber(string prompt)
{
string text; int
number;
WriteText(prompt, 20, 14);
text = Console.ReadLine();
number = int.Parse(text);
ClearText(20, 14, prompt.Length + text.Length); // Clear the text at this
line
return number;
}
static void ClearText(int x, int y, int length)
{
// Write space ' ' characters starting at x, y for 'length' times
WriteText(new String(' ', length), x, y);
}
static bool ProcessMenu(int option, int[] numbers)
{
bool quit = false;
switch (option)
{
case 1:
GetNumbers(numbers);
break;
case 2:
WriteText(string.Format("The smallest value is {0}",
FindSmallest(numbers)), 20, 15);
Console.ReadKey(); // Pause
break;
case 3:
WriteText(string.Format("The largest value is {0}",
FindLargest(numbers)), 20, 15);
Console.ReadKey(); // Pause
break;
case 4:
quit = IsQuitting();
break;
case 5:
quit = IsQuitting();
break;
default:
WriteText("Unknown option value entered", 20, 15);
Console.ReadKey(); // Pause break;
break;
}
return quit;
}
static void GetNumbers(int[] numbers)
{
for (int index = 0; index < MAXNUMBERS; index++)
{
numbers[index] = ReadNumber("Enter number: ");
}
}
static int FindSmallest(int[] numbers)
{
int smallest = numbers[0];
for (int index = 0; index < MAXNUMBERS - 1; index++) // <-- subtract 1
{
if (numbers[index + 1] < smallest)
{
smallest = numbers[index + 1];
}
}
return smallest;
}
static int FindLargest(int[] numbers)
{
int largest = numbers[0];
for (int index = 0; index < MAXNUMBERS - 1; index++) // <-- subtract 1
{
if (numbers[index + 1] > largest)
{
largest = numbers[index + 1];
}
}
return largest;
}
static bool IsQuitting()
{
string response; bool
quit = false;

WriteText("Do you really wish to quit? ", 20, 13);


response = Console.ReadLine();
if (response == "YES")
{
quit = true;
}
return quit;
}
}
}

Part C
Exercise 1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NamePriceQuantity
{
class Program
{
static void Main()
{
int N;
double P;
string name;

Console.WriteLine("Type your customer name:");


name = Convert.ToString(Console.ReadLine());

Console.WriteLine("Enter number of products:");


N = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter price for products £:");
P = Convert.ToDouble(Console.ReadLine());

double T = N * P;
Console.WriteLine("{0}, you have to pay £ {1}", name, T);
}
}
}

Exercise 2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Area
{
class Area
{
static void Main(string[] args)
{
Console.WriteLine("Length of rectangle:");
decimal Lenght = decimal.Parse(Console.ReadLine());
Console.WriteLine("Width of rectangle:");
decimal Width = decimal.Parse(Console.ReadLine());

decimal area1 = (Width - 3) * 7;


decimal area2 = (Length * Width) - area1 – (2*2);
Console.WriteLine("The area of your shape is {0}", area2);
}
}
}

Exercise 3:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SlotMachine
{
class Program
{

static void Main(string[] args)


{
Random randNumGen = new Random();
int credits = 50;

Console.WriteLine("******************************************");
Console.WriteLine("------------------------------------------");
Console.WriteLine("Welcome to SILVIA-DIANA's Slot Machine!");
Console.WriteLine("------------------------------------------");
Console.WriteLine("Press Spacebar to Roll the slot machine!");
Console.WriteLine("------------------------------------------");
Console.WriteLine("-------Credits Available: " + credits +
"--------------");
Console.WriteLine("******************************************");

ConsoleKeyInfo keyInfo = Console.ReadKey(true);

while (credits > 0)


{

Console.WriteLine("******************************************");
Console.WriteLine("---££----Credits Available: " + credits + "-----£
£-----");
Console.WriteLine("******************************************");

if (keyInfo.Key == ConsoleKey.Spacebar)
{
credits--;
string slot1;
string slot2;
string slot3;

int randNum1 = randNumGen.Next(10000, 20000);


int randNum2 = randNumGen.Next(30000, 40000);
int randNum3 = randNumGen.Next(50000, 60000);

Console.WriteLine("First Random Number: " + randNum1);


Console.WriteLine("Second Random Number: " + randNum2);
Console.WriteLine("Third Random Number: " + randNum3);

int modR1 = randNum1 % 32;


int modR2 = randNum2 % 32;
int modR3 = randNum3 % 32;

Console.WriteLine("------------------------------------------");
Console.WriteLine("1st Remainder Left over: " + modR1);
Console.WriteLine("2nd Remainder Left over: " + modR2);
Console.WriteLine("3rd Remainder Left over: " + modR3);
Console.WriteLine("------------------------------------------");

string[] slots = new string[32];

for (int i = 0; i < 8; i++)


slots[i] = "BANANA";
for (int i = 8; i < 15; i++)
slots[i] = "APPLE";
for (int i = 15; i < 21; i++)
slots[i] = "CHERRY";
for (int i = 21; i < 26; i++)
slots[i] = "BELL";
for (int i = 26; i < 30; i++)
slots[i] = "ROSE";
for (int i = 30; i < 32; i++)
slots[i] = "Lucky 7";

slot1 = slots[modR1];
slot2 = slots[modR2];
slot3 = slots[modR3];

Console.WriteLine("Slot 1: " + slot1);


Console.WriteLine("Slot 2: " + slot2);
Console.WriteLine("Slot 3: " + slot3);
if ((slot1 == "BANANA") && (slot2 == "BANANA") && (slot3 ==
"BANANA"))
{
credits = credits + 5;
Console.WriteLine("YOU WIN!! ££££££££ 5 CREDITS ££££££££");
}

else if ((slot1 == "APPLE") && (slot2 == "APPLE") && (slot3 ==


"APLE"))
{
credits = credits + 10;

Console.WriteLine("YOU WIN!! ££££££££ 10 CREDITS ££££££££");


}

else if ((slot1 == “CHERRY") && (slot2 == "CHERRY") && (slot3 ==


"CHERRY"))
{

credits = credits + 15;

Console.WriteLine("YOU WIN!! ££££££££ 15 CREDITS ££££££££");


}

else if ((slot1 == "BELL") && (slot2 == "BELL") && (slot3 ==


"BELL"))
{
credits = credits + 20;

Console.WriteLine("YOU WIN!! ££££££££ 20 CREDITS ££££££££");


}

else if ((slot1 == "ROSE") && (slot2 == "ROSE") && (slot3 ==


"ROSE"))
{
credits = credits + 40;

Console.WriteLine("YOU WIN!! ££££££££ 40 CREDITS ££££££££");


}

else if ((slot1 == "LUCKY 7") && (slot2 == "LUCKY 7") && (slot3 ==
"LUCKY 7"))
{

credits = credits + 100;

Console.WriteLine("JACKPOT!! ££££££££ 100 CREDITS ££££££££");


}

else
{

Console.WriteLine("BUST!! ##### NO CREDITS ######");


}

keyInfo = Console.ReadKey(true);

}
else
{
Console.WriteLine("WRONG KEY!... Press Spacebar");
keyInfo = Console.ReadKey(true);
}
}
}
}
}

Exercise 4:
using System;
using System.Collections.Generic;

namespace SnakeAndLadder
{
class Program
{
private static Dictionary<int, int> snl = new Dictionary<int, int>() {
{4, 14},
{9, 31},
{17, 7},
{20, 38},
{28, 84},
{40, 59},
{51, 67},
{54, 34},
{62, 19},
{63, 81},
{64, 60},
{71, 91},
{87, 24},
{93, 73},
{95, 75},
{99, 78},
};
private static Random rand = new Random();
private const bool sixesThrowAgain = true;

static int Turn(int player, int square)


{
while (true)
{
int roll = rand.Next(1, 6);
Console.Write("Player {0}, on square {0}, rolls a {0}", player,
square, roll);
if (square + roll > 100)
{
Console.WriteLine(" but cannot move.");
}
else
{
square += roll;
Console.WriteLine(" and moves to square {0}", square);
if (square == 100) return 100;
int next = square;
if (snl.ContainsKey(square))
{
next = snl[square];
}
if (square < next)
{
Console.WriteLine("Yay! Landed on a ladder. Climb up to {0}.",
next);
if (next == 100) return 100;
square = next;
}
else if (square > next)
{
Console.WriteLine("Oops! Landed on a snake. Slither down to
{0}.", next);
}
}
if (roll < 6 || !sixesThrowAgain) return square;
Console.WriteLine("Rolled a 6 so roll again.");
}
}
static void Main(string[] args)
{
// three players atarting on square one
int[] players = { 1, 1, 1 };
while (true)
{
for (int i = 0; i < players.Length; i++)
{
int ns = Turn(i + 1, players[i]);
if (ns == 100)
{
Console.WriteLine("Player {0} wins!", i + 1);
return;
}
players[i] = ns;
Console.WriteLine();
}
}
}
}
}

Potrebbero piacerti anche