三子棋游戏

三子棋游戏

游戏规则
本游戏为单机游戏,另一端为电脑操控随机落子,用户先落子。提示用户以坐标的形式在一个3*3大小的棋盘上落子,用户与电脑交替落子,当某一方的棋子能够横向或纵向或斜向连成直线时,此方获胜。
思路
为便于梳理思路以及提高可读性,将分为三个文件进行编写,一个头文件head.h,用来存放各种声明,注意:要用#ifndef与#endif将声明囊括,可以声明两个用以代表双方棋子的P_COLOR和C_COLOR。
一个源文件main.c,用以存放整个代码的大的逻辑框架,使代码读起来简洁明了。
还有一个game.c,用来存放几个函数。

以下是头文件head.h:

#ifndef _GAME_H_
#define _GAME_H_

#include <stdio.h>
#include <windows.h>
#include <time.h>

#pragma warning(disable:4996)

#define ROW 3  //行
#define COL 3  //列
#define P_COLOR 'O'
#define C_COLOR 'X'

void Play(char board[][COL], int row, int col);
void InitBoard(char board[][COL], int row, int col);
void ShowBoard(char board[][COL], int row, int col);
void ComputerMove(char board[][COL], int row, int col);
char Judge(char board[][COL], int row, int col);
#endif

头文件中声明进入游戏函数、初始化棋盘函数、展示棋盘函数、电脑落子函数、判断输赢函数。

以下是源文件main.c:

#include "game.h"

int main()
{
 int quit = 0;
 while (!quit)
 {
  Menu();
  int select = 0;
  scanf("%d", &select);
  switch (select)
  {
  case 1:
   play();
   break;
  case 2:
   quit = 1;
   break;
  default:
   printf("您的输入有误,请重新输入!\n");
   break;
  }
  }
 printf("ByeBye!");
 system("pause");
 return 0;
}

main.c文件中,在外层while循环中设置一个控制游戏状态的变量quit,当用户选择退出时,只需将quit置一即可完成退出操作。

以下是game.c:

#include "game.h"

void Menu()
{
 printf("###########################\n");
 printf("### 欢迎来到三子棋游戏!###\n");
 printf("###########################\n");
 printf("### 1:Play      2:Exit ###\n");
 printf("###########################\n");
 printf("### SELECT->> #############\n");
}

void play()
{
 srand(time(NULL));
 char board[ROW][COL];
 char result = 0;
 InitBoard(board,ROW,COL);  //初始化棋盘
 do {
  ShowBoard(board, ROW, COL);   //展示棋盘
  printf("请落子!\n");
  int x = 0;
  int y = 0;
  scanf("%d %d", &x, &y);
  if (x > 3 || x <= 0 || y > 3 || y <= 0)
  {
   printf("您输入的位置有误,请重新输入!\n");
   continue;
  }
  if (board[x - 1][y - 1] != ' ')
  {
   printf("该位置已被占用,请重新输入!\n");
   continue;
  }
  board[x - 1][y - 1] = P_COLOR;
  result=Judge(board, ROW, COL);  //判断输赢
  if (result != 'N')   //N->NEXT;F->FULL;
  {
   break;
  }
  ComputerMove(board, ROW, COL);  //电脑落子
  result = Judge(board, ROW, COL);
  if (result != 'N')
  {
   break;
  }
  } while (1);
 if (result = P_COLOR)
 {
  printf("恭喜你,你赢了!\n");
 }
 else if (result = C_COLOR)
 {
  printf("很遗憾,你输了!\n");
 }
 else
 {
  printf("还不错,平局!\n");
 }
 printf("你玩的还不错,要不要再来一局?\n");
}

void InitBoard(char board[][COL], int row, int col)
{                                     //初始化棋盘
 for (int i = 0; i < row; i++)
 {
  for (int j = 0; j < col; j++)
  {
   board[i][j] = ' ';
  }
 }
}

void ShowBoard(char board[][COL], int row, int col)
{
 printf("      1   2   3  \n");         //展示棋盘
 printf("    -------------\n");
 for (int i = 0; i < row; i++)
 {
  printf(" %d  |",i+1);
  for (int j = 0; j < /col; j++)
  {
   printf(" %c |", board[i][j]);
  }
  printf("\n");
  printf("    -------------\n");
 }
 }

void ComputerMove(char board[][COL], int row, int col)
{
 int x = rand() % 3 + 1;       //电脑落子
 int y = rand() % 3 + 1;
 while (1)
 {
  if (board[x][y] == ' ')
  {
   board[x][y] = C_COLOR;
   break;
  }
  else
  {
   x = rand() % 3 + 1;
   y = rand() % 3 + 1;
   continue;
  }
 }
}

char Judge(char board[][COL], int row, int col)   
{         //N->NEXT;F->FULL;判断输赢
 
 for (int i = 0; i < row; i++)  //行
 {
  if (board[i][0] != ' '&&board[i][0] == board[i][1] && board[i][0] == board[i][2])
  {
   if (board[i][0] == P_COLOR)
   {
    return P_COLOR;
    }
   else
   {
    return C_COLOR;
   }
  }
 }
 for (int j = 0; j < col; j++)    //列
 {
  if (board[0][j] != ' '&&board[0][j] == board[1][j] && board[0][j] == board[2][j])
  {
   if (board[0][j] == P_COLOR)
   {
    return P_COLOR;
   }
   else
   {
    return C_COLOR;
   }
  }
 }
 if (board[1][1] != ' '&&board[1][1] == board[0][0] && board[1][1] == board[2][2])
 {
  if (board[1][1] == P_COLOR)         //右斜
  {
   return P_COLOR;
  }
  else
  {
   return C_COLOR;
  }
 }
 if (board[1][1] != ' '&&board[1][1] == board[0][2] && board[1][1] == board[2][0])
 {
  if (board[1][1] == P_COLOR)
  {
   return P_COLOR;
  }
  else
  {
   return C_COLOR;
  }
 }
 for (int i = 0; i < ROW; i++)
 {
  for (int j = 0; j < COL; j++)
  {
   if (board[i][j] == ' ')
   {
    return 'N';
   }
  }
 }
 return 'F';
}

在电脑落子函数中,使用随机数来对电脑落子位置进行控制,判断随机数生成的位置有无被占用,若有被占用,则需重新生成电脑落子位置,用while循环来控制。
在判断输赢函数中,首先判断是否有人获胜,其次判断是否为满。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值