当前位置 : 主页 > 编程语言 > c语言 >

C语言实现三子棋

来源:互联网 收集:自由互联 发布时间:2021-05-09
本文实例为大家分享了C语言实现三子棋的具体代码,供大家参考,具体内容如下 代码内有详细注释 源.c代码如下: #define _CRT_SECURE_NO_WARNINGS#include"Game.h"void Game(){ char result = 0; //创建棋盘

本文实例为大家分享了C语言实现三子棋的具体代码,供大家参考,具体内容如下

代码内有详细注释

源.c代码如下:

#define _CRT_SECURE_NO_WARNINGS

#include"Game.h"

void Game()
{
 char result = 0;
 //创建棋盘
 char board[ROW][COL] = { 0 };
 //初始化棋盘
 Init_board(board, ROW, COL);
 //打印棋盘
 Print_board(board, ROW, COL);
 while (1)
 {
 //玩家走棋
 printf("玩家走棋:\n");
 Player_move(board, ROW, COL);
 Print_board(board, ROW, COL);
 //判断游戏状态 继续 平局 玩家胜 电脑胜
 result = Game_state(board, ROW, COL);
 if (result != 'C')
 {
  break;
 }
 //电脑走棋
 printf("电脑走棋:\n");
 Computer_move(board, ROW, COL);
 Print_board(board, ROW, COL);
 //判断游戏状态 继续 平局 玩家胜 电脑胜
 result = Game_state(board, ROW, COL);
 if (result != 'C')
 {
  break;
 }
 }
 if (result == 'X')
 printf("玩家胜\n");
 else if (result == 'O')
 printf("电脑胜\n");
 else
 printf("平局\n");
 printf("再来一局请输入1,退出请输入0\n");
}

void Menu()
{
 int input = 0;
 printf("*****************************\n");
 printf("**** 1.play  2.exit  ****\n");
 printf("*****************************\n");
 do
 {
 printf("请选择:\n");
 scanf("%d", &input);
 switch (input)
 {
 case 1:
  Game();
  break;
 case 0:
  printf("退出游戏\n");
  break;
 default:
  printf("请输入1或0\n");
  break;
 }
 } while (input);
}

int main()
{
 //设置随机数起点
 srand(time(NULL));
 //写一个游戏
 //打印菜单
 Menu();
 return 0;
}

Game.h代码如下:

#include<stdio.h>//NULL
#include<time.h>//time
#include<stdlib.h>//rand srand

#define ROW 3
#define COL 3

void Game();
void Init_board(char board[ROW][COL],int row, int col);
void Print_board(char board[ROW][COL], int row, int col);
void Player_move(char board[ROW][COL], int row, int col);
void Computer_move(char board[ROW][COL], int row, int col);
char Game_state(char board[ROW][COL], int row, int col);

Game.c代码如下:

#define _CRT_SECURE_NO_WARNINGS

#include"Game.h"

void Init_board(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++)
 {
  board[i][j] = ' ';
 }
 }
}
void Print_board(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(j == col - 1)
  printf(" %c \n", board[i][j]);
  else
  printf(" %c |", board[i][j]);
 }
 if (i < row -1)
  printf("---|---|---\n");
 }
}
void Player_move(char board[ROW][COL], int row, int col)
{
 int x = 0, y = 0;
 //判断是否可以落子 1.可以落子 2.已占用 3.坐标非法
 while (1)
 {
 printf("请输入要落子的坐标:\n");
 scanf("%d%d", &x, &y);
 printf("\n");
 if (x >= 1 && x <= row && y >= 1 && y <= col)
 {
  if (board[x - 1][y - 1] == ' ')
  {
  board[x - 1][y - 1] = 'X';
  break;
  }
  else
  printf("坐标已占用,请重新输入\n");
 }
 else
  printf("坐标非法,请重新输入\n");

 }
}
void Computer_move(char board[ROW][COL], int row, int col)
{
 int x = 0, y = 0;
 while(1)
 {
 x = rand() % row;
 y = rand() % col;
 if (board[x][y] == ' ')
 {
  board[x][y] = 'O';
  break;
 }
 }
 printf("\n");
}
int Full_board(char board[ROW][COL], int row, int col)
{
 int i = 0, j = 0;
 for (i = 0; i < row; i++)
 {
 for (j = 0; j < col; j++)
 {
  if (board[i][j] == ' ')
  return 0;
 }
 }
 return 1;
}
char Game_state(char board[ROW][COL], int row, int col)
{
 int i = 0, j = 0;
 //判断行是否成立
 for (i = 0; i < row; i++)
 {
 if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
  return board[i][0];
 }
 //判断列是否成立
 for (i = 0; i < col; i++)
 {
 if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
  return board[0][i];
 }//判断对角线是否成立
 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
 return board[0][0];
 if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
 return board[0][2];
 //判断是否平局
 if (Full_board(board, ROW, COL) == 1)
 return 'D';
 //上述情况都不成立,游戏继续
 return 'C';
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

网友评论