Advertisement
Guest User

Untitled

a guest
Apr 17th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define NIBBLE_0 vec4(81, 87, 69, 82)
  2. #define NIBBLE_1 vec4(65, 83, 68, 70)
  3. #define NIBBLE_2 vec4(90, 88, 67, 86)
  4. #define NIBBLE_3 vec4(84, 89 ,85, 73)
  5. #define NIBBLE_4 vec4(71, 72, 74, 75)
  6. int s1;
  7. int s2;
  8. int s3;
  9. int s4;
  10. int s5;
  11. int s6;
  12. int s7;
  13. int s8;
  14. int s9;
  15.  
  16. int get_square(int index)
  17. {
  18.     if      (index <= 1) { return s1; }
  19.     else if (index == 2) { return s2; }
  20.     else if (index == 3) { return s3; }
  21.     else if (index == 4) { return s4; }
  22.     else if (index == 5) { return s5; }
  23.     else if (index == 6) { return s6; }
  24.     else if (index == 7) { return s7; }
  25.     else if (index == 8) { return s8; }
  26.     else                 { return s9; }
  27. }
  28.  
  29. void set_square(int index, int value)
  30. {
  31.     if      (index <= 1) { s1 = value; }
  32.     else if (index == 2) { s2 = value; }
  33.     else if (index == 3) { s3 = value; }
  34.     else if (index == 4) { s4 = value; }
  35.     else if (index == 5) { s5 = value; }
  36.     else if (index == 6) { s6 = value; }
  37.     else if (index == 7) { s7 = value; }
  38.     else if (index == 8) { s8 = value; }
  39.     else                 { s9 = value; }
  40. }
  41.  
  42. bool validate_nibble_value(int value)
  43. {
  44.     return (value) >= 1 && (value) <= 9;
  45. }
  46.  
  47. vec2 map_nibble_value(int value)
  48. {
  49.     int v = value - 1;
  50.     if (!validate_nibble_value(value)) { return vec2(-1.0, -1.0); }
  51.     float y = floor((float(v) / 3.0)) / 3.0;
  52.     float x = mod(float(v), 3.0) / 3.0;
  53.  
  54.     return vec2(x, y) + vec2(0.16666);
  55. }
  56.  
  57.  
  58. bool check_sequence(vec2 uv, int iA, int iB, int iC,out vec4 fragColor)
  59. {
  60.     int a = get_square(iA);
  61.     int b = get_square(iB);
  62.     int c = get_square(iC);
  63.    
  64.     bool result = a != 0 && a == b && b == c;
  65.    
  66.     if (result)
  67.     {
  68.        
  69.         vec2 aP = map_nibble_value(iA);
  70.         vec2 bP = map_nibble_value(iB);
  71.         vec2 cP = map_nibble_value(iC);
  72.        
  73.         float aD = (abs(aP.x - uv.x) + abs(aP.y - uv.y));
  74.         if (aD < 0.1 && aD > 0.09) { fragColor = vec4(0.0, 1.0, 0.0, 1.0); }
  75.        
  76.         float bD = (abs(bP.x - uv.x) + abs(bP.y - uv.y));
  77.         if (bD < 0.1 && bD > 0.09) { fragColor = vec4(0.0, 1.0, 0.0, 1.0); }
  78.        
  79.         float cD = (abs(cP.x - uv.x) + abs(cP.y - uv.y));
  80.         if (cD < 0.1 && cD > 0.09) { fragColor = vec4(0.0, 1.0, 0.0, 1.0); }
  81.     }
  82.    
  83.     return result;
  84. }
  85.  
  86. bool check_subsequence(int a, int b, int c, int v)
  87. {
  88.     return a == v && a == b && c == 0;
  89. }
  90.  
  91. bool check_game_won(vec2 uv,out vec4 fragColor)
  92. {
  93.     return
  94.         check_sequence(uv, 1, 2, 3,fragColor) ||
  95.         check_sequence(uv, 4, 5, 6,fragColor) ||
  96.         check_sequence(uv, 7, 8, 9,fragColor) ||
  97.         check_sequence(uv, 1, 4, 7,fragColor) ||
  98.         check_sequence(uv, 2, 5, 8,fragColor) ||
  99.         check_sequence(uv, 3, 6, 9,fragColor) ||
  100.         check_sequence(uv, 1, 5, 9,fragColor) ||
  101.         check_sequence(uv, 3, 5, 7,fragColor);
  102. }
  103.  
  104. int check_key(float k)
  105. {
  106.     float x = (k) / 255.0;
  107.     return int(texture(iChannel0, vec2(x, 1.0)).r > 0.0);
  108. }
  109.  
  110. int check_nibble(vec4 nibble)
  111. {
  112.     return
  113.         check_key(nibble.x) +
  114.         check_key(nibble.y) * 2 +
  115.         check_key(nibble.z) * 4 +
  116.         check_key(nibble.w) * 8;
  117. }
  118.  
  119.  
  120. bool display_cross(vec2 uv, int x,out vec4 fragColor)
  121. {
  122.     vec2 p = map_nibble_value(x);
  123.    
  124.     if (abs(uv.x - p.x) < 0.1 &&
  125.         abs(uv.y - p.y) < 0.1 &&
  126.         (
  127.         abs(uv.y - (p.y - 0.01)) > abs(uv.x - p.x) &&
  128.         abs(uv.y - (p.y + 0.01)) < abs(uv.x - p.x) ||
  129.         abs(uv.y - (p.y - 0.01)) < abs(uv.x - p.x) &&
  130.         abs(uv.y - (p.y + 0.01)) > abs(uv.x - p.x))
  131.         )
  132.     {
  133.        
  134.         fragColor = vec4(1.0, 0.0, 0.0, 1.0);
  135.         return true;
  136.     }
  137.  
  138.     return false;
  139. }
  140.  
  141. bool display_nought(vec2 uv, int x,out vec4 fragColor)
  142. {
  143.     vec2 p = map_nibble_value(x);
  144.    
  145.     if (distance(uv, p) < 0.1 &&
  146.         distance(uv, p) > 0.08)
  147.     {
  148.         fragColor = vec4(0.0, 0.0, 1.0, 1.0);
  149.         return true;
  150.     }
  151.     return false;
  152. }
  153.  
  154. bool display_binary(vec2 uv, int nibble, vec2 corner, vec2 extents,out vec4 fragColor)
  155. {
  156.     if (uv.x > corner.x && uv.x < extents.x && uv.y > corner.y && uv.y < extents.y)
  157.     {
  158.         fragColor = vec4(0.3, 0.0, 0.0, 1.0);;
  159.         int value = nibble;
  160.        
  161.         for (int n = 0; n < 4; n++)
  162.         {
  163.             float width = float(n) / 4.0 * (extents.x - corner.x);
  164.             float nextWidth = float(n+1) / 4.0 * (extents.x - corner.x);
  165.            
  166.             int bit = int(mod(float(value), 2.0));
  167.             value /= 2;
  168.            
  169.             if (bit > 0 && uv.x - corner.x > width && uv.x - corner.x < nextWidth)
  170.             {
  171.                 fragColor = vec4(1.0, 0.0, 0.0, 1.0);
  172.             }
  173.         }  
  174.         return true;
  175.     }
  176.     return false;
  177. }
  178.  
  179. int check_all_sub_sequences_horizontal(int check_for)
  180. {
  181.     if      (check_subsequence(s1, s2, s3, check_for)) { return 3; }
  182.     else if (check_subsequence(s2, s3, s1, check_for)) { return 1; }
  183.     else if (check_subsequence(s1, s3, s2, check_for)) { return 2; }
  184.    
  185.     else if (check_subsequence(s4, s5, s6, check_for)) { return 6; }
  186.     else if (check_subsequence(s5, s6, s4, check_for)) { return 4; }
  187.     else if (check_subsequence(s4, s6, s5, check_for)) { return 5; }
  188.    
  189.     else if (check_subsequence(s7, s8, s9, check_for)) { return 9; }
  190.     else if (check_subsequence(s8, s9, s7, check_for)) { return 7; }
  191.     else if (check_subsequence(s9, s7, s8, check_for)) { return 8; }
  192.     else { return -1; }
  193. }
  194.  
  195. int check_all_sub_sequences_vertical(int check_for)
  196. {
  197.     if      (check_subsequence(s1, s4, s7, check_for)) { return 7; }
  198.     else if (check_subsequence(s4, s7, s1, check_for)) { return 1; }
  199.     else if (check_subsequence(s1, s7, s4, check_for)) { return 4; }
  200.    
  201.     else if (check_subsequence(s2, s5, s8, check_for)) { return 8; }
  202.     else if (check_subsequence(s5, s8, s2, check_for)) { return 2; }
  203.     else if (check_subsequence(s2, s8, s5, check_for)) { return 5; }
  204.    
  205.     else if (check_subsequence(s3, s6, s9, check_for)) { return 9; }
  206.     else if (check_subsequence(s6, s9, s3, check_for)) { return 3; }
  207.     else if (check_subsequence(s3, s9, s6, check_for)) { return 6; }
  208.     else { return -1; }
  209. }
  210.  
  211. int check_all_sub_sequences_diagonal(int check_for)
  212. {
  213.     if      (check_subsequence(s1, s5, s9, check_for)) { return 9; }
  214.     else if (check_subsequence(s5, s9, s1, check_for)) { return 1; }
  215.     else if (check_subsequence(s1, s9, s5, check_for)) { return 5; }
  216.     else if (check_subsequence(s3, s5, s7, check_for)) { return 7; }
  217.     else if (check_subsequence(s5, s7, s3, check_for)) { return 3; }
  218.     else if (check_subsequence(s3, s7, s5, check_for)) { return 5; }
  219.     else { return -1; }
  220. }
  221.  
  222. int check_all_sub_sequences(int check_for)
  223. {
  224.     int h = check_all_sub_sequences_horizontal(check_for);
  225.     if (h > 0) { return h; }
  226.     int v = check_all_sub_sequences_vertical(check_for);
  227.     if (v > 0) { return v; }
  228.     int d = check_all_sub_sequences_diagonal(check_for);
  229.     return d;
  230. }
  231.  
  232. int check_expansion(int index)
  233. {
  234.     if (index == 1)
  235.     {
  236.         if      (s5 == 0) { return 5; }
  237.         else if (s2 == 0) { return 2; }
  238.         else if (s4 == 0) { return 4; }
  239.     }
  240.     else if (index == 2)
  241.     {
  242.         if      (s5 == 0) { return 5; }
  243.         else if (s1 == 0) { return 1; }
  244.         else if (s3 == 0) { return 3; }
  245.         else if (s4 == 0) { return 4; }
  246.         else if (s6 == 0) { return 6; }
  247.     }
  248.     else if (index == 3)
  249.     {
  250.         if      (s5 == 0) { return 5; }
  251.         else if (s2 == 0) { return 2; }
  252.         else if (s6 == 0) { return 6; }
  253.     }
  254.     else if (index == 4)
  255.     {
  256.         if      (s5 == 0) { return 5; }
  257.         else if (s1 == 0) { return 1; }
  258.         else if (s2 == 0) { return 2; }
  259.         else if (s7 == 0) { return 7; }
  260.         else if (s8 == 0) { return 8; }
  261.     }
  262.     else if (index == 5)
  263.     {
  264.         if      (s1 == 0) { return 1; }
  265.         else if (s2 == 0) { return 2; }
  266.         else if (s3 == 0) { return 3; }
  267.         else if (s4 == 0) { return 4; }
  268.         else if (s6 == 0) { return 6; }
  269.         else if (s7 == 0) { return 7; }
  270.         else if (s8 == 0) { return 8; }
  271.         else if (s9 == 0) { return 9; }
  272.     }
  273.     else if (index == 6)
  274.     {
  275.         if      (s5 == 0) { return 5; }
  276.         else if (s3 == 0) { return 3; }
  277.         else if (s2 == 0) { return 2; }
  278.         else if (s8 == 0) { return 8; }
  279.         else if (s9 == 0) { return 9; }
  280.     }
  281.     else if (index == 7)
  282.     {
  283.         if      (s5 == 0) { return 5; }
  284.         else if (s4 == 0) { return 4; }
  285.         else if (s8 == 0) { return 8; }
  286.     }
  287.     else if (index == 8)
  288.     {
  289.         if      (s5 == 0) { return 5; }
  290.         else if (s7 == 0) { return 7; }
  291.         else if (s9 == 0) { return 9; }
  292.         else if (s4 == 0) { return 4; }
  293.         else if (s6 == 0) { return 6; }
  294.     }
  295.     else if (index == 9)
  296.     {
  297.         if      (s5 == 0) { return 5; }
  298.         else if (s8 == 0) { return 8; }
  299.         else if (s6 == 0) { return 6; }
  300.     }
  301.     return -1;
  302. }
  303.  
  304. int check_all_expansions(int check_for)
  305. {
  306.     int center = check_expansion(5);
  307.     if (s5 == check_for && center > 0) { return center; }
  308.    
  309.     for (int n = 1; n <= 4; n++)
  310.     {
  311.         int result = check_expansion(n);
  312.         if (get_square(n) == check_for && result > 0) { return result; }
  313.     }
  314.    
  315.     for (int n = 6; n <= 9; n++)
  316.     {
  317.         int result = check_expansion(n);
  318.         if (get_square(n) == check_for && result > 0) { return result; }
  319.     }
  320.    
  321.     return -1;
  322. }
  323.  
  324.  
  325. int computer_player()
  326. {
  327.    
  328.     int decision = 0;
  329.    
  330.     //Win if you can
  331.     decision = check_all_sub_sequences(2);
  332.     if (decision > 0) { return decision; }
  333.    
  334.     //Block if your opponent is about to
  335.     decision = check_all_sub_sequences(1);
  336.     if (decision > 0) { return decision; }
  337.    
  338.     //Expand from an already-held square
  339.     decision = check_all_expansions(2);
  340.     if (decision > 0) { return decision; }
  341.    
  342.     //Seize the center if its free
  343.     if (s5 == 0) { return 5; }
  344.    
  345.     //Pick the first empty square otherwise
  346.     for (int n = 1; n <= 9; n++)
  347.     {
  348.         if (get_square(n) == 0)
  349.         {
  350.             return n;
  351.         }
  352.     }
  353.    
  354.     //Something went wrong
  355.     return -1;
  356. }
  357.  
  358. void gameplay(vec2 uv, int nibble0, int nibble1, int nibble2, int nibble3, int nibble4,out vec4 fragColor)
  359. {
  360.     for (int n = 1; n <= 9; n++)
  361.     {
  362.         set_square(n, 0);
  363.     }
  364.    
  365.     if (!(validate_nibble_value(nibble0) && get_square(nibble0) == 0)) { return; }
  366.     set_square(nibble0, 1);
  367.     display_cross(uv, nibble0,fragColor);
  368.    
  369.     int ai_move_1 = computer_player();
  370.     set_square(ai_move_1, 2);
  371.     display_nought(uv, ai_move_1,fragColor);
  372.    
  373.     if (!(validate_nibble_value(nibble1) && get_square(nibble1) == 0)) { return; }
  374.     set_square(nibble1, 1);
  375.     display_cross(uv, nibble1,fragColor);
  376.    
  377.     int ai_move_2 = computer_player();
  378.     set_square(ai_move_2, 2);
  379.     display_nought(uv, ai_move_2,fragColor);
  380.    
  381.     if (!(validate_nibble_value(nibble2) && get_square(nibble2) == 0)) { return; }
  382.     set_square(nibble2, 1);
  383.     display_cross(uv, nibble2,fragColor);
  384.    
  385.     if (check_game_won(uv,fragColor)) { return; }
  386.    
  387.     int ai_move_3 = computer_player();
  388.     set_square(ai_move_3, 2);
  389.     display_nought(uv, ai_move_3,fragColor);
  390.    
  391.     if (check_game_won(uv,fragColor)) { return; }
  392.    
  393.     if (!(validate_nibble_value(nibble3) && get_square(nibble3) == 0)) { return; }
  394.     set_square(nibble3, 1);
  395.     display_cross(uv, nibble3,fragColor);
  396.    
  397.     if (check_game_won(uv,fragColor)) { return; }
  398.    
  399.     int ai_move_4 = computer_player();
  400.     set_square(ai_move_4, 2);
  401.     display_nought(uv, ai_move_4,fragColor);
  402.    
  403.     if (check_game_won(uv,fragColor)) { return; }
  404.    
  405.     if (!(validate_nibble_value(nibble4) && get_square(nibble4) == 0)) { return; }
  406.     set_square(nibble4, 1);
  407.     display_cross(uv, nibble4,fragColor);
  408.    
  409.     if (check_game_won(uv,fragColor)) { return; }
  410.  
  411.    
  412.     return;
  413. }
  414.  
  415. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  416. {
  417.    
  418.     vec2 uv = fragCoord.xy / iResolution.xy;
  419.     vec2 mp = iMouse.xy / iResolution.xy;
  420.     uv.x *= iResolution.x / iResolution.y;
  421.     mp.x *= iResolution.x / iResolution.y;
  422.     int nibble0 = check_nibble(NIBBLE_0);
  423.     int nibble1 = check_nibble(NIBBLE_1);
  424.     int nibble2 = check_nibble(NIBBLE_2);
  425.     int nibble3 = check_nibble(NIBBLE_3);
  426.     int nibble4 = check_nibble(NIBBLE_4);
  427.  
  428.    
  429.     if (uv.x > 1.0)
  430.     {
  431.         fragColor = vec4(0.0);
  432.        
  433.         if (mp.x < 1.0)
  434.         {
  435.             int yVal = int(mp.y * 3.0);
  436.             int xVal = int(mp.x * 3.0);
  437.             int mVal = yVal * 3 + xVal + 1;
  438.            
  439.             if (validate_nibble_value(mVal))
  440.             {
  441.                 display_binary(uv, mVal, vec2(1.0, 0.975), vec2(1.1, 1.0),fragColor);
  442.             }
  443.         }
  444.        
  445.         display_binary(uv, nibble0, vec2(1.0, 0.025 * 4.0), vec2(1.1, 0.025 * 5.0),fragColor);
  446.         display_binary(uv, nibble1, vec2(1.0, 0.025 * 3.0), vec2(1.1, 0.025 * 4.0),fragColor);
  447.         display_binary(uv, nibble2, vec2(1.0, 0.025 * 2.0), vec2(1.1, 0.025 * 3.0),fragColor);
  448.         display_binary(uv, nibble3, vec2(1.0, 0.025 * 1.0), vec2(1.1, 0.025 * 2.0),fragColor);
  449.         display_binary(uv, nibble4, vec2(1.0, 0.025 * 0.0), vec2(1.1, 0.025 * 1.0),fragColor);
  450.         return;
  451.     }
  452.    
  453.     fragColor = vec4(0.5 + 0.5*float(
  454.         abs(uv.x - 0.3333) < 0.01 || abs(uv.x - 0.6666) < 0.01 ||
  455.         abs(uv.y - 0.3333) < 0.01 || abs(uv.y - 0.6666) < 0.01));
  456.     gameplay(uv, nibble0, nibble1, nibble2, nibble3, nibble4,fragColor);
  457. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement