off to a good start?

i am just getting started and am not nearly done and havent even gotten around to getting the 'O' to happen yet. but am i at least getting there? tips?

only reason i am posting it now is because i am leaving for a while tonight and wont be back till later.




#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>

int main()
{
int POne = 1;
int PTwo = 0;

int one = 0;
int two = 0;
int three = 0;
int four = 0;
int five = 0;
int six = 0;
int seven = 0;
int eight = 0;
int nine = 0;

ALLEGRO_DISPLAY *Screen;

ALLEGRO_EVENT_QUEUE *EventQueue;
ALLEGRO_EVENT Event;

ALLEGRO_BITMAP *TicTacToe = NULL;
ALLEGRO_BITMAP *X = NULL;
ALLEGRO_BITMAP *O = NULL;

bool Exit = false;

al_init();
al_init_image_addon();
al_install_mouse();

Screen = al_create_display(96, 128);

EventQueue = al_create_event_queue();
al_register_event_source(EventQueue, al_get_display_event_source(Screen));
al_register_event_source(EventQueue, al_get_mouse_event_source());

TicTacToe = al_load_bitmap("TicTacToe.png");
X = al_load_bitmap("X.png");
O = al_load_bitmap("O.png");

al_draw_bitmap(TicTacToe, 0, 0, 0);

while(Exit == false)
{
ALLEGRO_EVENT ev;

al_wait_for_event(EventQueue, &ev);

al_draw_bitmap(TicTacToe, 0, 0, 0);

if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
{
if(ev.mouse.button & 1 && POne == 1 && one == 0 && Event.mouse.x >= 0 && Event.mouse.x <= 32 && Event.mouse.y >= 0 && Event.mouse.y <= 32)
{
one = 1;
POne == 0;
PTwo == 1;
}
else if(ev.mouse.button & 2 && POne == 0 && one == 0 && Event.mouse.x >= 0 && Event.mouse.x <= 32 && Event.mouse.y >= 0 && Event.mouse.y <= 32)
{
one = 2;
POne == 1;
PTwo == 0;
}

if(ev.mouse.button & 1 && POne == 1 && two == 0 && Event.mouse.x >= 32 && Event.mouse.x <= 64 && Event.mouse.y >= 0 && Event.mouse.y <= 32)
{
two = 1;
POne == 0;
PTwo == 1;
}
else if(ev.mouse.button & 1 && POne == 0 && two == 0 && Event.mouse.x >= 32 && Event.mouse.x <= 64 && Event.mouse.y >= 0 && Event.mouse.y <= 32)
{
two = 2;
POne == 1;
PTwo == 0;
}

if(ev.mouse.button & 1 && POne == 1 && three == 0 && Event.mouse.x >= 64 && Event.mouse.x <= 96 && Event.mouse.y >= 0 && Event.mouse.y <= 32)
{
three = 1;
POne == 0;
PTwo == 1;
}
else if(ev.mouse.button & 1 && POne == 0 && three == 0 && Event.mouse.x >= 64 && Event.mouse.x <= 96 && Event.mouse.y >= 0 && Event.mouse.y <= 32)
{
three = 2;
POne == 1;
PTwo == 0;
}

if(ev.mouse.button & 1 && POne == 1 && four == 0 && Event.mouse.x >= 0 && Event.mouse.x <= 32 && Event.mouse.y >= 32 && Event.mouse.y <= 64)
{
four = 1;
POne == 0;
PTwo == 1;
}
else if(ev.mouse.button & 1 && POne == 0 && four == 0 && Event.mouse.x >= 0 && Event.mouse.x <= 32 && Event.mouse.y >= 32 && Event.mouse.y <= 64)
{
four = 2;
POne == 1;
PTwo == 0;
}

if(ev.mouse.button & 1 && POne == 1 && five == 0 && Event.mouse.x >= 32 && Event.mouse.x <= 64 && Event.mouse.y >= 32 && Event.mouse.y <= 64)
{
five = 1;
POne == 0;
PTwo == 1;
}
else if(ev.mouse.button & 1 && POne == 0 && five == 0 && Event.mouse.x >= 64 && Event.mouse.x <= 96 && Event.mouse.y >= 0 && Event.mouse.y <= 32)
{
five = 2;
POne == 1;
PTwo == 0;
}

if(ev.mouse.button & 1 && POne == 1 && six == 0 && Event.mouse.x >= 64 && Event.mouse.x <= 96 && Event.mouse.y >= 0 && Event.mouse.y <= 32)
{
six = 1;
POne == 0;
PTwo == 1;
}
else if(ev.mouse.button & 1 && POne == 0 && six == 0 && Event.mouse.x >= 64 && Event.mouse.x <= 96 && Event.mouse.y >= 0 && Event.mouse.y <= 32)
{
six = 2;
POne == 1;
PTwo == 0;
}

if(ev.mouse.button & 1 && POne == 1 && seven == 0 && Event.mouse.x >= 0 && Event.mouse.x <= 32 && Event.mouse.y >= 32 && Event.mouse.y <= 64)
{
four = 1;
POne == 0;
PTwo == 1;
}
else if(ev.mouse.button & 1 && POne == 0 && seven == 0 && Event.mouse.x >= 0 && Event.mouse.x <= 32 && Event.mouse.y >= 32 && Event.mouse.y <= 64)
{
four = 2;
POne == 1;
PTwo == 0;
}

if(ev.mouse.button & 1 && POne == 1 && eight == 0 && Event.mouse.x >= 32 && Event.mouse.x <= 64 && Event.mouse.y >= 32 && Event.mouse.y <= 64)
{
five = 1;
POne == 0;
PTwo == 1;
}
else if(ev.mouse.button & 1 && POne == 0 && eight == 0 && Event.mouse.x >= 64 && Event.mouse.x <= 96 && Event.mouse.y >= 0 && Event.mouse.y <= 32)
{
five = 2;
POne == 1;
PTwo == 0;
}

if(ev.mouse.button & 1 && POne == 1 && nine == 0 && Event.mouse.x >= 64 && Event.mouse.x <= 96 && Event.mouse.y >= 0 && Event.mouse.y <= 32)
{
six = 1;
POne == 0;
PTwo == 1;
}
else if(ev.mouse.button & 1 && POne == 0 && nine == 0 && Event.mouse.x >= 64 && Event.mouse.x <= 96 && Event.mouse.y >= 0 && Event.mouse.y <= 32)
{
six = 2;
POne == 1;
PTwo == 0;
}
}

if(one == 1)
{
al_draw_bitmap(X, 0, 0, 0);
}
else if(one == 2)
{
al_draw_bitmap(O, 0, 0, 0);
}
if(two == 1)
{
al_draw_bitmap(X, 32, 0, 0);
}
else if(two == 2)
{
al_draw_bitmap(O, 32, 0, 0);
}
if(three == 1)
{
al_draw_bitmap(X, 64, 0, 0);
}
else if(three == 2)
{
al_draw_bitmap(O, 64, 0, 0);
}
if(four == 1)
{
al_draw_bitmap(X, 0, 32, 0);
}
else if(four == 2)
{
al_draw_bitmap(O, 0, 32, 0);
}
if(five == 1)
{
al_draw_bitmap(X, 32, 32, 0);
}
else if(five == 2)
{
al_draw_bitmap(O, 32, 32, 0);
}
if(six == 1)
{
al_draw_bitmap(X, 64, 32, 0);
}
else if(six == 2)
{
al_draw_bitmap(O, 64, 32, 0);
}
if(seven == 1)
{
al_draw_bitmap(X, 0, 64, 0);
}
else if(seven == 2)
{
al_draw_bitmap(O, 0, 64, 0);
}
if(eight == 1)
{
al_draw_bitmap(X, 32, 64, 0);
}
else if(eight == 2)
{
al_draw_bitmap(O, 32, 64, 0);
}
if(nine == 1)
{
al_draw_bitmap(X, 64, 64, 0);
}
else if(nine == 2)
{
al_draw_bitmap(O, 64, 64, 0);
}

al_flip_display();

al_wait_for_event(EventQueue, &Event);

if(Event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
Exit = true;
}
}

return 0;
}
tic tac toe game

the board is 96x96 and x and o are 32x32
I don't personally use allegro but I think there could be some improvements.

I feel like you could make a grid of tiles. Here is pseudo code for a tile class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
enum Player {X, O, NA};

struct Rect
{
    float x;
    float y;
    float width;
    float height;
};

class Tile
{
    public:
        Tile(Rect rect);
        
        bool onMouseEnter(/*Allegro Mouse*/ mouse);
        bool onMouseExit(/*Allegro Mouse*/ mouse);
        bool onMouseDown(/*Allegro Mouse*/ mouse);
        
        bool changeTile(Player player);
    private:
        Rect rect;
        /*Allegro sprite*/ sprite;
        
        bool hovering;
        
        Player player;
        //You could also possibly add the mouse here 
        //and maybe an "Update" function
        //that will constantly call the onMouseEnter/Exit/Down and do w/e is required with them
};

Tile::Tile(Rect rect)
    : rect(rect), sprite(/*default image*/), hovering(false), player(Player::NA)
{
    /*Anything else you may need*/
}

bool Tile::onMouseEnter(/*Allegro Mouse*/ mouse)
{
    if(!hovering && mouse.x >= rect.x && mouse.x <= rect.x + rect.width
    && mouse.y >= rect.y && mouse.y <= rect.y + rect.height)
    {
        hovering = true;
    }
    
    return hovering;
}

bool Tile::onMouseExit(/*Allegro Mouse*/ mouse)
{
    if(!hovering) return false;
    else if((mouse.x < rect.x || mouse.x > rect.x + rect.width)
    && (mouse.y < rect.y || mouse.y > rect.y + rect.height))
    {
        hovering = false;
    }
    
    return !hovering;
}

bool Tile::onMouseDown(/*Allegro Mouse*/ mouse)
{
    return hovering && mouse.isDown(); //replace the mouse.isDown() with w/e function you use to detect
}

bool Tile::changeTile(Player player)
{
    if(this->player != Player::NA)
    {
        this->player = player;
        
        //change the tile to a new image
        
        return true;
    }
    
    return false;
}


You will need some sort of "Manager" or an update function that will do stuff like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if(tile.onMouseEnter())
{
    //highlight it or w.e
    //maybe make it red if a tile is already there
}

if(tile.onMouseExit())
{
    //stop highlighting or w.e
}

if(tile.onMouseDown() && tile.changeTile(currentPlayer))
{
    currentPlayer = currentPlayer == Player::X ? Player::O : Player::X;
}
else
{
    //tell player there is already a tile there maybe give it a red glow for
    //a little bit?
}


That's just my 2cents though.
Topic archived. No new replies allowed.