正文

极其脑残的XO游戏程序2009-09-29 15:16:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/shao/48663.html

分享到:

 

#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++){
}

阅读(1269) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册