#include "api2/api2.h" var buttons; var normalColorBrush; var winColorBrush; var coverColorBrush; int winButtonIndex; var infoLabel; var timer; int startShufflingInterval = 200; int currentShufflingInterval; bool s = false; void newGameButtonClick(not_used void *context, not_used uint32_t index, AFObject *sender, not_used AFObject *eventArguments) { AFSetInterval(timer, currentShufflingInterval = startShufflingInterval); AFSetEnabled(timer, true); AFSetCaption(infoLabel, AFS("The game is on!")); } void cardButtonClick(not_used void *context, not_used uint32_t index, AFObject *sender, not_used AFObject *eventArguments) { var winButton = AFGetObjectAtIndex(buttons, winButtonIndex); for_each (button, index in buttons) { if (winButton == sender) { AFSetCaption(infoLabel, AFS("You won!")); } else { AFSetCaption(infoLabel, AFS("Maybe next time!")); } } } void timeIsUp(void *context, not_used uint32_t index, not_used AFObject *sender, not_used AFObject *eventArguments) { winButtonIndex = rand() % 3; for_each (button, index in buttons) { if (winButtonIndex == index) { AFSetBackgroundBrush(button, winColorBrush); } else { AFSetBackgroundBrush(button, normalColorBrush); } } currentShufflingInterval -= 2; AFSetInterval(timer, currentShufflingInterval); if (currentShufflingInterval <= 100) { AFSetEnabled(timer, false); for_each (button, index in buttons) { AFSetBackgroundBrush(button, coverColorBrush); AFSetButtonBackgroundBrush(button, coverColorBrush); AFSetButtonHoverBackgroundBrush(button, coverColorBrush); } } } int main() { buttons = AFCreateMutableArrayList(); normalColorBrush = AFCreateSolidColorBrushWithRgb(100, 200, 50); winColorBrush = AFCreateSolidColorBrushWithRgb(50, 0, 150); coverColorBrush = AFCreateSolidColorBrushWithRgb(250, 10, 10); AFObtain(buttons); AFObtain(normalColorBrush); AFObtain(winColorBrush); AFObtain(coverColorBrush); var application = AFCreateApplication(); var window = AFCreateWindow(); AFAppendComponent(application, window); AFSetCaption(window, AFS("Three")); AFSetContentHorizontalAlignmentWithInt32(window, cAFHorizontalAlignmentCenter); AFSetContentVerticalAlignmentWithInt32(window, cAFVerticalAlignmentCenter); var mainPanel = AFCreateFlowGridPanel(); AFSetContent(window, mainPanel); AFSetColumnsCount(mainPanel, 1); AFSetRowsCount(mainPanel, 2); AFSetVerticalSpacing(mainPanel, 25); var buttonPanel = AFCreateFlowGridPanel(); AFAppendComponent(mainPanel, buttonPanel); AFSetColumnsCount(buttonPanel, 3); AFSetRowsCount(buttonPanel, 1); AFSetHorizontalSpacing(buttonPanel, 5); for (size_t index = 0; index < 3; ++index) { var button = AFCreateButton(); AFAppendComponent(buttonPanel, button); AFSetFrameWithRectangle(button, AFRectangleMake(0, 0, 100, 100)); AFAppendObject(buttons, button); AFAddEventHandlerFunction(button, AFMouseClick, NULL, cardButtonClick); } var statusPanel = AFCreateFlowGridPanel(); AFAppendComponent(mainPanel, statusPanel); AFSetColumnsCount(statusPanel, 1); AFSetRowsCount(statusPanel, 2); AFSetVerticalSpacing(statusPanel, 25); infoLabel = AFCreateLabel(); AFAppendComponent(statusPanel, infoLabel); var button = AFCreateButton(); AFAppendComponent(statusPanel, button); AFSetCaption(button, AFS("New game")); AFAddEventHandlerFunction(button, AFMouseClick, NULL, newGameButtonClick); timer = AFCreateTimer(); AFObtain(timer); AFAddEventHandlerFunction(timer, AFTimeIsUp, application, timeIsUp); AFShow(window); AFRunMainLoop(application); AFRelease(buttons); return 0; }