1 #include <iostream> 2 #include <iomanip> 3 #include <cstdlib> 4 #include<ctime> 5 using namespace std; 6 class player{ 7 public: 8 char manplay(int a); 9 char computerplay(int a); 10 int a; 11 char s[3][3]; 12 }; 13 int main() 14 { 15 int a,b,c,d,e,f,g,h,i; 16 player man,computer; 17 srand(time(0)); 18 19 cin>>a; 20 man.manplay(a); 21 22 b=rand()%9+1; 23 while(b==a){ 24 b=rand()%9+1; 25 } 26 cout<<b<<endl; 27 computer.computerplay(b); 28 29 cin>>c; 30 man.manplay(c); 31 32 d=rand()%9+1; 33 while(d==a||d==b||d==c){ 34 d=rand()%9+1; 35 } 36 cout<<d<<endl; 37 computer.computerplay(d); 38 39 cin>>e; 40 man.manplay(e); 41 42 f=rand()%9+1; 43 while(f==a||f==b||f==c||f==d||f==e){ 44 f=rand()%9+1; 45 } 46 cout<<f<<endl; 47 computer.computerplay(f); 48 49 cin>>g; 50 man.manplay(g); 51 52 h=rand()%9+1; 53 while(h==a||h==b||h==c||h==d||h==e||h==f||h==g) 54 { 55 h=rand()%9+1; 56 } 57 cout<<h<<endl; 58 computer.computerplay(h); 59 60 61 cin>>i; 62 man.manplay(i); 63 64 65 66 67 68 69 70 } 71 char player::manplay(int a){ 72 s[a/3-1][a%3-1]='o'; 73 cout<<s[a/3-1][a%3-1]<<endl; 74 } 75 char player::computerplay(int a){ 76 s[a/3-1][a%3-1]='x'; 77 cout<<s[a/3-1][a%3-1]<<endl; 78 }
本知识属于个人知识成果,如需转载,请邮件请求