wm: mmind

Download patch

ref: c25c807edfd4d1947581f89d95774a5e553dc371
author: mkf <mkf>
date: Tue Apr 18 03:00:04 EDT 2023

import sources

--- /dev/null
+++ b/game.cpp
@@ -1,0 +1,139 @@
+/*
+	Mastermind impl.
+
+	todo:
+	fix XXXs in code,
+*/
+
+#include <iostream>
+
+using namespace std;
+
+// io functions
+void printrules();
+uint getlen();
+uint getguess();
+void compere(uint goal, uint guess);
+
+// aux functions
+uint length(uint n);
+uint gengoal(uint n);
+int digit(int number, int index);
+
+void printrules()
+{
+	cout << "Hi," << endl;
+}
+
+uint getlen()
+{
+	int len;
+	/* hopefully not long */
+	cout << "how long? " << endl;
+	cin >> len;
+	while(len < 1)
+	{
+		cerr << "Sorry, please enter a 1+ number" << endl;
+		len = getlen();
+	}
+	return len;
+}
+
+uint getguess()
+{
+	/* "sang bozorg neshane nazadan ast" */
+	char guess[512];
+	int i = 0, temp = 0;
+	cout << "guess? " << endl;
+	cin >> guess;
+	while(guess[i] != '\0')
+	{
+		/* see if it's either a number or space,
+		SCREAM loudly otherwise! */
+		if((guess[i] >= '0') && (guess[i] <= '9'))
+		{
+			/* because	we can't put ascii into int directly */
+			temp = (guess[i] - '0') + temp;
+			temp *= 10;
+		}
+
+		else if(guess[i] != ' ')
+		{
+			cerr << "That's not a number" << endl;
+			return getguess();
+		}	
+	}
+	return temp;
+}
+
+uint length(uint n)
+{
+	uint t = 0;
+	while(n > 0)
+	{
+		n /= 10;
+		t++;
+	}
+	return t;
+}
+
+/* generates an uint, with n digits,
+	used to make a goal */
+uint gengoal(uint n)
+{
+	uint num = 0;
+	/* it's technically legal, but
+	we get a int that's shorter than n */
+	do
+        num = rand() % 10;
+    while (num == 0)
+
+	while(n > 0)
+	{
+		num *= 10;
+		num += rand()%10;
+		n--;
+	}
+	return num;
+}
+
+/* we could also use arrays for this,
+	alas, that would a can of worms 
+	digit(number, index) = number[index] */
+int digit(int number, int index)
+{
+	if((index > length(number)) || (index < 0))
+		return -1;
+
+	int i = 0, j;
+	while(i < index)
+	{
+		j = number % 10;
+		number /= 10; 
+		i++;
+	}
+	return j;
+}
+
+void compere(uint goal, uint guess)
+{
+	int i = 0;
+	while(i < length(goal))
+	{
+		if(digit(goal, i) == digit(guess, i))
+			cout << '#';
+		else
+			cout << 'X';
+		i++;
+	}
+}
+
+int main()
+{
+	uint len, goal;
+	printrules();
+	len = getlen();
+	goal = gengoal(len);
+	cout << len << endl;
+	cout << goal << endl;
+}
\ No newline at end of file