C语言实现简单的三子棋项目

时间:2022-12-03 15:14:40

前言

三子棋是黑白棋的一种。三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙、井字棋等。将正方形对角线连起来,相对两边依次摆上三个双方棋子,只要将自己的三个棋子走成一条线,对方就算输了。但是,有很多时候会出现和棋的情况。

多文件形式

在实现游戏的首先,需要创建test.c game.c game.h三个文件。
test.c主要进行游戏的测试。
game.c主要进行游戏内部一些函数的具体实现。
game.h主要是一些声明,宏定义。

游戏逻辑

1、打印一个游戏的简易菜单。
2、初始化棋盘。
3、打印棋盘。
4、玩家下棋(键盘输入坐标) 打印棋盘,判断输赢。
5、电脑下棋(随机坐标) 打印棋盘,判断输赢。

游戏实现

C语言实现简单的三子棋项目

1.打印菜单

在屏幕打印一个简易菜单,提示玩家选择。

void menu()
{
 printf("**************************
");
 printf("********  1.play  ********
");
 printf("********  0.exit  ********
");
 printf("**************************
");
}

C语言实现简单的三子棋项目

2.初始化棋盘

将3x3的数组初始化为空格。

void InitBoard(char board[ROW][COL], int row, int col)
{
 int i = 0;
 for (i = 0; i < row; i++)
 {
  int j = 0;
  for (j = 0; j < col; j++)
  {
   board[i][j] = " ";
  }
 }
}

3.打印棋盘

在屏幕打印当前棋盘,让玩家选择下一步所要走哪一步。

void DisplayBoard(char board[ROW][COL], int row, int col)
{
 int i = 0;
 for (i = 0; i < row; i++)
 {
  int j = 0;
  for (j = 0; j < col; j++)
  {
   printf(" %c ", board[i][j]);
   if (j < col - 1)
    printf("|");
  }
  printf("
");
  if (i < row - 1)
  {
   for (j = 0; j < col; j++)
   {
    printf("---");
    if (j < col - 1)
     printf("|");
   }
  }
  printf("
");
 }
}

4.玩家下棋

玩家在键盘上输入坐标,所输入的合法范围是1<=x,y<=3,根据玩家所输入将对应数组赋值为星号"*"。

void PlayerMove(char board[ROW][COL], int row, int col)
{
 printf("玩家走:>
");

 int x = 0;
 int y = 0;
 while (1)
 {
  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] = "*";
    break;
   }
   else
   {
    printf("坐标被占用,请重新输入
");
   }
  }
  else
  {
   printf("下标越界,请重新输入
");
  }
 }

}

5.电脑下棋

生成随机数,在所对应数组赋值为井号‘#"。

void ComputerMove(char board[ROW][COL], int row, int col)
{
 int x = 0;
 int y = 0;
 printf("电脑走:>
");
 x = rand() % row;
 y = rand() % col;
 while (1)
 {
  x = rand() % row;
  y = rand() % col;
  if (board[x][y] == " ")
  {
   board[x][y] = "#";
   break;
  }
 }
}

6.判断输赢

1.玩家赢返回星号
2.电脑赢返回井号
3.平局返回P
4.棋盘为满,继续游戏返回C

char Iswin(char board[ROW][COL], int row, int col)
{
 int i = 0;
 //行
 for (i = 0; i < row; i++)
 {
  if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != " ")
  {
   return board[i][1];
  }
 }
 //列
 for (i = 0; i < col; i++)
 {
  if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != " ")
  {
   return board[1][i];
  }
 }
 //对角线
 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != " ")
 {
  return board[1][1];
 }
 if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != " ")
 {
  return board[1][1];
 }

 if (IsFull(board, ROW, COL))
 {
  return "P";
 }
 return "C";
}

7.判断棋盘是否满

int IsFull(char board[ROW][COL], int row, int col)
{
 int i = 0;
 int j = 0;
 for (i = 0; i < row; i++)
 {
  for (j = 0; j < col; j++)
  {
   if (board[i][j] == " ")
    return 0;
  }
 }
 return 1;
}

具体代码

test.c

#include "game.h"

void menu()
{
 printf("**************************
");
 printf("********  1.play  ********
");
 printf("********  0.exit  ********
");
 printf("**************************
");
}

void game()
{
 char board[ROW][COL];
 //初始化棋盘
 InitBoard(board, ROW, COL);
 //打印棋盘
 DisplayBoard(board, ROW, COL);
 //玩家下棋
 while (1)
 {
  PlayerMove(board, ROW, COL);
  DisplayBoard(board, ROW, COL);
  if (Iswin(board, ROW, COL) != "C")
  {
   break;
  }
  ComputerMove(board, ROW, COL);
  DisplayBoard(board, ROW, COL);
  if (Iswin(board, ROW, COL) != "C")
  {
   break;
  }
 }
 if (Iswin(board, ROW, COL) == "*")
 {
  printf("玩家赢
");
 }
 else if (Iswin(board, ROW, COL) == "#")
 {
  printf("电脑赢
");
 }
 else
 {
  printf("平局
");
 }
}

int main()
{
 int input = 0;
 do
 {
  printf("请选择:
");
  menu();
  scanf("%d", &input);
  switch (input)
  {
  case 1:
   game();
   break;
  case 0:
   printf("退出游戏
");
   break;
  default:
   printf("输入错误
");
   break;
  }

 } while (input);

 return 0;
}

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void InitBoard(char board[ROW][COL], int row, int col)
{
 int i = 0;
 for (i = 0; i < row; i++)
 {
  int j = 0;
  for (j = 0; j < col; j++)
  {
   board[i][j] = " ";
  }
 }
}

void DisplayBoard(char board[ROW][COL], int row, int col)
{
 int i = 0;
 for (i = 0; i < row; i++)
 {
  int j = 0;
  for (j = 0; j < col; j++)
  {
   printf(" %c ", board[i][j]);
   if (j < col - 1)
    printf("|");
  }
  printf("
");
  if (i < row - 1)
  {
   for (j = 0; j < col; j++)
   {
    printf("---");
    if (j < col - 1)
     printf("|");
   }
  }
  printf("
");
 }
}

void PlayerMove(char board[ROW][COL], int row, int col)
{
 printf("玩家走:>
");

 int x = 0;
 int y = 0;
 while (1)
 {
  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] = "*";
    break;
   }
   else
   {
    printf("坐标被占用,请重新输入
");
   }
  }
  else
  {
   printf("下标越界,请重新输入
");
  }
 }

}

void ComputerMove(char board[ROW][COL], int row, int col)
{
 int x = 0;
 int y = 0;
 printf("电脑走:>
");
 x = rand() % row;
 y = rand() % col;
 while (1)
 {
  x = rand() % row;
  y = rand() % col;
  if (board[x][y] == " ")
  {
   board[x][y] = "#";
   break;
  }
 }
}

int IsFull(char board[ROW][COL], int row, int col)
{
 int i = 0;
 int j = 0;
 for (i = 0; i < row; i++)
 {
  for (j = 0; j < col; j++)
  {
   if (board[i][j] == " ")
    return 0;
  }
 }
 return 1;
}

char Iswin(char board[ROW][COL], int row, int col)
{
 int i = 0;
 //行
 for (i = 0; i < row; i++)
 {
  if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != " ")
  {
   return board[i][1];
  }
 }
 //列
 for (i = 0; i < col; i++)
 {
  if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != " ")
  {
   return board[1][i];
  }
 }
 //对角线
 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != " ")
 {
  return board[1][1];
 }
 if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != " ")
 {
  return board[1][1];
 }

 if (IsFull(board, ROW, COL))
 {
  return "P";
 }
 return "C";
}

game.h

#pragma once

#include <stdio.h>
#define ROW 3
#define COL 3

void InitBoard(char board[ROW][COL], int row, int col);

void DisplayBoard(char board[ROW][COL], int row, int col);

void PlayerMove(char board[ROW][COL], int row, int col);

void ComputerMove(char board[ROW][COL], int row, int col); 

char Iswin(char board[ROW][COL], int row, int col);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_53027918/article/details/119249846