Cows and Bulls

  This is a game i've writen recently where the computer generates a random 4 digit number and the uset then has to guess that nunber step by step. When the user guesses a number correctly and its in the correct location it is a bull, if the number is correct but not in the rigvht location then it is a cow. If it's neither... you'll have to guess harder!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BullsAndCows
{
    class Program
    {
        public static string[] ORDINAL = new string[] { "1st", "2nd", "3rd", "4th" };

        static void Main(string[] args)
        {
            PlayGame();
            Console.ReadKey();
        }

        private static bool hasDuplicates(string value)
        {
            for (int i = 0; i < value.Length; i++)
            {
                if (value.LastIndexOf(value[i]) != i)
                {
                    return true;
                }
            }
            return false;
        }
        public static void PlayGame()
        {
            int computersNo;

            int bulls = 0;

            bulls = 0;
            computersNo =GetRandomNumber();
            Console.WriteLine("\nBulls and Cows - New Game: " + computersNo);
            int guessCount = 0;
            while (bulls < 4)
            {
                guessCount++;
                bulls = getUserGuess(computersNo.ToString());
                //If user wants to quit -- break out
                if (bulls < 0)
                {                     
                    break;
                }
                if (bulls == 4)
                {
                    Console.WriteLine("\nWell Done! you have correctly guessed the randomly generated number which was: " + computersNo+". You had "+ guessCount.ToString()+" attempts");
                }
            }
            Console.WriteLine("Goodbye. The game has ended.");
        }

        public static int getUserGuess(string computersNumbers)
        {
            int bulls = 0;

            //user guess
            string usersNumbers = "";
            int intGuess = 0;
            bool inputok = false;

            inputok = false;
            while (!inputok)
            {
                Console.Write("\nEnter 4 digit guess (or q to quit): ");
                usersNumbers = Console.ReadLine();
                if (usersNumbers == "exit")
                    return -1;
                else
                {
                    //Test users input is a number
                    if (usersNumbers.Length == 4)
                    {
                        inputok = int.TryParse(usersNumbers, out intGuess);
                    }

                    if (!inputok)
                        Console.WriteLine("Invalid Input: 4 digits please!");
                    else
                    {
                        if (hasDuplicates(usersNumbers.ToString()))
                        {
                            inputok = false;
                            Console.WriteLine("Invalid Input: no duplicate numbers!");
                        }
                    }
                }
            } //end while loop

            //We now have a valid user response: now mark the response in terms of bulls and cows
            bulls = markUsersResponse(computersNumbers, usersNumbers);

            return bulls;
        }

        public static int markUsersResponse(string computersNumbers, string usersNumbers)
        {
            // check each of the computers numbers against each of the Users numbers
            int bulls = 0;
            int cows = 0;
            bool match = false;
            StringBuilder results = new StringBuilder();
            for (int n = 0; n <= usersNumbers.Length - 1; n++)
            {
                match = false;
                for (int i = 0; i <= computersNumbers.Length - 1; i++)
                {
                    //Is there a match
                    if (usersNumbers[n] == computersNumbers[i])
                    {
                        //YES
                        match = true;
                        if (n == i)
                        {
                            bulls++;
                            results.AppendLine("The digit " + usersNumbers[n] + " is in the " + ORDINAL[n] + " position in the randomly generated number and also in the " + ORDINAL[n] + " position in the players number. this is a bull.");
                        }
                        else
                        {
                            cows++;
                            results.AppendLine("The digit " + usersNumbers[n] + " is in the " + GetPosition(computersNumbers, computersNumbers[i].ToString()) + " position in the randomly generated number and in the " + GetPosition(usersNumbers, usersNumbers[n].ToString()) + " position in the players number. This is a cow.");
                        }
                    }
                }
                if (match == false)
                {
                    // results.AppendLine("Digit " + (n + 1).ToString() + " is Wrong");
                }
            }
            Console.WriteLine(results.ToString());

            return bulls;
        }

        public static int GetRandomNumber()
        {
            int mynumber = 0;
            bool duplicates = true;
            Random random = new Random();

            while (duplicates == true)
            {
                duplicates = false;
                mynumber = random.Next(1000, 9999);
                string s = mynumber.ToString();
                duplicates = hasDuplicates(s);
            }
            return mynumber;
        }

        public static string GetPosition(string a, string b)
        {
            int i = a.LastIndexOf(b);
            return ORDINAL[i];
        }


    }
}

 

##############################

PYTHON

########################

import random

Ordinal= ["1st","2nd","3rd","4th"]

def GetRandomNumber():

    #rnd=5099
    flag= True
    while flag == True:
        rnd= random.randint(1000,9999)
        flag=HasDuplicates(str(rnd))
    return rnd

def HasDuplicates(a):
    retValue=False
    for target in range(0,len(a)):
        last_pos= len(a) - 1- a[::-1].index(a[target])
        if(last_pos!= target):
            retValue=True
    return retValue

def getUserGuess():
    #leading zero issue
    flag= True
    while flag==True:
        userGuess= input("Enter a 4 digit number(NO duplicates)")
        if(userGuess=="exit"):
            return -1
        flag=HasDuplicates(userGuess)

        for i in userGuess:
            if not i.isnumeric():
                flag=True

    return userGuess

def markUsersGuess(UserNum, ComputerNum):
    bulls=0
    cows=0
    match= False
    for i in range(len(UserNum)):
        match=False
        for n in range(len(ComputerNum)):
            if UserNum[i]== ComputerNum[n]:
                match=True
                if i==n:
                    bulls+=1
                    print("The digit ", UserNum[i]," is in the ", Ordinal[i]," position in the randomly generated number and is also in the ",Ordinal[n]," position in the players number. This is a bull")
                else:
                    cows+=1
                    print("The digit ", UserNum[i]," is in the ", GetPosition(ComputerNum,ComputerNum[n])," position in the randomly generated number and in the ", GetPosition(UserNum,UserNum[i])," position in the players number. This is a cow")
    return bulls


def GetPosition(a,b):
    last_pos= len(a) - 1- a[::-1].index(b)
    return Ordinal[last_pos]

ComputerNum= GetRandomNumber()
bulls=0
guesses=0

print(ComputerNum)
while (bulls<4):
    guesses+=1
    usersGuess=getUserGuess()
    if usersGuess== -1:
        break
    bulls=markUsersGuess(str(usersGuess), str(ComputerNum))
if bulls==4:
    print("Well done! You have correctly guessed the randomly generated number, which was: ", ComputerNum," with ",guesses, " attempts.")
else:
    print("You have chosen to exit. The randomly generated number was: ", ComputerNum)
print("Bye! Game Over...")