⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 五子棋.txt

📁 数据结构课程设计
💻 TXT
📖 第 1 页 / 共 2 页
字号:
#include "stdafx.h"

#include<iostream>
#include<iomanip>
#include<stack>  
using namespace std;

struct chessman{
    chessman();
    chessman(int x,int y,bool z){row=x;column=y;who=z;}
    int row;
    int column;
    bool who;
    chessman& operator= (const chessman& old_chess);
};    
chessman& chessman::operator=(const chessman& old_chess){
    this->row=old_chess.row;
    this->column=old_chess.column;
    this->who=old_chess.who;
    return *this;
}    

class Five_chess{
    public:
        Five_chess();
        void board_display();//显示棋盘 
        bool jiqi();
	bool Win(int row,int column,int c);//判断胜负 
        void draw();//和棋 
        void again();//重下一盘 
        void regret();//悔棋 
        void display_winner();//显示哪方获胜 
        void menu();//菜单 
        bool putin(int heng , int shu , int ren);
        bool putin(int row,int column);//往棋盘放入一个子 
        void giveup();
        int who_now(){ return (count%2+1);}//1代表黑,2代表白
        int get_chess(int row,int column){return board[row][column];}//取得某坐标的状态 
    private:
        bool ptab[16][16][572];//玩家获胜表
        bool ctab[16][16][572];//电脑获胜表
        int win1[2][572];
        int i,j,k;
        int count;
        int grades[2][16][16];
        void start();//欢迎界面 
        void init();
        stack<chessman> black;
        stack<chessman> white;
        int board[17][17];//1代表黑,2代表白,0代表空 
};

//棋盘显示函数,要把整个棋盘显示出来 
void Five_chess::board_display(){
    cout<<endl;
    cout<<"      ";
    for( i=1;i<=15;i++) 
    cout<<setw(2)<<i; 
    cout<<endl;
    for( i=0;i<=16;i++){
        if((i>=1)&&(i<=15))    
        cout<<setw(5)<<i;
        for (int j=0;j<=16;j++)
        switch(board[i][j]){
            case -1:cout<<" ";break;
            case 0:
                 {if((i==1)&&(j==1))
                  cout<<"┏";
                  else if((i==1)&&(j==15))
                  cout<<"┓";
                  else if((i==15)&&(j==1))
                  cout<<"┗"; 
                  else if((i==15)&&(j==15))
                  cout<<"┛";
                  else if(((i==8)&&(j==8))||((i==4)&&(j==4))||((i==4)&&(j==12))||((i==12)&&(j==4))||((i==12)&&(j==12)))
                  cout<<"+";
                  else if((i==1)&&(j>=2)&&(j<=14))
                  cout<<"┯";
                  else if((i==15)&&(j>=2)&&(j<=14))
                  cout<<"┷";
                  else if((i>=2)&&(i<=14)&&(j==1))
                  cout<<"┠";
                  else if((i>=2)&&(i<=14)&&(j==15))
                  cout<<"┨";
                  else
                  cout<<"┼";
                 };
                 break;
            case 1:cout<<"●";break;
            case 2:cout<<"○";break;
        }
        cout<<'\n';
    }
    if(who_now()==1)
    { 
       
        cout<<"1 菜单  0 退出 "<<endl; 
        cout<<"黑方下:"<<'\n';
    }
    else  
    { 
        
        cout<<"1 菜单  0 退出"<<endl; 
        cout<<"白方下:"<<'\n';
    }
}    

bool  Five_chess :: putin(int heng, int shu ,int ren )
             {
                  board[heng][shu] = ren;
                  for(i=0;i<572;i++)
	               	{if(ren==2)
		            	if(ctab[heng][shu][i])
	              		{
	                			win1[1][i]++;
	                   			ptab[heng][shu][i]=false;
	                      			win1[0][i]=7;
                                                             		}
               		if(ren==1)
		            	if(ptab[heng][shu][i])
	              		{
	                			win1[0][i]++;
	                   			ctab[heng][shu][i]=false;
	                      			win1[1][i]=7;
                                                             		}
                                                                     }
				  return true;
		}//显示下一步的状况 (谁下,谁赢) 
		
bool Five_chess :: jiqi(){
     
	int m,n,i,max=0;
	int u,v;

	for( m=1;m<16;m++)
		for(n=1;n<16;n++)
		{
			grades[0][m][n]=0;
			grades[1][m][n]=0;

			if(board[m][n]==0)
			{
				for(i=0;i<572;i++)
				{
					//计算玩家在空格上的获胜分数
					if(ptab[m][n][i]&&win1[0][i]!=7)
					{
						switch(win1[0][i])
						{
						case 0:
							grades[0][m][n]+=1;
							break;
						case 1:
							grades[0][m][n]+=200;
							break;
						case 2:
							grades[0][m][n]+=400;
							break;							
						case 3:
							grades[0][m][n]+=2000;
							break;
						case 4:
							grades[0][m][n]+=10000;
							break;
						}
					}
					//计算电脑或玩家二在空格上的获胜分数
					if(ctab[m][n][i]&&win1[1][i]!=7)
					{
						switch(win1[1][i])
						{
						case 0:
							grades[1][m][n]+=1;
							break;
						case 1:
							grades[1][m][n]+=210;
							break;
						case 2:
							grades[1][m][n]+=420;
							break;							
						case 3:
							grades[1][m][n]+=2100;
							break;
						case 4:
							grades[1][m][n]+=20000;
							break;
						}
					}					
				}
				if(max==0)
				{
					u=m;v=n;
				}
				if(grades[0][m][n]>max)
				{
					max=grades[0][m][n];
					u=m;v=n;
				}
				else if(grades[1][m][n]==max)
				{
					if(grades[1][m][n]>grades[1][u][v])
					{
						u=m;v=n;
					}
				}
				if(grades[1][m][n]>max)
				{
					u=m;v=n;
				}
				else if(grades[1][m][n]==max)
				{
					if(grades[0][m][n]>grades[0][u][v])
					{
						u=m;v=n;
					}
				}
			}
		}

		putin(u,v,2);
		putin(u,v);
	//	board[u][v]=2;
        return Win(u,v,2);
     }
 bool Five_chess::Win(int col,int row,int c)   {
  int   nh,nv,nd,ni,x,y;        
  c=who_now();
  //以下统计水平方向的连子数:   
          nh=1;  
          x=col;
          while(board[x-1][row]==c){   
             nh=nh+1;
             x=x-1;
          }     
          x=col;   
          while(board[x+1][row]==c){
             nh=nh+1;
             x=x+1; 
          }  
        
  //以下统计垂直方向的连子数:   
          nv=1; 
          y=row;     
          while(board[col][y-1]==c){    
             nv=nv+1;
             y=y-1; 
          }    
          y=row;   
          while(board[col][y+1]==c){
             nv=nv+1;
             y=y+1;
          }  
  
  //以下统计正对角线方向的连子数:   
          nd=1;   
          x=col;
          y=row;   
          while(board[x-1][y-1]==c){  
             nd=nd+1;
             x=x-1;   
             y=y-1;
          }     
          x=col;
          y=row;   
          while(board[x+1][y+1]==c){    
             nd=nd+1; 
             x=x+1;  
             y=y+1;
          }   
 
  //以下统计反对角线方向的连子数:   
          ni=1;   
          x=col;
          y=row;   
          while(board[x-1][y+1]==c){    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -