Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

40
LINES

< > BotCompany Repo | #484 // Image distinguisher by randomly selected color

Lua code

1  
get("#356") -- num2hex
2  
get("#485") -- containsColor
3  
4  
-- Job: Distinguish two classes of images
5  
-- Two images are supplied at once, one of each class.
6  
-- Task is to find out if the two images are swapped or not.
7  
--
8  
-- input variables: img1, img2, solution [to last pair]
9  
-- output: a function returning true or false (swapped or no)
10  
11  
function selectColor(img)
12  
  local x, y = math.random(0, img.width-1), math.random(0, img.height-1)
13  
  color = img.getInt(x, y)
14  
end
15  
16  
return function()
17  
  -- select a new color when last guess was wrong
18  
  if solution ~= guess then color = nil end
19  
  
20  
  if color == nil then
21  
    if solution == nil then
22  
      lastimg1, lastimg2 = img1, img2
23  
      return false -- just return something
24  
    end
25  
    selectColor(lastimg1)
26  
    class2sampled = solution
27  
    print("color of class "..(class2sampled and "2" or "1")..": ".. num2hex(color))
28  
  end
29  
  
30  
  lastimg1, lastimg2 = img1, img2
31  
  
32  
  flag1 = containsColor(img1, color)
33  
  flag2 = containsColor(img2, color)
34  
  if flag1 == flag2 then
35  
    color = nil
36  
    return false
37  
  end
38  
  guess = flag1 == class2sampled
39  
  return guess
40  
end

Author comment

Began life as a copy of #483

test run  test run with input  download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #484
Snippet name: Image distinguisher by randomly selected color
Eternal ID of this version: #484/1
Text MD5: 570216e2e5c9b9c845e8e558916f517e
Author: stefan
Category:
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-03-04 22:40:53
Source code size: 1133 bytes / 40 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 627 / 243
Referenced in: [show references]