Array handling in a CCG

Ask gaming related questions (AHK v1.1 and older)
cmikaitis
Posts: 14
Joined: 30 Sep 2013, 14:58

Array handling in a CCG

13 Dec 2013, 21:24

I'm hoping someone can help. I can't get my head around a seemingly simple issue. I'm playing a Collectible Card Game where you have a 'deck' of cards and a 'hand' of available cards to play. I want to create a preferred order of play for my deck, and have it played in that order (when possible). I have been successful in getting the script to select the most appropriate card available, but I'm not sure how to expand it to have it exclude that card on the next run-through.

Ideally, I want it to be able to tell how many cards are in my deck, and be able to click them all one after another in my preferred order, i.e....

My Deck has 10 cards, and I want them played in the order - a, a, b, a, c, d, b, a, e

Let's say my starting hand contained cards b,c,d, it should play b, then remove that instance of 'b' from the queue, so that if I drew an 'a' next, it would play that, but if I drew the second 'b' next, it would play 'c' instead, since the first 'b' had already been played.

My code is below. All it really does is to go through the ordered list (defense.txt) and pick the first available card that is in my hand.... The imagesearch and click work just fine, but I'm not sure how to modify my array. As you can see, my current strategy is to rewrite the used array value with an empty string. This doesn't help since I need to loop this whole thing for (# of cards in deck) cycles, removing a used card each time. Any help would be greatly appreciated. Please ask questions if you think you can help. I'm bad at explaining things, but I don't think this is actually a tough problem, I'm just too close to it to see the solution....

Code: Select all

^+m::
{

;Locate my hand on the playfield
HandLocator := ".\Actions\Hand.png"
ImageSearch, TempX, TempY, 0, 0, 1920, 1080, %HandLocator%
Hand1X := TempX - 5
Hand1Y := TempY + 15
Hand2X := Hand1X + 270
Hand2Y := Hand1Y + 120
;


; Create my preferred order card list from a text file
DeckCount = 0
Loop, Read, Defense.txt
{
    DeckCount += 1
    Array%DeckCount% := A_LoopReadLine
}
;


; Play cards in order
Loop %DeckCount%
{	
CardCheck := Array%A_Index%

ImageSearch, CardX, CardY, %Hand1X%, %Hand1Y%, %Hand2X%, %Hand2Y%, %CardCheck%
If Errorlevel = 0
{
Click, %CardX%, %CardY%
Array%A_Index% = ""
MouseMove 1000,250
}
If Errorlevel = 1
Continue
;If ErrorLevel = 2
;msgbox, Problem
}
}
cmikaitis
Posts: 14
Joined: 30 Sep 2013, 14:58

Re: Array handling in a CCG

18 Dec 2013, 21:29

Ok. So, I've tried to troubleshoot all my problems away, but it just isn't working.... here is my current code and I'm tearing my hair out trying to get it to work. Does anyone have any suggestions???? My main problem with coding is I never know when to use %variable%, % variable, or variable (no percent signs). That, I think, is a lot of my problem. Can anyone find some easy internal logic that I am failing at... I am literally msgboxing or tooltipping all variables at all times and can't isolate what the problem is.

Please Halp!

Code: Select all

#Persistent
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#singleinstance force ;Prevents error message when reloading.
CoordMode Mouse, Screen ;'Screen' is necessary for MouseGetPos
CoordMode Pixel, Relative ;'Relative' is necessary for ImageSearch

*$+esc::exitapp ; in case of emergency, hit shift escape to exit script
^+f12::reload  ; edit the script, save it, then hit Ctrl-Shift-F12 to restart it.
+f11::listvars ; Displays the script's variables: their names and current contents.
^F12::Pause
XButton2::Del

^+m::
{
	;Locate my hand on the playfield
	HandLocator := ".\Actions\Hand.png"
	ImageSearch, TempX, TempY, 0, 0, 1920, 1080, %HandLocator%
	Hand1X := TempX - 5
	Hand1Y := TempY + 15
	Hand2X := Hand1X + 270
	Hand2Y := Hand1Y + 120

	; Create my preferred order card list from a text file
	DeckCount = 0
	Loop, Read, Mission 300.txt
	{
		DeckCount += 1
		Array%DeckCount% := A_LoopReadLine 
		;msgbox, % Array%DeckCount%
	}
	PlayCount := DeckCount-1 ;Overall loop done for 'max cards-1' times
	;msgbox %DeckCount%
	;msgbox %PlayCount%
	
	Loop %Playcount% ;Play cards in order (last card played automatically)
	{
		LoopNumber := 1
		Loop %DeckCount% ;Find card to play
		{
			;msgbox %A_Index%
			CardCheck := Array%LoopNumber%
			Tooltip, %CardCheck%
			sleep 1000

			ImageSearch, CardX, CardY, %Hand1X%, %Hand1Y%, %Hand2X%, %Hand2Y%, %CardCheck%
			If Errorlevel = 0
			{
				;msgbox, 0
				Click, %CardX%, %CardY%
				Array%LoopNumber% = ""
				MouseMove 1000,250
				sleep 8000
			}
			If Errorlevel = 1
			{
				LoopNumber := %LoopNumber% + 1
				;msgbox, %LoopNumber%
				continue
			}
			If ErrorLevel = 2
			{
				LoopNumber := %LoopNumber% + 1
				msgbox, Problem finding %CardCheck%
			}
		}
	}
}
strobo
Posts: 125
Joined: 30 Sep 2013, 15:24

Re: Array handling in a CCG

19 Dec 2013, 07:53

Simple advice for you, (+) for do, (-) for not do:

Code: Select all

	(+): x := "hello world"
	(-): x = hello world
	(+): x := "hello" . var
	(-): x = hello %var%
	(+): some_command,% var, maybe_some_string, ...
	(-): some_command, %var%, maybe_some_string, ...
	(+): array := {} ; yupp, it must be initialized.
		 array[var] := "bla"
	(-): array%var% := "bla"
In the end you don't need to use %var% at all (well, despite some pseudo array output from some commands.)
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: Array handling in a CCG

19 Dec 2013, 15:15

cmikaitis wrote:My Deck has 10 cards, and I want them played in the order - a, a, b, a, c, d, b, a, e

Let's say my starting hand contained cards b,c,d, it should play b, then remove that instance of 'b' from the queue, so that if I drew an 'a' next, it would play that, but if I drew the second 'b' next, it would play 'c' instead, since the first 'b' had already been played.
Based on your description above this is how I would approach the problem.

Code: Select all

global Order := ["a","a","b","a","c","d","b","a","e"]

global Deck := ["a","a","a","a","b","b","c","d","e"]

global Hand:= {}

Draw(3)

Loop 
{
	Order_Display := "", Deck_Display := "", Hand_Display := ""

	for Index, Card in Order
		Order_Display .= Card " "

	for Index, Card in Deck
		Deck_Display .= Card " "

	for Index, Card in Hand
		Hand_Display .= Card " "

	MsgBox % "Turn -`t" A_Index "`nOrder -`t" Order_Display "`nDeck -`t" Deck_Display "`nHand -`t" Hand_Display "`nPlay -`t" Play()
	Draw(1)
} until !Hand[1]

Draw(N)
{
	Loop % N
	{
		Random, X, 1, Deck.MaxIndex()
		Hand.Insert(Deck[X])
		Deck.Remove(X)
	}
	return
}

Play()
{
	for Index, Card in Order
		if Search_Hand(Card)
			break
	Order.Remove(Index)
	Hand.Remove(Search_Hand(Card))
	return Card
}

Search_Hand(Search)
{
	for Index, Card in Hand
		if (Card = Search)
			return Index
	return
}
Hopefully you can learn from this example code and use it to suit your purposes.

I would suggest in general to learn how to use Objects and move away from using pseudo arrays like array%var% and use the real deal with is array[var].

It can take a little bit of thinking to learn the syntax of Objects but it is worth it.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 115 guests