Caro

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Caro as PDF for free.

More details

  • Words: 1,135
  • Pages: 12
Đây là chương trình caro 2 người chơi. chương trình viết bằng ngôn ngữ c, đặc biệt có hỗ trợ con trỏ chuột.

/* caro game with 2 players*/

#include <stdio.h> #include <dos.h> #include #include #include #include <stdlib.h> /***********************************************************************/ /*ma lenh dieu khien con tro chuot*/

int number_buttons = 2; int mouse_size = 16;/*kich thuoc chuot*/

unsigned char mouse_there; unsigned char mouse_visible;

unsigned char lbutton_down, rbutton_down, bbutton_down, lbutton_up, rbutton_up, bbutton_up;

union regs mregs;/*hop giua cac thanh ghi 16 bit va 8 bit*/

/*hop regs dung de lay va nhan thong tin tu ham thuc hien ngat mem int86() */

unsigned char lbutton, rbutton;

void reset_mouse() { mouse_there = 0; mouse_size = 16; mouse_visible = 0; if (getvect(0x33) != 0l)//goi vecto ngat 0x33 { mregs.x.ax = 0;/*khoi tao chuot (chuc nang 0 cua ngat 0x33)*/ int86(0x33, &mregs, &mregs);/*ham thuc hien ngat mem */ if (mregs.x.ax != 0)/* neu da khoi tao chuot*/ { mouse_there = 1; number_buttons = mregs.x.bx; lbutton_down = 0; rbutton_down = 0; bbutton_down = 0; } } }

void show_mouse() { if (mouse_there) {

mregs.x.ax = 1;/*lam hien con tro chuot(chuc nang 1 cua ngat 0x33)*/ int86(0x33, &mregs, &mregs); mouse_visible = 1; } }

void hide_mouse() { if (mouse_there && mouse_visible) { mregs.x.ax = 2;/*lam an con tro chuot(chuc nang 2 cua ngat 0x33)*/ int86(0x33, &mregs, &mregs); mouse_visible = 0; } }

void get_mouse_button(unsigned char *lbutton, unsigned char *rbutton, int *x, int *y) { if (mouse_there) { mregs.x.ax = 3;/*cho biet trang thai num va vi tri con tro(chuc nang 3 cua ngat 0x33)*/ int86(0x33, &mregs, &mregs); *lbutton = (mregs.x.bx == 1) ? 1 : 0;//trang thai an hoac nha cua num trai chuot *rbutton = (mregs.x.bx == 2) ? 1 : 0;//trang thai an hoac nha cua num phai chuot if (mregs.x.bx == 3) *rbutton = *lbutton = 1; *x = mregs.x.cx;/*vi tri ngang hien tai cua con tro chuot*/

*y = mregs.x.dx;/*vi tri doc hien tai cua con tro chuot*/ } } /*********************************************************************/ #define max 25 #define height 16 //chieu dai canh cua moi o trong ban co int bang[max][max];//ban co xem nhu mang 2 chieu gom 25 dong va 25 cot int xleft, yleft;//toa do goc tren ben trai cua ban co int order;//gia tri dai dien cho tung o trong ban co int cont=1; char player1[80],player2[80];

void print_order() { setfillstyle(close_dot_fill,lightblue);//chon kieu va mau cho bar bar(xleft+max*height+30, 40, xleft+max*height+150, 60);//ve bar switch(order) { case 0 :

setcolor(blue);

outtextxy(xleft+max*height+50,45,player1); break; case 1 :

setcolor(red);

outtextxy(xleft+max*height+50,45,player2); break; } setcolor(lightblue); outtextxy(xleft+max*height+10,400,".fullname:ngo dang anh tuan"); }

void vebang() { int i, j; xleft = 10;//gia tri xleft yleft = (getmaxy() - max * height) / 2;//gia tri yleft setcolor(yellow);//mau cac duong ke o ban co for (i=0; i<=max; i++) { line(xleft, yleft + i*height, xleft + max*height, yleft + i*height);//ve cac duong ngang line(xleft + i*height, yleft, xleft + i*height, yleft + max*height);//ve cac duong doc } rectangle(xleft-1, yleft-1,xleft+max*height+1,yleft+max*height+1);//ve khung lon bao quanh ban co rectangle(xleft-2, yleft-2,xleft+max*height+2,yleft+max*height+2);//ve khung lon bao quanh ban co order = 0; /*danh dau cac o trong ban co de thuc hien thuat toan xu ly*/ for (i=0; i<max; i++) for (j=0; j<max; j++) bang[i][j] = -1;//moi o duoc danh dau -1 }

void get_row_col(int x, int y, int *row, int *col) { *col = (x - xleft) / height;/*chi so cot cua mang 2 chieu*/ *row = (y - yleft) / height;/*chi so dong cua mang 2 chieu*/ }

/*thuat toan xu ly*/ int check(int row, int col) { int i; int s1=0,s2=0,s3=0,s4=0,s5=0,s6=0,s7=0,s8=0;//khoi tao cac bien dem int a=0;//dat co

/**********************************/ /* kiem tra hang */

for (i=1; i<=4; i++) { if ( col+i < max) if (bang[row][col+i] == order)//kiem tra cac o giong nhau lien tiep ben phai o hien hanh s1++; else break; } for (i=1; i<=4; i++) { if (col-i >= 0 ) if (bang[row][col-i] == order)//kiem tra cac o giong nhau lien tiep ben trai o hien hanh s2++; else break; }

if((s1+s2)>=4)a=1;

/***********************************/ /* kiem tra cot */

for (i=1; i<=4; i++) { if (row+i < max) if (bang[row+i][col] == order)//kiem tra so o giong nhau lien tiep phia duoi o hien hanh s3++; else break; } for (i=1; i<=4; i++) { if (row-i >= 0) if (bang[row-i][col] == order)//kiem tra so o giong nhau lien tiep phia tren o hien hanh s4++; else break; } if((s3+s4)>=4)a=1;

/************************************/ /* kiem tra duong cheo tu trai sang phai theo chieu tu tren xuong */

for (i=1; i<=4; i++) {

if (col+i<max &&

row+i<max)

if (bang[row+i][col+i] == order)//kiem tra so o giong nhau lien tiep phia duoi ben phai o hien hanh s5++; else break; } for (i=1; i<=4; i++) { if ( col-i>=0&& row-i>=0) if (bang[row-i][col-i] == order)//kiem tra so o giong nhau lien tiep phia tren ben trai o hien hanh s6++; else break; } if((s5+s6)>=4)a=1;

/*************************************/ /* kiem tra duong cheo tu phai sang trai theo chieu tu tren xuong */

for (i=1; i<=4; i++) { if(row-i>=0&&col+i<max) if (bang[row-i][col+i] == order)//kiem tra so o giong nhau lien tiep phia tren ben phai o hien hanh s7++; else break; }

for (i=1; i<=4; i++) { if(row+i<max&&col-i>=0) if (bang[row+i][col-i] == order)//kiem tra so o giong nhau lien tiep phia duoi ben trai o hien hanh s8++; else break; } if((s7+s8)>=4)a=1;

/***************************************/ return a; }

void print_win() { switch(order) { case 0 :setcolor(blue); settextstyle(triplex_font,horiz_dir,2); outtextxy(xleft+max*height+60, 140, "blue won"); settextstyle(gothic_font,horiz_dir,4); outtextxy(xleft+max*height+25, 160, "congratulation!"); break; case 1 : setcolor(red); settextstyle(triplex_font,horiz_dir,2); outtextxy(xleft+max*height+60, 140, "red won"); settextstyle(gothic_font,horiz_dir,4);

outtextxy(xleft+max*height+25, 160, "congratulation!"); break; } setcolor(lightblue); settextstyle(default_font,horiz_dir,1); outtextxy(xleft+max*height+30, 200, "press f2 to continue!"); settextstyle(default_font,horiz_dir,1); outtextxy(xleft+max*height+30,210, "press f10 to quit!"); }

void play() { char key; int x, y, gameover = 0; unsigned char lbutton, rbutton = 0; int row, col; do{ setbkcolor(3); rectangle(0, 0, getmaxx(), getmaxy()); vebang(); reset_mouse();//khoi tao con tro chuot while (!gameover) { print_order(); while (lbutton == 1 || rbutton == 1) get_mouse_button(&lbutton, &rbutton, &x, &y); show_mouse(); do {

get_mouse_button(&lbutton, &rbutton, &x, &y); }while (lbutton == 0 && rbutton == 0); hide_mouse(); if(lbutton == 1) { get_row_col(x, y, &row, &col); if(bang[row][col]==-1 && row>=0 && col>=0 && row<max && col<max) { bang[row][col] = order;//danh dau o trong ban co bang gia tri dai dien order if (check(row, col))//dieu kien ham check(row,col) tra ve 1 { print_win(); gameover = 1; } switch(order) { case 0 :setcolor(blue); outtextxy(col*height+xleft+5, row*height+yleft+5, "o"); order = 1;/*gan cho order gia tri dai dien cua nguoi choi tiep

theo*/

break; case 1 :setcolor(red); outtextxy(col*height+xleft+5, row*height+yleft+5, "x"); order = 0;/*gan cho order gia tri dai dien cua nguoi choi tiep theo*/ break; } } }

} key=getch(); if(key==68)cont=0;/*an f10 de thoat*/ if(key==60)break;/*an f2 de tiep tuc choi*/ }while(key!=68); } void main() { int grdrive = detect, grmode,graphic_error; initgraph(&grdrive, &grmode, "c:\\borlandc\\bgi"); graphic_error=graphresult(); if(graphic_error!=grok) { cout<<"graphic error!"<

Related Documents

Caro
November 2019 15
Caro
November 2019 18
Caro Caro Ulisse
July 2020 45
Blog Caro
May 2020 7
Caro Sidwell
November 2019 12
Blog Caro
May 2020 11