zoukankan      html  css  js  c++  java
  • 小游戏二之---------------五子棋

    1.五子棋是一比较容易写的小游戏,很适合用来练手,作为练手,不必弄太复杂,所以就不弄电脑AI了,只是玩家之间的对战(AI下次再写)。

    2.五子棋的难点在于如何判断输赢,其实很简单。每次下棋,就判断该棋子的四个方向,横竖方向,还有两个对角线(分成四个部分,左上,左下,右上,右下)方向是否满足五个棋子。

    3.用一个二维数组来表示棋盘(chessBoard[N][N]),值等于0,就表示该位置没有棋子,1表示该位置是玩家1的棋子,2则是玩家2的棋子。

    4.运行结果如下:

    代码如下(C++),有详细注释

      1 #include<iostream>
      2 #include<string>
      3 #include<stdlib.h> 
      4 #define N 11 //棋盘大小 
      5 using namespace std;
      6 
      7 //五子棋游戏 
      8 //人对战人 
      9 
     10 void printBoard(int  chessBoard[N][N]);//打印棋盘 
     11 //验证输入是否有效,不能超过 N,不能输入除了数字以外的东西 
     12 bool isValid(string row,string col,int chessBoard[N][N]);
     13 //玩游戏函数 
     14 void playGame(int chessBoard[N][N],int player);
     15 //判断输赢情况 
     16 void judge(int chessBoard[N][N],int row,int col,int player);
     17 //判断棋盘是否满了 
     18 bool isFull(int chessBoard[N][N]);
     19 int main()
     20 {
     21     int chessBoard[N][N]={0};//N*N 棋盘 
     22                           //0表示该位置还没有棋子 1表示玩家1的棋子 2玩家2的棋子 
     23     printBoard(chessBoard);
     24     int player;
     25     while(true)
     26     {    
     27         player=1;//1 表示玩家1,1先手 
     28         playGame(chessBoard,player);
     29         player=2;//2 表示玩家2 
     30         playGame(chessBoard,player);
     31     }
     32     return 0;
     33 }
     34 
     35 void printBoard(int  chessBoard[N][N])
     36 {    
     37 
     38     int i,j;
     39     for(i=0;i<N;i++)
     40         cout<<"  "<<i+1<<" ";
     41     cout<<endl;
     42     for(i=0;i<N;i++)
     43     {
     44         for(j=0;j<N;j++)
     45         {    
     46             if(chessBoard[i][j]==1)
     47                 cout<<"|_o_"; //o表示玩家1棋子 
     48             else if(chessBoard[i][j]==2)
     49                 cout<<"|_x_";//x表示玩家2棋子 
     50             else
     51                 cout<<"|___"; 
     52         }
     53         cout<<"|";
     54         cout<<" "<<i+1<<"  "<<endl<<endl;
     55     }
     56 }
     57 
     58 
     59 bool isValid(string row,string col,int chessBoard[N][N])
     60 {    
     61     int r=atoi(row.c_str());//字符串转int 
     62     int c=atoi(col.c_str());//如果不是纯数字字符串,则返回零,表示转换失败 
     63     if(r==0||c==0)//返回零,转换失败,输入不正确 
     64         return false;
     65     r--;//数组下标从零开始 
     66     c--;//而输入的行列是从1开始的,所以减一 
     67     
     68     //超过棋盘大小,输入无效 
     69     if(r<0||r>N||c<0||c>N)
     70         return false;
     71     //该位置不是空,输入无效 
     72     if(chessBoard[r][c]!=0)
     73         return false;
     74     return true;
     75 }
     76 
     77 void playGame(int chessBoard[N][N],int player)
     78 {    
     79     string row,col;
     80     cout<<"玩家 "<<player<<" 回合,输入下棋的位置(行 列): "; 
     81     cin>>row>>col;
     82     //直到输入有效则退出循环 
     83     while(isValid(row,col,chessBoard)==false)
     84     {
     85         cout<<"输入无效,棋盘的行或列不能超过棋盘大小 "; 
     86         cout<<"请再次输入"<<endl;
     87         cout<<"玩家 "<<player<<" 回合,输入下棋的位置(行 列): "; 
     88         cin>>row>>col;
     89     }
     90     int r=atoi(row.c_str());//sting 转 int 
     91     int c=atoi(col.c_str());
     92     r--;
     93     c--;
     94     if(player==1)//玩家1 
     95         chessBoard[r][c]=1;
     96     else
     97         chessBoard[r][c]=2;
     98         
     99     //打印本次棋盘 
    100     printBoard(chessBoard);
    101     //判断输赢 
    102     judge(chessBoard,r,c,player);
    103 }
    104 
    105 void judge(int chessBoard[N][N],int row,int col,int player)
    106 {
    107     int i;
    108     int num[6]={0};
    109     for(i=0;i<N;i++)
    110     {    //判断行 
    111         if(chessBoard[row][i]==player)
    112             num[0]++;
    113         else 
    114             num[0]=0; 
    115         //等于5则赢    
    116         if(num[0]==5)
    117             break;
    118             
    119         //判断列 
    120         if(chessBoard[i][col]==player)
    121             num[1]++;
    122         else
    123               num[1]=0;
    124               
    125         if(num[1]==5)
    126             break;
    127     }
    128     //对角线,只需判断五个即可 
    129     for(i=0;i<5;i++)
    130     {    
    131         //左上部分 
    132         if(row-i>=0&&col-i>=0&&chessBoard[row-i][col-i]==player)
    133             num[2]++;
    134         //右下部分 
    135         if(row+i<N&&col+i<N&&chessBoard[row+i][col+i]==player)
    136             num[3]++;
    137     }
    138     
    139     for(i=0;i<5;i++)
    140     {    //右上部分 
    141         if(row+i<N&&col-i>=0&&chessBoard[row+i][col-i]==player)
    142             num[4]++;
    143         //左下部分 
    144         if(row-i>=0&&col+i<N&&chessBoard[row-i][col+i]==player)
    145             num[5]++;
    146     }
    147     //这六个部分是否有连续相同5个棋子的 
    148     for(i=0;i<6;i++)
    149         if(num[i]==5)
    150         {
    151             cout<<"玩家 "<<player<<""<<endl;
    152             exit(0);//退出程序 
    153         }
    154     //棋盘满,平局    
    155     if(isFull(chessBoard)==true)
    156     {
    157             cout<<"平局"<<endl;
    158             exit(0);//退出程序 
    159     }     
    160 }
    161 
    162 bool isFull(int chessBoard[N][N])
    163 {    
    164 
    165     for(int i=0;i<N;i++)
    166         for(int j=0;j<N;j++)
    167             if(chessBoard[i][j]==0)
    168                 return false;
    169     return true;
    170 }
  • 相关阅读:
    Unique Binary Search Trees 解答
    Unique Paths II 解答
    Unique Paths 解答
    Maximum Subarray 解答
    Climbing Stairs 解答
    House Robber II 解答
    House Robber 解答
    Valid Palindrome 解答
    Container With Most Water 解答
    Remove Duplicates from Sorted List II 解答
  • 原文地址:https://www.cnblogs.com/duichoumian/p/12552156.html
Copyright © 2011-2022 走看看