#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_color.h>

static int width = 560, height = 500;

int main()
{
	al_init();
	al_init_primitives_addon();
	al_init_image_addon();
	al_init_font_addon();
	al_init_ttf_addon();
	al_install_mouse();


	if (!al_init())
		return -1;

	ALLEGRO_EVENT_QUEUE *queue = NULL;

	ALLEGRO_TIMER *timer;

	timer = al_create_timer(1.0 / 15);
	al_start_timer(timer);

	al_install_keyboard();

	queue = al_create_event_queue();
	al_register_event_source(queue, al_get_keyboard_event_source());
	al_register_event_source(queue, al_get_timer_event_source(timer));

	ALLEGRO_DISPLAY *display = NULL;

	ALLEGRO_BITMAP *background = NULL;
	ALLEGRO_BITMAP *brown = NULL;
	ALLEGRO_BITMAP *blue = NULL;
	ALLEGRO_BITMAP *purple = NULL;
	ALLEGRO_BITMAP *green = NULL;
	ALLEGRO_BITMAP *orange = NULL;
	ALLEGRO_BITMAP *brownyem = NULL;
	ALLEGRO_BITMAP *blueyem = NULL;
	ALLEGRO_BITMAP *purpleyem = NULL;
	ALLEGRO_BITMAP *greenyem = NULL;
	ALLEGRO_BITMAP *orangeyem = NULL;
	ALLEGRO_BITMAP *switchbutton = NULL;

	display = al_create_display(width, height);

	if (!display)
		return -1;

	int pointx1[] = { 20,135,250,365,480 };
	int pointx2[] = { 20,135,250,365,480 };
	int pointx3[] = { 20,135,250,365,480 };
	int pointx4[] = { 20,135,250,365,480 };
	int pointx5[] = { 20,135,250,365,480 };

	int a = 0, b = 1, c = 2, d = 3, e = 4;


	background = al_load_bitmap("background.png");
	brown = al_load_bitmap("brown.png");
	blue = al_load_bitmap("blue.png");
	purple = al_load_bitmap("purple.png");
	green = al_load_bitmap("green.png");
	orange = al_load_bitmap("orange.png");

	brownyem = al_load_bitmap("brownyem.png");
	blueyem = al_load_bitmap("blueyem.png");
	purpleyem = al_load_bitmap("purpleyem.png");
	greenyem = al_load_bitmap("greenyem.png");
	orangeyem = al_load_bitmap("orangeyem.png");
	switchbutton = al_load_bitmap("switchbutton.png");

	al_draw_bitmap(background, 0, 0, 0);
	al_draw_bitmap(switchbutton, 82, 459, 0);
	al_draw_bitmap(switchbutton, 197, 459, 0);
	al_draw_bitmap(switchbutton, 312, 459, 0);
	al_draw_bitmap(switchbutton, 427, 459, 0);

	al_draw_bitmap(brown, pointx1[a], 375, 0);
	al_draw_bitmap(blue, pointx1[b], 375, 0);
	al_draw_bitmap(purple, pointx1[c], 375, 0);
	al_draw_bitmap(green, pointx1[d], 375, 0);
	al_draw_bitmap(orange, pointx1[e], 375, 0);


	while(1)
	{
		srand(time(NULL));
		int x, y, q;
		al_flip_display();
		ALLEGRO_EVENT ev;
		al_wait_for_event(queue, &ev);
		int brownpointy = 50, bluepointy = 50, greenpointy = 27, orangepointy = 50, purplepointy = 50;
		
		if (ev.type == ALLEGRO_EVENT_TIMER)
		{

			q = rand() % 5;
			y = 1 + rand() % 5;
			if (q == 0 && y == 1)
			{	
				al_draw_bitmap(brownyem, pointx1[q], brownpointy, 0);

					al_draw_bitmap(brownyem, pointx1[q], brownpointy, 0);
			}
			if (q == 1 && y == 1)
			{
				al_draw_bitmap(brownyem, pointx1[q], brownpointy, 0);

			}
			if (q == 2 && y == 1)
			{
				al_draw_bitmap(brownyem, pointx1[q], brownpointy, 0);
			}
			if (q == 3 && y == 1)
			{
				al_draw_bitmap(brownyem, pointx1[q], brownpointy, 0);
			}
			if (q == 4 && y == 1)
			{
				al_draw_bitmap(brownyem, pointx1[q], brownpointy, 0);
			}
			if (q == 0 && y == 2)
			{
				al_draw_bitmap(blueyem, pointx1[q], bluepointy, 0);
			}
			if (q == 1 && y == 2)
			{
				al_draw_bitmap(blueyem, pointx1[q], bluepointy, 0);
			}
			if (q == 2 && y == 2)
			{
				al_draw_bitmap(blueyem, pointx1[q], bluepointy, 0);
			}
			if (q == 3 && y == 2)
			{
				al_draw_bitmap(blueyem, pointx1[q], bluepointy, 0);
			}
			if (q == 4 && y == 2)
			{
				al_draw_bitmap(blueyem, pointx1[q], bluepointy, 0);
			}

			if (q == 0 && y == 3)
			{
				al_draw_bitmap(purpleyem, pointx1[q], purplepointy, 0);
			}
			if (q == 1 && y == 3)
			{
				al_draw_bitmap(purpleyem, pointx1[q], purplepointy, 0);
			}
			if (q == 2 && y == 3)
			{
				al_draw_bitmap(purpleyem, pointx1[q], purplepointy, 0);
			}
			if (q == 3 && y == 3)
			{
				al_draw_bitmap(purpleyem, pointx1[q], purplepointy, 0);
			}
			if (q == 4 && y == 3)
			{
				al_draw_bitmap(purpleyem, pointx1[q], purplepointy, 0);
			}

			if (q == 0 && y == 4)
			{
				al_draw_bitmap(greenyem, pointx1[q], greenpointy, 0);
			}
			if (q == 1 && y == 4)
			{
				al_draw_bitmap(greenyem, pointx1[q], greenpointy, 0);
			}
			if (q == 2 && y == 4)
			{
				al_draw_bitmap(greenyem, pointx1[q], greenpointy, 0);
			}
			if (q == 3 && y == 4)
			{
				al_draw_bitmap(greenyem, pointx1[q], greenpointy, 0);
			}
			if (q == 4 && y == 4)
			{
				al_draw_bitmap(greenyem, pointx1[q], greenpointy, 0);
			}

			if (q == 0 && y == 5)
			{
				al_draw_bitmap(orangeyem, pointx1[q], orangepointy, 0);
			}
			if (q == 1 && y == 5)
			{
				al_draw_bitmap(orangeyem, pointx1[q], orangepointy, 0);
			}
			if (q == 2 && y == 5)
			{
				al_draw_bitmap(orangeyem, pointx1[q], orangepointy, 0);
			}
			if (q == 3 && y == 5)
			{
				al_draw_bitmap(orangeyem, pointx1[q], orangepointy, 0);
			}
			if (q == 4 && y == 5)
			{
				al_draw_bitmap(orangeyem, pointx1[q], orangepointy, 0);
			}
		}
		

		if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_A)
		{
			if (a == 0)
			{
				if (b == 1)
				{
					a += 1;
					b -= 1;
					al_draw_bitmap(brown, pointx1[a], 375, 0);
					al_draw_bitmap(blue, pointx1[b], 375, 0);

				}
				else if (c == 1)
				{
					a += 1;
					c -= 1;
					al_draw_bitmap(brown, pointx1[a], 375, 0);
					al_draw_bitmap(purple, pointx1[c], 375, 0);
				}
				else if (d == 1)
				{
					a += 1;
					d -= 1;
					al_draw_bitmap(brown, pointx1[a], 375, 0);
					al_draw_bitmap(green, pointx1[d], 375, 0);
				}
				else if (e == 1)
				{
					a += 1;
					e -= 1;
					al_draw_bitmap(brown, pointx1[a], 375, 0);
					al_draw_bitmap(orange, pointx1[e], 375, 0);
				}
			}
			else if (b == 0)
			{
				if (a == 1)
				{
					b += 1;
					a -= 1;
					al_draw_bitmap(blue, pointx1[b], 375, 0);
					al_draw_bitmap(brown, pointx1[a], 375, 0);
				}
				else if (c == 1)
				{
					b += 1;
					c -= 1;
					al_draw_bitmap(blue, pointx1[b], 375, 0);
					al_draw_bitmap(purple, pointx1[c], 375, 0);
				}
				else if (d == 1)
				{
					b += 1;
					d -= 1;
					al_draw_bitmap(blue, pointx1[b], 375, 0);
					al_draw_bitmap(green, pointx1[d], 375, 0);
				}
				else if (e == 1)
				{
					b += 1;
					e -= 1;
					al_draw_bitmap(blue, pointx1[b], 375, 0);
					al_draw_bitmap(orange, pointx1[e], 375, 0);
				}
			}
			else if (c == 0)
			{
				if (a == 1)
				{
					c += 1;
					a -= 1;
					al_draw_bitmap(brown, pointx1[a], 375, 0);
					al_draw_bitmap(purple, pointx1[c], 375, 0);
				}
				else if (b == 1)
				{
					c += 1;
					b -= 1;
					al_draw_bitmap(blue, pointx1[b], 375, 0);
					al_draw_bitmap(purple, pointx1[c], 375, 0);
				}
				else if (d == 1)
				{
					c += 1;
					d -= 1;
					al_draw_bitmap(purple, pointx1[c], 375, 0);
					al_draw_bitmap(green, pointx1[d], 375, 0);
				}
				else if (e == 1)
				{
					c += 1;
					e -= 1;
					al_draw_bitmap(purple, pointx1[c], 375, 0);
					al_draw_bitmap(orange, pointx1[e], 375, 0);
				}
			}
			else if (d == 0)
			{
				if (a == 1)
				{
					d += 1;
					a -= 1;
					al_draw_bitmap(brown, pointx1[a], 375, 0);
					al_draw_bitmap(green, pointx1[d], 375, 0);
				}
				else if (b == 1)
				{
					d += 1;
					b -= 1;
					al_draw_bitmap(blue, pointx1[b], 375, 0);
					al_draw_bitmap(green, pointx1[d], 375, 0);
				}
				else if (c == 1)
				{
					d += 1;
					c -= 1;
					al_draw_bitmap(purple, pointx1[c], 375, 0);
					al_draw_bitmap(green, pointx1[d], 375, 0);
				}
				else if (e == 1)
				{
					d += 1;
					e -= 1;
					al_draw_bitmap(green, pointx1[d], 375, 0);
					al_draw_bitmap(orange, pointx1[e], 375, 0);
				}
			}
			else if (e == 0)
			{
				if (a == 1)
				{
					e += 1;
					a -= 1;
					al_draw_bitmap(brown, pointx1[a], 375, 0);
					al_draw_bitmap(orange, pointx1[e], 375, 0);
				}
				else if (b == 1)
				{
					e += 1;
					b -= 1;
					al_draw_bitmap(blue, pointx1[b], 375, 0);
					al_draw_bitmap(orange, pointx1[e], 375, 0);
				}
				else if (c == 1)
				{
					e += 1;
					c -= 1;
					al_draw_bitmap(purple, pointx1[c], 375, 0);
					al_draw_bitmap(orange, pointx1[e], 375, 0);
				}
				else if (d == 1)
				{
					e += 1;
					d -= 1;
					al_draw_bitmap(green, pointx1[d], 375, 0);
					al_draw_bitmap(orange, pointx1[e], 375, 0);
				}
			}
		}

		if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_S)
		{
			if (a == 1)
			{
				if (b == 2)
				{
					a += 1;
					b -= 1;
				}
				else if (c == 2)
				{
					a += 1;
					c -= 1;
				}
				else if (d == 2)
				{
					a += 1;
					d -= 1;
				}
				else if (e == 2)
				{
					a += 1;
					e -= 1;
				}
			}
			else if (b == 1)
			{
				if (a == 2)
				{
					b += 1;
					a -= 1;
				}
				else if (c == 2)
				{
					b += 1;
					c -= 1;
				}
				else if (d == 2)
				{
					b += 1;
					d -= 1;
				}
				else if (e == 2)
				{
					b += 1;
					e -= 1;
				}
			}
			else if (c == 1)
			{
				if (a == 2)
				{
					c += 1;
					a -= 1;
				}
				else if (b == 2)
				{
					c += 1;
					b -= 1;
				}
				else if (d == 2)
				{
					c += 1;
					d -= 1;
				}
				else if (e == 2)
				{
					c += 1;
					e -= 1;
				}
			}
			else if (d == 1)
			{
				if (a == 2)
				{
					d += 1;
					a -= 1;
				}
				else if (b == 2)
				{
					d += 1;
					b -= 1;
				}
				else if (c == 2)
				{
					d += 1;
					c -= 1;
				}
				else if (e == 2)
				{
					d += 1;
					e -= 1;
				}
			}
			else if (e == 1)
			{
				if (a == 2)
				{
					e += 1;
					a -= 1;
				}
				else if (b == 2)
				{
					e += 1;
					b -= 1;
				}
				else if (c == 2)
				{
					e += 1;
					c -= 1;
				}
				else if (d == 2)
				{
					e += 1;
					d -= 1;
				}
			}
			al_draw_bitmap(brown, pointx1[a], 375, 0);
			al_draw_bitmap(blue, pointx1[b], 375, 0);
			al_draw_bitmap(purple, pointx1[c], 375, 0);
			al_draw_bitmap(green, pointx1[d], 375, 0);
			al_draw_bitmap(orange, pointx1[e], 375, 0);
		}

		if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_D)
		{
			if (a == 2)
			{
				if (b == 3)
				{
					a += 1;
					b -= 1;
				}
				else if (c == 3)
				{
					a += 1;
					c -= 1;
				}
				else if (d == 3)
				{
					a += 1;
					d -= 1;
				}
				else if (e == 3)
				{
					a += 1;
					e -= 1;
				}
			}
			else if (b == 2)
			{
				if (a == 3)
				{
					b += 1;
					a -= 1;
				}
				else if (c == 3)
				{
					b += 1;
					c -= 1;
				}
				else if (d == 3)
				{
					b += 1;
					d -= 1;
				}
				else if (e == 3)
				{
					b += 1;
					e -= 1;
				}
			}
			else if (c == 2)
			{
				if (a == 3)
				{
					c += 1;
					a -= 1;
				}
				else if (b == 3)
				{
					c += 1;
					b -= 1;
				}
				else if (d == 3)
				{
					c += 1;
					d -= 1;
				}
				else if (e == 3)
				{
					c += 1;
					e -= 1;
				}
			}
			else if (d == 2)
			{
				if (a == 3)
				{
					d += 1;
					a -= 1;
				}
				else if (b == 3)
				{
					d += 1;
					b -= 1;
				}
				else if (c == 3)
				{
					d += 1;
					c -= 1;
				}
				else if (e == 3)
				{
					d += 1;
					e -= 1;
				}
			}
			else if (e == 2)
			{
				if (a == 3)
				{
					e += 1;
					a -= 1;
				}
				else if (b == 3)
				{
					e += 1;
					b -= 1;
				}
				else if (c == 3)
				{
					e += 1;
					c -= 1;
				}
				else if (d == 3)
				{
					e += 1;
					d -= 1;
				}
			}
			al_draw_bitmap(brown, pointx1[a], 375, 0);
			al_draw_bitmap(blue, pointx1[b], 375, 0);
			al_draw_bitmap(purple, pointx1[c], 375, 0);
			al_draw_bitmap(green, pointx1[d], 375, 0);
			al_draw_bitmap(orange, pointx1[e], 375, 0);
		}

		if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_F)
		{
			if (a == 3)
			{
				if (b == 4)
				{
					a += 1;
					b -= 1;
				}
				else if (c == 4)
				{
					a += 1;
					c -= 1;
				}
				else if (d == 4)
				{
					a += 1;
					d -= 1;
				}
				else if (e == 4)
				{
					a += 1;
					e -= 1;
				}
			}
			else if (b == 3)
			{
				if (a == 4)
				{
					b += 1;
					a -= 1;
				}
				else if (c == 4)
				{
					b += 1;
					c -= 1;
				}
				else if (d == 4)
				{
					b += 1;
					d -= 1;
				}
				else if (e == 4)
				{
					b += 1;
					e -= 1;
				}
			}
			else if (c == 3)
			{
				if (a == 4)
				{
					c += 1;
					a -= 1;
				}
				else if (b == 4)
				{
					c += 1;
					b -= 1;
				}
				else if (d == 4)
				{
					c += 1;
					d -= 1;
				}
				else if (e == 4)
				{
					c += 1;
					e -= 1;
				}
			}
			else if (d == 3)
			{
				if (a == 4)
				{
					d += 1;
					a -= 1;
				}
				else if (b == 4)
				{
					d += 1;
					b -= 1;
				}
				else if (c == 4)
				{
					d += 1;
					c -= 1;
				}
				else if (e == 4)
				{
					d += 1;
					e -= 1;
				}
			}
			else if (e == 3)
			{
				if (a == 4)
				{
					e += 1;
					a -= 1;
				}
				else if (b == 4)
				{
					e += 1;
					b -= 1;
				}
				else if (c == 4)
				{
					e += 1;
					c -= 1;
				}
				else if (d == 4)
				{
					e += 1;
					d -= 1;
				}
			}
			al_draw_bitmap(brown, pointx1[a], 375, 0);
			al_draw_bitmap(blue, pointx1[b], 375, 0);
			al_draw_bitmap(purple, pointx1[c], 375, 0);
			al_draw_bitmap(green, pointx1[d], 375, 0);
			al_draw_bitmap(orange, pointx1[e], 375, 0);
		}

	}

	al_rest(400);
}


