How to resolve the algorithm Bulls and cows step by step in the AutoHotkey programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bulls and cows step by step in the AutoHotkey programming language

Table of Contents

Problem Statement

Bulls and Cows   is an old game played with pencil and paper that was later implemented using computers.

Create a four digit random number from the digits   1   to   9,   without duplication. The program should:

The score is computed as:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Bulls and cows step by step in the AutoHotkey programming language

Source code in the autohotkey programming language

length:=4, Code:="" ; settings

While StrLen(Code) < length {
	Random, num, 1, 9
	If !InStr(Code, num)
		Code .= num
}
Gui, Add, Text, w83 vInfo, I'm thinking of a %length%-digit number with no duplicate digits.
Gui, Add, Edit, wp vGuess, Enter a guess...
Gui, Add, Button, wp Default vDefault, Submit
Gui, Add, Edit, ym w130 r8 vHistory ReadOnly
Gui, Show
Return

ButtonSubmit:
	If Default = Restart
		Reload
	Gui, Submit, NoHide
	If (StrLen(Guess) != length)
		GuiControl, , Info, Enter a %length%-digit number.
	Else If Guess is not digit
		GuiControl, , Info, Enter a %length%-digit number.
	Else
	{
		GuiControl, , Info
		GuiControl, , Guess
		If (Guess = Code)
		{
			GuiControl, , Info, Correct!
			GuiControl, , Default, Restart
			Default = Restart
		}
		response := Response(Guess, Code)
		Bulls := SubStr(response, 1, InStr(response,",")-1)
		Cows := SubStr(response, InStr(response,",")+1)
		GuiControl, , History, % History . Guess ": " Bulls " Bulls " Cows " Cows`n"
	}
Return

GuiEscape:
GuiClose:
	ExitApp

Response(Guess,Code) {
	Bulls := 0, Cows := 0
	Loop, % StrLen(Code)
		If (SubStr(Guess, A_Index, 1) = SubStr(Code, A_Index, 1))
			Bulls++
		Else If (InStr(Code, SubStr(Guess, A_Index, 1)))
			Cows++
	Return Bulls "," Cows
}


  

You may also check:How to resolve the algorithm Hash join step by step in the Perl programming language
You may also check:How to resolve the algorithm Truth table step by step in the 11l programming language
You may also check:How to resolve the algorithm Phrase reversals step by step in the Python programming language
You may also check:How to resolve the algorithm Compound data type step by step in the Maple programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the Tailspin programming language