Chào anh chị =-=’
Em là mem mới, hy vọng sẽ được các anh chị giúp đỡ.
Tình hình là em mới tạo được trò cờ Caro (bản 3x3)
cơ mà em thấy phần code kiểm tra người thắng nó hơi dài và không được khả thi khi mở rộng bàn cờ ra cho lắm
Mong mọi người góp ý =-=’
#include<iostream>
#include<stdio.h>
#include <conio.h>
int winner(void);
bool blank(void);
char a[3][3]={'*','*','*','*','*','*','*','*','*'};
using namespace std;
int main(){
system("cls");
int i,j;
cout<<" GAME CO CARO CHO 2 NGUOI\n";
// in ban co 3x3
cout<<" __1__ __2__ __3__ ";
cout<<"\n";
for(i=0;i<6;i++){
for(j=0;j<3;j++){
if(i%2==0) cout<<" | |";
else
if(j==0) cout<<i/2+1<<" |__"<<a[i/2][j]<<"__| ";
else cout<<" |__"<<a[i/2][j]<<"__| ";
}
cout<<"\n";
}
bool end=false;
int k=1; //Luot choi cua X va Y
while(end==false){
int x,y;
cout<<"Nhap toa do:\n";
cout<<"x= "; cin>>x;
cout<<"y= "; cin>>y;
while((x>3||x<1)||(y>3||y<1)||a[y-1][x-1]!='*'){
cout<<"Nhap lai toa do:\n";
cout<<"x= "; cin>>x;
cout<<"y= "; cin>>y;
}
system("cls");
if(k%2==0) a[y-1][x-1]='O';
else a[y-1][x-1]='X';
k++;
cout<<" __1__ __2__ __3__ ";
cout<<"\n";
for(i=0;i<6;i++){
for(j=0;j<3;j++){
if(i%2==0) cout<<" | |";
else {
if(j==0) cout<<i/2+1<<" |__"<<a[i/2][j]<<"__| ";
else cout<<" |__"<<a[i/2][j]<<"__| ";
}
}
cout<<"\n";
}
int c=winner();
if(c==0){
cout<<"Hoa\n";
char check;
cout<<"Ban co muon choi lai? (c/k)";
cin>>check;
if(check=='k') end=true;
else{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]='*';
system("cls");
cout<<" __1__ __2__ __3__ ";
cout<<"\n";
for(i=0;i<6;i++){
for(j=0;j<3;j++){
if(i%2==0) cout<<" | |";
else
if(j==0) cout<<i/2+1<<" |__"<<a[i/2][j]<<"__| ";
else cout<<" |__"<<a[i/2][j]<<"__| ";
}
cout<<"\n";
}
}
}
if(c==1){
cout<<"X thang\n";
char check;
cout<<"Ban co muon choi lai? (c/k)";
cin>>check;
if(check=='k') end=true;
else{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]='*';
system("cls");
cout<<" __1__ __2__ __3__ ";
cout<<"\n";
for(i=0;i<6;i++){
for(j=0;j<3;j++){
if(i%2==0) cout<<" | |";
else
if(j==0) cout<<i/2+1<<" |__"<<a[i/2][j]<<"__| ";
else cout<<" |__"<<a[i/2][j]<<"__| ";
}
cout<<"\n";
}
}
}
if(c==2){
cout<<"Y thang\n";
char check;
cout<<"Ban co muon choi lai? (c/k)";
cin>>check;
if(check=='k') end=true;
else{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]='*';
system("cls");
cout<<" __1__ __2__ __3__ ";
cout<<"\n";
for(i=0;i<6;i++){
for(j=0;j<3;j++){
if(i%2==0) cout<<" | |";
else
if(j==0) cout<<i/2+1<<" |__"<<a[i/2][j]<<"__| ";
else cout<<" |__"<<a[i/2][j]<<"__| ";
}
cout<<"\n";
}
}
}
}
return 0;
}
//Ham check nguoi thang
int winner(void){
int i,j,s=-1;
for(i=0;i<=2;i++){
if(a[i][0]==a[i][1]&&a[i][1]==a[i][2]&&a[i][2]=='X') s=1;
if(a[i][0]==a[i][1]&&a[i][1]==a[i][2]&&a[i][2]=='O') s=2;
}
for(i=0;i<=2;i++){
if(a[0][i]==a[1][i]&&a[1][i]==a[2][i]&&a[2][i]=='X') s=1;
if(a[0][i]==a[1][i]&&a[1][i]==a[2][i]&&a[2][i]=='O') s=2;
}
if(a[0][0]==a[1][1]&&a[1][1]==a[2][2]&&a[2][2]=='X') s=1;
if(a[0][2]==a[1][1]&&a[1][1]==a[2][0]&&a[2][0]=='X') s=1;
if(a[0][0]==a[1][1]&&a[1][1]==a[2][2]&&a[2][2]=='O') s=2;
if(a[0][2]==a[1][1]&&a[1][1]==a[2][0]&&a[2][0]=='O') s=2;
if(blank()==false) s=0;
return s;
}
//Ham check o trong trong ban co
bool blank(void){
int i,j;
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
if(a[i][j]=='*') return true;
return false;
}