#include "DxLib.h"
#include <time.h>
#define BLOCKSIZE 50
#define SPACEX 100
#define SPACEY 100
#define ERASE 0
#define HIDE 0
#define NHIDE 1
#define FLAG 1
//カード用配列。
int map[4][4];
//白ブロック用配列。
int hide[4][4];
int white;
//クリック判定。
bool getClick(void) {
// マウス左ボタンが押されているかどうか。
if( (GetMouseInput() & MOUSE_INPUT_LEFT ) != 0 ) {
return true;
}
return false;
}
//盤面の生成。
void mapEdit(int size) {
int x, y;
int tmp, rand1, rand2, cnt = 1;
for( x = 0; x < size; x++ ) {
for( y = 0; y < size; y++ ) {
map[x][y] = cnt;
if( y % 2 != 0 ) {
cnt++;
}
}
}
//並び替え。
srand((unsigned int)time(NULL));
for( x = 0; x < size; x++ ) {
rand1 = GetRand(size-1); rand2 = GetRand(size-1);
tmp = map[x][rand1]; map[x][rand1] = map[x][rand2]; map[x][rand2] = tmp;
}
for( y = 0; y < size; y++) {
rand1 = GetRand(size-1); rand2 = GetRand(size-1);
tmp = map[rand1][y]; map[rand1][y] = map[rand2][y]; map[rand2][y] = tmp;
}
}
//カードの生成。
void cardEdit(int size) {
int x, y;
for( x = 0; x < size; x++ ) {
for( y = 0; y < size; y++ ) {
if( map[x][y] != ERASE ) {
if( map[x][y] == 1 ) {
DrawBox( x*(640/size), y*(480/size), (x+1)*(640/size), (y+1)*(480/size), GetColor( 255,0,0 ), TRUE );
} else if( map[x][y] == 2 ) {
DrawBox( x*(640/size), y*(480/size), (x+1)*(640/size), (y+1)*(480/size), GetColor( 0,255,0 ), TRUE );
} else if( map[x][y] == 3 ) {
DrawBox( x*(640/size), y*(480/size), (x+1)*(640/size), (y+1)*(480/size), GetColor( 0,0,255 ), TRUE );
} else if( map[x][y] == 4 ) {
DrawBox( x*(640/size), y*(480/size), (x+1)*(640/size), (y+1)*(480/size), GetColor( 126,126,0 ), TRUE );
} else if( map[x][y] == 5 ) {
DrawBox( x*(640/size), y*(480/size), (x+1)*(640/size), (y+1)*(480/size), GetColor( 126,0,126 ), TRUE );
} else if( map[x][y] == 6 ) {
DrawBox( x*(640/size), y*(480/size), (x+1)*(640/size), (y+1)*(480/size), GetColor( 0,126,126 ), TRUE );
} else if( map[x][y] == 7 ) {
DrawBox( x*(640/size), y*(480/size), (x+1)*(640/size), (y+1)*(480/size), GetColor( 126,126,126 ), TRUE );
} else if( map[x][y] == 8 ) {
DrawBox( x*(640/size), y*(480/size), (x+1)*(640/size), (y+1)*(480/size), GetColor( 255,255,0 ), TRUE );
}
}
}
}
}
//白ブロックの生成。
void blockEdit(int size) {
int x, y;
for( x = 0; x < size; x++ ) {
for( y = 0; y < size; y++ ) {
if( hide[x][y] != NHIDE ) {
DrawBox( x*(640/size), y*(480/size), (x+1)*(640/size), (y+1)*(480/size), white, TRUE );
DrawBox( x*(640/size), y*(480/size), (x+1)*(640/size), (y+1)*(480/size), GetColor(0,0,0), FALSE);
}
}
}
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
int posX = 0, posY = 0;
int size = 4;
int tmp = 0, tmpx = 0, tmpy = 0;
int cnt = 0, elemCnt = 0;
int x, y; //x:X座標 y:Y座標
int backHandle;
int backMosaic[16];
int mosaicArray[4][4];
//白色取得。
white = GetColor(255,255,255);
//ウィンドウ化と初期化処理。
if( ChangeWindowMode( TRUE ) != DX_CHANGESCREEN_OK || DxLib_Init() == -1 ) return -1;
//背景の読み込み。
backHandle = LoadGraph("yune.bmp");
//backHandle = LoadGraph("mosaic.bmp");
//画像を分割してimage配列に保存。
LoadDivGraph("yune_mosaic.bmp", 16, 4, 4, 640/size, 480/size, backMosaic);
//LoadDivGraph("mosaic_test.bmp", 16, 4, 4, 640/size, 480/size, backMosaic);
//描画先を裏画面に設定。
SetDrawScreen( DX_SCREEN_BACK );
//マウスを表示状態にする。
SetMouseDispFlag( TRUE );
//盤面生成。
mapEdit(size);
//ESCAPEキーが押されるまでプログラムを続行。
while( !ProcessMessage() && !CheckHitKey( KEY_INPUT_ESCAPE ) && !ClearDrawScreen() ) {
//カードが全て消えたら終了。
if( cnt == size*size ) {
cardEdit(size);
DrawGraph(0, 0, backHandle, FALSE);
for( x = 0; x < size; x++ ) {
for( y = 0; y < size; y++ ) {
if( mosaicArray[x][y] == FLAG ) {
elemCnt = (x+(y*size));
DrawGraph(x*(640/size), y*(480/size), backMosaic[elemCnt], FALSE);
elemCnt = (tmpx+(tmpy*size));
DrawGraph(tmpx*(640/size), tmpy*(480/size), backMosaic[elemCnt], FALSE);
}
}
}
ScreenFlip();
//Sleep(5000);
//break;
}
//マウスポインタの座標を取得。
GetMousePoint( &posX, &posY );
//座標を表示。
//DrawFormatString( 20, 20, white, "マウスの座標( %d, %d )", posX, posY );
//背景の表示。
DrawGraph(0, 0, backHandle, FALSE);
for( x = 0; x < size; x++ ) {
for( y = 0; y < size; y++ ) {
if( mosaicArray[x][y] == FLAG ) {
elemCnt = (x+(y*size));
DrawGraph(x*(640/size), y*(480/size), backMosaic[elemCnt], FALSE);
elemCnt = (tmpx+(tmpy*size));
DrawGraph(tmpx*(640/size), tmpy*(480/size), backMosaic[elemCnt], FALSE);
}
}
}
//ScreenFlip();
//カードの生成。
cardEdit(size);
//白ブロックの生成。
blockEdit(size);
//クリックしたら。
if( getClick() ) {
for( x = 0; x < size; x++ ) {
for( y = 0; y < size; y++ ) {
//x、yの決定。
if( !(posX < x*(640/size) || posX > ((x+1)*(640/size))) && !(posY < y*(480/size) || posY > ((y+1)*(480/size))) ) {
//すでに消えているカードがクリック出来ないように。
if( map[x][y] != ERASE ) {
//クリック1回目のカードがクリック出来ないように。
if( hide[x][y] != NHIDE ) {
//1回目のクリックなら。
if( tmp == ERASE ) {
//何度もクリック判定されないように時間稼ぎ。
Sleep(200);
//白ブロックを消す。
hide[x][y] = NHIDE;
//2枚を比較するために一時的に記憶。
tmp = map[x][y]; tmpx = x; tmpy = y;
//2回目なら。
} else {
//白ブロックを消す。
hide[x][y] = NHIDE;
//カードの生成。
cardEdit(size);
//白ブロックの生成。
blockEdit(size);
//裏画面反映。
ScreenFlip();
//2枚目のカードを可視化するための時間稼ぎ。
Sleep(500);
//2枚のカードが同じだったら。
if( tmp == map[x][y] ) {
//カードを消して、tmpの初期化。
map[tmpx][tmpy] = ERASE; map[x][y] = ERASE;
tmp = ERASE; tmpx = ERASE; tmpy = ERASE;
//消えたカードのカウント。
cnt += 2;
//違ったら。
} else {
//モザイク画像描画フラグ。
mosaicArray[x][y] = FLAG; mosaicArray[tmpx][tmpy] = FLAG;
//カードを隠して、tmpの初期化。
hide[tmpx][tmpy] = HIDE; hide[x][y] = HIDE;
tmp = ERASE; tmpx = ERASE; tmpy = ERASE;
}
}
//クリックしたところがクリック済みなら。
} else {
hide[x][y] = HIDE;
tmp = ERASE; tmpx = ERASE; tmpy = ERASE;
Sleep(100);
}
}
}
}
}
}
//裏画面反映。
ScreenFlip();
}
//DXライブラリ終了処理。
DxLib_End();
return 0;
}