C++五子棋(六)——游戏结束

2021/7/18 20:38:28

本文主要是介绍C++五子棋(六)——游戏结束,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

规则原理

如图

1 2 3 4 5

判断游戏结束

  • chessData.h
//row,col	表示当前落子
bool checkWin(ChessData* game, int row, int col);
  • 横、竖、斜(斜有两种)共四种情况,每种情况根据当前落子往后遍历5个子,有一种符合就胜利
  • chessData.cpp
bool checkWin(ChessData* game, int row, int col){
  //横
  for(int i = 0; i < 5; i++){
    if(col - i >= 0 &&
      col - i + 4 < BOARD_GRAD_SIZE &&
      game->chessMap[row][col-i] == game->chessMap[row][col-i+1] &&
      game->chessMap[row][col-i] == game->chessMap[row][col-i+2] &&
      game->chessMap[row][col-i] == game->chessMap[row][col-i+3] &&
      game->chessMap[row][col-i] == game->chessMap[row][col-i+4]){return true;}
  }
  
  //竖
  for(int i = 0; i < 5; i++){
    if(row - i >= 0 &&
      row - i + 4 < BOARD_GRAD_SIZE &&
      game->chessMap[row-i][col] == game->chessMap[row-i+1][col] &&
      game->chessMap[row-i][col] == game->chessMap[row-i+2][col] &&
      game->chessMap[row-i][col] == game->chessMap[row-i+3][col] &&
      game->chessMap[row-i][col] == game->chessMap[row-i+4][col]){return true;}
  }
  
  // “/”方向
  for(int i = 0; i < 5; i++){
    if(row + i < BOARD_GRAD_SIZE &&
       row + i - 4 >= 0 &&
       col - i >= 0 &&
       col - i + 4 < BOARD_GRAD_SIZE &&
      game->chessMap[row+i][col-i] == game->chessMap[row+i-1][col-i+1] &&
      game->chessMap[row+i][col-i] == game->chessMap[row+i-2][col-i+2] &&
      game->chessMap[row+i][col-i] == game->chessMap[row+i-3][col-i+3] &&
      game->chessMap[row+i][col-i] == game->chessMap[row+i-4][col-i+4]){return true;}
  }
  
  // “\”方向
  for(int i = 0; i < 5; i++){
    if(row - i >= 0 &&
       row - i - 4 < BOARD_GRAD_SIZE &&
       col - i >= 0 &&
       col - i + 4 < BOARD_GRAD_SIZE &&
      game->chessMap[row-i][col-i] == game->chessMap[row-i+1][col-i+1] &&
      game->chessMap[row-i][col-i] == game->chessMap[row-i+2][col-i+2] &&
      game->chessMap[row-i][col-i] == game->chessMap[row-i+3][col-i+3] &&
      game->chessMap[row-i][col-i] == game->chessMap[row-i+4][col-i+4]){return true;}
  }
  
  return false;
  
}

调用接口

  • main.cpp
#include <stdio.h>
bool checkOver(){
  if(checkWin(&game, clickPosRow, clickPosCol)){
    Sleep(1500);
    if(game.playFlag = false){
      //黑棋胜利,此时标记已经转为白棋落子
      mciSendString("play res/不错.mp3", 0, 0, 0);
      loadimage(0, "res/胜利.jpg");
      score += 100;	//更新分数
    }else{
      mciSendString("play res/失败.mp3", 0, 0, 0);
      loadimage(0, "res/失败.jpg");
      score -= 100;	//同理
    }
    
    
    //用于显示分数
    char scoreText[64];
    sprintf(scoreText, "当前分数:%d", score);
    outtextxy(310, 800, scoreText);
    
    //记录分数
    FILE* fp = fopen("score.data", "wb");
    fwrite(&score, sizeof(score), 1, fp);
    fclose(fp);
    
    
    getch();
    return true;
    
  }
  
  return false;
  
}

显示分数

  • main.cpp
#define INIT_SCORE 1000
int score; //全局变量
void initScore(){
  //分数字体设置
  settextcolor(WHITE); //color
  settextstyle(50, 0, "微软雅黑");	//style
  
  FILE *fp = fopen("score.data", "rb");
  if(fp == NULL){
    score = INIT_SCORE;
  }else{
    fread(&score, sizeof(score), 1, fp);
  }
  if (fp)fclose(fp);
  
}
  • 然后在main.cpp文件的 init() 函数定义中 继续 添加代码
initScore();	//这一行添加到init()函数定义中

结束

到这里五子棋的全部功能已经实现了,但是你会发现程序无法运行。这是理所当然的,因为我们的main函数还没有写,在下一篇文章(也就是本项目的最后一章)我们将完善main函数并做该项目的总结。



这篇关于C++五子棋(六)——游戏结束的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程