Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define NIBBLE_0 vec4(81, 87, 69, 82)
- #define NIBBLE_1 vec4(65, 83, 68, 70)
- #define NIBBLE_2 vec4(90, 88, 67, 86)
- #define NIBBLE_3 vec4(84, 89 ,85, 73)
- #define NIBBLE_4 vec4(71, 72, 74, 75)
- int s1;
- int s2;
- int s3;
- int s4;
- int s5;
- int s6;
- int s7;
- int s8;
- int s9;
- int get_square(int index)
- {
- if (index <= 1) { return s1; }
- else if (index == 2) { return s2; }
- else if (index == 3) { return s3; }
- else if (index == 4) { return s4; }
- else if (index == 5) { return s5; }
- else if (index == 6) { return s6; }
- else if (index == 7) { return s7; }
- else if (index == 8) { return s8; }
- else { return s9; }
- }
- void set_square(int index, int value)
- {
- if (index <= 1) { s1 = value; }
- else if (index == 2) { s2 = value; }
- else if (index == 3) { s3 = value; }
- else if (index == 4) { s4 = value; }
- else if (index == 5) { s5 = value; }
- else if (index == 6) { s6 = value; }
- else if (index == 7) { s7 = value; }
- else if (index == 8) { s8 = value; }
- else { s9 = value; }
- }
- bool validate_nibble_value(int value)
- {
- return (value) >= 1 && (value) <= 9;
- }
- vec2 map_nibble_value(int value)
- {
- int v = value - 1;
- if (!validate_nibble_value(value)) { return vec2(-1.0, -1.0); }
- float y = floor((float(v) / 3.0)) / 3.0;
- float x = mod(float(v), 3.0) / 3.0;
- return vec2(x, y) + vec2(0.16666);
- }
- bool check_sequence(vec2 uv, int iA, int iB, int iC,out vec4 fragColor)
- {
- int a = get_square(iA);
- int b = get_square(iB);
- int c = get_square(iC);
- bool result = a != 0 && a == b && b == c;
- if (result)
- {
- vec2 aP = map_nibble_value(iA);
- vec2 bP = map_nibble_value(iB);
- vec2 cP = map_nibble_value(iC);
- float aD = (abs(aP.x - uv.x) + abs(aP.y - uv.y));
- if (aD < 0.1 && aD > 0.09) { fragColor = vec4(0.0, 1.0, 0.0, 1.0); }
- float bD = (abs(bP.x - uv.x) + abs(bP.y - uv.y));
- if (bD < 0.1 && bD > 0.09) { fragColor = vec4(0.0, 1.0, 0.0, 1.0); }
- float cD = (abs(cP.x - uv.x) + abs(cP.y - uv.y));
- if (cD < 0.1 && cD > 0.09) { fragColor = vec4(0.0, 1.0, 0.0, 1.0); }
- }
- return result;
- }
- bool check_subsequence(int a, int b, int c, int v)
- {
- return a == v && a == b && c == 0;
- }
- bool check_game_won(vec2 uv,out vec4 fragColor)
- {
- return
- check_sequence(uv, 1, 2, 3,fragColor) ||
- check_sequence(uv, 4, 5, 6,fragColor) ||
- check_sequence(uv, 7, 8, 9,fragColor) ||
- check_sequence(uv, 1, 4, 7,fragColor) ||
- check_sequence(uv, 2, 5, 8,fragColor) ||
- check_sequence(uv, 3, 6, 9,fragColor) ||
- check_sequence(uv, 1, 5, 9,fragColor) ||
- check_sequence(uv, 3, 5, 7,fragColor);
- }
- int check_key(float k)
- {
- float x = (k) / 255.0;
- return int(texture(iChannel0, vec2(x, 1.0)).r > 0.0);
- }
- int check_nibble(vec4 nibble)
- {
- return
- check_key(nibble.x) +
- check_key(nibble.y) * 2 +
- check_key(nibble.z) * 4 +
- check_key(nibble.w) * 8;
- }
- bool display_cross(vec2 uv, int x,out vec4 fragColor)
- {
- vec2 p = map_nibble_value(x);
- if (abs(uv.x - p.x) < 0.1 &&
- abs(uv.y - p.y) < 0.1 &&
- (
- abs(uv.y - (p.y - 0.01)) > abs(uv.x - p.x) &&
- abs(uv.y - (p.y + 0.01)) < abs(uv.x - p.x) ||
- abs(uv.y - (p.y - 0.01)) < abs(uv.x - p.x) &&
- abs(uv.y - (p.y + 0.01)) > abs(uv.x - p.x))
- )
- {
- fragColor = vec4(1.0, 0.0, 0.0, 1.0);
- return true;
- }
- return false;
- }
- bool display_nought(vec2 uv, int x,out vec4 fragColor)
- {
- vec2 p = map_nibble_value(x);
- if (distance(uv, p) < 0.1 &&
- distance(uv, p) > 0.08)
- {
- fragColor = vec4(0.0, 0.0, 1.0, 1.0);
- return true;
- }
- return false;
- }
- bool display_binary(vec2 uv, int nibble, vec2 corner, vec2 extents,out vec4 fragColor)
- {
- if (uv.x > corner.x && uv.x < extents.x && uv.y > corner.y && uv.y < extents.y)
- {
- fragColor = vec4(0.3, 0.0, 0.0, 1.0);;
- int value = nibble;
- for (int n = 0; n < 4; n++)
- {
- float width = float(n) / 4.0 * (extents.x - corner.x);
- float nextWidth = float(n+1) / 4.0 * (extents.x - corner.x);
- int bit = int(mod(float(value), 2.0));
- value /= 2;
- if (bit > 0 && uv.x - corner.x > width && uv.x - corner.x < nextWidth)
- {
- fragColor = vec4(1.0, 0.0, 0.0, 1.0);
- }
- }
- return true;
- }
- return false;
- }
- int check_all_sub_sequences_horizontal(int check_for)
- {
- if (check_subsequence(s1, s2, s3, check_for)) { return 3; }
- else if (check_subsequence(s2, s3, s1, check_for)) { return 1; }
- else if (check_subsequence(s1, s3, s2, check_for)) { return 2; }
- else if (check_subsequence(s4, s5, s6, check_for)) { return 6; }
- else if (check_subsequence(s5, s6, s4, check_for)) { return 4; }
- else if (check_subsequence(s4, s6, s5, check_for)) { return 5; }
- else if (check_subsequence(s7, s8, s9, check_for)) { return 9; }
- else if (check_subsequence(s8, s9, s7, check_for)) { return 7; }
- else if (check_subsequence(s9, s7, s8, check_for)) { return 8; }
- else { return -1; }
- }
- int check_all_sub_sequences_vertical(int check_for)
- {
- if (check_subsequence(s1, s4, s7, check_for)) { return 7; }
- else if (check_subsequence(s4, s7, s1, check_for)) { return 1; }
- else if (check_subsequence(s1, s7, s4, check_for)) { return 4; }
- else if (check_subsequence(s2, s5, s8, check_for)) { return 8; }
- else if (check_subsequence(s5, s8, s2, check_for)) { return 2; }
- else if (check_subsequence(s2, s8, s5, check_for)) { return 5; }
- else if (check_subsequence(s3, s6, s9, check_for)) { return 9; }
- else if (check_subsequence(s6, s9, s3, check_for)) { return 3; }
- else if (check_subsequence(s3, s9, s6, check_for)) { return 6; }
- else { return -1; }
- }
- int check_all_sub_sequences_diagonal(int check_for)
- {
- if (check_subsequence(s1, s5, s9, check_for)) { return 9; }
- else if (check_subsequence(s5, s9, s1, check_for)) { return 1; }
- else if (check_subsequence(s1, s9, s5, check_for)) { return 5; }
- else if (check_subsequence(s3, s5, s7, check_for)) { return 7; }
- else if (check_subsequence(s5, s7, s3, check_for)) { return 3; }
- else if (check_subsequence(s3, s7, s5, check_for)) { return 5; }
- else { return -1; }
- }
- int check_all_sub_sequences(int check_for)
- {
- int h = check_all_sub_sequences_horizontal(check_for);
- if (h > 0) { return h; }
- int v = check_all_sub_sequences_vertical(check_for);
- if (v > 0) { return v; }
- int d = check_all_sub_sequences_diagonal(check_for);
- return d;
- }
- int check_expansion(int index)
- {
- if (index == 1)
- {
- if (s5 == 0) { return 5; }
- else if (s2 == 0) { return 2; }
- else if (s4 == 0) { return 4; }
- }
- else if (index == 2)
- {
- if (s5 == 0) { return 5; }
- else if (s1 == 0) { return 1; }
- else if (s3 == 0) { return 3; }
- else if (s4 == 0) { return 4; }
- else if (s6 == 0) { return 6; }
- }
- else if (index == 3)
- {
- if (s5 == 0) { return 5; }
- else if (s2 == 0) { return 2; }
- else if (s6 == 0) { return 6; }
- }
- else if (index == 4)
- {
- if (s5 == 0) { return 5; }
- else if (s1 == 0) { return 1; }
- else if (s2 == 0) { return 2; }
- else if (s7 == 0) { return 7; }
- else if (s8 == 0) { return 8; }
- }
- else if (index == 5)
- {
- if (s1 == 0) { return 1; }
- else if (s2 == 0) { return 2; }
- else if (s3 == 0) { return 3; }
- else if (s4 == 0) { return 4; }
- else if (s6 == 0) { return 6; }
- else if (s7 == 0) { return 7; }
- else if (s8 == 0) { return 8; }
- else if (s9 == 0) { return 9; }
- }
- else if (index == 6)
- {
- if (s5 == 0) { return 5; }
- else if (s3 == 0) { return 3; }
- else if (s2 == 0) { return 2; }
- else if (s8 == 0) { return 8; }
- else if (s9 == 0) { return 9; }
- }
- else if (index == 7)
- {
- if (s5 == 0) { return 5; }
- else if (s4 == 0) { return 4; }
- else if (s8 == 0) { return 8; }
- }
- else if (index == 8)
- {
- if (s5 == 0) { return 5; }
- else if (s7 == 0) { return 7; }
- else if (s9 == 0) { return 9; }
- else if (s4 == 0) { return 4; }
- else if (s6 == 0) { return 6; }
- }
- else if (index == 9)
- {
- if (s5 == 0) { return 5; }
- else if (s8 == 0) { return 8; }
- else if (s6 == 0) { return 6; }
- }
- return -1;
- }
- int check_all_expansions(int check_for)
- {
- int center = check_expansion(5);
- if (s5 == check_for && center > 0) { return center; }
- for (int n = 1; n <= 4; n++)
- {
- int result = check_expansion(n);
- if (get_square(n) == check_for && result > 0) { return result; }
- }
- for (int n = 6; n <= 9; n++)
- {
- int result = check_expansion(n);
- if (get_square(n) == check_for && result > 0) { return result; }
- }
- return -1;
- }
- int computer_player()
- {
- int decision = 0;
- //Win if you can
- decision = check_all_sub_sequences(2);
- if (decision > 0) { return decision; }
- //Block if your opponent is about to
- decision = check_all_sub_sequences(1);
- if (decision > 0) { return decision; }
- //Expand from an already-held square
- decision = check_all_expansions(2);
- if (decision > 0) { return decision; }
- //Seize the center if its free
- if (s5 == 0) { return 5; }
- //Pick the first empty square otherwise
- for (int n = 1; n <= 9; n++)
- {
- if (get_square(n) == 0)
- {
- return n;
- }
- }
- //Something went wrong
- return -1;
- }
- void gameplay(vec2 uv, int nibble0, int nibble1, int nibble2, int nibble3, int nibble4,out vec4 fragColor)
- {
- for (int n = 1; n <= 9; n++)
- {
- set_square(n, 0);
- }
- if (!(validate_nibble_value(nibble0) && get_square(nibble0) == 0)) { return; }
- set_square(nibble0, 1);
- display_cross(uv, nibble0,fragColor);
- int ai_move_1 = computer_player();
- set_square(ai_move_1, 2);
- display_nought(uv, ai_move_1,fragColor);
- if (!(validate_nibble_value(nibble1) && get_square(nibble1) == 0)) { return; }
- set_square(nibble1, 1);
- display_cross(uv, nibble1,fragColor);
- int ai_move_2 = computer_player();
- set_square(ai_move_2, 2);
- display_nought(uv, ai_move_2,fragColor);
- if (!(validate_nibble_value(nibble2) && get_square(nibble2) == 0)) { return; }
- set_square(nibble2, 1);
- display_cross(uv, nibble2,fragColor);
- if (check_game_won(uv,fragColor)) { return; }
- int ai_move_3 = computer_player();
- set_square(ai_move_3, 2);
- display_nought(uv, ai_move_3,fragColor);
- if (check_game_won(uv,fragColor)) { return; }
- if (!(validate_nibble_value(nibble3) && get_square(nibble3) == 0)) { return; }
- set_square(nibble3, 1);
- display_cross(uv, nibble3,fragColor);
- if (check_game_won(uv,fragColor)) { return; }
- int ai_move_4 = computer_player();
- set_square(ai_move_4, 2);
- display_nought(uv, ai_move_4,fragColor);
- if (check_game_won(uv,fragColor)) { return; }
- if (!(validate_nibble_value(nibble4) && get_square(nibble4) == 0)) { return; }
- set_square(nibble4, 1);
- display_cross(uv, nibble4,fragColor);
- if (check_game_won(uv,fragColor)) { return; }
- return;
- }
- void mainImage( out vec4 fragColor, in vec2 fragCoord )
- {
- vec2 uv = fragCoord.xy / iResolution.xy;
- vec2 mp = iMouse.xy / iResolution.xy;
- uv.x *= iResolution.x / iResolution.y;
- mp.x *= iResolution.x / iResolution.y;
- int nibble0 = check_nibble(NIBBLE_0);
- int nibble1 = check_nibble(NIBBLE_1);
- int nibble2 = check_nibble(NIBBLE_2);
- int nibble3 = check_nibble(NIBBLE_3);
- int nibble4 = check_nibble(NIBBLE_4);
- if (uv.x > 1.0)
- {
- fragColor = vec4(0.0);
- if (mp.x < 1.0)
- {
- int yVal = int(mp.y * 3.0);
- int xVal = int(mp.x * 3.0);
- int mVal = yVal * 3 + xVal + 1;
- if (validate_nibble_value(mVal))
- {
- display_binary(uv, mVal, vec2(1.0, 0.975), vec2(1.1, 1.0),fragColor);
- }
- }
- display_binary(uv, nibble0, vec2(1.0, 0.025 * 4.0), vec2(1.1, 0.025 * 5.0),fragColor);
- display_binary(uv, nibble1, vec2(1.0, 0.025 * 3.0), vec2(1.1, 0.025 * 4.0),fragColor);
- display_binary(uv, nibble2, vec2(1.0, 0.025 * 2.0), vec2(1.1, 0.025 * 3.0),fragColor);
- display_binary(uv, nibble3, vec2(1.0, 0.025 * 1.0), vec2(1.1, 0.025 * 2.0),fragColor);
- display_binary(uv, nibble4, vec2(1.0, 0.025 * 0.0), vec2(1.1, 0.025 * 1.0),fragColor);
- return;
- }
- fragColor = vec4(0.5 + 0.5*float(
- abs(uv.x - 0.3333) < 0.01 || abs(uv.x - 0.6666) < 0.01 ||
- abs(uv.y - 0.3333) < 0.01 || abs(uv.y - 0.6666) < 0.01));
- gameplay(uv, nibble0, nibble1, nibble2, nibble3, nibble4,fragColor);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement