Bác nào có sourece code game caro viết = java xin cho em tham khảo.
em đang cần gấp. kính mong nọi ng giúp đỡ. Vui lòng send qua mail: "nvhoan88@yahoo.com.vn". Xin cảm ơn trc!!!
Bác nào có sourece code game caro viết = java xin cho em tham khảo.
em đang cần gấp. kính mong nọi ng giúp đỡ. Vui lòng send qua mail: "nvhoan88@yahoo.com.vn". Xin cảm ơn trc!!!
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class CaroMainForm extends javax.swing.JFrame {
static int HUMAN =Cell.X;
static int COMPUTER=Cell.O;
static int HumanScore=0,ComputerScore=0;
int hang = 20, cot = 20;
Cell[][] cell = new Cell[hang][cot];
Computer cp ;
public static boolean ready=false;
timeManager tm;
public CaroMainForm() {
super("Caro Demo");
try { UIManager.setLookAndFeel("com.sun.java.swing.plaf. windows.WindowsClassicLookAndFeel");
} catch (Exception e) {
}
for (int i = 0; i < hang; i++) {
for (int j = 0; j < cot; j++) {
cell[i][j] = new Cell(this);
cell[i][j].x=i;
cell[i][j].y=j;
}
}
initComponents();
this.setLocationRelativeTo(this);
cp = new Computer(this);
}
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
buttonGroup2 = new javax.swing.ButtonGroup();
jpCaro = new javax.swing.JPanel();
jpDay = new javax.swing.JPanel(new BorderLayout(111, 30));
lbStHuman = new javax.swing.JLabel();
lbStHuman.setText("You");
lbHuman = new javax.swing.JLabel();
lbHuman.setText("0");
lbStComputer = new javax.swing.JLabel();
lbStComputer.setText(" Computer");
lbComputer = new javax.swing.JLabel();
lbComputer.setText("0");
jLabel5 = new javax.swing.JLabel();
jLabel5.setText("||");
lbTime = new javax.swing.JLabel();
lbTime.setFont(new java.awt.Font("Tahoma", 1, 12));
lbTime.setForeground(new java.awt.Color(153, 0, 0));
jrdX = new javax.swing.JRadioButton();
buttonGroup1.add(jrdX);
jrdX.setFont(new java.awt.Font("Tahoma", 1, 12));
jrdX.setSelected(true);
jrdX.setText("X ");
jrdX.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
jrdXItemStateChanged(evt);
}
});
jrdO = new javax.swing.JRadioButton();
buttonGroup1.add(jrdO);
jrdO.setFont(new java.awt.Font("Tahoma", 1, 11));
jrdO.setForeground(new java.awt.Color(0, 0, 204));
jrdO.setText("O ");
jrdOrder1 = new javax.swing.JRadioButton();
buttonGroup2.add(jrdOrder1);
jrdOrder1.setSelected(true);
jrdOrder1.setText("First");
jrdOrder2 = new javax.swing.JRadioButton();
buttonGroup2.add(jrdOrder2);
jrdOrder2.setText("Second");
jbStartReset = new javax.swing.JButton();
jbStartReset.setText("Start Game");
jbStartReset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbStartResetActionPerformed(evt);
}
});
jbStartReset1 = new javax.swing.JButton();
jbStartReset1.setText("Exit Game");
jbStartReset1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbStartReset1ActionPerformed(evt);
}
});
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout(10, 10));
this.setSize(896, 657);//co cua panel
jpCaro.setBorder(javax.swing.BorderFactory.createE tchedBorder(new java.awt.Color(0, 0, 204), null));
jpCaro.setLayout(new java.awt.GridLayout(hang,cot,0,0));
for (int i = 0; i < hang; i++) {
for (int j = 0; j < cot; j++) {
jpCaro.add(cell[i][j].jb);
}
}
//add jpCaro vao frame
this.add(jpCaro, BorderLayout.CENTER);
//add pannel goc duoi trai
JPanel jpGocDuoiPhai = new javax.swing.JPanel();
jpGocDuoiPhai.setBorder(javax.swing.BorderFactory. createTitledBorder(""));
jpGocDuoiPhai.setLayout(new BorderLayout(5, 5));
JPanel jpKetQua=new JPanel();
jpKetQua.setLayout(new FlowLayout(FlowLayout.LEFT, 12, 0));
jpKetQua.add(lbStHuman);
jpKetQua.add(lbHuman);
jpKetQua.add(jLabel5);
jpKetQua.add(lbComputer);
jpKetQua.add(lbStComputer);
jpGocDuoiPhai.add(jpKetQua,BorderLayout.NORTH);
jpGocDuoiPhai.add(new JPanel(),BorderLayout.WEST);
jpGocDuoiPhai.add(lbTime,BorderLayout.CENTER);
//add pannel goc duoi trai
JPanel jpGocduoiTrai=new JPanel(new BorderLayout( 0, 0));
JPanel jpXO=new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
jpXO.add(jrdX);
jpXO.add(jrdO);
//
JPanel jpFirstSecond=new JPanel(new FlowLayout(FlowLayout.LEFT, 11, 0));
jpFirstSecond.add(jrdOrder1);
jpFirstSecond.add(jrdOrder2);
JPanel jpButton=new JPanel(new FlowLayout(FlowLayout.LEFT, 50, 0));
jpButton.add(jbStartReset);
jpButton.add(jbStartReset1);
jpGocduoiTrai.add(jpXO, BorderLayout.NORTH);
jpGocduoiTrai.add(jpFirstSecond, BorderLayout.CENTER);
jpGocduoiTrai.add(jpButton, BorderLayout.EAST);
jpGocduoiTrai.add(new JPanel(),BorderLayout.SOUTH);
//add vao pannel jpDay
jpDay.add(jpGocduoiTrai,BorderLayout.CENTER);
jpDay.add(jpGocDuoiPhai,BorderLayout.EAST);
jpDay.add(new Panel(),BorderLayout.SOUTH);
//add pannel jpDay vao frame
this.add(jpDay,BorderLayout.SOUTH);
}
private void jrdXItemStateChanged(java.awt.event.ItemEvent evt) {
if (jrdX.isSelected()) {
HUMAN =Cell.X;
COMPUTER=Cell.O;
} else {
HUMAN =Cell.O;
COMPUTER=Cell.X;
}
}
private void jbStartResetActionPerformed(java.awt.event.ActionE vent evt) {
if(jbStartReset.getText().equalsIgnoreCase("Start Game")){
int x=hang/2-1,y=cot/2-1;
if(!jrdOrder1.isSelected()){
int stateTempt;
int computer = (HUMAN==Cell.X)? Cell.O:Cell.X ;
if(computer==Cell.X){
stateTempt=Cell.X;
cell[x][y].jb.setText("X");
}else{
stateTempt=Cell.O;
cell[x][y].jb.setText("O");
cell[x][y].jb.setForeground(Color.blue);
}
cp.Put(stateTempt,x,y);
}
jrdO.setEnabled(false);
jrdX.setEnabled(false);
jrdOrder1.setEnabled(false);
jrdOrder2.setEnabled(false);
jbStartReset.setText(" Reset ");
ready=true;
tm=new timeManager(lbTime);
tm.start();
}else{
lbTime.setText("");
for (int i = 0; i < hang; i++) {
for (int j = 0; j < cot; j++) {
cell[i][j].reset();
}
}
cp.Area=new int[hang][cot];
for (int i = 0; i < hang; i++) {
for (int j = 0; j < cot; j++) {
cp.Area[i][j] = 0;
}
}
ready=false;
cp=null;
cp=new Computer(this);
jrdO.setEnabled(true);
jrdX.setEnabled(true);
jrdOrder1.setEnabled(true);
jrdOrder2.setEnabled(true);
jbStartReset.setText("Start Game");
ready=false;
tm.stop();
}
}
private void jbStartReset1ActionPerformed(java.awt.event.Action Event evt) {
// TODO add your handling code here:
int luaChon=JOptionPane.showConfirmDialog(this, "Da muon ket thuc roi sao ?", "Choi tiep di!!", JOptionPane.YES_NO_CANCEL_OPTION);
if(luaChon==0)
System.exit(0);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CaroMainForm().setVisible(true);
}
});
}
class timeManager extends Thread{
JLabel lb;
public timeManager(JLabel lb) {
this.lb = lb;
}
public void run() {
// timeRun();
hh=0;mm=0;ss=00;
String time;
//System.out.println("bat dau tinh gio");
while(true){
try {
Thread.sleep(1000);
} catch (Exception e) {
}
ss++;
if(ss==60){
ss=00;
mm++;
}
if(mm==60){
mm=00;
hh++;
}
time=(hh>9)?"":"0";
time+=Integer.valueOf(hh).toString()+" : ";
time+=(mm>9)?"":"0";
time+=Integer.valueOf(mm).toString()+" : ";
time+= (ss>9)?"":"0";
time+=Integer.valueOf(ss).toString();
lb.setText(time);
}
}
int hh,mm,ss;
}
}
class Computer {
Cell[][] cell;
int hang, cot;
public int Score;
int Area[][];
final int AREASIZE = 2;
public int numPiece;
int MAXPIECENUM ;
int Dx[];
int Dy[];
public boolean Space;
final int D_UP = 0;
final int D_UPRIGHT = 1;
final int D_RIGHT = 2;
final int D_DOWNRIGHT = 3;
final int D_DOWN = 4;
final int D_DOWNLEFT = 5;
final int D_LEFT = 6;
final int D_UPLEFT = 7;
final int SC_WIN = 10000000;
final int SC_WIN2 =1000000 ;
CaroMainForm cmf;
public Computer(CaroMainForm cmf) {
this.cmf=cmf;
this.cell = cmf.cell;
this.hang = cmf.hang;
this.cot = cmf.cot;
Dx = new int[8];
Dy = new int[8];
Dx[0] = -1; Dy[0] = 0;
Dx[1] = -1; Dy[1] = 1;
Dx[2] = 0; Dy[2] = 1;
Dx[3] = 1; Dy[3] = 1;
Dx[4] = 1; Dy[4] = 0;
Dx[5] = 1; Dy[5] = -1;
Dx[6] = 0; Dy[6] = -1;
Dx[7] = -1; Dy[7] = -1;
Area = new int[hang][cot];
for(int x=0; x < hang; x++){
for(int y=0; y < cot; y++){
Area[x][y] = 0;
}
}
MAXPIECENUM = hang*cot;
numPiece = 0;
Score=0;
col=cell[0][0].jb.getBackground();
}
JButton preCell=null;
Color col;
void setTextCell(int x,int y){
String text=(CaroMainForm.HUMAN==Cell.X)?"O" :"X";
cell[x][y].jb.setText(text);
cell[x][y].jb.setBackground(java.awt.Color.lightGray);
if(text.equalsIgnoreCase("O")){
cell[x][y].jb.setForeground(Color.blue);
}
if(preCell!=null){
preCell.setBackground(col);
}
preCell=cell[x][y].jb;
}
public void Think(int state ) {
//
Cell Best=new Cell();
cmf.setCursor(Cursor.WAIT_CURSOR);
int x, y=0;
int BestScore = 0;
int eBestScore = 0;
int eBestX = 0;
int eBestY = 0;
int eState = (state == Cell.X) ? Cell.O : Cell.X;
Best.x = 0;
Best.y = 0;
//duyet qua tat ca cac o tren ban co
for (x = 0; x < hang; x++) {
for (y = 0; y < cot; y++) {
//neu trang thai cua ô do la Empty
if (cell[x][y].State == Cell.EMPTY && Area[x][y] > 0) {
// computer thu danh vao o do ,sau do tinh diem
Put(state, x, y);
GetScore(state, x, y);
if (Score > BestScore) {
BestScore = Score;
Best.x = x;
Best.y = y;
}
Remove(x, y);
//computer gia su con nguoi se danh vao o do ,sau do tinh diem
Put(eState, x, y);
GetScore(eState, x, y);
if (Score > eBestScore) {
eBestScore = Score;
eBestX = x;
eBestY = y;
}
Remove(x, y);
}
}
}
//neu diem dat duoc cua computer >= SC_WIN (diem thang tuyet doi) thi computer se danh vao o do
if (BestScore >= SC_WIN) {
Put(state, Best.x, Best.y);
cell[Best.x][Best.y].jb.setText("O");
setTextCell(Best.x, Best.y);
CaroMainForm.ComputerScore++;
cmf.tm.stop();
CaroMainForm.lbComputer.setText(Integer.valueOf(Ca roMainForm.ComputerScore).toString());
JOptionPane.showMessageDialog(cmf, "He he con ga!!co gang len !!", " computer win !!", JOptionPane.INFORMATION_MESSAGE);
CaroMainForm.ready=false;
cmf.setCursor(Cursor.DEFAULT_CURSOR);
return ;
}
//neu diem dat dc cua con nguoi >=SC_WIN thi computer phai chan
if (eBestScore >= SC_WIN) {
Best.x = eBestX;
Best.y = eBestY;
}
else if (eBestScore >= SC_WIN2) {
if (BestScore < SC_WIN2) {
Best.x = eBestX;
Best.y = eBestY;
}
}
else if (BestScore < SC_WIN2 &&numPiece > 3) {
Best.x = -2;
Best.y = -2;
Perspective(state, eState, Best);
} else {
if (eBestScore > BestScore) {
Best.x = eBestX;
Best.y = eBestY;
}
}
// neu tat ca cac o tren ban co deu da duoc danh thi tra ve
if (numPiece ==MAXPIECENUM) {
return ;
}
Put(state, Best.x, Best.y);
setTextCell(Best.x, Best.y);
cmf.setCursor(Cursor.DEFAULT_CURSOR);
}
void Perspective(int state,int eState,Cell Best) {
int x,y;
int score=0;
int BestScore = 0;
Best.x = 0;
Best.y = 0;
// duyet qua tat cac o tren ban co
for(x=0;x<hang;x++){
for(y=0;y<cot;y++){
if( cell[x][y].State == Cell.EMPTY &&Area[x][y] > 0 ){
Put(state,x,y);
score = GetGlobalScore(state);
score += PerspectiveIter( state,eState);
if(score > BestScore) {
BestScore = score;
Best.x = x; Best.y = y;
}
Remove(x,y);
}
}
}
}
int PerspectiveIter(int state,int eState) {
int score = 0;
int num = 0;
int x,y,x2,y2;
for(x=0;x<hang;x++){
for(y=0;y<cot;y++){
if(cell[x][y].State == Cell.EMPTY &&Area[x][y] > 0 ){
Put(eState,x,y);
for(x2=0;x2<hang;x2++){
for(y2=0;y2<cot;y2++){
if(cell[x2][y2].State == Cell.EMPTY &&Area[x2][y2] > 0 ){
Put(state,x2,y2);
GetScore(state,x2,y2);
if(Score > 20){
score += GetGlobalScore(state) - GetGlobalScore(eState);
num++;
}
Remove(x2,y2);
}
}
}
Remove(x,y);
}
}
}
if(num == 0)
return score/num ;
}
void GetScore(int state,int x,int y) {
int d1,d2;
int check2open=0;
int check3open=0;
int check3close=0;
int check4open=0;
int check4close=0;
int check5 = 0;
int checklong=0;
int num;
boolean space1;
Score = 0;
for(d1=D_UP,d2=D_DOWN; d1 <= D_DOWNRIGHT; d1++,d2++) {
num = GetSequence(state,x,y,d1);
space1 = Space;
num += GetSequence(state,x,y,d2) - 1 ;
switch (num) {
case 2:
if( space1 && Space )
check2open++;
break;
case 3:
if( space1 && Space )
check3open++;
else if( space1 || Space )
check3close++;
break;
case 4:
if( space1 && Space )
check4open++;
else if (space1 || Space )
check4close++;
break;
case 5:
check5++;
break;
default:
if( num > 5 )
checklong++;
break;
}
}
if(check5 + checklong > 0) {
Score = SC_WIN;
} else if(check4open > 0) {
Score = SC_WIN2;
} else if(check4close >= 2) {
Score = SC_WIN2;
} else if( (check4open + check4close) > 0 && check3open > 0) {
Score = SC_WIN2;
} else if( check3open >= 2) {
Score = 30000;
} else if( check4close > 0 ) {
Score = 100;
} else if( check3open + check3close >= 2 ){
Score = 1200;
} else if( check3open > 0 ){
Score = 1000*check3open;
} else if( check3close >= 2 ){
Score = 120;
} else if( check2open > 0 ) {
Score = 100*check2open;
}
else {
Score = 1;
}
}
public int Find5Block(int state, int x, int y) {
int max=0, a=0;
max = GetSequence(state, x, y, D_UP) + GetSequence(state, x, y, D_DOWN) - 1;
a = GetSequence(state, x, y, D_LEFT) + GetSequence(state, x, y, D_RIGHT) - 1;
max = Math.max(max, a);
a = GetSequence(state, x, y, D_UPLEFT) + GetSequence(state, x, y, D_DOWNRIGHT) - 1;
max = Math.max(max, a);
a = GetSequence(state, x, y, D_UPRIGHT) + GetSequence(state, x, y, D_DOWNLEFT) - 1;
max = Math.max(max, a);
if (max >= 5) {
return max;
}
return max;
}
public int GetSequence(int state, int x, int y, int direction) {
int num = 0;
int dx = Dx[direction];
int dy = Dy[direction];
Space = false;
while (cell[x][y].State == state) {
num++;
x += dx;
y += dy;
if (x < 0 || x >= hang || y < 0 || y >= cot) {
break;
}
if (cell[x][y].State == Cell.EMPTY) {
Space = true;
break;
}
}
return num;
}
public void Put(int state,int x,int y) {
if(cell[x][y].State == Cell.EMPTY) {
cell[x][y].State=state;
numPiece++;
int x1,x2,y1,y2;
x1 = (x-AREASIZE < 0) ? 0 : x-AREASIZE ;
x2 = (x+AREASIZE >= hang) ? hang-1 : x+AREASIZE;
y1 = (y-AREASIZE < 0) ? 0 : y-AREASIZE ;
y2 = (y+AREASIZE >= cot) ? cot-1 : y+AREASIZE;
for( ; x1 <= x2; x1++){
for(y = y1 ; y <= y2; y++) {
Area[x1][y]++;
}
}
}
}
public void Remove(int x,int y) {
if(cell[x][y].State != Cell.EMPTY) {
cell[x][y].State = Cell.EMPTY;
numPiece--;
int x1,x2,y1,y2;
x1 = (x-AREASIZE < 0) ? 0 : x-AREASIZE ;
x2 = (x+AREASIZE >= hang) ? hang-1 : x+AREASIZE;
y1 = (y-AREASIZE < 0) ? 0 : y-AREASIZE ;
y2 = (y+AREASIZE >= cot) ? cot-1 : y+AREASIZE;
for( ; x1 <= x2; x1++){
for( y = y1; y <= y2; y++) {
Area[x1][y]--;
}
}
}
}
int num2open,num3open,num3close,num4open,num4close,num 5;
public int GetGlobalScore(int state) {
int x,y;
boolean spacecheck;
num2open=0;
num3open=0;
num3close=0;
num4open=0;
num4close=0;
num5=0;
for(x=0; x < hang; x++){
y=0; spacecheck=false;
do {
if(cell[x][y].State == Cell.EMPTY) {
spacecheck=true;
y++;
} else {
y += GetGlobalSequence(state,x,y,D_RIGHT,spacecheck);
spacecheck=false;
}
} while(y < cot);
}
// duyet qua cac cot tren ban co,tinh xem dat duoc bao nhieu dẫy num2open,num3open,num3close....
for(y=0; y <cot; y++){
x=0; spacecheck=false;
do {
if(cell[x][y].State == Cell.EMPTY) {
spacecheck=true;
x++;
} else {
x += GetGlobalSequence(state,x,y,D_DOWN,spacecheck);
spacecheck=false;
}
} while(x <hang);
}
for(int i=0; i < hang*2-1; i++){
int ret;
if(i < hang){
x = 0; y = i;
} else {
x = i-hang+1; y=0;
}
spacecheck = false;
do {
if(cell[x][y].State == Cell.EMPTY) {
spacecheck=true;
x++; y++;
} else {
ret = GetGlobalSequence(state,x,y,D_UPRIGHT,spacecheck);
x+=ret; y+=ret;
spacecheck=false;
}
} while(x < hang && y < cot );
}
for(int i=0; i <hang*2-1; i++){
int ret;
if(i < hang){
x = 0; y = i;
} else {
x = i-hang+1; y = hang-1;
}
spacecheck = false;
do {
if(cell[x][y].State == Cell.EMPTY) {
spacecheck=true;
x++; y--;
} else {
ret = GetGlobalSequence(state,x,y,D_DOWNRIGHT,spacecheck );
x+=ret; y-=ret;
spacecheck=false;
}
} while(x < hang&& y >= 0 );
}
int score = num5 * 10000 + num4open * 5000 + num3open * 500
+ num3close * 10 + num2open * 20 + 1;
if(num4close >= 2)
score += 4000;
else
score += num4close * 400;
return score;
}
//================================================== ============================================
int GetGlobalSequence(int state,int x,int y,int direction,boolean spacecheck) {
int num = GetSequence(state,x,y,direction);
if(num==0)
return 1;
if(num==2){
if(Space && spacecheck)
num2open++;
} else if(num==3) {
if(Space && spacecheck)
num3open++;
else if(Space || spacecheck)
num3close++;
} else if(num==4){
if(Space && spacecheck)
num4open++;
else if(Space || spacecheck)
num4close++;
} else if(num >= 5) {
num5++;
}
return num;
}
}
class Cell {
public int State=0;
static final int EMPTY = 0;
static final int X = 1;
static final int O = 2;
int x=0;
int y=0;
public JButton jb=new JButton("");
CaroMainForm cmf;
Cell ce;
public Cell() {
}
public Cell(CaroMainForm cf) {
jb.setFont(new java.awt.Font("Tahoma", 1, 12));
cmf=cf;
ce=this;
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!CaroMainForm.ready)
return;
if(State==EMPTY){
int stateTemp;
if(CaroMainForm.HUMAN==X){
stateTemp=X;
jb.setText("X");
}else{
stateTemp=O;
jb.setText("O");
jb.setForeground(Color.blue);
}
cmf.cp.Put(stateTemp, x, y);
if(cmf.cp.Find5Block(CaroMainForm.HUMAN, x, y)>=5){
//System.out.println("you win");
CaroMainForm.HumanScore++;
CaroMainForm.lbHuman.setText(Integer.valueOf(CaroM ainForm.HumanScore).toString());
cmf.tm.stop();
JOptionPane.showMessageDialog(cmf, "Hic hic toi thua roi!!", "You win !!", JOptionPane.INFORMATION_MESSAGE);
CaroMainForm.ready=false;
}
else{
stateTemp=(State==X) ? O : X;
//cmf.cp.Think(stateTemp, ce);
cmf.cp.Think(stateTemp);
}
}
};
});
}
//================================================== =============================================
public void reset(){
State=EMPTY;
jb.setText("");
jb.setBackground(cmf.cp.col);
jb.setForeground(new java.awt.Color(0, 0, 0));
}
}
sao nó bị sai vậy bạn. Mình vào netbean tạo như thế mà sai
Bookmarks