三子棋--用c实现

三子棋游戏

三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙等。顾名思义,只需在九宫格棋盘的某一行,某一列或某一对角线上连成一条线,即为赢。使用多文件的方式实现该程序。

代码如下:

头文件 chess.h :

#ifndef _CHESS_H_
#define _CHESS_H_

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

#define ROW 3
#define COL 3

#define PLAYER_COLOR 'X'
#define COMPUTER_COLOR 'O'

#pragma warning(disable:4996)

void Game();
void InitBoard(char board[][COL],int row,int col);
void ShowBoard(char board[][COL], int row, int col);
void PlayerMove(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"chess.h"
void Menu()
{
 printf("#################################\n");
 printf("##  1 . Play         2 . Exit  ##\n");
 printf("#################################\n");
 printf("Please select: ");
}
int main()
{
 int select = 0;
 int quit = 0;
 while (!quit)
 {
  Menu();
  scanf("%d", &select);
  switch (select)
  {
  case 1:
   Game();
   break;
  case 2:
   printf("再见!\n");
   quit = 1;
   break;
  default:
   printf("输入有误,请重新输入!\n");
   break;
  }
 }
 system("pause");
 return 0;
}

游戏文件 chess.c :

#include"chess.h"
void InitBoard(char board[][COL], int row, int col)
{
 int i = 0;
 for (; i < row; i++){
  int j = 0;
  for (; j < col; j++) {
   board[i][j] = ' ';
  }
 }
}
void ShowBoard(char board[][COL], int row, int col)
{
 
 printf("   | 1 | 2 | 3 \n");
 printf("---------------\n");
 int i = 1;
 for (; i <= 3; i++) {
  printf(" %d | %c | %c | %c \n", i, board[i - 1][0], board[i - 1][1], board[i - 1][2]);
  if (i != 3) {
   printf("---------------\n");
  }
 }
 printf("\n");
}
void PlayerMove(char board[][COL], int row, int col)
{
 while (1) {
  int x = 0;
  int y = 0;
  printf("请输入你要选择的坐标:");
  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] = PLAYER_COLOR;
    break;
   }
   else {
    printf("你的落子位置已被占用,请重新输入!\n");
   }
  }
  else {
   printf("你输入的坐标不合法,请重新输入!\n");
  }
 }
}
int GetRandom(int start, int end)
{
 //[start,end]
 return rand() % (end - start + 1) + start;
}
void ComputerMove(char board[][COL], int row, int col)
{
 while (1) {
  int x = GetRandom(1, 3);
  int y = GetRandom(1, 3);
  if (board[x - 1][y - 1] == ' ') {
   board[x - 1][y - 1] = COMPUTER_COLOR;
   break;
  }
 }
}
//return 'N'->next,'X'->player win,'O'->computer win,'E'->equal
char Judge(char board[][COL], int row, int col)
{
 int i = 0;
 //判定行
 for (; i < row; i++) {
  if (board[i][0] != ' '&& board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
   return board[i][0];
  }
 }
 //判定列
 for (; i < col; i++) {
  if (board[0][i] != ' '&& board[0][i] == board[1][i] && board[1][i] == board[2][i]) {
   return board[0][i];
  }
 }
 //判定主对角线
 if (board[0][0] != ' '&& board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
  return board[1][1];
 }
 //判定副对角线
 if (board[0][2] != ' '&& board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
  return board[1][1];
 }
 //判定是否还有空位置
 for (i = 0; i < row; i++) {
  int j = 0;
  for (j = 0; j < col; j++) {
   if (board[i][j] == ' ')
    return 'N';
  }
 }
 //平局
 return 'E';
}
void Game()
{
 char result = '\0';
 char board[ROW][COL];
 InitBoard(board,ROW,COL);
 ShowBoard(board, ROW, COL);
 srand((unsigned int)time(NULL));
 while (1)
 {
  PlayerMove(board, ROW, COL);
  ShowBoard(board, ROW, COL);
  result = Judge(board, ROW, COL);
  if (result != 'N')
  {
   break;
  }
  system("CLS");
  ComputerMove(board, ROW, COL);
  ShowBoard(board, ROW, COL);
  result = Judge(board, ROW, COL);
  if (result != 'N')
  {
   break;
  }
 }
 switch (result)
 {
 case 'X':
  printf("恭喜你,你赢了!\n");
  break;
 case 'O':
  printf("很遗憾,你输了。\n");
  break;
 case 'E':
  printf("平局!\n");
  break;
 default:
  printf("Bug?!\n");
  break;
 }
 printf("再来一局?\n");
}

结果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值