Advertisement
Dimitar46

3. Memory game

May 31st, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading.Channels;
  6.  
  7. namespace _3._Memory_game
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<string> sequence = Console.ReadLine().Split().ToList();
  14.            
  15.             int moves = 0;
  16.             while (true)
  17.  
  18.             {
  19.                 string command = Console.ReadLine();
  20.                 if (command == "end")
  21.                 {
  22.                     break;
  23.                 }
  24.                 moves++;
  25.                 int[] indexes = command.Split().Select(int.Parse).ToArray();
  26.                 int firstIndex = indexes[0];
  27.                 int secondIndex = indexes[1];
  28.                 if (indexes[0] < indexes[1])
  29.                 {
  30.                     firstIndex = indexes[1];
  31.                     secondIndex = indexes[0];
  32.                 }
  33.  
  34.                 if (firstIndex == secondIndex || firstIndex < 0 || secondIndex < 0 || secondIndex >= sequence.Count ||
  35.                     firstIndex >= sequence.Count)
  36.                 {
  37.                     Console.WriteLine("Invalid input! Adding additional elements to the board");
  38.                     //add additional elements
  39.                     int middle = sequence.Count / 2;
  40.                     sequence.Insert(middle, $"-{moves}a");
  41.                     sequence.Insert(middle + 1, $"-{moves}a");
  42.                 }
  43.                 else
  44.                 {
  45.  
  46.                     if (sequence[firstIndex] == sequence[secondIndex])
  47.                     {
  48.                         Console.WriteLine($"Congrats! You have found matching elements - {sequence[firstIndex]}!");
  49.  
  50.                         sequence.RemoveAt(firstIndex);
  51.                         sequence.RemoveAt(secondIndex);
  52.  
  53.                     }
  54.                     else
  55.                     {
  56.                         Console.WriteLine("Try again!");
  57.                         continue;
  58.                     }
  59.                     if (sequence.Count == 0)
  60.                     {
  61.                         Console.WriteLine($"You have won in {moves} turns!");
  62.                         return;
  63.                     }
  64.  
  65.                 }
  66.             }
  67.  
  68.  
  69.  
  70.             if (sequence.Count > 0)
  71.             {
  72.                 Console.WriteLine("Sorry you lose :(");
  73.                 Console.WriteLine(string.Join(" ", sequence));
  74.             }
  75.            
  76.            
  77.         }
  78.     }
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement