Full source

 avatar
unknown
c_cpp
3 years ago
7.0 kB
8
Indexable
using System;
//Problem: Every single number gets eliminated

namespace testconsole
{
    internal class Program
    {
        int correct = 0;
        int wrong = 0;
        int number = 1234;
        public int[] list = new int[9877];
        int firstOne;
        int arrayItemPick;
        string playerNumber;
        string cInput1;
        string cInput2;
        int cInput1int;
        int cInput2int;
        int digit1;
        int digit2;
        int digit3;
        int digit4;
        bool value = false;
        public static bool win_;    
        int computerGuessNumber = 0;
        Random rnd = new Random();
        static void Main(string[] args)

        {
            Program some = new Program();
            some.askForNumber();
            some.createArray();
            some.avoidImposible();
   
            do
            {
                some.checkWin();
                some.computerGuessNumber++;
                some.selectArrayItem();
                some.checkForCorrectness();
                
                foreach (int i in some.list)
                {
                    if (some.list[some.arrayItemPick] != 0)
                    {
                        some.firstOne = some.list[some.arrayItemPick];
                        some.value = some.Function(some.firstOne, i, some.cInput1int, some.cInput2int);
                        if (some.value == false)
                        {
                            some.list[i] = 0;
                            Console.WriteLine("eliminating " + some.list[i]);
                        }
                        if (some.cInput1int == 4)
                        {
                            win_ = true;
                        }
                        Console.WriteLine("cInput1int: " + some.cInput1int + " cInput2int: " + some.cInput2int + " player number: " + some.playerNumber + " value: " + some.value + " arrayItemPick: " + some.arrayItemPick + " some.firstone: " + some.list[some.arrayItemPick]);
                        Console.WriteLine(some.arrayItemPick + " " + some.list[some.arrayItemPick]);
                        Console.WriteLine(some.list[1935]);
                    }
                }
            } while (win_ is false);
        }
        
        public void createArray()
        {
            // Creates array that ranges from 1234-9876
            do
            {
                list[number] = number;
                number = number + 1;
            } while (number < 9877);
        }
        
        public void avoidImposible()
        {
            foreach (int i in list)
            {
                if (list[i] != 0)
                {
                    digit1 = list[i] / 1000;

                    digit2 = list[i] - (digit1 * 1000);
                    digit2 = digit2 / 100;

                    digit3 = list[i] - (digit2 * 100 + digit1 * 1000);
                    digit3 = digit3 / 10;

                    digit4 = list[i] - (digit2 * 100 + digit1 * 1000 + digit3 * 10);

                    if (digit1 == 0 || digit2 == 0 || digit3 == 0 || digit4 == 0)
                    {
                        list[i] = 0;
                    }
                    if (digit1 == digit2 || digit1 == digit3 || digit1 == digit4 || digit2 == digit3 || digit2 == digit4 || digit3 == digit4)
                    {
                        list[i] = 0;
                    }
                }
            }
        }

        public void printArray()
        {
            foreach (int i in list)
            {
                if (list[i] != 0)
                {
                    Console.WriteLine(list[i]);
                }
            }
        }

        public void askForNumber()
        {
            Console.WriteLine("Enter your 4 digit number that does not contain 0's or repeating digits: ");
            playerNumber = Console.ReadLine();
            Console.WriteLine("Enter in '{possitive number},{negative number}' form: ");
        }

        public void selectArrayItem()
        {
            arrayItemPick = rnd.Next(0, list.Length);
            if (list[arrayItemPick] != 0)
            {
                Console.WriteLine(list[arrayItemPick]);
            }
            else
            {
                selectArrayItem();
            }
        }

        public void checkForCorrectness()
        {
            string correctnessInput = Console.ReadLine();
            string cInput1 = ToSplit(correctnessInput, 0);
            string cInput2 = ToSplit(correctnessInput, 1);
            int cInput1int = Convert.ToInt32(cInput1);
            int cInput2int = Convert.ToInt32(cInput2);

        }

        string ToSplit(string a, int b)
        {
            return a.Split(',')[b];
        }
        bool Function(int num1, int num2, int otherCorrect, int otherWrong)
        {
            correct = 0;
            wrong = 0;
            int num11 = num1 / 1000;
            int num12 = (num1 - num11 * 1000) / 100;
            int num13 = (num1 - num12 * 100 + num11 * 1000) / 10;
            int num14 = (num1 - num12 * 100 + num11 * 1000 + num13 * 10) / 10;

            int num21 = num2 / 1000;
            int num22 = (num2 - num21 * 1000) / 100;
            int num23 = (num2 - num22 * 100 + num21 * 1000) / 10;
            int num24 = (num2 - num22 * 100 + num21 * 1000 + num23 * 10) / 10;

            if (num11 == num21)
            {
                correct++;
            }
            if (num11 == num22 || num11 == num23 || num11 == num24)
            {
                wrong++;
            }
            if (num12 == num22)
            {
                correct++;
            }
            if (num12 == num21 || num12 == num23 || num12 == num24)
            {
                wrong++;
            }
            if (num13 == num23)
            {
                correct++;
            }
            if (num13 == num21 || num13 == num22 || num13 == num24)
            {
                wrong++;
            }
            if (num14 == num24)
            {
                correct++;
            }
            if (num14 == num21 || num14 == num22 || num14 == num23)
            {
                wrong++;
            }


            Console.WriteLine("Correct: " + correct + " Wrong: " + wrong);
            Console.WriteLine("otherCorrect: " + otherCorrect + " otherWrong: " + otherWrong);
            if (correct == otherCorrect && wrong == otherWrong)
            {   
                return true;
            }
            else
            {
                return false;
            }
        }


        public void checkWin()
        {
            if (win_ == true)
            {
                Console.WriteLine("Computer Guessed your number in " + computerGuessNumber + " tries!");
                Console.ReadKey();
            }
        }
    }
}
Editor is loading...