还有,我正在学VC++,我知道这是面向对象的编程语言。但是里面的“类”我实在是学得一头雾水,根本主摸不清一点头绪———我很茫然,不知道怎么学。。。
我以前学过VFP,对编程还是有一点小了解。。只是“类”这个概念实在让我费解。。
按你的要求与想法 ,我昨晚抽时间给你做了一个,但是此做法很多不足
你可以再考虑优化以下,比如说电脑随机的产生位置,如果N*N个位置中
已经有很少的位置的话那么随机产生可能一直得不到正确的位置,还有你
可以重新设置该游戏的UI,使难度增大。。
////////////////////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
using namespace std;
const int N = 10;
const int M = 3;
void display(int map[N][N]);
void initialize(int map[N][N]);
bool process(int map[N][N]);
int YouInput(int map[N][N]);
bool CheckPos(int map[N][N],int pos);
int ComputerInput(int map[N][N]);
bool CheckWin(int map[N][N],int pos);
int main()
{
int map[N][N];
initialize(map);
display(map);
bool IsWin = process(map);
if ( IsWin ) cout<<"You win!"<<endl;
else cout<<"You lose!"<<endl;
return 0;
}
void initialize(int map[N][N])
{
int index = 0,j;
for(int i = 0 ; i < N ; i++)
for(j = 0 ; j < N ; j++)
map[i][j] = ++index;
}
void display(int map[N][N])
{
for(int i = 0 ; i < N ; i++)
{
for(int j = 0 ; j < N ; j++)
cout<<setw(4)<<map[i][j];
cout<<endl;
}
}
bool process(int map[N][N])
{
bool win = false;
bool lose = false;
int YourPos,ComputerPos;
do
{
YourPos = YouInput(map);
//display again
display(map);
//check if you win
win = CheckWin(map,YourPos);
//computer will set the value of a position is -1 randomly
if( !win )
{
ComputerPos = ComputerInput(map);
display(map);
//check if computer win
lose = CheckWin(map,ComputerPos);
}
}while (!win && !lose);
if ( win ) return true;
else return false;
}
//check if the pos is right
bool CheckPos(int map[N][N],int pos)
{
if ( pos > N*N || pos < 1 ) return false;
if ( 0 == map[(pos-1)/N][(pos-1)%N] ||
(-1 == map[(pos-1)/N][(pos-1)%N] ) ) return false;
return true;
}
int YouInput(int map[N][N])
{
int pos ;
cout<<"input your position: ";
cin>>pos;
while ( !CheckPos(map,pos) )
{
cout<<"the position is not right! " ;
cout<<"input your position: ";
cin>>pos;
}
//make the value of the postion is 0
map[(pos-1)/N][(pos-1)%N] = 0;
return pos;
}
//compter create the postions randomly
int ComputerInput(int map[N][N])
{
int pos;
pos = 1 + rand()%(N*N);
while ( !CheckPos(map,pos) ) pos = 1 + rand()%(N*N);
//make the value of the postion is -1
map[(pos-1)/N][(pos-1)%N] = -1;
cout<<"computer's last position is : "<<pos<<endl;
return pos;
}
bool CheckWin(int map[N][N],int pos)
{
int i = (pos-1)/10;
int j = (pos-1)%10;
if ( i >= M-1 )
if( map[i][j] == map[i-1][j] && map[i][j] == map[i-2][j] )
return true;
if ( i >= M-1 && j >= M-1 )
if( map[i][j] == map[i-1][j-1] && map[i][j] == map[i-2][j-2] )
return true;
if ( j >= M-1 )
if( map[i][j] == map[i][j-1] && map[i][j] == map[i][j-2] )
return true;
if ( i >= M-1 && j <= N-M )
if( map[i][j] == map[i-1][j+1] && map[i][j] == map[i-2][j+2] )
return true;
if ( i <= N-M )
if( map[i][j] == map[i+1][j] && map[i][j] == map[i+2][j] )
return true;
if ( i <= M-1 && j <= M-1 )
if( map[i][j] == map[i+1][j+1] && map[i][j] == map[i+2][j+2] )
return true;
if ( j <= N-M )
if( map[i][j] == map[i][j+1] && map[i][j] == map[i][j+2] )
return true;
if ( i <= N-M && j >= M-1 )
if( map[i][j] == map[i+1][j-1] && map[i][j] == map[i+2][j-2] )
return true;
if ( i >= 1 && i < N-1 )
if( map[i][j] == map[i-1][j] && map[i][j] == map[i+1][j] )
return true;
if ( j >= 1 && j < N-1 )
if( map[i][j] == map[i][j-1] && map[i][j] == map[i][j+1] )
return true;
if ( i >= 1 && i < N-1 && j >= 1 && j < N-1 )
if( (map[i][j] == map[i-1][j-1] && map[i][j] == map[i+1][j+1]) ||
(map[i][j] == map[i-1][j+1] && map[i][j] == map[i+1][j-1]) )
return true;
return false;
}