亲宝软件园·资讯

展开

C语言三子棋源代码 C语言实现三子棋源代码

白龙码~ 人气:0
想了解C语言实现三子棋源代码的相关内容吗,白龙码~在本文为您仔细讲解C语言三子棋源代码的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C语言,三子棋,下面大家一起来学习吧。

效果如下:

代码实现:

头文件部分

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.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);
Player(char board[ROW][COL], int row, int col);
Computer(char board[ROW][COL], int row, int col);
char Judge(char board[ROW][COL], int row, int col);

函数部分

#include "game.h"
void Initboard(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] = ' ';
}
//棋盘
// x | x | x 
//---|---|---
// x | x | x 
//---|---|---
// x | x | x 
void Displayboard(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++)
 {
 printf(" %c ", board[i][j]);
 if (j < col - 1)
 printf("|");
 }
 printf("\n");
 //打印分隔行
 if(i<row-1)
 for (j = 0; j < col; j++)
 {
 printf("---");
 if (j < col - 1)
  printf("|");
 }
 printf("\n");
 }
}
 Player(char board[ROW][COL], int row, int col)
{
 while (1)
 {
 int x = 0;
 int y = 0;
 printf("请输入你的棋子坐标(如第一行第一列为1 1):\n");
 scanf("%d%d", &x, &y);
 if (x <= row && x >= 1 && y <= col && y >= 1)
 {
 if (board[x - 1][y - 1] == ' ')
 {
 board[x - 1][y - 1] = '*';
 break;
 }
 else
 {
 printf("该处已有棋子,请重新输入\n");
 continue;
 }
 }
 else
 printf("坐标范围错误,请重新输入");
 }
}
 Computer(char board[ROW][COL], int row, int col)
 {
 int x = 0;
 int y = 0;
 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)//返回1表示棋盘已满,0表示棋盘没满
 {
 int i, j;
 for (i = 0; i < row; i++)
 {
 for (j = 0; j < col; j++)
 if (board[i][j] == ' ')
  return 0;
 } 
 return 1;
 }

 char Judge(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][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[0][0]!=' '
 || board[0][2] == board[1][1]&&board[1][1] == board[2][0]&&board[0][2] != ' ')
 return board[1][1];
 //平局的情况(棋盘满了)
 if (IsFull( board , ROW,COL) ==1)
 {
 return 'E';
 }
 return 'C';
 }

主函数部分

#include "game.h"

void menu()
{
 printf("***************************\n");
 printf("***1.开始游戏 0.退出游戏***\n");
 printf("***************************\n");
 printf("请输入数字1或0:");
}
void game()
{
 char r;
 char board[ROW][COL] = { 0 };
 //初始化棋盘(清空棋盘)
 Initboard(board, ROW, COL);
 //打印棋盘
 Displayboard(board, ROW, COL);
 //人机对弈
 while (1)
 {
 Player(board, ROW, COL);
 Displayboard(board, ROW, COL);
 r = Judge(board, ROW, COL);
 if (r != 'C')
 break;
 Computer(board, ROW, COL);
 Displayboard(board, ROW, COL);
 r = Judge(board, ROW, COL);
 if (r != 'C')
 break;
 }
 //判断输赢
 if (r == '*')
 {
 printf("玩家赢\n");
 }
 else if (r == '#')
 {
 printf("电脑赢\n");
 }
 else if (r == 'E')
 {
 printf("平局\n");
 }
}
int main()
{
 srand((unsigned int)time(NULL));
 int input = 0;
 do
 {
 menu();
 scanf("%d", &input);
 switch (input)
 {
 case 1:
 printf("开始游戏\n");
 game();
 break;
 case 0:
 printf("退出游戏\n");
 break;
 default:
 printf("请输入正确的选项\n");
 break;
 }
 } while (input);
 printf("游戏结束!");
 return 0;
}

加载全部内容

相关教程
猜你喜欢
用户评论