当前位置:编程学习 > C/C++ >>

C++编程问题。达人指点下

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const char X='X';
const char O='O';
const char EMPTY=' ';
const char TIE='T';
const char NO_ONE='N';
void instructions();
char askYseNo(string question);
int askNumber(string question,int high,int low=0);
char humanPiece();//确定人类的棋子
char opponent(char piece);//计算对方的棋子
void displayBoard(const vector<char>& board);//显示棋盘
char winner(const vector<char>& board);
bool isLegal(const vector<char>& board,int move);//检测移动合法
int humanMove(const vector<char>&,char human);
int computerMove(vector<char>board,char computer);
void announceWinner(char winner,char computer,char human);
int main()
{
 int move;
 const int NUM_SQUARES=9;//常量用大写字母表示
 vector<char> board(NUM_SQUARES,EMPTY);//9大小的矢量,为空。
 
 instructions();
 char human=humanPiece();
 char computer=opponent(human);
 char turn=X;
 displayBoard(board);
 while (winner(board)==NO_ONE)
 {
  if (turn==human)
  {
   move = humanMove(board,human);
   board[move]=human;
  }
  else
  {
   move=computerMove(board,computer);
   board[move]=computer;
  }
  displayBoard(board);
  turn=opponent(turn);
 }
 announceWinner(winner(board),computer,human);
 return 0;
}
void instructions()
{
 cout<<"Welcome to the ultimate man-machine showdown:Tic-Tac-toe.\n";
 cout<<"--where human brain is pit against silicon processor\n\n";
 cout<<"Make your move known by entering a number,0-8,The number\n";
 cout<<"corresponds to the desired board position,as illustrated:\n\n";
 cout<<"Prepare yourself,human. The battle is about to begin.\n\n";

}
char askYesNo(string question)
{
 char response;
 do
 {
  cout<<question<<"(y/n):";
  cin>>response;
 }while (response!='y'&&response!='n');
 return response;
}
int askNumber(string question,int high,int low)//输入一个范围
{
 int number;
 do
 {
  cout<<question<<"("<<low<<"-"<<high<<"):";
  cin>>number;
 }while (number>high||number<low);
 return number;
}
char humanPiece()//询问玩家是否先走
{
 char go_first = askYesNo("No you require the first move?");
 if(go_first=='y')
 {
  cout<<"\nThen take the first move. You will need it.\n";
  return X;
 }
 else
 {
  cout<<"\nYour bravery will be your undoing... I will go first.\n";
  return 0;
 }
//获得棋子并返回对手的棋子
char opponent(char piece)
{
 if(piece==X)
  return O;
 else
  return X;
}
void displayBoard(const vector<char>& board)
{
 cout<<"\n\t"<<board[0]<<" | "<<board[1]<<" | "<<board[2];
 cout<<"\n\t"<<"---------";
 cout<<"\n\t"<<board[3]<<" | "<<board[4]<<" | "<<board[5];
 cout<<"\n\t"<<"---------";
 cout<<"\n\t"<<board[6]<<" | "<<board[7]<<" | "<<board[8];
 cout<<"\n\n";
}
char winner(const vector<char>& board)
{
 const int WINNING_ROWS[8][3]={{0,1,2},
 {3,4,5},
 {6,7,8},
 {0,3,6},
 {1,4,7},
 {2,5,8},
 {0,4,8},
 {2,4,6},};//获胜的情况
 const int TOTAL_ROWS=8;
 for(int row=0;row<TOTAL_ROWS;++row)
 {
  if ((board[WINNING_ROWS[row][0] !=EMPTY)&&
   (board[WINNING_ROWS[row][0] ==board[WINNING_ROWS[row][1]])&&
   (board[WINNING_ROWS[row][1]]==board[WINNING_ROWS[row][2]]))
  {
   return board[WINNING_ROWS[row][0]];
  }
 }
 if (count(board.begin(),board.end(),EMPTY)==0)
  return TIE;
 return NO_ONE;
}
inline bool isLegal(int move, const vector<char>& board)
{
 return (board[move]==EMPTY);
}
int humanMove(const vector<char>&board,char human)
{
 int move = askNumber("Where will you move?",(board.size()-1));
 while (!isLegal(move,board))
 {
  cout<<"\nThat square is already occupied,foolish human.\n";
  move = askNumber("where will you move?",(board.size()-1));
 }
 cout<<"Fine...\n";
 return move;
}
int computerMove(vector<char> board,char computer)//AI程序
{
 cout<<"I shall take square number ";
 for(int=0;move<board.size();++move)
 {
  if (isLegal(move,board))
  {
   board[move]=computer;
   if (winner(board)==computer)
   {
    cout<<move<<endl;
    return move;
   }
   board[move]=EMPTY;//检测这次移动,撤销它
  }
 }
 char human=opponent(computer);//人类获胜,阻止它
 for(int move=0;move <board.size();++move)
 {
  if(isLegal(move,board))
  {
   board[move]=human;
   if (winner(board)==human)
   {
    cout<<move<<endl;
    return move;
   }
  }
 }
 const int BEST_MOVE[]={4,0,2,6,8,1,3,5,7};
 for(int i=0;i<board.size();++i)
 {
  int move = BEST_MOVES[i];
  if (isLegal(move,board))
  {
   cout<<move<<endl;
   return move;
  }
 }
}
announceWinner()
void announceWinner(char winner,char computer,char human)
{
 if(winner==computer)
 {
  cout<<winer<<"'s won!\n";
  cout<<"As I predicted,human,I am triumphant once more --proof\n";
  cout<<"that computer are superior to humans in all regards.\n";
 }
 else if (winner==human)
 {
  cout<<winner<<"'s won!\n";
  cout<<"No,no! It cannot be! Somehow you tricked me, human.\n";
  cout<<"But never again! I,the computer,so swear it!\n";
 }
 else
 {
  cout<<"It's a tie.\n";
  cout<<"You were most lucky,human,and somehow managed to tie me.\n";
  cout<<"Celebrate...for this is the best you will ever achieve.\n";
 }
}

指点下错误的地方,小弟我才学。不太明白。谢谢了。我在线等

答案:修改完毕,以后细心点,代码修改如下,带错误注释:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const char X='X';
const char O='O';
const char EMPTY=' ';
const char TIE='T';
const char NO_ONE='N';
void instructions();
char askYseNo(string question);
int askNumber(string question,int high,int low=0);
char humanPiece();//确定人类的棋子
char opponent(char piece);//计算对方的棋子
void displayBoard(const vector<char>& board);//显示棋盘
char winner(const vector<char>& board);
bool isLegal(const vector<char>& board,int move);//检测移动合法
int humanMove(const vector<char>&,char human);
int computerMove(vector<char>board,char computer);
void announceWinner(char winner,char computer,char human);
int main()
{
 int move;
 const int NUM_SQUARES=9;//常量用大写字母表示
 vector<char> board(NUM_SQUARES,EMPTY);//9大小的矢量,为空。
 
 instructions();
 char human=humanPiece();
 char computer=opponent(human);
 char turn=X;
 displayBoard(board);
 while (winner(board)==NO_ONE)
 {
  if (turn==human)
  {
   move = humanMove(board,human);
   board[move]=human;
  }
  else
  {
   move=computerMove(board,computer);
   board[move]=computer;
  }
  displayBoard(board);
  turn=opponent(turn);
 }
 announceWinner(winner(board),computer,human);
 return 0;
}
void instructions()
{
 cout<<"Welcome to the ultimate man-machine showdown:Tic-Tac-toe.\n";
 cout<<"--where human brain is pit against silicon processor\n\n";
 cout<<"Make your move known by entering a number,0-8,The number\n";
 cout<<"corresponds to the desired board position,as illustrated:\n\n";
 cout<<"Prepare yourself,human. The battle is about to begin.\n\n";
 
}
char askYesNo(string question)
{
 char response;
 do
 {
  cout<<question<<"(y/n):";
  cin>>response;
 }while (response!='y'&&response!='n');
 return response;
}
int askNumber(string question,int high,int low)//输入一个范围
{
 int number;
 do
 {
  cout<<question<<"("<<low<<"-"<<high<<"):";
  cin>>number;
 }while (number>high||number<low);
 return number;
}
char humanPiece()//询问玩家是否先走
{
 char go_first = askYesNo("No you require the first move?");
 if(go_first=='y')
 {
  cout<<"\nThen take the first move. You will need it.\n";
  return X;
 }
 else
 {
  cout<<"\nYour bravery will be your undoing... I will go first.\n";
  return 0;
 }
}//少了个}
//获得棋子并返回对手的棋子
char opponent(char piece)
{
 if(piece==X)
  return O;
 else
  return X;
}
void displayBoard(const vector<char>& board)
{
 cout<<"\n\t"<<board[0]<<" | "<<board[1]<<" | "<<board[2];
 cout<<"\n\t"<<"---------";
 cout<<"\n\t"<<board[3]<<" | "<<board[4]<<" | "<<board[5];
 cout<<"\n\t"<<"---------";
 cout<<"\n\t"<<board[6]<<" | "<<board[7]<<" | "<<board[8];
 cout<<"\n\n";
}
char winner(const vector<char>& board)
{
 const int WINNING_ROWS[8][3]={{0,1,2},
 {3,4,5},
 {6,7,8},
 {0,3,6},
 {1,4,7},
 {2,5,8},
 {0,4,8},
 {2,4,6},};//获胜的情况
 const int TOTAL_ROWS=8;
 for(int row=0;row<TOTAL_ROWS;++row)
 {
  if ((board[WINNING_ROWS[row][0]] !=EMPTY)&&//少了]
   (board[WINNING_ROWS[row][0]]/*少了]*/ ==board[WINNING_ROWS[row][1]])&&
   (board[WINNING_ROWS[row][1]]==board[WINNING_ROWS[row][2]]))
  {
   return board[WINNING_ROWS[row][0]];
  }
 }
 if (count(board.begin(),board.end(),EMPTY)==0)
  return TIE;
 return NO_ONE;
}
inline bool isLegal(int move, const vector<char>& board)
{
 return (board[move]==EMPTY);
}
int humanMove(const vector<char>&board,char human)
{
 int move = askNumber("Where will you move?",(board.size()-1));
 while (!isLegal(move,board))
 {
  cout<<"\nThat square is already occupied,foolish human.\n";
  move = askNumber("where will you move?",(board.size()-1));
 }
 cout<<"Fine...\n";
 return move;
}
int computerMove(vector<char> board,char computer)//AI程序
{
 cout<<"I shall take square number ";
 for(int move /*少了move*/=0;move<board.size();++move)
 {
  if (isLegal(move,board))
  {
   board[move]=computer;
   if (winner(board)==computer)
   {
    cout<<move<<endl;
    return move;
   }
   board[move]=EMPTY;//检测这次移动,撤销它
  }
 }
 char human=opponent(computer);//人类获胜,阻止它
 for(/*去掉int*/move=0;move <board.size();++move)
 {
  if(isLegal(move,board))
  {
   board[move]=human;
   if (winner(board)==human)
   {
    cout<<move<<endl;
    return move;
   }
  }
 }
 const int BEST_MOVE[]={4,0,2,6,8,1,3,5,7};
 for(int i=0;i<board.size();++i)
 {
  int move = BEST_MOVE[i];//BEST_MOVE输入错误。多了个s
  if (isLegal(move,board))
  {
   cout<<move<<endl;
   return move;
  }
 }
}
//announceWinner()
void announceWinner(char winner,char computer,char human)
{
 if(winner==computer)
 {
  cout<<winner/*少了n,输入错误*/<<"'s won!\n";
  cout<<"As I predicted,human,I am triumphant once more --proof\n";
  cout<<"that computer are superior to humans in all regards.\n";
 }
 else if (winner==human)
 {
  cout<<winner<<"'s won!\n";
  cout<<"No,no! It cannot be! Somehow you tricked me, human.\n";
  cout<<"But never again! I,the computer,so swear it!\n";
 }
 else
 {
  cout<<"It's a tie.\n";
  cout<<"You were most lucky,human,and somehow managed to tie me.\n";
  cout<<"Celebrate...for this is the best you will ever achieve.\n";
 }
}


------------------c++部落------------Hello Word!--------
无论你是初学者还是专家,只要你热爱编程、交流、分享,欢迎加入~~

上一个:C++程序设计难学吗
下一个:这道C++程序哪错了?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,