0% found this document useful (0 votes)
65 views12 pages

Hiện thực công việc (Code) : #include using namespace void char int

The document describes a C++ program that implements a game of Tic-Tac-Toe. The program allows two players to alternate turns placing X's and O's on a board until someone achieves n consecutive marks in a row, column or diagonal, where n is set by the players. It initializes the board based on player-defined size, tracks turns, checks for wins after each move, and allows players to continue playing another game or quit. The program runs as expected but has some limitations like not allowing mid-game quitting and lacking special test cases.

Uploaded by

Pham Thanh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views12 pages

Hiện thực công việc (Code) : #include using namespace void char int

The document describes a C++ program that implements a game of Tic-Tac-Toe. The program allows two players to alternate turns placing X's and O's on a board until someone achieves n consecutive marks in a row, column or diagonal, where n is set by the players. It initializes the board based on player-defined size, tracks turns, checks for wins after each move, and allows players to continue playing another game or quit. The program runs as expected but has some limitations like not allowing mid-game quitting and lacking special test cases.

Uploaded by

Pham Thanh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Hin thc cng vic (Code)

#include <iostream>
using namespace std;
void main() {
char caro[28][55];
int a = 0, b = 0, c = 0;
cout << "<Co Caro>" << endl << "1. Bat dau tro choi" << endl <<
"2. Huong dan" << endl << "3. Thoat" << endl;
do {
cout << "Ban lua chon: ";
cin >> a;
}
while (a!=1 && a!=2 && a!=3);
if (a == 2) {
cout <<endl << "<Luat choi>" << endl << "- Dau tien, ban
hay chon kich thuoc cua ban co va so nuoc lien tiep de thang." << endl
<<
"- Tiep theo, hai nguoi choi se lan luot chon toa do de dien
nuoc di O va X vao ban co." << endl << "- Neu nhap toa do da co nuoc di,
nguoi choi phai nhap lai toa do." << endl <<
"- Nguoi thang se la nguoi co lien tuc n quan tren hang doc,
ngang hoac cheo (so quan nay duoc ban chon truoc khi bat dau choi)." <<
endl << endl;
cout << "1. Bat dau choi" << endl << "2. Thoat" << endl;
do {
cout << "Ban muon: ";
cin >> b;
}
while (b!= 1 && b!=2);
}
loop1:
if (a==3 || b==2) {cout << "Da thoat tro choi" <<
endl;}
loop2: if (a==1 || b==1) {
system ("cls");
int ngang = 0, doc = 0, n = 0, i = 0, j = 0, d = 0, e = 0, f = 0,
g = 0, k = 0, l = 0, x = 0, y = 0, r = 1, dem = 0;
char p,q;
char z[10],t[10];
do {
cout << "Moi ban chon kich thuoc ngang <dau cach> doc cua
ban co(toi da 26 <dau cach> 26): ";
cin >> ngang >> doc;
}
while (ngang > 26 && doc > 26);
do {
cout << "Nhap so nuoc thang (tu 3 toi 5): ";
cin >> n;

}
while (n<3 || n>5);
cout << "Nhap ten nguoi choi 1: ";
cin >> z;
cout << "Nhap ten nguoi choi 2: ";
cin >> t;
for (i = 0; i <= doc+1; ++i) {
for (j = 0; j <= 2*ngang+2; ++j) {
if (i==0||i==doc+1) {
if(j==0||j==1||j==2*ngang+1||
j==2*ngang+2) {caro[i][j]='*';}
else {
if(j%2 == 1) {caro[i][j]='*';}
if(j%2 == 0) {caro[i][j]=(char)
('A'+j/2-1);}
}
}
else {
if (j==0||j==2*ngang+2) {caro[i]
[j]=(char)('A'+i-1);}
else {
if (j%2 == 0) {caro[i][j] = '_';}
else {caro[i][j] = ' ';}
}
}
}
}
system ("cls");
for (x=0;x<=doc+1;++x) {
for (y=0;y<=2*ngang+2;++y) {cout << caro[x][y];}
cout << endl;
}
for (dem=0;dem<=ngang*doc;++dem) {
if (dem%2 == 0) {
do {
cout << "Luot choi cua " << z << "
(hang cot): ";
cin >> p >> q;
k = p - 'A' + 1;
l = 2*(q-'A'+1);
}
while (caro[k][l] == 'X'||caro[k][l] == 'O');
caro[k][l] = 'O';
system("cls");
d = 0; e = 0; f = 0; g = 0;
for (x=0;x<=doc+1;++x) {
for (y=0;y<=2*ngang+2;++y) {cout
<< caro[x][y];}
cout << endl;

}
for (i=k-n+1;i<=k+n-2;i++){
if (caro[i][l] == caro[i+1][l] &&
caro[i+1][l] == 'O') {d = d+1;}
if (caro[i][l] != caro[i+1][l]) {d = 0;}
if (d == n-1) {break;}
}
for (i=l-2*n+2;i<=l+2*n-4;i++){
if (caro[k][i] == caro[k][i+2] &&
caro[k][i+2] == 'O') {e = e+1;}
if (caro[k][i] != caro[k][i+2]) {e = 0;}
if (e == n-1) {break;}
}
for (i=-n+1;i<=n-2;i++){
if (caro[k+i][l+2*i] == caro[k+i+1]
[l+2*i+2] && caro[k+i+1][l+2*i+2] == 'O') {f = f+1;}
if (caro[k+i][l+2*i] != caro[k+i+1]
[l+2*i+2]) {f = 0;}
if (caro[k-i][l+2*i] == caro[k-i-1]
[l+2*i+2] && caro[k-i-1][l+2*i+2] == 'O') {g = g+1;}
if (caro[k-i][l+2*i] != caro[k-i-1]
[l+2*i+2]) {g = 0;}
if (f == n-1 || g == n-1) {break;}
}
if (d == n-1 || e == n-1 || f == n-1 || g ==
n-1) {
cout << z << " thang" << endl;
break;
}
}
if (dem%2!=0) {
do {
cout << "Luot choi cua " << t << "
(hang cot): ";
cin >> p >> q;
k = p - 'A' + 1;
l = 2*(q-'A'+1);
}
while (caro[k][l] == 'O'||caro[k][l] == 'X');
caro[k][l] = 'X';
system("cls");
d = 0; e = 0; f = 0; g = 0;
for (x=0;x<=doc+1;++x) {
for (y=0;y<=2*ngang+2;++y) {cout
<< caro[x][y];}
cout << endl;
}
for (i=k-n+1;i<=k+n-2;i++){

if (caro[i][l] == caro[i+1][l] &&


caro[i+1][l] == 'X') {d = d+1;}
if (caro[i][l] != caro[i+1][l]) {d = 0;}
if (d == n-1) {break;}
}
for (i=l-2*n+2;i<=l+2*n-4;i++){
if (caro[k][i] == caro[k][i+2] &&
caro[k][i+2] == 'X') {e = e+1;}
if (caro[k][i] != caro[k][i+2]) {e = 0;}
if (e == n-1) {break;}
}
for (i=-n+1;i<=n-2;i++){
if (caro[k+i][l+2*i] == caro[k+i+1]
[l+2*i+2] && caro[k+i+1][l+2*i+2] == 'X') {f = f+1;}
if (caro[k+i][l+2*i] != caro[k+i+1]
[l+2*i+2]) {f = 0;}
if (caro[k-i][l+2*i] == caro[k-i-1]
[l+2*i+2] && caro[k-i-1][l+2*i+2] == 'X') {g = g+1;}
if (caro[k-i][l+2*i] != caro[k-i-1]
[l+2*i+2]) {g = 0;}
if (f == n-1 || g == n-1) {break;}
}
if (d == n-1 || e == n-1 || f == n-1 || g ==
n-1) {
cout << t << " thang" << endl;
break;
}
}
}
do {
cout << endl << "1. Choi tiep" << endl << "2. Thoat"
<< endl;
cout << "Ban muon: ";
cin >> c;
}
while (c!=1 && c!=2);
if (c==1) {
a = 1;
goto loop2;
}
if (c==2) {
a = 3;
goto loop1;
}
}
}

Kt qu lm vic

Cc testcase di y hin th lin tc ta theo di qu trnh chy


ca chng trnh
-

Bm Crt + F7 v F5 bt u chng trnh


Hin th cc s la chn 1 - 2 3

Chn 2

Chn 1

Tip tc nhp theo cc lnh yu cu hin th trn mn hnh chng trnh

Hin th

Tin hnh nhp vo v tr cn nh theo qui lut: Hng <du cch> Ct

Ln lt tng ngi chi thay phin nh vo v tr mong mun ti khi no


thng

Sau khi thng chng trnh hin th 2 Option la chn: Chi tip hay Thot.
Nu la chn Chi tip th mn hnh s hin th v khung nhp cc thng s
cho ln chi tip theo

Tng kt v nh gi
Bi lm:
Kt qu t c
- hin th c menu ngi chi la chn
- Chy ng cc mc
- Yu cu ngi chi nhp li khi nhp sai yu cu
- V c bn c ra mn hnh theo kch thc yu cu ca ngi chi nhp vo
( gii hn 26 hng 26 ct, s nc thng t 3 5 nc, tn ngi chi ti a
10 k t)
- Yu cu ngi chi nhp nc i v lp li cho c 2 ngi chi (nc nhp vo
phi vit hoa , qui lut nhp Hng trc Ct sau VD : Hng K ct L nhp
KL)
- Sau khi nhp nc i v hin th ra mn hnh.
- Chy ng 3 testcase bnh thng ng vi N=3,4,5.
- Sau khi chin thng, hi ngi chi tip tc chi thc hin vn khc hay l thot
chng trnh
Hn ch:
-

C 2 ngi chi cha thot game c khi ang chi


Cha to c 3 testcase c bit( ng vi n 2 kiu) ng vi N=3,4,5.

- Nhp ch thng s khng hin ra nc i mong mun


Ngi chi
Kt qu
t
t
t
t

Hunh Thanh Tm
Dng Tin Quang
Nguyn c Thin
Bi Hong Sn

TI LIU THAM KHO


[1] Paul Deitel Harvey Deitel, C++ - How to program Eight Edition, NXB
Prentice Hall.
[2] D.S.Malik, C++ Programming: From Problem Analysis to Program
Design, NXB Cengate Learning.
[3] V Vn Vin, T hc lp trnh vi ngn ng C.
[4] GS Phm Vn t, Gio trnh k thut lp trnh C, NXB Hng c.

You might also like