PDA

View Full Version : Source Code Chương trình Caro Người - Máy,gửi các bạn tham khảo .



quangvu
16-11-2002, 09:59
Chương trình caro này mình sư tập được trên Net ,dịch ngược lại mã nguồn và xin được Post lên cho các bạn yêu thích Java xem qua :
1. Chương trình viết bằng Java Applet ,vì vậy bạn chỉ cần chạy file "Caro Game.html" băng IE là có thể đánh caro với máy được rồi đó :)
2. Source Code của Caro la "JavaCaro.java" ,vân chỉ một file duy nhất.
3.Chương trình này của một tác giả nước ngoài ,xin vui lòng đừng sửa tên tác giả thành . . . tên mình.Nếu bạn phát triển thêm bạn phải ghi "Developed by" trước hoặc sau tên tác giả.
Chúc thành công .

thetai_deptrai
27-10-2004, 15:03
dowloal về bằng cách nào vậy quangvu ???

lehoainam2005
19-12-2004, 16:22
Bạn hãy post lên lại đi, không down được

tp_keni
23-10-2006, 08:33
khong down duoc ban oi

nhannt04
02-11-2006, 18:51
down đc mà.Mình down bình thường thấy có gì đâu.Các bạn hãy thử lại xem
Have fun !!

dangkiroi
04-11-2006, 20:56
load duoc nhung co chay duoc dau ông oi :-w

hoangduc206
25-11-2006, 17:33
huhu...co ban nao co bai viet bang AWT ko vay?minh dang rat rat rat can......

tuancp
05-12-2006, 11:52
Sao mình ko down dc,bạn up lại đi

hungnt_vip
08-12-2006, 14:24
down ve trang trang thoi

hoangthienphuc
11-12-2006, 14:46
Khong down dc bạn ơi!
Up lai di

dat@
18-12-2006, 18:00
GoMoku.java
******************x


/*
This applet lets two uses play GoMoku (a.k.a Pente) against each
other. Black always starts the game. When a player gets five-in-a-row,
that player wins. The game ends in a draw if the board is filled
before either player wins.

This file defines two classes: the main applet class, GoMuku,
and a canvas class, GoMokuCanvas.

It is assumed that this applet is 330 pixels wide and 240 pixels high!

*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class GoMoku extends Applet {

/* The main applet class only lays out the applet. The work of
the game is all done in the GoMokuCanvas object. Note that
the Buttons and Label used in the applet are defined as
instance variables in the GoMokuCanvas class. The applet
class gives them their visual appearance and sets their
size and positions.*/

public void init() {

setLayout(null); // I will do the layout myself.

setBackground(new Color(0,150,0)); // Dark green background.

/* Create the components and add them to the applet. */

GoMokuCanvas board = new GoMokuCanvas();
// Note: The constructor creates the buttons board.resignButton
// and board.newGameButton and the Label board.message.
add(board);

board.newGameButton.setBackground(Color.lightGray) ;
add(board.newGameButton);

board.resignButton.setBackground(Color.lightGray);
add(board.resignButton);

board.message.setForeground(Color.green);
board.message.setFont(new Font("Serif", Font.BOLD, 14));
add(board.message);

/* Set the position and size of each component by calling
its setBounds() method. */

board.setBounds(16,16,172,172); // Note: size MUST be 172-by-172 !
board.newGameButton.setBounds(210, 60, 100, 30);
board.resignButton.setBounds(210, 120, 100, 30);
board.message.setBounds(0, 200, 330, 30);
}

} // end class GoMoku




class GoMokuCanvas extends Canvas implements ActionListener, MouseListener {

Button resignButton; // Current player can resign by clicking this button.
Button newGameButton; // This button starts a new game. It is enabled only
// when the current game has ended.

Label message; // A label for displaying messages to the user.

int[][] board; // The data for the board is kept here. The values
// in this array are chosen from the following constants.

static final int EMPTY = 0, // Represents an empty square.
WHITE = 1, // A white piece.
BLACK = 2; // A black piece.

boolean gameInProgress; // Is a game currently in progress?

int currentPlayer; // Whose turn is it now? The possible values
// are WHITE and BLACK. (This is valid only while
// a game is in progress.)

int win_r1, win_c1, win_r2, win_c2; // When a player wins by getting five or more
// pieces in a row, the squares at the
// ends of the row are (win_r1,win_c1)
// and (win_r2,win_c2). A red line is
// drawn between these squares. When there
// are no five pieces in a row, the value of
// win_r1 is -1. The values are set in the
// count() method. The value of win_r1 is
// tested in the paint() method.


public GoMokuCanvas() {
// Constructor. Create the buttons and label. Listen for mouse
// clicks and for clicks on the buttons. Create the board and
// start the first game.
setBackground(Color.lightGray);
addMouseListener(this);
setFont(new Font("Serif", Font.BOLD, 14));
resignButton = new Button("Resign");
resignButton.addActionListener(this);
newGameButton = new Button("New Game");
newGameButton.addActionListener(this);
message = new Label("",Label.CENTER);
board = new int[13][13];
doNewGame();
}


public void actionPerformed(ActionEvent evt) {
// Respond to user's click on one of the two buttons.
Object src = evt.getSource();
if (src == newGameButton)
doNewGame();
else if (src == resignButton)
doResign();
}


void doNewGame() {
// Begin a new game.
if (gameInProgress == true) {
// This should not be possible, but it doesn't
// hurt to check.
message.setText("Finish the current game first!");
return;
}
for (int row = 0; row < 13; row++) // Fill the board with EMPTYs
for (int col = 0; col < 13; col++)
board[row][col] = EMPTY;
currentPlayer = BLACK; // BLACK moves first.
message.setText("BLACK: Make your move.");
gameInProgress = true;
newGameButton.setEnabled(false);
resignButton.setEnabled(true);
win_r1 = -1; // This value indicates that no red line is to be drawn.
repaint();
}


void doResign() {
// Current player resigns. Game ends. Opponent wins.
if (gameInProgress == false) {
// This should not be possible.
message.setText("There is no game in progress!");
return;
}
if (currentPlayer == WHITE)
message.setText("WHITE resigns. BLACK wins.");
else
message.setText("BLACK resigns. WHITE wins.");
newGameButton.setEnabled(true);
resignButton.setEnabled(false);
gameInProgress = false;
}


void gameOver(String str) {
// The game ends. The parameter, str, is displayed as a message.
message.setText(str);
newGameButton.setEnabled(true);
resignButton.setEnabled(false);
gameInProgress = false;
}


void doClickSquare(int row, int col) {
// This is called by mousePressed() when a player clicks on the
// square in the specified row and col. It has already been checked
// that a game is, in fact, in progress.

/* Check that the user clicked an empty square. If not, show an
error message and exit. */

if ( board[row][col] != EMPTY ) {
if (currentPlayer == BLACK)
message.setText("BLACK: Please click an empty square.");
else
message.setText("WHITE: Please click an empty square.");
return;
}

/* Make the move. Check if the board is full or if the move
is a winning move. If so, the game ends. If not, then it's
the other user's turn. */

board[row][col] = currentPlayer; // Make the move.
Graphics g = getGraphics();
drawPiece(g, currentPlayer, row, col);
g.dispose();

if (winner(row,col)) { // First, check for a winner.
if (currentPlayer == WHITE)
gameOver("WHITE wins the game!");
else
gameOver("BLACK wins the game!");
Graphics w = getGraphics();
drawWinLine(w);
w.dispose();
return;
}

boolean emptySpace = false; // Check if the board is full.
for (int i = 0; i < 13; i++)
for (int j = 0; j < 13; j++)
if (board[i][j] == EMPTY)
emptySpace = true;
if (emptySpace == false) {
gameOver("The game ends in a draw.");
return;
}

/* Continue the game. It's the other player's turn. */

if (currentPlayer == BLACK) {
currentPlayer = WHITE;
message.setText("WHITE: Make your move.");
}
else {
currentPlayer = BLACK;
message.setText("BLACK: Make your move.");
}

} // end doClickSquare()


private boolean winner(int row, int col) {
// This is called just after a piece has been played on the
// square in the specified row and column. It determines
// whether that was a winning move by counting the number
// of squares in a line in each of the four possible
// directions from (row,col). If there are 5 squares (or more)
// in a row in any direction, then the game is won.

if (count( board[row][col], row, col, 1, 0 ) >= 5)
return true;
if (count( board[row][col], row, col, 0, 1 ) >= 5)
return true;
if (count( board[row][col], row, col, 1, -1 ) >= 5)
return true;
if (count( board[row][col], row, col, 1, 1 ) >= 5)
return true;

/* When we get to this point, we know that the game is not
won. The value of win_r1, which was changed in the count()
method, has to be reset to -1, to avoid drawing a red line
on the board. */

win_r1 = -1;
return false;

} // end winner()


private int count(int player, int row, int col, int dirX, int dirY) {
// Counts the number of the specified player's pieces starting at
// square (row,col) and extending along the direction specified by
// (dirX,dirY). It is assumed that the player has a piece at
// (row,col). This method looks at the squares (row + dirX, col+dirY),
// (row + 2*dirX, col + 2*dirY), ... until it hits a square that is
// off the board or is not occupied by one of the players pieces.
// It counts the squares that are occupied by the player's pieces.
// Furthermore, it sets (win_r1,win_c1) to mark last position where
// it saw one of the player's pieces. Then, it looks in the
// opposite direction, at squares (row - dirX, col-dirY),
// (row - 2*dirX, col - 2*dirY), ... and does the same thing.
// Except, this time it sets (win_r2,win_c2) to mark the last piece.
// Note: The values of dirX and dirY must be 0, 1, or -1. At least
// one of them must be non-zero.

int ct = 1; // Number of pieces in a row belonging to the player.

int r, c; // A row and column to be examined

r = row + dirX; // Look at square in specified direction.
c = col + dirY;
while ( r >= 0 && r < 13 && c >= 0 && c < 13 && board[r][c] == player ) {
// Square is on the board and contains one of the players's pieces.
ct++;
r += dirX; // Go on to next square in this direction.
c += dirY;
}

win_r1 = r - dirX; // The next-to-last square looked at.
win_c1 = c - dirY; // (The LAST one looked at was off the board or
// did not contain one of the player's pieces.

r = row - dirX; // Look in the opposite direction.
c = col - dirY;
while ( r >= 0 && r < 13 && c >= 0 && c < 13 && board[r][c] == player ) {
// Square is on the board and contains one of the players's pieces.
ct++;
r -= dirX; // Go on to next square in this direction.
c -= dirY;
}

win_r2 = r + dirX;
win_c2 = c + dirY;

// At this point, (win_r1,win_c1) and (win_r2,win_c2) mark the endpoints
// of the line of pieces belonging to the player.

return ct;

} // end count()


public void paint(Graphics g) {

/* Draw a two-pixel black border around the edges of the canvas,
and draw grid lines in darkGray. */

g.setColor(Color.darkGray);
for (int i = 1; i < 13; i++) {
g.drawLine(1 + 13*i, 0, 1 + 13*i, getSize().height);
g.drawLine(0, 1 + 13*i, getSize().width, 1 + 13*i);
}
g.setColor(Color.black);
g.drawRect(0,0,getSize().width-1,getSize().height-1);
g.drawRect(1,1,getSize().width-3,getSize().height-3);

/* Draw the pieces that are on the board. */

for (int row = 0; row < 13; row++)
for (int col = 0; col < 13; col++)
if (board[row][col] != EMPTY)
drawPiece(g, board[row][col], row, col);

/* If the game has been won, then win_r1 >= 0. Draw a line to mark
the five winning pieces. */

if (win_r1 >= 0)
drawWinLine(g);

} // end paint()


private void drawPiece(Graphics g, int piece, int row, int col) {
// Draw a piece in the square at (row,col). The color is specified
// by the piece parameter, which should be either BLACK or WHITE.
if (piece == WHITE)
g.setColor(Color.white);
else
g.setColor(Color.black);
g.fillOval(3 + 13*col, 3 + 13*row, 10, 10);
}


private void drawWinLine(Graphics g) {
// Draw a 2-pixel wide red line from the middle of the square at
// (win_r1,win_c1) to the middle of the square at (win_r2,win_c2).
// This routine is called to mark the 5 pieces that won the game.
// The values of the variables are set in the count() method.
g.setColor(Color.red);
g.drawLine( 8 + 13*win_c1, 8 + 13*win_r1, 8 + 13*win_c2, 8 + 13*win_r2 );
if (win_r1 == win_r2)
g.drawLine( 8 + 13*win_c1, 7 + 13*win_r1, 8 + 13*win_c2, 7 + 13*win_r2 );
else
g.drawLine( 7 + 13*win_c1, 8 + 13*win_r1, 7 + 13*win_c2, 8 + 13*win_r2 );
}


public Dimension getPreferredSize() {
// Specify desired size for this component. Note:
// the size MUST be 172 by 172.
return new Dimension(172, 172);
}


public Dimension getMinimumSize() {
return new Dimension(172, 172);
}


public void mousePressed(MouseEvent evt) {
// Respond to a user click on the board. If no game is
// in progress, show an error message. Otherwise, find
// the row and column that the user clicked and call
// doClickSquare() to handle it.
if (gameInProgress == false)
message.setText("Click \"New Game\" to start a new game.");
else {
int col = (evt.getX() - 2) / 13;
int row = (evt.getY() - 2) / 13;
if (col >= 0 && col < 13 && row >= 0 && row < 13)
doClickSquare(row,col);
}
}


public void mouseReleased(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }


} // end class GoMokuCanvas

luongthuanthanh
29-08-2007, 20:28
Cám ơn bạn nhiều nha! game hay tuyệt :D

xuanman
28-09-2007, 12:48
tro choi hay lam !
copy code roi bo vao eclipse runas la chay thoi

HitmanC47
29-09-2007, 11:15
Nhưng đoạn code trên chỉ đánh người với người chứ ko có đánh với máy...

binhkien
17-10-2007, 08:50
sao minh tai ve ma giai nen khong duoc
mong bac chi cach dum

sonC0609k
19-10-2007, 13:37
thuật toán của người này viết mình đọc vào cảm thấy khó hiểu

pean
20-10-2007, 13:47
game hay lắm cám ơn bạn

niemtin
29-11-2007, 11:04
GoMoku.java
******************x

nhưng bạn ơi có thể giải thích sơ được không ?
những nơi tính toán có nhứng con số minh không nghi ra được là tại sao có
ví dụ như sao phải 172 chứ v.v.v.
xin chỉ giúp cho nhé
cảm ơn

tamtam282003
03-02-2008, 03:11
có bạn nào có caro mà chơi trên web sever khong vay?

minhkhai
03-02-2008, 11:08
thi cuối kì vừa rùi tui làm caro thấy chẳng co j ghê cả

minhkhai
03-02-2008, 11:10
tui làm cảo bằng C# nếu cần thì gửi tin nhắn cho tui thienkiemma@gmail.com tui sẽ gửi cho

svxdhn-49XF
22-02-2008, 20:41
Up lên đâu đó đi bạn ơi :D down cho tiện chứ send mail nhác lắm ~x(

thithaoptit
07-03-2008, 08:53
Chương trình caro này mình sư tập được trên Net ,dịch ngược lại mã nguồn và xin được Post lên cho các bạn yêu thích Java xem qua :
1. Chương trình viết bằng Java Applet ,vì vậy bạn chỉ cần chạy file "Caro Game.html" băng IE là có thể đánh caro với máy được rồi đó :)
2. Source Code của Caro la "JavaCaro.java" ,vân chỉ một file duy nhất.
3.Chương trình này của một tác giả nước ngoài ,xin vui lòng đừng sửa tên tác giả thành . . . tên mình.Nếu bạn phát triển thêm bạn phải ghi "Developed by" trước hoặc sau tên tác giả.
Chúc thành công .

Xin chào mọi người!
Ai có source code chơi cờ caro giữa người và máy(apllet của java) đã giới thiệu trên xin cho mình với. Địa chỉ mail là thithaoptit@gmail.com

tubachkhoa2004
07-03-2008, 09:54
Tớ có một cái code chơi cờ caro đơn giản người và máy, thuật toán cũng ngắn nhưng không được sâu sắc lắm:

demo : http://tubachkhoa2004.t35.com/gameatAI/thagach.htm

tubachkhoa2004
07-03-2008, 09:58
Code các thủ tục cơ bản viết bằng Javascript:

************************************************** *****

var rMax; cMax; gMax;
var cost,pos,dir;
var whoTurns;
var lookForEmpty=new Image();
var lookForYou=new Image();
var lookForMe=new Image();
var Go = new Array ();
************************************************** *****
checkImage(counter,image) {
return document.images[counter]==image
}
************************************************** *****
checkBoard() {
for(i=0;i<cMax;i++) if(checkImage(i,lookForEmpty)) return false
alert ('Bảng đầy, hãy chơi lại ván khác !')
}
************************************************** *****
function AI() {
whoWins=checkForWinner(lookForMe)
whoWins=="Idle" ? whoTurn="You" : alert(whoWins + "wins")
}
************************************************** *****
function dropDown(posCol) {
whoTurn == "You" ? (dropIt(posCol) ? whoTurn = "Me" : return) : return
AI()
}
************************************************** *****
function dropIt(posCol) {
for (i=posCol;i<rMax*cMax;i=i+cMax)
if (! checkImage(i,lookForEmpty)) return stickIt(i-cMax)
return checkBoard()
}
************************************************** *****
function stickIt(pos) {
return (pos>-1 && document.images[pos]=(whoTurn == "You" ? lookForYou : lookForMe) )
}
************************************************** *****
function checkRound(counter,next,loop,lookForMe,lookForYou) {
gramMa=0;
for (i=0;i<gMax;i++)
if (checkImage(counter + i*loop,lookForYou)
|| checkImage(counter + i*loop+cMax,lookForEmpty)) return false
else if (checkImage(counter + i*loop, lookForMe)
|| counter + i*loop == next) gramMa ++
if checkImage(counter,lookForEmpty) {
if (counter==pos && gramMax == cost) cost++
if checkImage(counter+gMax*loop,lookForEmpty) gramMa ++}
if ( gramMa > cost ) {cost=gramMa ; pos = counter ; dir=loop}
return (cost>=gMax)
}
************************************************** *****
function checkWin(counter,next,lookForMe,lookForYou){
hang=Math.floor(counter/cMax);cot=counter-hang*cMax;
return ((cot < cMax - gMax + 1
&& checkRound(counter,next,1,lookForMe,lookForYou))
|| (hang < cMax - gMax + 1
&& checkRound(counter,next,cMax,lookForMe,lookForYou) )
|| (hang < cMax - gMax + 1
&& cot < cMax - gMax + 1
&& checkRound(counter,next,cMax+1,lookForMe,lookForYo u))
|| (hang > cMax - gMax
&& cot < cMax - gMax + 1
&& checkRound(counter,next,-cMax-1,lookForMe,lookForYou)))
}
************************************************** *****
function checkForNext(next,lookForMe) {
if (next<0) return false
start=next-(gMax-1)*cMax; if (start<0) start=0
stop=next+(gMax-1)*cMax;if (stop>rMax*cMax) stop=rMax*cMax
for (i=start;i<stop;i++)
if (checkWin(i,next,lookForYou,lookForMe)
|| checkWin(i,next,lookForMe,lookForYou)) return true
return false
}
************************************************** *****
function checkForWinner (lookForMe) {
cost=0;pos=0;dir=0;
for (i=0;i<rMax*cMax;i++)
if (checkWin(i,-1,lookForYou,lookForMe)) return "You"
cost--
for (i=0;i<rMax*cMax;i++)
if (checkWin(i,-1,lookForMe,lookForYou)) return "Me"
feet=0
if (cost!=0) {
for (i=0;i<gMax;i++)
if (checkImage(pos + i*dir, lookForEmpty)
&& (!checkForNext(pos + i*dir -cMax,lookForMe)
|| feet==0)) {Go[feet]=pos + i*dir ; feet++}
stickIt(Go[Math.floor(Math.round()*feet)])
}
else {
while (!dropIt(Math.floor(Math.round()*cMax)))
dropIt(Math.floor(Math.round()*cMax))
}
return "Idle"
}
************************************************** *****
function makeBoard() {
rMax=row.op.item(row.selectedIndex).innerHTML
//<select id=row><option id=op></option></select>
rMax=col.op.item(col.selectedIndex).innerHTML
gMax=gra.op.item(gra.selectedIndex).innerHTML
whoTurns = (box.checked ? "You" : "Me")
//<input type=checkbox id=box>
lookForEmpty.src=bground.op.item(bground.selectedI ndex).innerHTML + ".gif"
lookForYou=forYou.item(forYou.selectedIndex)
//<img id=forYou src="you.gif">
lookForMe=forMe.item(forMe.selectedIndex)
str="<table>"
for (i=0;i<rMax;i++) {
str+="<tr>"
for (j=0;j<cMax;j++)
str+=['<td><a href="javascript:dropDown('+j+')">
<img src="'+lookForEmpty+'" height=20 width=20></a></td>']
str+="<tr>"
}
Board.innerHTML=str + "</table>" //<div id=Board></div>
}

dinh tung
31-05-2008, 07:25
mình đang cần gấp đoạn code caro viết bằng c#.bạn nào có thể giúp mình được ko?!!!!!!!!!!!!!!!!!

[=========> Bổ sung bài viết <=========]

cám ơn các bạn trước nha

[=========> Bổ sung bài viết <=========]

ban "Minh Khai" oi giúp mình nhé

ronado
10-07-2008, 08:47
cho mình code này với nha bạn ơi email là hoahuutran@gmail.com ... cảm ơn bạn

binhinfo
16-09-2008, 08:39
down game nhấn vào caro.zip thì nó ra link là cái hình ma :(

[=========> Bổ sung bài viết <=========]

Đây là code caro của C++ ne ban tham khảo thử
/////////////////////

#include <iostream.h>
#include <string.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <graphics.h>

int M[22][23];
int Col=240, Row=220, I=10, J=11;
int Play=1,cont=1;
char Player1[10],Player2[10];

void Win(char name[]);
void Choi();
int Thang(int , int , int );
void XuatVT(int , int , int );
void Chu(int , int , int );
void Khung(int , int , int );
void Khoidau();
void Nen();
void Phien(char st[], int ,int );
void KTDH(void);

void main(void)
{ Khoidau();
KTDH();
do{ Play=1;Col=240; Row=220; I=10; J=11;
for(int i=0; i<22; i++)
for(int j=0; j<23; j++)
M[i][j]=0;
cleardevice();
Nen();
Choi();
}while(cont==1);
closegraph();
}

void Win(char name[])
{
settextstyle(1,0,9);
setcolor(15);
outtextxy(10,10,name);
outtextxy(10,100," won");
}

void Choi()
{ char key;
Khung(Col,Row,1);
XuatVT(I,J,3);
char Chuoi[236]=" This is program demo CARO game between two players. Press F2 to new game, F10 to quit game and when you finish a game, press any key to a new game. This program was written by................................";
int l=strlen(Chuoi);
Phien(Player1,170,15+BLINK);
do{ do{ settextstyle(2,0,5);
setcolor(15);
outtextxy(1,460,Chuoi);
delay(200);
setcolor(1);
outtextxy(1,460,Chuoi);
char st=Chuoi[0];
for(int i=0; i<l; i++)
Chuoi[i]=Chuoi[i+1];
Chuoi[l]=st;
}while(!kbhit());
key=getch();
if(key==0) key=getch();
if(key==77)
{ Khung(Col,Row,3);
XuatVT(I,J,1);
Col=Col+20; J++;
if(Col==480) {Col=20; J=0;}
Khung(Col,Row,1);
XuatVT(I,J,3);
}
if(key==75)
{ Khung(Col,Row,3);
XuatVT(I,J,1);
Col=Col-20; J--;
if(Col==0) {Col=460;J=22;}
Khung(Col,Row,1);
XuatVT(I,J,3);
}
if(key==80)
{ Khung(Col,Row,3);
XuatVT(I,J,1);
Row=Row+20; I++;
if(Row==460) {Row=20; I=0;}
Khung(Col,Row,1);
XuatVT(I,J,3);
}
if(key==72)
{ Khung(Col,Row,3);
XuatVT(I,J,1);

Row=Row-20; I--;
if(Row==0) {Row=440; I=21;}
Khung(Col,Row,1);
XuatVT(I,J,3);
}
if(key==13)
if(M[i][J]==0)
{ Chu(Col,Row,Play);
if(Play==1)
{ M[i][J]=1;
if(Thang(I,J,Play)==Play)
{ Win(Player1);getch();
Play=-1;return;
}
Play=-1;
Phien(Player1,170,5);
Phien(Player2,270,15+BLINK);
}
else
{ M[i][J]=-1;
if(Thang(I,J,Play)==Play)
{ Win(Player2);getch();
Play=1;return;
}
Play=1;
Phien(Player2,270,5);
Phien(Player1,170,15+BLINK);
}
}
if(key==60) break;
if(key==68) {cont=0;return;}
}while(key!=68);
}

int Thang(int x, int y, int s)
{ if(M[x][y-4]!=-s&&M[x][y-3]==s&&M[x][y-2]==s&&M[x][y-1]==s&&M[x][y]==s&&M[x][y+1]!=-s)
return(s);
if(M[x][y-3]!=-s&&M[x][y-2]==s&&M[x][y-1]==s&&M[x][y]==s&&M[x][y+1]==s&&M[x][y+2]!=-s)
return(s);
if(M[x][y-2]!=-s&&M[x][y-1]==s&&M[x][y]==s&&M[x][y+1]==s&&M[x][y+2]==s&&M[x][y+3]!=-s)
return(s);
if(M[x][y-1]!=-s&&M[x][y]==s&&M[x][y+1]==s&&M[x][y+2]==s&&M[x][y+3]==s&&M[x][y+4]!=-s)
return(s);

if(M[x-4][y]!=-s&&M[x-3][y]==s&&M[x-2][y]==s&&M[x-1][y]==s&&M[x][y]==s&&M[x+1][y]!=-s)
return(s);
if(M[x-3][y]!=-s&&M[x-2][y]==s&&M[x-1][y]==s&&M[x][y]==s&&M[x+1][y]==s&&M[x+2][y]!=-s)
return(s);
if(M[x-2][y]!=-s&&M[x-1][y]==s&&M[x][y]==s&&M[x+1][y]==s&&M[x+2][y]==s&&M[x+3][y]!=-s)
return(s);
if(M[x-1][y]!=-s&&M[x][y]==s&&M[x+1][y]==s&&M[x+2][y]==s&&M[x+3][y]==s&&M[x+4][y]!=-s)
return(s);

if(M[x-4][y-4]!=-s&&M[x-3][y-3]==s&&M[x-2][y-2]==s&&M[x-1][y-1]==s&&M[x][y]==s&&M[x+1][y+1]!=-s)
return(s);
if(M[x-3][y-3]!=-s&&M[x-2][y-2]==s&&M[x-1][y-1]==s&&M[x][y]==s&&M[x+1][y+1]==s&&M[x+2][y+2]!=-s)
return(s);
if(M[x-2][y-2]!=-s&&M[x-1][y-1]==s&&M[x][y]==s&&M[x+1][y+1]==s&&M[x+2][y+2]==s&&M[x+3][y+3]!=-s)
return(s);
if(M[x-1][y-1]!=-s&&M[x][y]==s&&M[x+1][y+1]==s&&M[x+2][y+2]==s&&M[x+3][y+3]==s&&M[x+4][y+4]!=-s)
return(s);

if(M[x-4][y+4]!=-s&&M[x-3][y+3]==s&&M[x-2][y+2]==s&&M[x-1][y+1]==s&&M[x][y]==s&&M[x+1][y-1]!=-s)
return(s);
if(M[x-3][y+3]!=-s&&M[x-2][y+2]==s&&M[x-1][y+1]==s&&M[x][y]==s&&M[x+1][y-1]==s&&M[x+2][y-2]!=-s)
return(s);
if(M[x-2][y+2]!=-s&&M[x-1][y+1]==s&&M[x][y]==s&&M[x+1][y-1]==s&&M[x+2][y-2]==s&&M[x+3][y-3]!=-s)
return(s);
if(M[x-1][y+1]!=-s&&M[x][y]==s&&M[x+1][y-1]==s&&M[x+2][y-2]==s&&M[x+3][y-3]==s&&M[x+4][y-4]!=-s)
return(s);

return(0);
}

void XuatVT(int x, int y, int color)
{ settextstyle(1,0,1);
setcolor(color);
char c1[3],c2[3],c3[3];
itoa(x+1,c1,10);
itoa(y+1,c2,10);
outtextxy(580,30,c1);
outtextxy(580,60,c2);
}

void Chu(int x, int y, int status)
{ settextstyle(1,0,1);
if(status==1)
{ setcolor(14);
outtextxy(x-5,y-13,"O");
}
if(status==-1)
{ setcolor(5);
outtextxy(x-5,y-13,"X");
}
}
void Khung(int x, int y, int color)
{ setcolor(color);
line(x-3,y-10,x-7,y-10);
line(x-7,y-10,x-7,y-3);
line(x-7,y+3,x-7,y+8);
line(x-7,y+8,x-3,y+8);
line(x+3,y+8,x+7,y+8);
line(x+7,y+8,x+7,y+3);
line(x+7,y-3,x+7,y-10);
line(x+7,y-10,x+3,y-10);
}

void Khoidau()
{ window(1,1,80,25);textbackground(0);textcolor(15); clrscr();
gotoxy(40-5,2);cout<<"CARO GAME";
window(10,4,26,4);textbackground(1);textcolor(14); clrscr();
cout<<" Player 1's name ";
do{ window(14,6,50,6);textbackground(0);clrscr();
window(14,6,22,6);textbackground(3);textcolor(5);c lrscr();
fflush(stdin);gets(Player1);
}while(strlen(Player1)>8);
if(strlen(Player1)==0) strcpy(Player1,"Player 1");
window(54,4,70,4);textbackground(1);textcolor(14); clrscr();
cout<<" Player 2's name ";
do{ window(58,6,79,6);textbackground(0);clrscr();
window(58,6,66,6);textbackground(3);textcolor(5);c lrscr();
fflush(stdin);gets(Player2);
}while(strlen(Player2)>8);
if(strlen(Player2)==0) strcpy(Player2,"Player 2");
return;
}

void Nen()
{
setcolor(1);
setfillstyle(1,1);
rectangle(480,1,640,480);
rectangle(1,460,480,480);
floodfill(500,200,1);
floodfill(200,470,1);
setbkcolor(3);
setcolor(6);
for(int i=0; i<24; i++)
line(1,20*i,480,20*i);
for(i=0; i<25; i++)
line(20*i,1,20*i,460);
settextstyle(1,0,1);
setcolor(3);
outtextxy(500,30,"Hang = ");
outtextxy(500,60,"Cot = ");
settextstyle(2,0,5);
setcolor(15);
outtextxy(500,150,"Player 1's name");
outtextxy(500,250,"Player 2's name");
settextstyle(1,0,3);
setcolor(5);
outtextxy(510,170,Player1);
outtextxy(510,270,Player2);
}

void Phien(char st[], int col,int mau)
{ settextstyle(1,0,3);
setcolor(mau);
outtextxy(510,col,st);
}

void KTDH(void)
{ int L;
char* grPats = "C:\borlandc\BGI";
do{ int grDR = DETECT;
int grMD;
initgraph(&grDR,&grMD,grPats);
L = graphresult();
if(L!=grOk)
{ cout<<"\n Loi Khoi tao do hoa: "<<grapherrormsg(L);
if(L==grFileNotFound) // neu co loi duong dan thi nhap lai
{ cout<<"\n Cho duong dan toi BGI hoac an <Ctrl-Break> de dung Chuong trinh\n";
cout<<" Duong dan la: ";
fflush(stdin);
gets(grPats);
}
else exit(0);
}
} while(L!=grOk);
return;
}
///////////////////////////

[=========> Bổ sung bài viết <=========]

Đây là code caro của C++ ne ban tham khảo thử
/////////////////////

#include <iostream.h>
#include <string.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <graphics.h>

int M[22][23];
int Col=240, Row=220, I=10, J=11;
int Play=1,cont=1;
char Player1[10],Player2[10];

void Win(char name[]);
void Choi();
int Thang(int , int , int );
void XuatVT(int , int , int );
void Chu(int , int , int );
void Khung(int , int , int );
void Khoidau();
void Nen();
void Phien(char st[], int ,int );
void KTDH(void);

void main(void)
{ Khoidau();
KTDH();
do{ Play=1;Col=240; Row=220; I=10; J=11;
for(int i=0; i<22; i++)
for(int j=0; j<23; j++)
M[i][j]=0;
cleardevice();
Nen();
Choi();
}while(cont==1);
closegraph();
}

void Win(char name[])
{
settextstyle(1,0,9);
setcolor(15);
outtextxy(10,10,name);
outtextxy(10,100," won");
}

void Choi()
{ char key;
Khung(Col,Row,1);
XuatVT(I,J,3);
char Chuoi[236]=" This is program demo CARO game between two players. Press F2 to new game, F10 to quit game and when you finish a game, press any key to a new game. This program was written by................................";
int l=strlen(Chuoi);
Phien(Player1,170,15+BLINK);
do{ do{ settextstyle(2,0,5);
setcolor(15);
outtextxy(1,460,Chuoi);
delay(200);
setcolor(1);
outtextxy(1,460,Chuoi);
char st=Chuoi[0];
for(int i=0; i<l; i++)
Chuoi[i]=Chuoi[i+1];
Chuoi[l]=st;
}while(!kbhit());
key=getch();
if(key==0) key=getch();
if(key==77)
{ Khung(Col,Row,3);
XuatVT(I,J,1);
Col=Col+20; J++;
if(Col==480) {Col=20; J=0;}
Khung(Col,Row,1);
XuatVT(I,J,3);
}
if(key==75)
{ Khung(Col,Row,3);
XuatVT(I,J,1);
Col=Col-20; J--;
if(Col==0) {Col=460;J=22;}
Khung(Col,Row,1);
XuatVT(I,J,3);
}
if(key==80)
{ Khung(Col,Row,3);
XuatVT(I,J,1);
Row=Row+20; I++;
if(Row==460) {Row=20; I=0;}
Khung(Col,Row,1);
XuatVT(I,J,3);
}
if(key==72)
{ Khung(Col,Row,3);
XuatVT(I,J,1);

Row=Row-20; I--;
if(Row==0) {Row=440; I=21;}
Khung(Col,Row,1);
XuatVT(I,J,3);
}
if(key==13)
if(M[i][J]==0)
{ Chu(Col,Row,Play);
if(Play==1)
{ M[i][J]=1;
if(Thang(I,J,Play)==Play)
{ Win(Player1);getch();
Play=-1;return;
}
Play=-1;
Phien(Player1,170,5);
Phien(Player2,270,15+BLINK);
}
else
{ M[i][J]=-1;
if(Thang(I,J,Play)==Play)
{ Win(Player2);getch();
Play=1;return;
}
Play=1;
Phien(Player2,270,5);
Phien(Player1,170,15+BLINK);
}
}
if(key==60) break;
if(key==68) {cont=0;return;}
}while(key!=68);
}

int Thang(int x, int y, int s)
{ if(M[x][y-4]!=-s&&M[x][y-3]==s&&M[x][y-2]==s&&M[x][y-1]==s&&M[x][y]==s&&M[x][y+1]!=-s)
return(s);
if(M[x][y-3]!=-s&&M[x][y-2]==s&&M[x][y-1]==s&&M[x][y]==s&&M[x][y+1]==s&&M[x][y+2]!=-s)
return(s);
if(M[x][y-2]!=-s&&M[x][y-1]==s&&M[x][y]==s&&M[x][y+1]==s&&M[x][y+2]==s&&M[x][y+3]!=-s)
return(s);
if(M[x][y-1]!=-s&&M[x][y]==s&&M[x][y+1]==s&&M[x][y+2]==s&&M[x][y+3]==s&&M[x][y+4]!=-s)
return(s);

if(M[x-4][y]!=-s&&M[x-3][y]==s&&M[x-2][y]==s&&M[x-1][y]==s&&M[x][y]==s&&M[x+1][y]!=-s)
return(s);
if(M[x-3][y]!=-s&&M[x-2][y]==s&&M[x-1][y]==s&&M[x][y]==s&&M[x+1][y]==s&&M[x+2][y]!=-s)
return(s);
if(M[x-2][y]!=-s&&M[x-1][y]==s&&M[x][y]==s&&M[x+1][y]==s&&M[x+2][y]==s&&M[x+3][y]!=-s)
return(s);
if(M[x-1][y]!=-s&&M[x][y]==s&&M[x+1][y]==s&&M[x+2][y]==s&&M[x+3][y]==s&&M[x+4][y]!=-s)
return(s);

if(M[x-4][y-4]!=-s&&M[x-3][y-3]==s&&M[x-2][y-2]==s&&M[x-1][y-1]==s&&M[x][y]==s&&M[x+1][y+1]!=-s)
return(s);
if(M[x-3][y-3]!=-s&&M[x-2][y-2]==s&&M[x-1][y-1]==s&&M[x][y]==s&&M[x+1][y+1]==s&&M[x+2][y+2]!=-s)
return(s);
if(M[x-2][y-2]!=-s&&M[x-1][y-1]==s&&M[x][y]==s&&M[x+1][y+1]==s&&M[x+2][y+2]==s&&M[x+3][y+3]!=-s)
return(s);
if(M[x-1][y-1]!=-s&&M[x][y]==s&&M[x+1][y+1]==s&&M[x+2][y+2]==s&&M[x+3][y+3]==s&&M[x+4][y+4]!=-s)
return(s);

if(M[x-4][y+4]!=-s&&M[x-3][y+3]==s&&M[x-2][y+2]==s&&M[x-1][y+1]==s&&M[x][y]==s&&M[x+1][y-1]!=-s)
return(s);
if(M[x-3][y+3]!=-s&&M[x-2][y+2]==s&&M[x-1][y+1]==s&&M[x][y]==s&&M[x+1][y-1]==s&&M[x+2][y-2]!=-s)
return(s);
if(M[x-2][y+2]!=-s&&M[x-1][y+1]==s&&M[x][y]==s&&M[x+1][y-1]==s&&M[x+2][y-2]==s&&M[x+3][y-3]!=-s)
return(s);
if(M[x-1][y+1]!=-s&&M[x][y]==s&&M[x+1][y-1]==s&&M[x+2][y-2]==s&&M[x+3][y-3]==s&&M[x+4][y-4]!=-s)
return(s);

return(0);
}

void XuatVT(int x, int y, int color)
{ settextstyle(1,0,1);
setcolor(color);
char c1[3],c2[3],c3[3];
itoa(x+1,c1,10);
itoa(y+1,c2,10);
outtextxy(580,30,c1);
outtextxy(580,60,c2);
}

void Chu(int x, int y, int status)
{ settextstyle(1,0,1);
if(status==1)
{ setcolor(14);
outtextxy(x-5,y-13,"O");
}
if(status==-1)
{ setcolor(5);
outtextxy(x-5,y-13,"X");
}
}
void Khung(int x, int y, int color)
{ setcolor(color);
line(x-3,y-10,x-7,y-10);
line(x-7,y-10,x-7,y-3);
line(x-7,y+3,x-7,y+8);
line(x-7,y+8,x-3,y+8);
line(x+3,y+8,x+7,y+8);
line(x+7,y+8,x+7,y+3);
line(x+7,y-3,x+7,y-10);
line(x+7,y-10,x+3,y-10);
}

void Khoidau()
{ window(1,1,80,25);textbackground(0);textcolor(15); clrscr();
gotoxy(40-5,2);cout<<"CARO GAME";
window(10,4,26,4);textbackground(1);textcolor(14); clrscr();
cout<<" Player 1's name ";
do{ window(14,6,50,6);textbackground(0);clrscr();
window(14,6,22,6);textbackground(3);textcolor(5);c lrscr();
fflush(stdin);gets(Player1);
}while(strlen(Player1)>8);
if(strlen(Player1)==0) strcpy(Player1,"Player 1");
window(54,4,70,4);textbackground(1);textcolor(14); clrscr();
cout<<" Player 2's name ";
do{ window(58,6,79,6);textbackground(0);clrscr();
window(58,6,66,6);textbackground(3);textcolor(5);c lrscr();
fflush(stdin);gets(Player2);
}while(strlen(Player2)>8);
if(strlen(Player2)==0) strcpy(Player2,"Player 2");
return;
}

void Nen()
{
setcolor(1);
setfillstyle(1,1);
rectangle(480,1,640,480);
rectangle(1,460,480,480);
floodfill(500,200,1);
floodfill(200,470,1);
setbkcolor(3);
setcolor(6);
for(int i=0; i<24; i++)
line(1,20*i,480,20*i);
for(i=0; i<25; i++)
line(20*i,1,20*i,460);
settextstyle(1,0,1);
setcolor(3);
outtextxy(500,30,"Hang = ");
outtextxy(500,60,"Cot = ");
settextstyle(2,0,5);
setcolor(15);
outtextxy(500,150,"Player 1's name");
outtextxy(500,250,"Player 2's name");
settextstyle(1,0,3);
setcolor(5);
outtextxy(510,170,Player1);
outtextxy(510,270,Player2);
}

void Phien(char st[], int col,int mau)
{ settextstyle(1,0,3);
setcolor(mau);
outtextxy(510,col,st);
}

void KTDH(void)
{ int L;
char* grPats = "C:\borlandc\BGI";
do{ int grDR = DETECT;
int grMD;
initgraph(&grDR,&grMD,grPats);
L = graphresult();
if(L!=grOk)
{ cout<<"\n Loi Khoi tao do hoa: "<<grapherrormsg(L);
if(L==grFileNotFound) // neu co loi duong dan thi nhap lai
{ cout<<"\n Cho duong dan toi BGI hoac an <Ctrl-Break> de dung Chuong trinh\n";
cout<<" Duong dan la: ";
fflush(stdin);
gets(grPats);
}
else exit(0);
}
} while(L!=grOk);
return;
}
///////////////////////////

[=========> Bổ sung bài viết <=========]

Các bác nào có code caro viết bằng Java share lại cho mình đi cảm ơn nhiều .Có gì mới tui share lại cho

tieu_thu2705
27-10-2008, 19:44
mình cần code viết bằng C, Các bạn có thể cho mình xin được không. Mình cần gấp lắm,giúp mình với.

khankhan.com
10-11-2008, 03:22
download ko dc ! bro ơi

MyLifeForShare
10-11-2008, 07:23
Tại ddth tắt tính năng đính kèm tập tin rồi, zzzzz
Chuyên gia đào mồ, sống dậy từ 2002 đây :D

jubilohien
14-11-2008, 20:18
Down ko duoc do hen, kiem tra lai dum he. OK!

Chicken-Soft
27-09-2009, 14:39
có cái nào bằng java ma người chơi với máy ko mấy bạn
có cho mình nhé.THANKS allllllllllllllllllllllllllllll

thachthucquy
06-11-2010, 20:24
ai còn source code ct này ko ,up lên dùm mình với
link die òy

phamhydro
10-04-2012, 22:16
down ko dc.............................................

hoangtulun
30-06-2012, 22:48
Tóm lại là down không được. hix