码迷,mamicode.com
首页 > 编程语言 > 详细

C语言实现的三子棋游戏

时间:2018-05-27 19:39:13      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:window   初始   stdio.h   cas   null   compute   ||   main   strong   

头文件

#ifndef __game_h__
#define __game_h__
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
extern void Get_board(char a[][3],int LINE,int LIST);
extern void Player_operation(char a[][3],int LINE,int LIST);
extern void Computer_operation(char a[][3],int LINE,int LIST);
extern void Init(char a[][3],int LINE,int LIST);
extern int Discriminant_board(char a[][3],int LINE,int LIST); 
extern int Winning_or_losing_discriminant(char a[][3],int LINE,int LIST);
#endif

游戏测试模块

#include "game.h"
 void menu()
{printf("######################\n");
 printf("####1.PALY  2.EXIT####\n");
 printf("######################\n");
}

void game()
{
      int  line = 3;
      int  list = 3;
      char board[][3] ={"a"};
      Init(board,line,list);
    while(1)
{ 
      Get_board(board,line,list);
          
   if(1==Winning_or_losing_discriminant(board,line,list))
  { printf("玩家赢\n");Sleep(2000);Init(board,line,list); Get_board(board,line,list);}

  if(2==Winning_or_losing_discriminant(board,line,list))
  {  printf("电脑赢\n");Sleep(2000);Init(board,line,list); Get_board(board,line,list);}

  if(Discriminant_board(board,line,list))
    {if(Winning_or_losing_discriminant(board,line,list)==0)
      { printf("平局\n");Sleep(2000);Init(board,line,list); Get_board(board,line,list);}}
     
     Player_operation(board,line,list);



        if(1==Winning_or_losing_discriminant(board,line,list))
  { printf("玩家赢\n");Sleep(2000);Init(board,line,list); Get_board(board,line,list);}

  if(2==Winning_or_losing_discriminant(board,line,list))
  {  printf("电脑赢\n");Sleep(2000);Init(board,line,list); Get_board(board,line,list);}

  if(Discriminant_board(board,line,list))
    {if(Winning_or_losing_discriminant(board,line,list)==0)
      { printf("平局\n");Sleep(2000);Init(board,line,list); Get_board(board,line,list);}}

     Computer_operation(board,line,list);
    }
      }

int main()
{ 
 int input = 0;
 srand((unsigned int)time(NULL));
 menu();
 do{
 printf("please input:>\n");
 scanf_s("%d",&input);
    switch(input)
    {case 1:
           game();
           break;
        case 2:
            printf("退出程序\n");
             goto end;
        default:
            printf("输入错误\n");
    }
  
  }while(1);
end:
 system("pause");
  return 0;
}

游戏实现函数模块

```
void Init(char a[][3],int LINE,int LIST)
{ int i = 0;
int j = 0;
for(i=0; i<LINE; i++)
for(j=0; j<LIST; j++)
a[i][j] = ‘ ‘;

  }//初始化棋盘

   void Get_board(char a[][3],int LINE,int LIST)
  {int i = 0;
  int j = 0;
  for(i=0; i<LINE; i++)
  { for(j=0; j<LIST; j++)
         {  printf("_%c_",a[i][j]);
     if(j<LIST-1)
  printf("|");
            
  }
 printf("\n");
 }

}//打印棋盘


 void Player_operation(char a[][3],int LINE,int LIST)
 { int x = 1;
   int y = 1;
   printf("玩家请走:>\n");

rp: scanf("%d%d",&x,&y);
if((x<4&&x>0)&&(y<4&&y>0)&&a[x-1][y-1]==‘ ‘)
a[x-1][y-1] = ‘X‘;
else if(a[x-1][y-1]!=‘ ‘)
{printf("输入错误,当前已被占用,请重新输入:>\n");
goto rp;}
else
printf("err\n");

 }//玩家操作


 void Computer_operation(char a[][3],int LINE,int LIST)
 {int x = 0;
  int y = 0;
  printf("电脑操作:>\n");
  while(1)
  {x = rand()%3;
   y = rand()%3;
  if((a[x][y]!=‘X‘)&&(a[x][y]!=‘0‘))
  {a[x][y] = ‘0‘;
    break;
   }
  }
 }//电脑操作


  int Discriminant_board(char a[][3],int LINE,int LIST)
  { int i = 0;
    int j = 0;
    int num = 0;
    for(i=0; i<LINE; i++)
    {   for(j=0; j<LIST; j++)
        {  if(a[i][j]==‘ ‘)
           num++;
            }
    }
    if(num)
    return 0;
    if(!num)
    return 1;
  }//判别式函数一

  int Winning_or_losing_discriminant(char a[][3],int LINE,int LIST)
  { int i = 0;
    int j = 0;
    for(i=0; i<LINE; i++)
      { if(((a[i][0]==‘X‘)&&(a[i][1]==‘X‘)&&(a[i][2]==‘X‘))||
           ((a[0][i]==‘X‘)&&(a[1][i]==‘X‘)&&(a[2][i]==‘X‘))||
           ((a[0][0]==‘X‘)&&(a[2][2]==‘X‘)&&(a[1][1]==‘X‘))||
           ((a[0][2]==‘X‘)&&(a[1][1]==‘X‘)&&(a[2][0]==‘X‘)))
           { return 1;}
        if(((a[i][0]==‘0‘)&&(a[i][1]==‘0‘)&&(a[i][2]==‘0‘))||
           ((a[0][i]==‘0‘)&&(a[1][i]==‘0‘)&&(a[2][i]==‘0‘))||
           ((a[0][0]==‘0‘)&&(a[2][2]==‘0‘)&&(a[1][1]==‘0‘))||
           ((a[0][2]==‘0‘)&&(a[1][1]==‘0‘)&&(a[2][0]==‘0‘)))
           {return 2;}
        else
           {return 0;}

       }
    
  }//判别函数二

C语言实现的三子棋游戏

标签:window   初始   stdio.h   cas   null   compute   ||   main   strong   

原文地址:https://www.cnblogs.com/liuzhengkai/p/9096954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!