Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
FiveLogic.java
Package: J2ME&Game.rar [view]
Upload User: gyyuli
Upload Date: 2013-07-09
Package Size: 3050k
Code Size: 18k
Category:
J2ME
Development Platform:
Java
- import java.io.PrintStream;
- import java.util.*;
- import javax.microedition.lcdui.Canvas;
- public class FiveLogic
- {
- private FiveCanvas cavFive;
- private int borderSize;
- private boolean computerFirst;
- private int degree;
- public static int STONE_NONE = 0;
- public static int STONE_COMPUTER = 1;
- public static int STONE_MAN = 2;
- private int stones[][];
- private Dot lastDot;
- private int stoneCounter[];
- private Stack steps;
- private Dot triedDot;
- private static int GAIN_BY_COMPUTER = 5;
- private static int GAIN_BY_NONE = 1;
- private boolean endOfGame;
- private boolean computerWon;
- private boolean thinking;
- private Random rnd;
- public FiveLogic(FiveCanvas cav, int bSize, boolean cFirst, int deg)
- {
- borderSize = 11;
- computerFirst = true;
- degree = 1;
- cavFive = cav;
- borderSize = bSize;
- computerFirst = cFirst;
- degree = deg;
- stones = new int[bSize][bSize];
- for(int r = 0; r < borderSize; r++)
- {
- for(int c = 0; c < borderSize; c++)
- stones[r][c] = 0;
- }
- lastDot = new Dot(borderSize);
- stoneCounter = new int[3];
- stoneCounter[1] = 0;
- stoneCounter[2] = 0;
- stoneCounter[0] = borderSize * borderSize;
- steps = new Stack();
- triedDot = new Dot(-1, -1);
- endOfGame = false;
- thinking = false;
- rnd = new Random();
- }
- public int[][] stones()
- {
- return stones;
- }
- public Dot lastDot()
- {
- return lastDot;
- }
- public Dot triedDot()
- {
- return triedDot;
- }
- public boolean gameEnd()
- {
- return endOfGame;
- }
- public boolean computerWon()
- {
- return computerWon;
- }
- public boolean thinking()
- {
- return thinking;
- }
- private boolean endOfGame()
- {
- endOfGame = false;
- for(int r = 0; r < borderSize; r++)
- {
- for(int c = 0; c < borderSize; c++)
- {
- if(stones[r][c] == 0 || existNLineWithMinFree(r, c, 5, 0, -1) == -1)
- continue;
- endOfGame = true;
- computerWon = stones[r][c] == 1;
- break;
- }
- if(endOfGame)
- break;
- }
- if(endOfGame)
- cavFive.notifyGameEnd();
- return endOfGame;
- }
- public void manGo(int row, int col)
- {
- if(row >= 0 && row < borderSize && col >= 0 && col < borderSize && stones[row][col] == 0)
- {
- goAt(row, col, 2);
- if(endOfGame())
- {
- if(computerWon)
- cavFive.setStatus("你输了!", 0xff0000, 2);
- else
- cavFive.setStatus("u4F60u8D62u4E86!", 65280, 1);
- } else
- {
- computerGo();
- }
- }
- }
- private void goAt(int row, int col, int playerStone)
- {
- int lastRow = lastDot.row;
- int lastCol = lastDot.col;
- stones[row][col] = playerStone;
- lastDot.setRowCol(row, col);
- cavFive.repaintAt(lastRow, lastCol);
- cavFive.repaintAt(row, col);
- switch(playerStone)
- {
- case 1: // '01'
- stoneCounter[1]++;
- break;
- case 2: // '02'
- stoneCounter[2]++;
- break;
- }
- stoneCounter[0]--;
- if(steps.size() > 10)
- steps.removeElementAt(0);
- steps.push(new Dot(row, col));
- }
- public boolean undo()
- {
- if(steps.size() >= 3)
- {
- Dot d = new Dot();
- d.copyFrom((Dot)steps.pop());
- stones[d.row][d.col] = 0;
- cavFive.repaintAt(d.row, d.col);
- d.copyFrom((Dot)steps.pop());
- stones[d.row][d.col] = 0;
- cavFive.repaintAt(d.row, d.col);
- d.copyFrom((Dot)steps.peek());
- lastDot.copyFrom(d);
- cavFive.repaintAt(d.row, d.col);
- return true;
- } else
- {
- return false;
- }
- }
- public void computerGo()
- {
- cavFive.setStatus("思考中...", 0, 0);
- cavFive.serviceRepaints();
- think();
- }
- public void think()
- {
- thinking = true;
- Dot dc = null;
- if((dc = thinkInNumber(1, 5)) == null && (dc = thinkInNumber(2, 5)) == null && (dc = to4B(1)) == null && (dc = to4B(2)) == null && (dc = toDouble4S_3B_2N1B(1, true)) == null && (dc = toDouble4S_3B_2N1B(2, true)) == null && (dc = toDouble4S_3B_2N1B(1, false)) == null && (dc = toDouble4S_3B_2N1B(2, false)) == null && (dc = toSingle4S_3B_2N1B(1)) == null)
- dc = toSingle4S_3B_2N1B(2);
- if(dc == null)
- dc = maxGainedDot();
- if(dc == null || stoneCounter[0] == 0)
- {
- cavFive.setStatus("平局!", 255, 3);
- } else
- {
- System.out.println("Gone!");
- goAt(dc.row, dc.col, 1);
- if(endOfGame())
- {
- if(computerWon)
- cavFive.setStatus("u4F60u8F93u4E86!", 0xff0000, 2);
- else
- cavFive.setStatus("u4F60u8D62u4E86 !", 65280, 1);
- } else
- {
- cavFive.setStatus("u8BF7u4E0Bu5B50...");
- }
- }
- thinking = false;
- }
- private Dot to4B(int playerStone)
- {
- if(stoneCounter[playerStone] < 3)
- return null;
- Dot dot = null;
- int maxGain = 0;
- for(int r = 1; r < borderSize - 1; r++)
- {
- for(int c = 1; c < borderSize - 1; c++)
- if(stones[r][c] == 0)
- {
- int cd[] = connectedIn8D(playerStone, r, c);
- int ed[] = expandedIn8D(playerStone, r, c);
- for(int i = 0; i < 4; i++)
- if(ed[i] > cd[i] && ed[i + 4] > cd[i + 4] && cd[i] + cd[i + 4] + 1 >= 4)
- {
- int gain = gainAt(r, c);
- if(gain > maxGain || gain > 0 && gain == maxGain && randomTrue())
- {
- maxGain = gain;
- dot = new Dot(r, c);
- }
- }
- }
- }
- return dot;
- }
- private Dot toSingle4S_3B_2N1B(int playerStone)
- {
- if(stoneCounter[playerStone] < 2)
- return null;
- Dot dot = null;
- for(int r = 0; r < borderSize; r++)
- {
- for(int c = 0; c < borderSize; c++)
- {
- if(stones[r][c] != 0 || find4S_3B_2N1BAt(r, c, playerStone, -1) == -1)
- continue;
- dot = new Dot(r, c);
- break;
- }
- if(dot != null)
- break;
- }
- return dot;
- }
- private Dot toDouble4S_3B_2N1B(int playerStone, boolean only4S)
- {
- if(stoneCounter[playerStone] < 4)
- return null;
- Dot dot = null;
- for(int rTest = 0; rTest < borderSize; rTest++)
- {
- for(int cTest = 0; cTest < borderSize; cTest++)
- {
- if(stones[rTest][cTest] != 0)
- continue;
- int cd[] = connectedIn8D(playerStone, rTest, cTest);
- if(cd[0] + cd[1] + cd[2] + cd[3] + cd[4] + cd[5] + cd[6] + cd[7] <= 0)
- continue;
- triedDot.setRowCol(rTest, cTest);
- stones[rTest][cTest] = playerStone;
- boolean found = false;
- int dFirst = find4S_3B_2N1B(playerStone, -1, rTest, cTest, only4S);
- if(dFirst != -1 && find4S_3B_2N1B(playerStone, dFirst, rTest, cTest, false) != -1)
- found = true;
- stones[rTest][cTest] = 0;
- triedDot.setRowCol(-1, -1);
- if(!found)
- continue;
- dot = new Dot(rTest, cTest);
- break;
- }
- if(dot != null)
- break;
- }
- return dot;
- }
- private int find4SAt(int row, int col, int playerStone, int exceptDirection)
- {
- int dFond = -1;
- int cd[] = connectedIn8D(playerStone, row, col);
- int ed[] = expandedIn8D(playerStone, row, col);
- for(int d = 0; d < 4; d++)
- {
- if(d == exceptDirection || stones[row][col] != playerStone)
- continue;
- int nConnect = cd[d] + cd[d + 4] + 1;
- int nFree1 = ed[d] - cd[d];
- int nFree2 = ed[d + 4] - cd[d + 4];
- boolean b4S = nConnect >= 4 && (nFree1 >= 1 || nFree2 >= 1);
- if(!b4S)
- continue;
- dFond = d;
- break;
- }
- return dFond;
- }
- private int find4S_3B_2N1BAt(int row, int col, int playerStone, int exceptDirection)
- {
- int dFond = -1;
- int cd[] = connectedIn8D(playerStone, row, col);
- int ed[] = expandedIn8D(playerStone, row, col);
- for(int d = 0; d < 4; d++)
- {
- if(d == exceptDirection)
- continue;
- if(stones[row][col] == playerStone)
- {
- int nConnect = cd[d] + cd[d + 4] + 1;
- int nFree1 = ed[d] - cd[d];
- int nFree2 = ed[d + 4] - cd[d + 4];
- boolean b4S = nConnect >= 4 && (nFree1 >= 1 || nFree2 >= 1);
- boolean b3B = nConnect >= 3 && nFree1 >= 1 && nFree2 >= 1;
- if(b4S || b3B)
- {
- dFond = d;
- break;
- }
- }
- if(stones[row][col] != 0)
- continue;
- int nFree1 = ed[d] - cd[d];
- int nFree2 = ed[d + 4] - cd[d + 4];
- boolean b2N1 = cd[d] >= 2 && cd[d + 4] >= 1 || cd[d] >= 1 && cd[d + 4] >= 2;
- boolean bSFree = nFree1 >= 1 && nFree2 >= 1;
- if(!b2N1 || !bSFree)
- continue;
- dFond = d;
- break;
- }
- return dFond;
- }
- private int find4S_3B_2N1B(int playerStone, int exceptDirection, int rTest, int cTest, boolean only4S)
- {
- int dFond = -1;
- int rMin = rTest - 3;
- if(rMin < 0)
- rMin = 0;
- int rMax = rTest + 3;
- if(rMax > borderSize)
- rMax = borderSize;
- int cMin = cTest - 3;
- if(cMin < 0)
- cMin = 0;
- int cMax = cTest + 3;
- if(cMax > borderSize)
- cMax = borderSize;
- for(int r = rMin; r < rMax; r++)
- {
- for(int c = cMin; c < cMax; c++)
- {
- if(stones[r][c] != playerStone && stones[r][c] != 0)
- continue;
- if(only4S)
- dFond = find4SAt(r, c, playerStone, exceptDirection);
- else
- dFond = find4S_3B_2N1BAt(r, c, playerStone, exceptDirection);
- if(dFond != -1)
- break;
- }
- if(dFond != -1)
- break;
- }
- return dFond;
- }
- private Dot thinkInNumber(int stonePlayer, int nl)
- {
- if(stoneCounter[stonePlayer] < nl - 1)
- return null;
- int maxGain = 0;
- Dot dot = null;
- for(int r = 0; r < borderSize; r++)
- {
- for(int c = 0; c < borderSize; c++)
- {
- int gain = thinkInNumberAt(stonePlayer, r, c, nl);
- if(gain > maxGain || gain > 0 && gain == maxGain && randomTrue())
- {
- maxGain = gain;
- dot = new Dot(r, c);
- }
- }
- }
- return dot;
- }
- private int thinkInNumberAt(int lineStone, int row, int col, int nl)
- {
- int lines = 0;
- int otherGain = 0;
- if(stones[row][col] == 0)
- {
- int cd[] = connectedIn8D(lineStone, row, col);
- int ed[] = expandedIn8D(lineStone, row, col);
- for(int i = 0; i < 4; i++)
- if(ed[i] + ed[i + 4] + 1 >= 5)
- {
- int l = cd[i] + cd[i + 4] + 1;
- if(l >= nl)
- lines++;
- else
- otherGain += 2 ^ l;
- }
- }
- return lines > 0 ? lines * 32 + otherGain : 0;
- }
- private int[] expandedIn8D(int stone, int row, int col)
- {
- int ed[] = new int[8];
- for(int d = 0; d < 8; d++)
- ed[d] = expandedIn1D(stone, row, col, d);
- return ed;
- }
- private int expandedIn1D(int stone, int row, int col, int direction)
- {
- int n = 0;
- int cn = 0;
- Dot d = new Dot(row, col);
- while(cn < 4)
- {
- d.copyFrom(forwardOneStep(d, direction));
- if(!d.inBorder(borderSize))
- break;
- int st = stones[d.row][d.col];
- if(st == 0)
- cn++;
- if(st != stone && st != 0)
- break;
- n++;
- }
- return n;
- }
- private Dot maxGainedDot()
- {
- Dot dotWithMaxGain = null;
- int maxGain = 0;
- for(int r = 0; r < borderSize; r++)
- {
- for(int c = 0; c < borderSize; c++)
- {
- int gain = gainAt(r, c);
- if(gain > maxGain || gain > 0 && gain == maxGain && randomTrue())
- {
- maxGain = gain;
- dotWithMaxGain = new Dot(r, c);
- }
- }
- }
- return dotWithMaxGain;
- }
- private int gainAt(int row, int col)
- {
- if(stones[row][col] == 0)
- {
- int gain = 0;
- for(int d = 0; d < 8; d++)
- {
- int gd = gainAtDirection(row, col, d);
- if(gd == 0)
- gain >>= 2;
- else
- gain += gd;
- }
- if(gain < 1)
- gain = 1;
- return gain;
- } else
- {
- return 0;
- }
- }
- private int gainAtDirection(int row, int col, int direction)
- {
- int gain = 0;
- Dot d = new Dot(row, col);
- int step = 0;
- do
- {
- d.copyFrom(forwardOneStep(d, direction));
- step++;
- if(!d.inBorder(borderSize))
- break;
- int stone = stones[d.row][d.col];
- if(stone == 2)
- break;
- int gainByStone = stone == 1 ? 5 : 1;
- gain += gainByStep(step) * gainByStone;
- } while(true);
- return gain;
- }
- private int gainByStep(int step)
- {
- int gain = (borderSize - step) / 2;
- if(gain < 1)
- gain = 1;
- return gain;
- }
- private int exist2N1(int row, int col, int playerStone, int exceptDirection)
- {
- int cd[] = connectedIn8D(playerStone, row, col);
- int ed[] = expandedIn8D(playerStone, row, col);
- int existDirection = -1;
- for(int i = 0; i < 4; i++)
- {
- if(i == exceptDirection || (cd[i] < 2 || cd[i + 4] < 1) && (cd[i] < 1 || cd[i + 4] < 2) || (ed[i] - cd[i]) + (ed[i + 4] - cd[i + 4]) <= 0)
- continue;
- existDirection = i;
- break;
- }
- return existDirection;
- }
- private int existNLineWithMinFree(int row, int col, int n, int minFree, int exceptDirection)
- {
- int stone = stones[row][col];
- int cd[] = connectedIn8D(stone, row, col);
- int ed[] = expandedIn8D(stone, row, col);
- int existDirection = -1;
- for(int i = 0; i < 4; i++)
- {
- if(i == exceptDirection || cd[i] + cd[i + 4] + 1 < n || (ed[i] - cd[i]) + (ed[i + 4] - cd[i + 4]) < minFree)
- continue;
- existDirection = i;
- break;
- }
- return existDirection;
- }
- private int[] connectedIn8D(int stone, int row, int col)
- {
- int cd[] = new int[8];
- for(int d = 0; d < 8; d++)
- cd[d] = connectedIn1D(stone, row, col, d);
- return cd;
- }
- private int connectedIn1D(int stone, int row, int col, int direction)
- {
- int n = 0;
- Dot d = new Dot(row, col);
- do
- {
- d.copyFrom(forwardOneStep(d, direction));
- if(d.inBorder(borderSize) && stones[d.row][d.col] == stone)
- n++;
- else
- return n;
- } while(true);
- }
- private Dot forwardOneStep(Dot d, int direction)
- {
- int r = d.row;
- int c = d.col;
- switch(direction)
- {
- case 0: // ''
- c++;
- break;
- case 1: // '01'
- r--;
- c++;
- break;
- case 2: // '02'
- r--;
- break;
- case 3: // '03'
- r--;
- c--;
- break;
- case 4: // '04'
- c--;
- break;
- case 5: // '05'
- r++;
- c--;
- break;
- case 6: // '06'
- r++;
- break;
- case 7: // '07'
- r++;
- c++;
- break;
- }
- return new Dot(r, c);
- }
- private boolean randomTrue()
- {
- return rnd.nextInt() % 2 == 0;
- }
- }