I want to run my Sudoku checker (written in method CheckSudokuSolution on the bottom) with two sudoku solutions.
i'm not very experienced in passing parameters and although my research cannot do that:
I need to pass to method CheckSudokuSolution first my 1. Sudoku (in Solution1) and then the second (in Solution2).
Should I change something with the Solution1(), Solution2()?
If you have time for explainment, I would be happy. Thanks.
My program:
static void Main(string[] args)
{
CheckSudokuSolution(s);
CheckSudokuSolution(s);
Console.ReadKey(true);
}
static void Solution1()
{
int[,] s = new int[,]
{
{ 4,3,5,2,6,9,7,8,1},
{ 6,8,2,5,7,1,4,9,3},
{ 1,9,7,8,3,4,5,6,2},
{ 8,2,6,1,9,5,3,4,7},
{ 3,7,4,6,8,2,9,1,5},
{ 9,5,1,7,4,3,6,2,8},
{ 5,1,9,3,2,6,8,7,4},
{ 2,4,8,9,5,7,1,3,6},
{ 7,6,3,4,1,8,2,5,9}
};
}
static void Solution2()
{
int[,] s = new int[,]
{
{ 1,5,2,4,8,9,3,7,6},
{ 7,3,9,2,5,6,8,4,1},
{ 4,6,8,3,7,1,2,9,5},
{ 3,8,7,1,2,4,6,5,9},
{ 5,9,1,7,6,3,4,2,8},
{ 2,4,6,8,9,5,7,1,3},
{ 9,1,4,6,3,7,5,8,7},
{ 6,2,5,9,4,8,1,3,7},
{ 8,7,3,5,1,2,9,6,4}
};
}
static void CheckSudokuSolution(int[,] s )
{
// code
}
In Main(), you have no variable named s, so you get an error with what you currently have. I suggest making Solution1 a static variable instead of a function:
static int[,] Solution1 = new int[,]
{
{ 4,3,5,2,6,9,7,8,1},
{ 6,8,2,5,7,1,4,9,3},
{ 1,9,7,8,3,4,5,6,2},
{ 8,2,6,1,9,5,3,4,7},
{ 3,7,4,6,8,2,9,1,5},
{ 9,5,1,7,4,3,6,2,8},
{ 5,1,9,3,2,6,8,7,4},
{ 2,4,8,9,5,7,1,3,6},
{ 7,6,3,4,1,8,2,5,9}
};
Now you can pass it in directly:
CheckSudokuSolution(Solution1);
Similarly for Solution2.
Not sure to get the all point there but you can use class in first place to see your arrays from the main then :
Send your arrays to CheckSudokuSolution().
Check the return value.
Exemple :
static void Main(string[] args)
{
int[,] s1 = CheckSudokuSolution(Solutions.s1);
int[,] s2 = CheckSudokuSolution(Solutions.s2);
// display sudoku s1
int i = 0;
foreach (var item in s1)
{
if (i == 9)
{
Console.WriteLine();
i = 0;
}
Console.Write(item.ToString());
i++;
}
Console.ReadKey(true);
}
public static class Solutions
{
public static int[,] s1 = new int[,]
{
{ 4,3,5,2,6,9,7,8,1},
{ 6,8,2,5,7,1,4,9,3},
{ 1,9,7,8,3,4,5,6,2},
{ 8,2,6,1,9,5,3,4,7},
{ 3,7,4,6,8,2,9,1,5},
{ 9,5,1,7,4,3,6,2,8},
{ 5,1,9,3,2,6,8,7,4},
{ 2,4,8,9,5,7,1,3,6},
{ 7,6,3,4,1,8,2,5,9}
};
public static int[,] s2 = new int[,]
{
{ 1,5,2,4,8,9,3,7,6},
{ 7,3,9,2,5,6,8,4,1},
{ 4,6,8,3,7,1,2,9,5},
{ 3,8,7,1,2,4,6,5,9},
{ 5,9,1,7,6,3,4,2,8},
{ 2,4,6,8,9,5,7,1,3},
{ 9,1,4,6,3,7,5,8,7},
{ 6,2,5,9,4,8,1,3,7},
{ 8,7,3,5,1,2,9,6,4}
};
}
static int[,] CheckSudokuSolution(int[,] s)
{
// Do something with s
return s;
}
}
Related
So here is my code: the idea is that in the input system class we find some data from the user, then listen to this data from the threading class and process it in a new thread, then we return the processed data to the main thread.
It works, but it is slow ... why is this?
I'm new to coding, so hopefully that explains the errors ...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Threading;
public class InputSystem : MonoBehaviour
{
public EventHandler<dataStore> onRequest;
public class dataStore : EventArgs
{
public int[] dataPass;
}
public int[] newData;
void Start()
{
int[] test = new int[] { 6, 6, 6 };
newData = test;
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
onRequest?.Invoke(this, new dataStore { dataPass = newData });
}
}
}
public class StartThread : MonoBehaviour
{
void Start()
{
InputSystem input = GetComponent<InputSystem >();
input.onRequest += findPath;
}
requestData pData;
returnData rData;
public void findPath(object sender, InputSystem.dataStore data)
{
pData = data.dataToPass;
RequestPath(method);
}
public static void RequestPath(Action doThing)
{
ThreadStart thread = delegate
{
doThing();
};
thread.Invoke();
}
public Queue<Action> toRun = new Queue<Action>();
public void method()
{
PathAlg test = new PathAlg();
Action toQueue = () =>
{
requestData data = pData;
rData.data = test.method(data);
};
lock (toRun)
{
toRun.Enqueue(toQueue);
}
}
void Update()
{
while (toRun.Count > 0)
{
lock (toRun)
{
Action runThis = toRun.Dequeue();
runThis();
foreach(int n in rData)
{
print(n);
}
}
}
}
}
public struct requestData
{
public int[] data;
}
public struct returnData
{
public int[] data;
}
public class pathAlg
{
public int[] method(requestData data)
{
int[] example = new int[data.Length];
for(int i = 0; i< example.Length; i++)
{
example[i] = i+1;
}
return example;
}
}
Solved this problem by using coroutines rather than threads.
So I want to be able to set all the variable to false except the Boolean referenced, but this variable varies so i cannot use the required 'ref' keyword. Is there a way in which the keyword is not needed or a better way of doing this
public static void ReadFile()
{
for(int i=0; i<5; i++)
{
if(this)
{
SetStatus(Program.systemAbortedHamiltons[i], i);
}
if(this)
{
SetStatus(Program.runningHamiltons[i], i);
}
}
}
public static void SetStatus(ref bool status, int i)
{
Console.WriteLine("SetStatus");
Program.systemAbortedHamiltons[i] = false;
Program.runningHamiltons[i] = false;
Program.userAbortedHamiltons[i] = false;
Program.methodEndHamiltons[i] = false;
status = true;
}
Hope this makes sense, any suggestions are greatly appreciated.
Classes in c# are Reference type and you can create an class like this
public class CheckStatus
{
public bool Status { get; set; }
}
then pass the an instance of class to method like this
public static void SetStatus(CheckStatus status,int i)
{
Console.WriteLine("SetStatus");
Program.systemAbortedHamiltons[i] = false;
Program.runningHamiltons[i] = false;
Program.userAbortedHamiltons[i] = false;
Program.methodEndHamiltons[i] = false;
status.Status = true;
}
and
public static void ReadFile()
{
CheckStatus status = new CheckStatus();
if(this)
{
status.Status = Program.systemAbortedHamiltons[i];
SetStatus(status, i);
Program.systemAbortedHamiltons[i] = status.Status;
}
if(this)
{
status.Status = Program.runningHamiltons[i];
SetStatus(status, i);
Program.runningHamiltons[i] = status.Status;
}
}
What you can do is pass the list itself as such: (assuming they are List<bool> but if not, just replace that with actual collection type)
public static void ReadFile()
{
if(/* condition 1 */)
{
SetStatus(Program.systemAbortedHamiltons, i);
}
if(/* condition 2 */)
{
SetStatus(Program.runningHamiltons, i);
}
}
public static void SetStatus(List<bool> statuses, int i)
{
Console.WriteLine("SetStatus");
Program.systemAbortedHamiltons[i] = false;
Program.runningHamiltons[i] = false;
Program.userAbortedHamiltons[i] = false;
Program.methodEndHamiltons[i] = false;
statuses[i] = true;
}
Below is a quick test to see if it works:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<bool> myList = new List<bool>
{
true, true, true
};
// Outputs "True"
Console.WriteLine(myList[0]);
SetFalse(myList, 0);
// Outputs "False"
Console.WriteLine(myList[0]);
}
static void SetFalse(List<bool> l, int i)
{
l[i] = false;
}
}
Outputs:
True
False
In the examples below I want to know a good way to make the bottom example function like the top example. I know that scope is the reason the bottom example does not work.
I am interested in doing this so I can tidy up the main body of my programs and eliminate some duplicated code.
namespace example_1
{
class Program
{
static void Main(string[] args)
{
int test = 5;
bool trigger = true;
if (trigger)
{
test++;
trigger = false;
}
}
}
}
namespace example_2
{
class Program
{
static void Main(string[] args)
{
int test = 5;
bool trigger = true;
if (trigger)
{
mod_test();
}
}
public static void mod_test()
{
test++;
trigger = false;
}
}
You can declare the properties outside of the methods, but still in the class :
class Program
{
// both of them are accessible within the class scope, but not outside
static int test = 5;
static bool trigger = true;
static void Main(string[] args)
{
if (trigger)
{
mod_test();
}
}
public static void mod_test()
{
test++;
trigger = false;
}
}
I think using a data container object is more suitable in this case. For example, in the following example, I wrapped the int and bool variables into a TestDataclass. This way you don't have to use global variables and still pass around the object reference for any kind of manipulation.
namespace example_3
{
class TestData
{
public bool Trigger { get; set; }
public int Test { get; set; }
}
class Program
{
static void Main(string[] args)
{
var testData = new TestData
{
Test = 5,
Trigger = true
};
if (testData.Trigger)
{
mod_test(testData);
}
}
public static void mod_test(TestData data)
{
data.Test++;
data.Trigger = false;
}
}
}
In this code I made two output functions and one main.
When I call output function in main the program give the error.
using System;
public class InitArray
{
public static void Main()
{
int[,] rectangular = { { 1, 3, 4 }, { 5, 2, 4 } };
int[][] jagged = { new int[] { 2, 3, 4 }, new int[] { 3, 4, 5 }};
}
public void OutputArray(int [,]array)
{
for(int row=0;row<array.GetLength(0);row++)
{
for (int column = 0; column < array.GetLength(1); column++)
Console.Write("{0} ", array[row, column]);
Console.WriteLine();
}
}
public void OutputArray(int [][]array)
{
foreach(var row in array)
{
foreach (var element in row)
Console.Write("{0} ", element);
Console.WriteLine();
}
}
}
This is main function and have two arrays one is jagged and other is rectangular type.
The defined function is without static keyword and I could not access in main function.
This second output function is also a non static function and this is also not access in main.
Anyone can tell me the the reason?
Non-static methods want instance; that's why either mark the method as static:
public static void OutputArray(int[][] array) {
...
}
public static void Main() {
...
OutputArray(...);
...
}
or create and provide the instance:
public void OutputArray(int[][] array) {
...
}
public static void Main() {
...
var instance = new InitArray();
instance.OutputArray(...);
...
}
I am looking to access a list from different methods in the same class.Is there an easier way to access the movieTitle list without making a new list for each method? Do I have to make a reference to the list in every method? or should I put them all into a separate class all together? My overall goal is to have a option menu that gets input from the user and depending on the input calls a method to list all movies, add a movie to the list, and pick a random movie from the list.
class Program
{
static void Main(string[] args)
{
optionMenu();
Console.Read();
}
static void optionMenu()
{
Console.Write("(a)LIST MOVIES|(b)ADD Movie|(c)RANDOM MOVIE");
string ui = Console.ReadLine();
if (ui == "a") { printNames(); }
else if (ui == "b") { addMovie(); }
else if (ui == "b") { randomPickMovie(); }
else { optionMenu(); }
}
static void printNames()
{
List<string> movieTitles = new List<string>();
/*list.....
/ movieTitles.Add("Jurassic Park");
/..........
/..........
*/..........
Console.WriteLine("Movies in your list...");
for (int i = 0; i < movieTitles.Count;i++)
{
Console.WriteLine("\t-" + movieTitles[i]);
}
}
static void addMovie()
{
Console.WriteLine("Enter a title:");
string newTitle = Console.ReadLine();
//I can't say...
//movieTitles.Add(newTitle);
//...? do I need to make an instance of the list?
}
static void randomPickMovie()
{
Random r = new Random();
int random = r.Next();
Console.WriteLine(movieTitle[random]);
//same here. How do I access the movie titles in the printName() method so
//I can randomly pick a movie from the list?
}
}
Below is one way make the movie list shared. This declares and initializes the list as a static member of the class (instead of a local variable in the methods).
This approach works well for simple programs, but having global state in a large program can be problematic because it becomes difficult to see which methods affect which global state so bugs can easily creep in. See below for another approach.
class Program
{
static void Main(string[] args)
{
...
}
static List<string> movieTitles = new List<string>();
static void optionMenu()
{
...
}
static void printNames()
{
Console.WriteLine("Movies in your list...");
for (int i = 0; i < movieTitles.Count;i++)
{
Console.WriteLine("\t-" + movieTitles[i]);
}
}
static void addMovie()
{
movieTitles.Add(newTitle);
}
static void randomPickMovie()
{
...
}
}
Another approach is to pass the data from one method to another. This makes it more obvious to see what methods use the movieList. It also allows us to specify additional restrictions, e.g. you can see that printNames only needs a read-only version of the list so you know that printNames can't modify the list. This approach is a little more work but it's a good habit to get into because it reduces bugs in the long term.
class Program
{
static void Main(string[] args)
{
...
}
static void optionMenu()
{
List<string> movieTitles = new List<string>();
string ui = Console.ReadLine();
if (ui == "a") { printNames(movieTitles); }
else if (ui == "b") { addMovie(movieTitles); }
else if (ui == "b") { randomPickMovie(movieTitles); }
else { optionMenu(); }
}
static void printNames(IReadOnlyList<string> movieTitles)
{
Console.WriteLine("Movies in your list...");
for (int i = 0; i < movieTitles.Count;i++)
{
Console.WriteLine("\t-" + movieTitles[i]);
}
}
static void addMovie(List<string> movieTitles)
{
movieTitles.Add(newTitle);
}
static void randomPickMovie(List<string> movieTitles)
{
...
}
}
See https://stackoverflow.com/questions/13295319/instance-field-vs-passing-method-parameter for another user's point of view on which approach is better.
as for me, I prefer doing it this way.
class Program
{
static void Main(string[] args)
{
Movie movie = new Movie();
int x = 0;
while (x < 1)
{
movie.optionMenu(); Console.Write("Do you want to exit?");
string response = Console.ReadLine();
if (response == "Y") { x = 1; }
}
Console.Read();
}
}
class Movie
{
public List<string> movieTitles { get; set; }
public Movie()
{
movieTitles = new List<string>();
}
public void optionMenu()
{
Console.Write("(a)LIST MOVIES|(b)ADD Movie|(c)RANDOM MOVIE");
string ui = Console.ReadLine();
if (ui == "a") { printNames(); }
else if (ui == "b") { addMovie(); }
else if (ui == "c") { randomPickMovie(); }
else { optionMenu(); }
}
public void printNames()
{
Console.WriteLine("Movies in your list...");
for (int i = 0; i < movieTitles.Count; i++)
{
Console.WriteLine("\t-" + movieTitles[i]);
}
}
public void addMovie()
{
Console.WriteLine("Enter a title:");
string newTitle = Console.ReadLine();
if (newTitle != "")
{
movieTitles.Add(newTitle);
Console.WriteLine("New movie successfully added!");
}
else
{
Console.WriteLine("Cannot add empty movie. Add movie failed.");
}
}
public void randomPickMovie()
{
if (movieTitles.Count != 0)
{
Random r = new Random();
int random = r.Next(0, movieTitles.Count - 1);
Console.WriteLine(movieTitles[random]);
}
else { Console.WriteLine("Movie list is empty."); }
}
}
Answer for all your questions is create MovieTitles property of type List<string> and access it like this:
class Program
{
static void Main(string[] args)
{
optionMenu();
Console.Read();
}
static List<string> movieTitles;
static List<string> MovieTitles
{
get
{
if (movieTitles == null)
CreateMoviesList();
return movieTitles;
}
}
static void CreateMoviesList()
{
movieTitles = new List<string>();
/*list.....
/ movieTitles.Add("Jurassic Park");
/..........
/..........
*/
}
static void optionMenu()
{
Console.Write("(a)LIST MOVIES|(b)ADD Movie|(c)RANDOM MOVIE");
string ui = Console.ReadLine();
if (ui == "a") { printNames(); }
else if (ui == "b") { addMovie(); }
else if (ui == "b") { randomPickMovie(); }
else { optionMenu(); }
}
static void printNames()
{
Console.WriteLine("Movies in your list...");
for (int i = 0; i < MovieTitles.Count; i++)
{
Console.WriteLine("\t-" + movieTitles[i]);
}
}
static void addMovie()
{
Console.WriteLine("Enter a title:");
string newTitle = Console.ReadLine();
MovieTitles.Add(newTitle);
}
static void randomPickMovie()
{
Random r = new Random();
int random = r.Next();
Console.WriteLine(MovieTitles[random]);
}
}
CreateMoviesList() create list of movies only once and can be use to print movies, randon pick and add movies later on.