Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Boardmessingaround
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             List<Char> board = new List<char>();
  10.             Console.WriteLine("Hello World!");
  11.             String s = Console.ReadLine();
  12.             foreach(char c in s)
  13.             {
  14.                 board.Add(c);
  15.             }
  16.             foreach (char c in board)
  17.             {
  18.                 Console.Write(c);
  19.             }
  20.             Console.Write('\n');
  21.             //char input = 's';
  22.             String sol = Solve(board);
  23.             if(sol==null||sol == "?")
  24.             {
  25.                 Console.WriteLine("No Solution");
  26.             }
  27.             else
  28.             {
  29.                 Console.WriteLine(sol);
  30.  
  31.             }
  32.             //Console.WriteLine(sol);
  33.            /* while(input != -1)
  34.             {
  35.                 Console.WriteLine("Board state");
  36.                 PrintBoard(board);
  37.                 input = Convert.ToInt32(Console.ReadLine());
  38.                 Remove(ref board, input);
  39.  
  40.             }
  41.             */
  42.             Console.Read();
  43.         }
  44.         public static void PrintBoard(List<char> board)
  45.         {
  46.             foreach (char c in board)
  47.             {
  48.                 Console.Write(c);
  49.             }
  50.             Console.Write('\n');
  51.  
  52.         }
  53.         public static void Remove(ref List<char> board, int i)
  54.         {
  55.             int len = board.Count;
  56.             //error checking
  57.             if(i>= len||i<0|| board[i]== '0')
  58.             {
  59.                 Console.WriteLine("Invalid choice");
  60.                 Console.WriteLine(i + " , " + len + " " + board[i]);
  61.             }
  62.             else
  63.             {
  64.                 board[i] = '.';
  65.                 if (i + 1 <len)
  66.                 {
  67.                     board[i + 1] = Flip(board[i + 1]);
  68.                 }
  69.                 if (i -1 >= 0)
  70.                 {
  71.                     board[i - 1] = Flip(board[i - 1]);
  72.                 }
  73.             }
  74.         }
  75.         public static char Flip(char f)
  76.         {
  77.             if (f == '.') return '.';
  78.             if (f == '0')
  79.             {
  80.                 return '1';
  81.             }
  82.             else
  83.             {
  84.                 return '0';
  85.             }
  86.         }
  87.  
  88.  
  89.         public static String Solve(List<char> board)
  90.         {
  91.             //check valid moves//if solved.
  92.             if (Solved(board))
  93.             {
  94.                 return "Solved!";
  95.             }
  96.             List<int> choices = ValidMoves(board);
  97.             String solution ="";
  98.             //loop through each choice
  99.             foreach(int i in choices)
  100.             {
  101.                 Console.WriteLine(choices.Count);
  102.                 List<char> temp = new List<char>();
  103.                 temp.AddRange(board);
  104.                 Remove(ref temp, i);
  105.                 if (temp.Equals(board))
  106.                 {
  107.                     Console.WriteLine("RIP");
  108.                 }
  109.                 solution = i +' ' + Solve(temp);
  110.                 if (solution[solution.Length-1] == '!') return solution;
  111.             }
  112.             //recurssion! Try each solution in order then call previous solution
  113.             //figure out possible starts.
  114.             //No solution found
  115.             return "?";
  116.         }
  117.  
  118.         //Returns a list of valid moves
  119.         public static List<int> ValidMoves(List<char> board)
  120.         {
  121.             List<int> moves = new List<int>();
  122.             for(int i = 0; i<board.Count; i++)
  123.             {
  124.                 if (board[i] == '1')
  125.                 {
  126.                     moves.Add(i);
  127.                 }
  128.             }
  129.             return moves;
  130.         }
  131.  
  132.         //Checks if the board is solved.
  133.         public static bool Solved(List<char> board)
  134.         {
  135.             bool solved = true;
  136.             foreach(char c in board)
  137.             {
  138.                 if (c != '.') solved = false;
  139.             }
  140.             return solved;
  141.         }
  142.        
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement