#include<iostream>//#include<cstdlib>using namespace std;void main(){ char chess[3][3]={'1','2','3', '4','5','6', '7','8','9'}; int i,j; char input1,input2; cout<<"The chess board is as follows(We use numbers to denote the chess):"<<endl; for(i=0;i<3;i++) for(j=0;j<3;j++){ cout<<chess[i][j]<<" "; if((j+1)%3==0) cout<<endl; }//////////////////////////////////////////////////////// // start game: for(int k=0;k<5;k++){ // input the chess of player1: cout<<"input the number represent the position where player1 want to set the chess:"<<endl; cin>>input1; for(i=0;i<3;i++) for(j=0;j<3;j++){ if(input1==chess[i][j]) chess[i][j]='O'; } cout<<"Now the chess board is as follows(O denotes player1's chess):"<<endl; for(i=0;i<3;i++) for(j=0;j<3;j++){ cout<<chess[i][j]<<" "; if((j+1)%3==0) cout<<endl; }/////////////////////////////////////////////////////////////////////////// //judge if player1 wins: int r1=0,c1=0; if(k>=2){ //下了至少3个棋子才开始判断 while(r1<3){ if(chess[0][r1]=='O'&&chess[1][r1]=='O'&&chess[2][r1]=='O'){ cout<<"player1 wins!"<<endl; break; } else r1++; } while(c1<3){ // ???????????????????????????????????????????????????????????? if(chess[c1][0]=='O'&&chess[c1][1]=='O'&&chess[c1][2]=='O'){ cout<<"player1 wins!"<<endl; break; } else c1++; }//while(c1<3) if(chess[0][0]=='O'&&chess[1][1]=='O'&&chess[2][2]=='O'){ cout<<"player1 wins!"<<endl; break; } if(chess[0][2]=='O'&&chess[1][1]=='O'&&chess[2][0]=='O'){ cout<<"player1 wins!"<<endl; break; } r1=0; c1=0; } //if(k>=2) /////////////////////////////////////////////////////////////////// // input the chess of player2: cout<<"input the number represent the position where player2 want to set the chess:"<<endl; cin>>input2; for(i=0;i<3;i++) for(j=0;j<3;j++){ if(input2==chess[i][j]) chess[i][j]='X'; } cout<<"Now the chess board is as follows(X denotes player2's chess):"<<endl; for(i=0;i<3;i++) for(j=0;j<3;j++){ cout<<chess[i][j]<<" "; if((j+1)%3==0) cout<<endl; }/////////////////////////////////////////////////////////////////////////// //judge if player2 wins: int r2=0,c2=0; if(k>=2){ //下了至少3个棋子才开始判断 while(r2<3){ if(chess[0][r2]=='X'&&chess[1][r2]=='X'&&chess[2][r2]=='X'){ cout<<"player2 wins!"<<endl; break; } else r2++; } while(c2<3){ if(chess[c2][0]=='X'&&chess[c2][1]=='X'&&chess[c2][2]=='X'){ cout<<"player2 wins!"<<endl; break; } else c2++; }//while(c2<3) if(chess[0][0]=='X'&&chess[1][1]=='X'&&chess[2][2]=='X'){ cout<<"player2 wins!"<<endl; break; } if(chess[0][2]=='X'&&chess[1][1]=='X'&&chess[2][0]=='X'){ cout<<"player2 wins!"<<endl; break; } r1=0; c1=0; } //if(k>=2) } //for(int k=0;k<5;k++){}

评论