www.pudn.com > LINkSee.rar > Cards.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace LinkSee
{
class Cards
{
public const int LEFT = 24;
public const int TOP = 30;
public const int WIDTH = 196;
public const int HEIGHT = 224;
protected int rows = 0;
protected int cols = 0;
public int count = 0;
protected int selected = -1;
public Card[] cards = null;
protected Game game = null;
//protected playingSound MusicPlayer = null;
public Cards(Game game, Image imageCard, ImageAttributes imageAttr)
{
this.game = game;
this.rows = HEIGHT / Card.HEIGHT;
this.cols = WIDTH / Card.WIDTH;
this.count = rows * cols;
this.cards = new Card[count];
//this.MusicPlayer = new playingSound();
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
if (this.game.type_max <= 0) this.game.type_max = 5;
int index = this.cols * row + col;
int left = Card.WIDTH * col + LEFT + 1;
int top = Card.HEIGHT * row + TOP + 1;
int type = (index / 2) % this.game.type_max;
this.cards[index] = new Card(game, imageCard, imageAttr,left,top);
this.cards[index].Type = type;
}
}
Reset();
}
private void Reset()
{
for (int i = 0; i < this.count; i++)
{
this.cards[i].Reset();//设置每个小图片的visible为true
}
for (int i = 1; i < this.count; i++)
{
int j = this.game.Random.Next(i, this.count);
int type = this.cards[i - 1].Type;
this.cards[i - 1].Type = this.cards[j].Type;
this.cards[j].Type = type;
}
this.selected = -1;
}
public void process()
{
for (int i = 0; i < this.count; i++)
{
if (this.cards[i].Visible)
{
this.cards[i].process();
}
}
}
public void Draw(Graphics gc)
{
for (int i = 0; i < this.count; i++)
{
if (this.cards[i].Visible)
{
this.cards[i].Draw(gc);
}
}
}
public void Hit(int x, int y)
{
if (y < TOP || y >= TOP + HEIGHT || x < LEFT || x >= LEFT + WIDTH)
{
return;
}
int row = (y - TOP) / Card.HEIGHT;
int col = (x - LEFT) / Card.WIDTH;
int selected = this.cols * row + col;//由点击坐标到数组下标的转换
if (this.cards[selected].Visible)
{
this.game.DrawSide(cards[selected].left,cards[selected].top, Card.WIDTH, Card.HEIGHT);
if (this.selected == -1) this.selected = selected;
else if (this.selected == selected) { }
else if (!Linked(this.selected, selected))
{
this.selected = selected;
}
else
{
this.game.sound.Play();
this.game.DrawBomb(cards[selected].left-3,cards[selected].top-3);
this.game.DrawBomb(cards[this.selected].left-3, cards[this.selected].top-3);
this.cards[selected].hide();
this.cards[this.selected].hide();
this.selected = -1;
}
}
}
private bool Linked(int src, int dest)
{
if (this.cards[src].Type != this.cards[dest].Type)
{
return false;
}
int rowSrc = src / cols;//由数组下标到位图坐标的转换!!!!!
int colSrc = src % cols;
int rowDest = dest / cols;
int colDest = dest % cols;
int rowST = rowSrc;
int colST = colSrc;
int rowDT = rowDest;
int colDT = colDest;
int rowMid = (rowSrc + rowDest) / 2;
int colMid = (colSrc + colDest) / 2;
bool inside = true;
int i = 0;
do
{
if (Connected(rowSrc, colSrc, rowMid - i, colST, rowMid - i, colDT, rowDest, colDest))
{//从中心点向下检测
rowST = rowDT = rowMid - i;
break;
}
else if (Connected(rowSrc, colSrc, rowST, colMid - i, rowDT, colMid - i, rowDest, colDest))
{//从中心点向左检测
colST = colDT = colMid - i;
break;
}
else if (Connected(rowSrc, colSrc, rowMid + i + 1, colST, rowMid + i + 1, colDT, rowDest, colDest))
{//从中心点向上检测
rowST = rowDT = rowMid + i + 1;
break;
}
else if (Connected(rowSrc, colSrc, rowST, colMid + i + 1, rowDT, colMid + i + 1, rowDest, colDest))
{//从中心点向右检
colST = colDT = colMid + i + 1;
break;
}
i++;
inside = (rowMid - i >= -1) || (rowMid + i + 1 <= this.rows) ||
(colMid - i >= -1) || (colMid + i + 1 <= this.cols);
} while (inside);
if (inside)
{
this.game.DrawLines(
LEFT + colSrc * Card.WIDTH + Card.WIDTH / 2,
TOP + rowSrc * Card.HEIGHT + Card.HEIGHT / 2,
LEFT + colST * Card.WIDTH + Card.WIDTH / 2,
TOP + rowST * Card.HEIGHT + Card.HEIGHT / 2,
LEFT + colDT * Card.WIDTH + Card.WIDTH / 2,
TOP + rowDT * Card.HEIGHT + Card.HEIGHT / 2,
LEFT + colDest * Card.WIDTH + Card.WIDTH / 2,
TOP + rowDest * Card.HEIGHT + Card.HEIGHT / 2
);
}
return inside;
}
public bool Connected(int rowSrc, int colSrc, int rowDest, int colDest, int rowST, int colST, int rowDT, int colDT)
{
return Through(rowSrc, colSrc, rowDest, colDest, true) &&
Through(rowDest, colDest, rowST, colST, (rowDest == rowSrc) && (colDest == colSrc)) &&
Through(rowST, colST, rowDT, colDT, (rowST == rowSrc) && (colST == colSrc));
}
public bool Through(int srcRow, int srcCol, int destRow, int destCol, bool skipSrc)
{
if ((srcRow == destRow) && (srcCol == destCol)) // 同一格
{
return true;
}
else if (srcRow == destRow) // 在同一行
{
int row = srcRow;
if ((row < -1) || (row > this.rows)) // 行在边界外
{
// 不能通过
return false;
}
else if ((row == -1) || (row == this.rows)) // 行在边界上
{
// 能通过
return true;
}
else // 行在边界内
{
int step = Math.Sign(destCol - srcCol);
for (int col = srcCol + (skipSrc ? step : 0); col != destCol; col += step)
{
if ((col < -1) || (col > this.cols) || // 列在边界外
((col >= 0) && (col < this.cols) && // 或列在边界内
this.cards[this.cols * row + col].Visible)) // 但有障碍
{
return false;
}
}
return true;
}
}
else if (srcCol == destCol) // 在同一列
{
int col = srcCol;
if ((col < -1) || (col > cols)) // 列在边界外
{
// 不能通过
return false;
}
else if ((col == -1) || (col == cols)) // 列在边界上
{
// 能通过
return true;
}
else // 列在边界内
{
int step = Math.Sign(destRow - srcRow);
for (int row = srcRow + (skipSrc ? step : 0); row != destRow; row += step)
{
if ((row < -1) || (row > this.rows) || // 行在边界外
((row >= 0) && (row < this.rows) && // 或行在边界内
this.cards[this.cols * row + col].Visible)) // 但有障碍
{
return false;
}
}
return true;
}
}
else // 不在同一条线上
{
return false;
}
}
public bool isAllcardHide()
{
int i = 0;
while (this.cards[i].Visible == false && i < this.count - 1)
{
i++;
}
if (i >= this.count - 1) return true;
else return false;
}
}
}