rendered paste body#include <allegro.h>
#include <iostream>
#include "resource.h"
#include "Game.h"
using namespace std;
BITMAP *background;
BITMAP *numbers;
BITMAP *numbers15;
BITMAP *soundWindow;
BITMAP *creditWindow;
BITMAP *cursor;
BITMAP *msgWindow;
BITMAP *congrats;
BITMAP *themeWindow;
BITMAP *welcome;
BITMAP *buffer;
DATAFILE *gfx;
DATAFILE *sounds;
FONT *mainfont;
SAMPLE *winsound;
SAMPLE *move;
MIDI *theme1;
MIDI *theme2;
bool soundWinShown = false;
bool creditWinShown = false;
bool msgWinShown = false;
bool eggWinShown = false;
bool windowOpen = false;
bool win = false;
int currentsong = 0;
int selected = -1;
int svolume = 255;
int mvolume = 255;
int boardsize = 0;
int moves = 0;
/**********************************************
* Function: game(void)
* Purpose: Default Constructor
**********************************************/
Game::Game()
{
}
/**********************************************
* Function: ~game(void)
* Purpose: Destructor
**********************************************/
Game::~Game()
{
destroy_bitmap(background);
destroy_bitmap(numbers);
destroy_bitmap(numbers15);
destroy_bitmap(soundWindow);
destroy_bitmap(creditWindow);
destroy_bitmap(msgWindow);
destroy_bitmap(congrats);
destroy_bitmap(welcome);
destroy_bitmap(cursor);
destroy_bitmap(themeWindow);
destroy_font(mainfont);
unload_datafile(gfx);
unload_datafile(sounds);
}
/**********************************************
* Function: initGame(void)
* Purpose: Initializes components of the game
*
* @return - True, success; False, fail
**********************************************/
bool Game::initGame()
{
// Prepare Allegro //*******************
if(install_allegro(GFX_AUTODETECT, &errno, atexit) != 0)
{
allegro_message("Error initializing Allegro Graphics | install_allegro(...)\n\n");
return false;
}
// Graphics //*******************
set_color_depth(32);
if(set_gfx_mode(GFX_AUTODETECT_WINDOWED, 600, 400, 0, 0) != 0)
{
allegro_message("Error initializing Allegro gfx_mode | set_gfx_mode(...)\n\n");
return false;
}
gfx = load_datafile("gfx/gfx.dat");
sounds = load_datafile("sound/sounds.dat");
loadImages();
set_window_title("Ascending Order");
clear(screen);
blit(background, screen, 0, 0, 0, 0, 600, 400);
set_palette(desktop_palette);
// Componets //*******************
if(install_keyboard() != 0)
{
allegro_message("Error initializing Allegro Keyboard | install_keyboard(...)\n\n");
return false;
}
setupMouse();
// Sounds //*******************
if(install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, "") != 0)
{
allegro_message("Error initializing Allegro sounds | install_sound(...)\n\n");
return false;
}
loadSounds();
// Misc //********************
srand(time(NULL));
randomizeTiles();
updateTiles();
songlength = get_midi_length(theme1);
play_midi(theme1, 0);
showMessage(1);
return true; // success!
}
/**********************************************
* Function: showMessage(int)
* Purpose: pass 1 to show 'Welcome', pass 2 to show 'Congrat.."
**********************************************/
void Game::showMessage(int number)
{
switch(number)
{
case 1:
masked_blit(welcome, screen, 0, 0, 377, 5, 300, 50);
break;
case 2:
masked_blit(congrats, screen, 0, 0, 377, 5, 300, 50);
break;
}
}
/**********************************************
* Function: gameUpdate(void)
* Purpose: Game Loop
**********************************************/
void Game::gameUpdate()
{
checkSong();
int mx = mouse_x;
int my = mouse_y;
detectSelection(mx, my);
if(win == false)
{
detectWin();
}
if(mouse_b & 1)
handleMouse(mx, my);
}
/**********************************************
* Function: loadImages(void)
* Purpose: Loading images into pointers
**********************************************/
void Game::loadImages()
{
themeSelector(1); // Call for Default Theme
//themeWindow = load_bitmap("gfx/theme.bmp", NULL);
themeWindow = (BITMAP *)gfx[THEME].dat;
//mainfont = load_font("gfx/font.pcx", 0, 0);
mainfont = (FONT *)gfx[MAINFONT].dat;
}
/**********************************************
* Function: themeSelector(int)
* Purpose: Easy way to load bitmaps for specific themes
**********************************************/
void Game::themeSelector(int selection)
{
switch(selection)
{
case 1: // default
//background = load_bitmap("gfx/background.bmp", NULL);
background = (BITMAP *)gfx[BACKGROUND].dat;
//numbers = load_bitmap("gfx/numbers.bmp", NULL);
numbers = (BITMAP *)gfx[NUMBERS].dat;
//numbers15 = load_bitmap("gfx/15numbers.bmp", NULL);
numbers15 = (BITMAP *)gfx[NUMBERS15].dat;
//cursor = load_bitmap("gfx/cursor.bmp", NULL);
cursor = (BITMAP *)gfx[CURSOR].dat;
//soundWindow = load_bitmap("gfx/sound.bmp", NULL);
soundWindow = (BITMAP *)gfx[SOUND].dat;
//creditWindow = load_bitmap("gfx/credits.bmp", NULL);
creditWindow = (BITMAP *)gfx[CREDITS].dat;
//msgWindow = load_bitmap("gfx/message.bmp", NULL);
msgWindow = (BITMAP *)gfx[MESSAGE].dat;
//congrats = load_bitmap("gfx/win.bmp", NULL);
congrats = (BITMAP *)gfx[CONGRATS].dat;
//welcome = load_bitmap("gfx/welcome.bmp", NULL);
welcome = (BITMAP *)gfx[WELCOME].dat;
scare_mouse();
set_mouse_sprite(cursor);
show_mouse(screen);
break;
case 2: // forest theme
//background = load_bitmap("gfx/backgroundG.bmp", NULL);
background = (BITMAP *)gfx[BACKGROUNDG].dat;
//numbers = load_bitmap("gfx/numbersG.bmp", NULL);
numbers = (BITMAP *)gfx[NUMBERSG].dat;
//numbers15 = load_bitmap("gfx/15numbersG.bmp", NULL);
numbers15 = (BITMAP *)gfx[NUMBERS15G].dat;
//cursor = load_bitmap("gfx/cursorG.bmp", NULL);
cursor = (BITMAP *)gfx[CURSORG].dat;
//soundWindow = load_bitmap("gfx/soundG.bmp", NULL);
soundWindow = (BITMAP *)gfx[SOUNDG].dat;
//creditWindow = load_bitmap("gfx/creditsG.bmp", NULL);
creditWindow = (BITMAP *)gfx[CREDITSG].dat;
//msgWindow = load_bitmap("gfx/messageG.bmp", NULL);
msgWindow = (BITMAP *)gfx[MESSAGEG].dat;
//congrats = load_bitmap("gfx/winG.bmp", NULL);
congrats = (BITMAP *)gfx[CONGRATSG].dat;
scare_mouse();
set_mouse_sprite(cursor);
show_mouse(screen);
break;
}
}
/**********************************************
* Function: loadSounds(void)
* Purpose: Loading sounds into pointers
**********************************************/
void Game::loadSounds()
{
//move = load_sample("samples/sound1.wav");
move = (SAMPLE *)sounds[SELECT].dat;
//winsound = load_sample("samples/win.wav");
winsound = (SAMPLE *)sounds[WIN].dat;
//theme1 = load_midi("music/peace.mid");
theme1 = (MIDI *)sounds[THEME1].dat;
//theme2 = load_midi("music/peace2.mid");
theme2 = (MIDI *)sounds[THEME2].dat;
}
/**********************************************
* Function: randomizeTiles(void)
* Purpose: Randomizes all of the tiles
**********************************************/
void Game::randomizeTiles()
{
int i, ninepo;
int num = 1;
int num2 = 1; // Variable to contain the tile switching with the blank spot
if(boardsize == 0)
{
ninepo = 8; // The position of the blank spot
for(i = 0; i <= 9; i++)
{
tiles[i] = num;
num++;
}
}
else
{
ninepo = 15; // The position of the blank spot
for(i = 0; i <= 16; i++)
{
tiles[i] = num;
num++;
}
}
/*
This for loop chooses a random direction, and switches the tile in that direction from
the blank spot, with the blank spot. It also checks to make sure that if the blank spot
won't switch with a tile that doesn't exist.
*/
if(boardsize == 0)
{
for(i = 0; i <= 200; i++)
{
num = rand() % 4;
if(num == 0 && ninepo != 0 && ninepo != 1 && ninepo != 2) // Up
{
num2 = tiles[ninepo - 3];
tiles[ninepo - 3] = tiles[ninepo];
tiles[ninepo] = num2;
ninepo -= 3;
}
if(num == 1 && ninepo != 6 && ninepo != 7 && ninepo != 8) // Down
{
num2 = tiles[ninepo + 3];
tiles[ninepo + 3] = tiles[ninepo];
tiles[ninepo] = num2;
ninepo += 3;
}
if(num == 2 && ninepo != 0 && ninepo != 3 && ninepo != 6) // Left
{
num2 = tiles[ninepo - 1];
tiles[ninepo - 1] = tiles[ninepo];
tiles[ninepo] = num2;
ninepo -= 1;
}
if(num == 3 && ninepo != 2 && ninepo != 5 && ninepo != 8) // Right
{
num2 = tiles[ninepo + 1];
tiles[ninepo + 1] = tiles[ninepo];
tiles[ninepo] = num2;
ninepo += 1;
}
}
}
else
{
for(i = 0; i <= 400; i++)
{
num = rand() % 4;
if(num == 0 && ninepo != 0 && ninepo != 1 && ninepo != 2 && ninepo != 3) // Up
{
num2 = tiles[ninepo - 4];
tiles[ninepo - 4] = tiles[ninepo];
tiles[ninepo] = num2;
ninepo -= 4;
}
if(num == 1 && ninepo != 12 && ninepo != 13 && ninepo != 14 && ninepo != 15) // Down
{
num2 = tiles[ninepo + 4];
tiles[ninepo + 4] = tiles[ninepo];
tiles[ninepo] = num2;
ninepo += 4;
}
if(num == 2 && ninepo != 0 && ninepo != 4 && ninepo != 8 && ninepo != 12) // Left
{
num2 = tiles[ninepo - 1];
tiles[ninepo - 1] = tiles[ninepo];
tiles[ninepo] = num2;
ninepo -= 1;
}
if(num == 3 && ninepo != 3 && ninepo != 7 && ninepo != 11 && ninepo != 15) // Right
{
num2 = tiles[ninepo + 1];
tiles[ninepo + 1] = tiles[ninepo];
tiles[ninepo] = num2;
ninepo += 1;
}
}
}
}
/**********************************************
* Function: updateTiles(void)
* Purpose: Redraws all the tiles to their
* current positions
**********************************************/
void Game::updateTiles()
{
if(boardsize == 0)
{
int i, j;
int x = 0;
for(i = 0 ; i < 3 ; i++)
{
for(j = 0 ; j < 3 ; j++)
{
int box = tiles[x]-1;
masked_blit(numbers, screen, box * 43, 0, 109+43*j+2*j, 144+43*i+2*i, 43, 43);
x++;
}
}
}
else
{
int i, j;
int x = 0;
for(i = 0 ; i < 4 ; i++)
{
for(j = 0 ; j < 4 ; j++)
{
int box = tiles[x]-1;
masked_blit(numbers15, screen, box * 43, 0, 91+43*j, 126+43*i, 43, 43);
x++;
}
}
}
}
/**********************************************
* Function: setupMouse(void)
* Purpose: Sets up the mouse
**********************************************/
void Game::setupMouse()
{
install_mouse();
scare_mouse();
set_mouse_sprite(cursor);
set_mouse_sprite_focus(0,0);
show_mouse(screen);
}
/**********************************************
* Function: handleMouse(void)
* Purpose: Handles mouse clicks
**********************************************/
void Game::handleMouse(int mx, int my)
{
if(boardsize == 0)
{
if((win == false) && (soundWinShown == false) && (creditWinShown == false) && (msgWinShown == false) && (eggWinShown == false))
{
int num;
num = tiles[selected];
if((tiles[selected + 3] == 9) && (selected != 6) && (selected != 7) && (selected != 8) && (selected != -1))
{
tiles[selected] = tiles[selected + 3];
tiles[selected + 3] = num;
play_sample(move, svolume, 128, 1000, FALSE);
moves++;
scare_mouse();
updateScreen();
updateTiles();
show_mouse(screen);
}
if((tiles[selected - 3] == 9) && (selected != 0) && (selected != 1) && (selected != 2) && (selected != -1))
{
tiles[selected] = tiles[selected - 3];
tiles[selected - 3] = num;
play_sample(move, svolume, 128, 1000, FALSE);
moves++;
scare_mouse();
updateScreen();
updateTiles();
show_mouse(screen);
}
if((tiles[selected + 1] == 9) && (selected != 2) && (selected != 5) && (selected != 8) && (selected != -1))
{
tiles[selected] = tiles[selected + 1];
tiles[selected + 1] = num;
play_sample(move, svolume, 128, 1000, FALSE);
moves++;
scare_mouse();
updateScreen();
updateTiles();
show_mouse(screen);
}
if((tiles[selected - 1] == 9) && (selected != 0) && (selected != 3) && (selected != 6) && (selected != -1))
{
tiles[selected] = tiles[selected - 1];
tiles[selected - 1] = num;
play_sample(move, svolume, 128, 1000, FALSE);
moves++;
scare_mouse();
updateScreen();
updateTiles();
show_mouse(screen);
}
}
}
else
{
if((win == false) && (soundWinShown == false) && (creditWinShown == false) && (msgWinShown == false) && (eggWinShown == false))
{
int num;
num = tiles[selected];
if((tiles[selected + 4] == 16) && (selected != 12) && (selected != 13) && (selected != 14) && (selected != 15) && (selected != -1))
{
tiles[selected] = tiles[selected + 4];
tiles[selected + 4] = num;
play_sample(move, svolume, 128, 1000, FALSE);
moves++;
scare_mouse();
updateScreen();
updateTiles();
show_mouse(screen);
}
if((tiles[selected - 4] == 16) && (selected != 0) && (selected != 1) && (selected != 2) && (selected != 3) && (selected != -1))
{
tiles[selected] = tiles[selected - 4];
tiles[selected - 4] = num;
play_sample(move, svolume, 128, 1000, FALSE);
moves++;
scare_mouse();
updateScreen();
updateTiles();
show_mouse(screen);
}
if((tiles[selected + 1] == 16) && (selected != 3) && (selected != 7) && (selected != 11) && (selected != 15) && (selected != -1))
{
tiles[selected] = tiles[selected + 1];
tiles[selected + 1] = num;
play_sample(move, svolume, 128, 1000, FALSE);
moves++;
scare_mouse();
updateScreen();
updateTiles();
show_mouse(screen);
}
if((tiles[selected - 1] == 16) && (selected != 0) && (selected != 4) && (selected != 8) && (selected != 12) && (selected != -1))
{
tiles[selected] = tiles[selected - 1];
tiles[selected - 1] = num;
play_sample(move, svolume, 128, 1000, FALSE);
moves++;
scare_mouse();
updateScreen();
updateTiles();
show_mouse(screen);
}
}
}
if(windowOpen == false)
{
if(mx >= 34 && mx <= 53 && my >= 4 && my <= 24)
{
updateScreen();
updateTiles();
textprintf_ex(screen, mainfont, 65, 350, makecol(255,255,255), -1, "You've Found a Secret!");
blit(themeWindow, screen, 0, 0, 30, 80, 288, 187); // Easter Egg! Click the #1 square
play_sample(winsound, svolume, 128, 1000, FALSE);
eggWinShown = true;
windowOpen = true;
}
// Menu Buttons
if(mx >= 375 && mx <= 573)
{
if((soundWinShown == false) && (creditWinShown == false))
{
// New Game Button
if(my >= 88 && my <= 125)
{
blit(msgWindow, screen, 0, 0, 30, 80, 288, 187);
play_sample(move, svolume, 128, 1000, FALSE);
msgWinShown = true;
windowOpen = true;
}
}
if((creditWinShown == false) && (msgWinShown == false))
{
// Sound Option Button
if(my >= 138 && my <= 170)
{
blit(soundWindow, screen, 0, 0, 30, 80, 288, 187);
play_sample(move, svolume, 128, 1000, FALSE);
soundWinShown = true;
windowOpen = true;
}
}
if((soundWinShown == false) && (msgWinShown == false))
{
// Credits Option Button
if(my >= 290 && my <= 324)
{
blit(creditWindow, screen, 0, 0, 30, 80, 288, 187);
play_sample(move, svolume, 128, 1000, FALSE);
creditWinShown = true;
windowOpen = true;
}
}
// Exit Option Button
if(my >= 340 && my <= 377)
{
m_done = true;
}
}
//Board Size Options
if(my >= 228 && my <= 256)
{
if(mx >= 408 && mx <= 468)
{
boardsize = 0; //3X3
newGame();
}
if(mx >= 480 && mx <= 540)
{
boardsize = 1; //4X4
newGame();
}
}
}
if(eggWinShown == true)
{
scare_mouse();
updateEggWin(mx, my);
show_mouse(screen);
}
if(msgWinShown == true)
{
scare_mouse();
updateMsgWin(mx, my);
show_mouse(screen);
}
if(creditWinShown == true)
{
scare_mouse();
updateCreditWin(mx, my);
show_mouse(screen);
}
if(soundWinShown == true)
{
scare_mouse();
updateSoundWin(mx, my);
show_mouse(screen);
}
rest(200);
}
/**********************************************
* Function: msgEggWin(int mx, int my)
* Purpose: To update and keep track of what is
* going on in the egg window
**********************************************/
void Game::updateEggWin(int mx, int my)
{
blit(themeWindow, screen, 0, 0, 30, 80, 288, 187);
if(mx >= 30 && mx <= 317 && my >= 80 && my <= 265)
{
// X Clicked
if(mx >= 290 && mx <= 314 && my >= 80 && my <= 95)
{
eggWinShown = false;
windowOpen = false;
updateScreen();
updateTiles();
}
if(mx >= 53 && mx <= 154 && my >= 136 && my <= 204)
{
themeSelector(2);
eggWinShown = false;
windowOpen = false;
updateScreen();
updateTiles();
}
if(mx >= 188 && mx <= 289 && my >= 136 && my <= 204)
{
themeSelector(1);
eggWinShown = false;
windowOpen = false;
updateScreen();
updateTiles();
}
}
rest(200);
}
/**********************************************
* Function: msgCreditwin(int mx, int my)
* Purpose: To update and keep track of what is
* going on in the msg window
**********************************************/
void Game::updateMsgWin(int mx, int my)
{
blit(msgWindow, screen, 0, 0, 30, 80, 288, 187);
if(mx >= 30 && mx <= 317 && my >= 80 && my <= 203)
{
// X Clicked
if(mx >= 290 && mx <= 314 && my >= 80 && my <= 95)
{
msgWinShown = false;
windowOpen = false;
updateScreen();
updateTiles();
}
if(mx >= 82 && mx <= 160 && my >= 151 && my <= 179) // Yes Selected
{
msgWinShown = false;
windowOpen = false;
newGame();
}
if(mx >= 163 && mx <= 260 && my >= 151 && my <= 179) // No Selected
{
msgWinShown = false;
windowOpen = false;
updateScreen();
updateTiles();
}
}
rest(200);
}
/**********************************************
* Function: updateCreditwin(int mx, int my)
* Purpose: To update and keep track of what is
* going on in the credit window
**********************************************/
void Game::updateCreditWin(int mx, int my)
{
blit(creditWindow, screen, 0, 0, 30, 80, 288, 187);
if(mx >= 30 && mx <= 317 && my >= 80 && my <= 265)
{
// X Clicked
if(mx >= 290 && mx <= 314 && my >= 80 && my <= 95)
{
creditWinShown = false;
windowOpen = false;
updateScreen();
updateTiles();
}
}
rest(200);
}
/**********************************************
* Function: updateSoundWin();
* Purpose: Updates the sound window
*
* 100%: 255 | 90%: 230 | 80%: 204 | 70%: 179 | 60%: 153 | 50%: 128 | 40%: 102 | 30%: 77 | 20%: 51 | 10%: 25 | 0%: 0
**********************************************/
void Game::updateSoundWin(int mx, int my)
{
blit(soundWindow, screen, 0, 0, 30, 80, 288, 187);
if(mx >= 30 && mx <= 317 && my >= 80 && my <= 265)
{
// X Clicked
if(mx >= 290 && mx <= 314 && my >= 80 && my <= 95)
{
soundWinShown = false;
windowOpen = false;
updateScreen();
updateTiles();
}
if(my >= 130 && my <= 151) // Row 1 [ music ]
{
if(mx >= 67 && mx <= 90)
{
mvolume = 0; set_volume(svolume, mvolume); // 0%
}
if(mx >= 100 && mx <= 130)
{
mvolume = 25; set_volume(svolume, mvolume); // 10%
}
if(mx >= 137 && mx <= 169)
{
mvolume = 51; set_volume(svolume, mvolume); // 20%
}
if(mx >= 179 && mx <= 207)
{
mvolume = 77; set_volume(svolume, mvolume); // 30%
}
if(mx >= 216 && mx <= 246)
{
mvolume = 102; set_volume(svolume, mvolume); // 40%
}
if(mx >= 253 && mx <= 285)
{
mvolume = 128; set_volume(svolume, mvolume); // 50%
}
}
// 100%: 255 | 90%: 230 | 80%: 204 | 70%: 179 | 60%: 153 | 50%: 128 | 40%: 102 | 30%: 77 | 20%: 51 | 10%: 25 | 0%: 0
if(my >= 155 && my <= 185) // Row 2 [ music ]
{
if(mx >= 83 && mx <= 113)
{
mvolume = 153; set_volume(svolume, mvolume); // 60%
}
if(mx >= 120 && mx <= 150)
{
mvolume = 179; set_volume(svolume, mvolume); // 70%
}
if(mx >= 159 && mx <= 188)
{
mvolume = 204; set_volume(svolume, mvolume); // 80%
}
if(mx >= 198 && mx <= 227)
{
mvolume = 230; set_volume(svolume, mvolume); // 90%
}
if(mx >= 234 && mx <= 266)
{
mvolume = 255; set_volume(svolume, mvolume); // 100%
}
}
if(my >= 211 && my <= 230) // Row 3 [ effects ]
{
if(mx >= 67 && mx <= 90)
{
svolume = 0; play_sample(move, svolume, 128, 1000, FALSE); // 0%
}
if(mx >= 100 && mx <= 130)
{
svolume = 25; play_sample(move, svolume, 128, 1000, FALSE); // 10%
}
if(mx >= 137 && mx <= 169)
{
svolume = 51; play_sample(move, svolume, 128, 1000, FALSE); // 20%
}
if(mx >= 179 && mx <= 207)
{
svolume = 77; play_sample(move, svolume, 128, 1000, FALSE); // 30%
}
if(mx >= 216 && mx <= 246)
{
svolume = 102; play_sample(move, svolume, 128, 1000, FALSE); // 40%
}
if(mx >= 253 && mx <= 285)
{
svolume = 128; play_sample(move, svolume, 128, 1000, FALSE); // 50%
}
}
if(my >= 235 && my <= 261) // Row 2 [ effects ]
{
if(mx >= 83 && mx <= 113)
{
svolume = 153; play_sample(move, svolume, 128, 1000, FALSE); // 60%
}
if(mx >= 120 && mx <= 150)
{
svolume = 179; play_sample(move, svolume, 128, 1000, FALSE); // 70%
}
if(mx >= 159 && mx <= 188)
{
svolume = 204; play_sample(move, svolume, 128, 1000, FALSE); // 80%
}
if(mx >= 198 && mx <= 227)
{
svolume = 230; play_sample(move, svolume, 128, 1000, FALSE); // 90%
}
if(mx >= 234 && mx <= 266)
{
svolume = 255; play_sample(move, svolume, 128, 1000, FALSE); // 100%
}
}
rest(200);
}
}
/**********************************************
* Function: updateScreen();
* Purpose: Updates the screen
**********************************************/
void Game::updateScreen()
{
blit(background, screen, 0, 0, 0, 0, 600, 400);
}
/**********************************************
* Function: detectSelection(void);
* Purpose: Detects when the mouse hovers over
* a tile
**********************************************/
void Game::detectSelection(int mx, int my)
{
if(boardsize == 0)
{
int i, j;
int x = 0;
selected = -1;
for(j = 0; j < 3; j++)
{
for(i = 0; i < 3; i++)
{
if((mx >= (113 + i * 43) && mx <= (113 + (i + 1) * 43)) && (my >= (148 + j * 43) && my <= (148 + (j + 1) * 43)))
{
selected = x;
}
x++;
}
}
}
else
{
int i, j;
int x = 0;
selected = -1;
for(j = 0; j < 4; j++)
{
for(i = 0; i < 4; i++)
{
if((mx >= (91 + i * 43) && mx <= (91 + (i + 1) * 43)) && (my >= (126 + j * 43) && my <= (126 + (j + 1) * 43)))
{
selected = x;
}
x++;
}
}
}
}
/**********************************************
* Function: detectWin(void);
* Purpose: Detects when a player wins
**********************************************/
void Game::detectWin()
{
int i;
int num = 0;
int num2 = 1;
if(boardsize == 0)
{
for(i = 0; i < 9; i++)
{
if(tiles[i] == num2)
{
++num;
}
num2++;
}
if(num == 9)
{
play_sample(winsound, svolume, 128, 1000, FALSE);
showMessage(2);
win = true;
textprintf_ex(screen, mainfont, 65, 350, makecol(255,255,255), -1, "You won in %i moves!", moves);
}
else
num = 0;
}
else
{
for(i = 0; i < 16; i++)
{
if(tiles[i] == num2)
{
++num;
}
num2++;
}
if(num == 16)
{
play_sample(winsound, svolume, 128, 1000, FALSE);
showMessage(2);
win = true;
textprintf_ex(screen, mainfont, 65, 350, makecol(255,255,255), -1, "You won in %i moves!", moves);
}
else
num = 0;
}
}
/**********************************************
* Function: newGame(void);
* Purpose: Starts a new game
**********************************************/
void Game::newGame()
{
moves = 0;
win = false;
randomizeTiles();
updateScreen();
updateTiles();
}
/**********************************************
* Function: checkSong(void);
* Purpose: Switches the songs when one is
* finished
**********************************************/
void Game::checkSong()
{
pos = midi_time;
if(pos == songlength)
{
if(currentsong == 0)
{
songlength = get_midi_length(theme2);
play_midi(theme2, 0);
currentsong = 1;
}
else if(currentsong == 1)
{
songlength = get_midi_length(theme1);
play_midi(theme1, 0);
currentsong = 0;
}
}
}