public List <FindPatternResult> getPossibleMove() { List <String> hash = new List <String>(); List <FindPatternResult> ret = new List <FindPatternResult>(); for (int y = 0; y < NUMCELL; y++) { for (int x = 0; x < NUMCELL; x++) { foreach (CandyPattern p in lstPattern) { /* if ((y == 7) && (x == 0)) * { * Console.WriteLine(""); * }*/ if (isTableMatch(table, p, x, y)) { FindPatternResult f = new FindPatternResult(); f.from = new Point(x + p.target[0].X, y + p.target[0].Y); f.to = new Point(x + p.target[1].X, y + p.target[1].Y); if ((table[f.from.X, f.from.Y] != null) && (table[f.to.X, f.to.Y] != null)) { if (!hash.Contains(f.ToString())) { ret.Add(f); hash.Add(f.ToString()); } } } } } } return(ret); }
private void button2_Click(object sender, EventArgs e) { board.load("table.txt"); currentBestMove = board.getBestMove(); lstResult = board.getPossibleMove(); imgMain.Refresh(); }
public void move(FindPatternResult f) { txtDebug.AppendText("Move " + f.ToString() + Environment.NewLine); lstHistory.Add(board); //flgDebug = true; board = board.process(f); flgDebug = false; currentBestMove = board.getBestMove(); lstResult = board.getPossibleMove(); imgMain.Refresh(); }
private void button4_Click(object sender, EventArgs e) { screen = new Bitmap("candycrush.png"); Bitmap pngImage = new Bitmap("candycrush/ref.png"); Utils.RECT rect = Utils.RECT.fromInt(0, 0, screen.Width, screen.Height); long start = System.Environment.TickCount; Utils.RECT scanRect = new Utils.RECT(); scanRect.left = rect.left; scanRect.top = rect.top; scanRect.right = (rect.left + rect.right) / 4; scanRect.bottom = (rect.top + rect.bottom) / 3; refPoint = Utils.SmartFindBitmap(screen, scanRect, pngImage); if (refPoint.X == 0) { txtDebug.AppendText("Not found"); return; } refPoint.X += 106; refPoint.Y += 69; Bitmap crop = Utils.cropImage(screen, new Rectangle(refPoint.X, refPoint.Y, cellW * NUMCELL, cellH * NUMCELL)); Utils.RECT CELLRECT = Utils.RECT.fromInt(0, 0, cellW, cellH); for (int i = 0; i < NUMCELL; i++) { for (int j = 0; j < NUMCELL; j++) { board.table[i, j] = null; Rectangle cellRect = new Rectangle((i * cellW), (j * cellH), cellW, cellH); Bitmap cropCell = Utils.cropImage(crop, cellRect); Utils.MyRawBitmapData cropCellBitmapData = new Utils.MyRawBitmapData(cropCell); BitmapSearch.BitmapSearchResult searchRet = candySearch.Search(cropCellBitmapData, CELLRECT, 2, 1000); if (searchRet != null) { Point pRet = searchRet.point; board.table[i, j] = htBmp[searchRet.bitmap].clone(); CellType ct = getCellTYpe(cropCellBitmapData, CELLRECT); board.cell[i, j] = ct; } } } long end = System.Environment.TickCount; dbgLine(refPoint + " " + (end - start)); mainRect = new Rectangle(refPoint.X, refPoint.Y, NUMCELL * cellW, NUMCELL * cellH); currentBestMove = board.getBestMove(); lstResult = board.getPossibleMove(); imgMain.Refresh(); }
public FindPatternResult getBestMove(int count = defaultRecursive) { if (count == defaultRecursive) { Console.WriteLine("getBestMove()"); } Score maxScore = new Score(); Board bestBoard; FindPatternResult bestMove = null; List <FindPatternResult> lstMove = getPossibleMove(); int numMove = 0; foreach (FindPatternResult f in lstMove) { numMove++; Board b = process(f); Score score = b.score(); if (count == defaultRecursive) { Console.WriteLine("Move_" + numMove + " " + f + " " + score); } if (count > 0) { FindPatternResult nextf = b.getBestMove(count - 1); if (nextf != null) { score = nextf.score; if (count == defaultRecursive) { Console.WriteLine("Move_" + numMove + " " + count + " " + f + " " + score); } } } //Console.WriteLine(" getBestMove()" + f + " score=" + score); if (score.getValue() > maxScore.getValue()) { maxScore = score; bestBoard = b; bestMove = f; bestMove.score = score; //Console.WriteLine(" getBestMove()" + bestMove + " score=" + maxScore); } } if (count == defaultRecursive) { Console.WriteLine("BestMove " + bestMove + " " + maxScore); } return(bestMove); }
private void btnPrev_Click(object sender, EventArgs e) { if (lstHistory.Count > 0) { board = lstHistory[lstHistory.Count - 1]; lstHistory.Remove(board); currentBestMove = board.getBestMove(); lstResult = board.getPossibleMove(); imgMain.Refresh(); } else { txtDebug.AppendText("No more move"); } }
private void btnNext_Click(object sender, EventArgs e) { // FindPatternResult f = board.getBestMove(); FindPatternResult f = new FindPatternResult(); f.to = new Point(7, 6); f.from = new Point(7, 7); if (f != null) { move(f); } else { txtDebug.AppendText("No more move"); } }
private void imgMain_MouseDown(object sender, MouseEventArgs e) { int w = cellW; int h = cellH; int cellX = e.X / w; int cellY = e.Y / h; if (isInFrom(lstResult, cellX, cellY) || isInTo(lstResult, cellX, cellY)) { if (moveFrom == null) { if (isInFrom(lstResult, cellX, cellY)) { moveFrom = new Pos2D(cellX, cellY); moveTo = null; } } else { if ((moveFrom.X == cellX) && (moveFrom.Y == cellY)) { moveFrom = null; } else { if (isInTo(lstResult, cellX, cellY)) { moveTo = new Pos2D(cellX, cellY); FindPatternResult f = new FindPatternResult(); f.to = moveTo.ToPoint(); f.from = moveFrom.ToPoint(); move(f); moveFrom = null; moveTo = null; } } } } else { moveFrom = null; } imgMain.Refresh(); }
private void button1_Click(object sender, EventArgs e) { String title = "Candy Crush"; Candy[,] table = board.table; Color[] refColors = new Color[] { Color.FromArgb(0xEBDEE4), // Color.FromArgb(235,223,228), }; if (Utils.activateWindow(title)) { screen = Utils.GetScreenShot(); Bitmap pngImage = new Bitmap("candycrush/ref.png"); Utils.RECT rect = new Utils.RECT(); IntPtr hwnd = Utils.GetForegroundWindow(); Utils.GetWindowRect(hwnd, out rect); long start = System.Environment.TickCount; txtDebug.AppendText("Active " + rect + " " + hwnd + "\n"); Utils.RECT scanRect = new Utils.RECT(); scanRect.left = rect.left; scanRect.top = rect.top; scanRect.right = (rect.left + rect.right) / 4; scanRect.bottom = (rect.top + rect.bottom) / 3; refPoint = Utils.SmartFindBitmap(screen, scanRect, pngImage); if (refPoint.X <= 0) { txtDebug.AppendText("Not found"); return; } refPoint.X += 106; refPoint.Y += 69; mainRect = new Rectangle(refPoint.X, refPoint.Y, NUMCELL * cellW, NUMCELL * cellH); screen = saveGetScreenShot(mainRect); screen.Save("candycrush.png"); //Utils.MouseMove(refPoint.X, refPoint.Y); Bitmap crop = Utils.cropImage(screen, new Rectangle(refPoint.X, refPoint.Y, cellW * NUMCELL, cellH * NUMCELL)); Utils.RECT CELLRECT = Utils.RECT.fromInt(0, 0, cellW, cellH); for (int i = 0; i < NUMCELL; i++) { for (int j = 0; j < NUMCELL; j++) { board.table[i, j] = null; Rectangle cellRect = new Rectangle((i * cellW), (j * cellH), cellW, cellH); Bitmap cropCell = Utils.cropImage(crop, cellRect); Utils.MyRawBitmapData cropCellBitmapData = new Utils.MyRawBitmapData(cropCell); BitmapSearch.BitmapSearchResult searchRet = candySearch.Search(cropCellBitmapData, CELLRECT, 2, 1000); if (searchRet != null) { Point pRet = searchRet.point; board.table[i, j] = htBmp[searchRet.bitmap].clone(); CellType ct = getCellTYpe(cropCellBitmapData, CELLRECT); board.cell[i, j] = ct; } } } } board.save("table.txt"); board.load("table.txt"); currentBestMove = board.getBestMove(); lstResult = board.getPossibleMove(); imgMain.Refresh(); //txtDebug.AppendText(table.ToString()); }
private void imgMain_Paint(object sender, PaintEventArgs e) { float w = cellW; float h = cellH; Graphics g = e.Graphics; if (screen != null) { g.DrawImage(screen, 0, 0, mainRect, GraphicsUnit.Pixel); } Font myFont = new Font("Arial", 14); Pen myPen1 = new Pen(Color.Black, 2); Pen myPen2 = new Pen(Color.White, 2); Pen myPen3 = new Pen(Color.Red, 3); Candy[,] table = board.table; for (int i = 0; i < NUMCELL; i++) { for (int j = 0; j < NUMCELL; j++) { if (board.isJelly(i, j)) { g.DrawRectangle(Pens.Black, new Rectangle((int)w * i, (int)h * j, (int)w - 1, (int)h - 1)); if (board.cell[i, j] == CellType.Jelly2) { g.DrawRectangle(Pens.Black, new Rectangle((int)w * i + 5, (int)h * j + 5, (int)w - 10, (int)h - 10)); } } if (table[i, j] != null) { g.DrawString(table[i, j].getName(), myFont, new SolidBrush(table[i, j].color), new PointF(w * i, h * j)); if (table[i, j].movenum != 0) { g.DrawString("" + table[i, j].movenum, myFont, new SolidBrush(table[i, j].color), new PointF(w * i + w / 2, h * j + h / 2)); } switch (table[i, j].type) { case CandyType.StripH: { g.DrawRectangle(new Pen(table[i, j].color, 2), new Rectangle((int)w * i, (int)h * j, (int)w - 1, (int)h - 1)); break; } case CandyType.StripV: { g.DrawRectangle(new Pen(table[i, j].color, 3), new Rectangle((int)w * i, (int)h * j, (int)w - 1, (int)h - 1)); break; } case CandyType.Big: { g.DrawRectangle(new Pen(table[i, j].color, 3), new Rectangle((int)w * i, (int)h * j, (int)w - 1, (int)h - 1)); break; } } } else { g.DrawLine(myPen1, w * i, h * j, w * (i + 1), h * (j + 1)); g.DrawLine(myPen1, w * i, h * (j + 1), w * (i + 1), h * j); } } } foreach (FindPatternResult p in lstResult) { g.DrawLine(myPen1, w * p.to.X + (w / 2) - 2, h * p.to.Y + (h / 2) + 2, w * p.from.X + w / 2 - 2, h * p.from.Y + h / 2 + 2); g.DrawLine(myPen2, w * p.to.X + w / 2, h * p.to.Y + h / 2, w * p.from.X + w / 2, h * p.from.Y + h / 2); } if (currentBestMove != null) { FindPatternResult p = currentBestMove; g.DrawLine(myPen1, w * p.to.X + (w / 2) - 2, h * p.to.Y + (h / 2) + 2, w * p.from.X + w / 2 - 2, h * p.from.Y + h / 2 + 2); g.DrawLine(myPen3, w * p.to.X + w / 2, h * p.to.Y + h / 2, w * p.from.X + w / 2, h * p.from.Y + h / 2); } if (moveFrom != null) { g.DrawRectangle(myPen3, new Rectangle((int)w * moveFrom.X, (int)h * moveFrom.Y, (int)w - 1, (int)h - 1)); } }
public Board process(FindPatternResult f) { Board ret = clone(); Candy cFrom = table[f.from.X, f.from.Y]; Candy cTo = table[f.to.X, f.to.Y]; ret.move(f.from, f.to); bool flgDo = true; int count = 0; while (flgDo) { flgDo = false; for (int y = 0; y < NUMCELL; y++) { for (int x = 0; x < NUMCELL; x++) { foreach (BoardPattern b in lstBoardPattern) { if (isTableMatch(ret.table, b, x, y)) { if (flgDebug) { Console.WriteLine("Match " + x + "," + y + "," + b.type); } switch (b.type) { case BoardPatternType.ThreeHorizontal: { ret.blow(x, y); ret.blow(x + 1, y); ret.blow(x + 2, y); flgDo = true; break; } case BoardPatternType.ThreeVirtical: { ret.blow(x, y); ret.blow(x, y + 1); ret.blow(x, y + 2); flgDo = true; break; } case BoardPatternType.FourHorizontal: { Candy c = ret.table[x + 2, y]; Point target = new Point(x + 2, y); if (cFrom != null) { c = cFrom; target = f.to; } for (int i = 0; i < 4; i++) { ret.blow(x + i, y); } ret.table[target.X, target.Y] = new Candy(c.color, CandyType.StripH); flgDo = true; break; } case BoardPatternType.FourVirtical: { int maxMove = 0; Point maxPoint = new Point(); Candy c = null; for (int i = 0; i < 4; i++) { if (ret.table[x, y + i].movenum > 0) { maxMove = ret.table[x, y + i].movenum; maxPoint = new Point(x, y + i); c = ret.table[x, y + i].clone(); } ret.blow(x, y + i); } if (c != null) { ret.table[maxPoint.X, maxPoint.Y] = new Candy(c.color, CandyType.StripH); } flgDo = true; break; } case BoardPatternType.Big: { //ret.dump("Big before"); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (b.isMatch(j, i, ret.table[x + i, y + j], null)) { ret.blow(x + i, y + j); } } } //ret.dump("Big after"); flgDo = true; break; } case BoardPatternType.MultiColorH: { for (int i = 0; i < 5; i++) { ret.blow(x + i, y); } flgDo = true; break; } case BoardPatternType.MultiColorV: { for (int i = 0; i < 5; i++) { ret.blow(x, y + i); } flgDo = true; break; } case BoardPatternType.SpecialH: { ret.blow(x, y); ret.blow(x + 1, y); flgDo = true; break; } case BoardPatternType.SpecialV: { ret.blow(x, y); ret.blow(x + 1, y); flgDo = true; break; } } } } } } ret.autoFill(); //if (count == 1) //{ // break; //} f = null; cFrom = null; cTo = null; count++; }// while //Console.WriteLine("process count=" + count); return(ret); }