- 论坛徽章:
- 0
|
五子棋源代码, 拿出来献丑了. 想请教两个个问题
1. 如何写出判断一步下完之后,是否赢棋. 我感觉我用的算法的时间复杂度比较高,请教高手能给出更优的算法来. 我的算法如下:
(1) 用int win(char) 函数来判断是否赢棋. 输入参数是一个字符: O 或者 X 分别代表下棋双方所持的棋. 因为n*n棋盘的由一个二维数组定义, 从头到尾搜索每个方格如果为'O'或者'X'. 就进行搜索看有没有连成一条线的五个棋子
1 2 3
* * *
4 * o * 5
* * *
6 7 8
如上图所示 o 代表当前位置.
(2)向左斜上和右斜下搜索,递归调用函数move_1(),计算左斜上与o一致的方格数.递归调用函数move_8()计算右斜下与o一致的方格数.两者相加,如果等于5个方格,则5字连成一线.同理计算, move_2()+move_7(), move_4()+move_5()和move_3()+move_6().如果都没有5字,这进入下一个方格,再进行如上的搜索算法,直到找到5字成一线的情况,或者搜索到棋盘最后一个方格.
2.这个程序是在最简单的wondows console application工程,所以每一步下棋用通过输入横坐标和纵坐标来确定下棋的位置.请问应该如何在这个wondows console application环境下调用函数让屏幕接受鼠标点击的消息,来通过鼠标点击确定下棋的位置.
// ------------------------------------ //
// File Name: b1.cpp //
// Function: Tick-Tac-Toe game //
// Author: Xiaomo Liu //
// Create date: 2005.06.22 //
// Last update: 2005.06.23 //
// ------------------------------------ //
#include <iostream.h>;
#include <string.h>;
const int n = 9; // 定义棋盘大小
const int m = 5; // 定义五字棋模式
int pl_count = 0;
class chessboard
{
public:
int count;
int pl_count;
char board[n][n];
chessboard();
~chessboard() {};
void initialize();
void display();
void input_tacky();
bool input_you();
bool input_me();
int win(char);
void move_1(int, int);
void move_2(int, int);
void move_3(int, int);
void move_4(int, int);
void move_5(int, int);
void move_6(int, int);
void move_7(int, int);
void move_8(int, int);
};
/////////////////////////////////////////////////////
// //
// Function name: chessboard::chessboard() //
// Function purpose: constructor function of the //
// chessboard class //
// //
/////////////////////////////////////////////////////
chessboard::chessboard()
{
count = 0;
pl_count =0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
board[j]= ' ';
}
}
}
////////// End of chessboard::chessboard() ////////
////////////////////////////////////////////////////
// //
// Function name: chessboard::initialize() //
// Function purpose: initialize the board, i.e //
// clear the board //
// //
////////////////////////////////////////////////////
void chessboard::initialize()
{
count = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
board[j]= ' ';
}
}
cout<<"Initilization finished!"<<endl;
cout<<endl<<endl;
}
//////////// End of chessboard::initialize() /////////////
////////////////////////////////////////////////
// //
// Function name: chessboard::display() //
// Function purpose: display the board //
// //
////////////////////////////////////////////////
void chessboard::display()
{
cout<<"At move "<<count<<"!"<<endl;
cout<<"Note: This is how the board is like: \n"<<endl;
cout<<"\t"<<" ";
for(int i = 0; i < n; i++)
{
if (i+1 < 10)
{
cout<<" "<<i+1<<" ";
}
else
{
cout<<" "<<i+1;
}
}
cout<<endl;
for(i = 0; i < n; i++)
{
cout<<"\t "<<" ";
for(int j = 0; j < n; j++)
{
cout<<"|---";
}
cout<<"|"<<endl;
if(i+1 < 10)
{
cout<<"\t "<<i+1<<" ";
}
else
{
cout<<"\t"<<i+1<<" ";
}
for(j = 0; j < n ; j++)
{
cout<<"| "<<board[j]<<" ";
}
cout<<"|"<<endl;
}
cout<<"\t "<<" ";
for(i = 0; i < n; i++)
{
cout<<"|---";
}
cout<<"|";
cout<<endl<<endl;
}
/////////// End of chessboard::display() /////////////
////////////////////////////////////////////////////
// //
// Function name: chessboard::input_me() //
// Function purpose: input a move on the board //
// by you //
// //
////////////////////////////////////////////////////
bool chessboard::input_me()
{
int row = 0;
int line = 0;
char c_row[2];
char c_line[2];
cout<<" lease input your move"<<endl;
cout<<" lease input your row"<<" ";
cin>;>;c_row;
cout<<endl;
cout<<" lease input your line"<<" ";
cin>;>;c_line;
cout<<endl;
row = c_row[0] + c_row[1] * 10;
row = row - 49;
line = c_line[0] + c_line[1] * 10;
line = line - 49;
if( (row < 0 || row >; n) || (line < 0 || line >; n) )
{
cout<<"Error! The input move of the board doesn't exit"<<endl;
cout<<"Input another move"<<endl;
cout<<endl;
return false;
//input_me();
}
else
{
if( board[row][line] == 'O'
|| board[row][line] == 'X' )
{
cout<<"Error! The input move of the board is already occupied"<<endl;
cout<<"Input another move"<<endl;
cout<<endl;
return false;
//input_me();
}
else if( board[row][line] == ' ' )
{
board[row][line] = 'X';
count++;
}
return true;
}
}
///////////// End of chessboard::input_me() //////////////
////////////////////////////////////////////////////
// //
// Function name: chessboard::input_you() //
// Function purpose: input a move on the board //
// by you //
// //
////////////////////////////////////////////////////
bool chessboard::input_you()
{
int row = 0;
int line = 0;
char c_row[2];
char c_line[2];
cout<<" lease input your move"<<endl;
cout<<" lease input your row"<<" ";
cin>;>;c_row;
cout<<endl;
cout<<" lease input your line"<<" ";
cin>;>;c_line;
cout<<endl;
row = c_row[0] + c_row[1] * 10;
row = row - 49;
line = c_line[0] + c_line[1] * 10;
line = line - 49;
if( (row < 0 || row >; n) || (line < 0 || line >; n) )
{
cout<<"Error! The input move of the board doesn't exit"<<endl;
cout<<"Input another move"<<endl;
cout<<endl;
return false;
}
else
{
if( board[row][line] == 'O'
|| board[row][line] == 'X' )
{
cout<<"Error! The input move of the board is already occupied"<<endl;
cout<<"Input another move"<<endl;
cout<<endl;
return false;
//input_you();
}
else if( board[row][line] == ' ' )
{
board[row][line] = 'O';
count++;
}
return true;
}
}
////////////// End of chessboard::input_you() //////////////
///////////////////////////////////////////////////////////
// //
// Function name: chessboard::win() //
// Function purpose: decide whether one wins the game //
// //
///////////////////////////////////////////////////////////
int chessboard::win(char type)
{
pl_count = 0;
for(int i = 0; i < n ; i++)
{
for(int j = 0; j < n; j++)
{
if (board[j] == type)
{
move_1(i, j);
move_8(i, j);
if (pl_count == (m-1)) break;
else pl_count = 0;
move_3(i, j);
move_6(i, j);
if (pl_count == (m-1)) break;
else pl_count = 0;
move_2(i, j);
move_7(i, j);
if (pl_count == (m-1)) break;
else pl_count = 0;
move_4(i, j);
move_5(i, j);
if (pl_count == (m-1)) break;
else pl_count = 0;
}
}
if (pl_count == (m-1)) break;
}
if (pl_count == (m-1)) return 1;
else return 0;
}
/////////////////// End of chessboard::win() ////////////////////
///////////////////////////////////////////////////////////
// //
// Function name: chessboard::move_?() //
// Function purpose: decide whether one wins the game //
// //
///////////////////////////////////////////////////////////
void chessboard::move_1(int i , int j)
{
if (i-1 < 0 || j-1 < 0)
{
}
else if (board[j] == board[i-1][j-1])
{
pl_count++;
move_1(i-1,j-1);
}
}
void chessboard::move_2(int i , int j)
{
if (i-1 < 0 )
{
}
else if (board[j] == board[i-1][j])
{
pl_count++;
move_2(i-1,j);
}
}
void chessboard::move_3(int i , int j)
{
if (i-1 < 0 || j+1 >; m-1 )
{
}
else if (board[j] == board[i-1][j+1])
{
pl_count++;
move_3(i-1,j+1);
}
}
void chessboard::move_4(int i , int j)
{
if ( j-1 < 0 )
{
}
else if (board[j] == board[j-1])
{
pl_count++;
move_4(i,j-1);
}
}
void chessboard::move_5(int i , int j)
{
if ( j+1 >; m-1 )
{
}
else if (board[j] == board[j+1])
{
pl_count++;
move_5(i,j+1);
}
}
void chessboard::move_6(int i , int j)
{
if (i+1 >; m-1 || j-1 < 0)
{
}
else if (board[j] == board[i+1][j-1])
{
pl_count++;
move_6(i+1,j-1);
}
}
void chessboard::move_7(int i , int j)
{
if (i+1 >; m-1)
{
}
else if (board[j] == board[i+1][j])
{
pl_count++;
move_7(i+1,j);
}
}
void chessboard::move_8(int i , int j)
{
if (i+1 >; m-1 || j+1 >; m-1 )
{
}
else if (board[j] == board[i+1][j+1])
{
pl_count++;
move_8(i+1,j+1);
}
}
///////////// End of chessboard::move_?() ///////////////
////////////////////////////////////////
// //
// Function name: main() //
// Function purpose: main function //
// //
////////////////////////////////////////
void main()
{
chessboard b1;
cout<<"Weclome to Tacky game"<<endl;
cout<<"Tacky is a Tic-Tac-Toe Robot and your aim is to beat the Tacky!"<<endl;
cout<<"Can you do it? \n"<<endl;
cout<<"Note: This is how the board is like: \n"<<endl;
b1.initialize();
b1.display();
cout<<"For a move, you enter something like a1, or c3 etc,depending on what box you want to move."<<endl;
cout<<"Remeber! you are 'O' and Tacky is 'X'"<<endl;
cout<<"Good luck!"<<endl;
cout<<endl<<endl;
int win = 0;
while(win == 0)
{
if (b1.count == (n * n))
{
cout<<"The tie!"<<endl;
break;
}
while(!b1.input_you());
b1.display();
win = b1.win('O');
if(win == 1)
{
cout<<"you win the game"<<endl;
break;
}
if (b1.count == (n*n))
{
cout<<"The tie!"<<endl;
break;
}
while(!b1.input_me());
b1.display();
win = b1.win('X');
if (win == 1)
{
cout<<"I win the game"<<endl;
break;
}
}
cout<<"End of the game"<<endl;
cout<<"The final result is as follows:"<<endl;
cout<<endl;
b1.display();
}
////////// end of main() /////////////
// --------------- end of b1.cpp -------------- // |
|