三子棋游戏编程

game.h                                                               //'v'表示玩家获胜。'l'表示电脑获胜。'e'表示平局。' '表示没下完。
#ifndef _GAME_H_                  
#define _GAME_H_
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
#define ROW 3;
#define COL 3;
#pragma warning(disable:4996)
void game();
#endif


game.c

#include "game.h"
static void displayBoard(char board[][COL],int row)                       //展示棋盘的函数。
{
 int i=0;
 for(;i<row;i++)
 {
  printf(" %c | %c | %c |\n",board[i][0],board[i][1],board[i][2]);     //创建棋盘。
  if(i<row-1){
  printf("---|---|---\n");
  }
 }
}
static void playerMove(char board[][COL],int row)                        //玩家下棋的函数。
{
 int x,y;
 do{
 printf("Please input the <x,y>: \n");                               //玩家输出所下的位置。
 scanf("%d%d",&x,&y);
 if(x>=1&&x<=3&&y>=1&&y<=3)                                          //如果所下的位置是空格就成功下了,否则就会提示这个位置已经被占了,请重新输入。
 {
  if(board[x-1][y-1]==' ')
  {
   board[x-1][y-1]='v';
   break;
  }
  else
  {
   printf("The place is used!Please try again!\n");
  }
 }
 else                                                               //如果输入的值超出了范围,会提醒玩家重新输入。
 {
  printf("Enter error!try again!\n");
 }
 }while(1);
}
static void computerMove(char board[][COL],int row)                //电脑下棋的函数。
{
 srand((unsigned)time(NULL));                                    //电脑下棋位置的随机数。
 do{
  int x=rand()%row;
  int y=rand()%COL;
  if(board[x][y]==' ')                                      //如果下棋位置是空格就成功,否则重新随机数下棋。
  {
   board[x][y]='l';
   break;
  }
 }while(1);
}
static int isFull(char board[][COL],int row)                           //判断棋盘是否满了。
{
 int i=0;
 for(;i<row;i++)
 {
  int j=0;
  for(;j<COL;j++)
  {
   if(board[i][j]==' ')                                      //如果有空格表示棋盘没满,否则就是满了。
   {
    return 0;
   }
  }
 }
 return 1;
}
static char isWin(char board[][COL],int row)                  //判断谁赢得函数。
{
 int i=0;                                                 
 for(;i<row;i++)
 {
  if(board[i][0]==board[i][1]&&board[i][1]==board[i][2])       //有一行都相等。
  {
   return board[i][0];
  }
 }
 for(i=0;i<COL;i++)
 {
  if(board[0][i]==board[1][i]&&board[1][i]==board[2][i])        //有一列都相等。
  {
   return board[0][i];
  }
 }
 if(board[0][0]==board[1][1]&&board[1][1]==board[2][2])          //对角线相等。
 {
  return board[0][0];
 }
 if(board[2][0]==board[1][1]&&board[1][1]==board[0][2])
 {
  return board[2][0];
 }
 if(isFull(board,row))                                                              //棋盘是否满的函数。
 {
  return 'e';                                                                    //满了就是和局,否则还没下完。
 }
 return ' ';
}

void game()
{                                                       
 char board[ROW][COL];
 char ret;
 memset(board,' ',ROW*COL);                          
 do{

  system("CLS");
  displayBoard(board,ROW);   //展示棋盘的函数。
  playerMove(board,ROW);     //玩家下棋的函数。v:player win.l:computer win.e:equal.' ':no end.
  ret=isWin(board,ROW);      //判断玩家是否赢。
  computerMove(board,ROW);  //电脑下棋的函数。
  ret=isWin(board,ROW);     //再一次判断电脑是否赢。
 }while(ret==' ');
 if(ret=='v')                                                  
 {
                displayBoard(board,ROW);                                 //玩家赢时输出此时棋盘和祝贺语。
  printf("Congratulations!You are victory!\n");
 }
 else if(ret=='l')
 {                                                               //电脑赢时输出此时棋盘和安慰语。
                displayBoard(board,ROW);
  printf("ByeBye!You are defeat!\n");
 }
 else if(ret=='e')
 {                                                               //和棋时输出此时棋盘。
                displayBoard(board,ROW);
  printf("Come on!Try again!\n");
 }
 else
 {
  printf("debug!\n");
 }
}
main.c
#include "game.h"
void menu ()
{
 printf("****************************************\n");   //建立一个游戏界面。
 printf("****   Welcome to play the Game!    ****\n");
 printf("****           1.PLAY               ****\n");  
 printf("****           2.EXIT               ****\n");
 printf("****           3.AUTHOR'WORDS       ****\n");
 printf("****************************************\n");
 printf("Please select the result: \n");
}
int main ()
{
 int select=0;
 do{               
 menu ();
 scanf("%d",&select);
 switch(select)                      //用switch来选择你所输出的select的值的作用。
 {
 case 1:
  game();
  break;
 case 2:
  exit(0);
 case 3:
  printf("I am kevin!  Nice to meet you!  Thanks for your participation and have a good time for you!\n");
  break;
 default:                                               //输出不是1,2,3时的结果。
  printf("Enter error!Please input again!\n");
  break;
 }
 }while(1);                                            //do while 循环保证可以一直玩只要玩家不退出。
 system("pause");
 return 0;
}





猜你喜欢

转载自blog.csdn.net/ymk1507050118/article/details/79989901