首页 > 代码库 > 随机数
随机数
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include<ctime>
using namespace std;
class player{
public:
char manplay(int a);
char computerplay(int a);
int a;
char s[3][3];
};
int main()
{
int a,b,c,d,e,f,g,h,i;
player man,computer;
srand(time(0));
cin>>a;
man.manplay(a);
b=rand()%9+1;
while(b==a){
b=rand()%9+1;
}
cout<<b<<endl;
computer.computerplay(b);
cin>>c;
man.manplay(c);
d=rand()%9+1;
while(d==a||d==b||d==c){
d=rand()%9+1;
}
cout<<d<<endl;
computer.computerplay(d);
cin>>e;
man.manplay(e);
f=rand()%9+1;
while(f==a||f==b||f==c||f==d||f==e){
f=rand()%9+1;
}
cout<<f<<endl;
computer.computerplay(f);
cin>>g;
man.manplay(g);
h=rand()%9+1;
while(h==a||h==b||h==c||h==d||h==e||h==f||h==g)
{
h=rand()%9+1;
}
cout<<h<<endl;
computer.computerplay(h);
cin>>i;
man.manplay(i);
}
char player::manplay(int a){
s[a/3-1][a%3-1]=‘o‘;
cout<<s[a/3-1][a%3-1]<<endl;
}
char player::computerplay(int a){
s[a/3-1][a%3-1]=‘x‘;
cout<<s[a/3-1][a%3-1]<<endl;
}
随机数