C语言实现三子棋源代码
程序员文章站
2022-06-09 20:33:32
本文实例为大家分享了c语言实现三子棋的具体代码,供大家参考,具体内容如下效果如下:代码实现:头文件部分#pragma once#include #include <...
本文实例为大家分享了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; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 2020-11-25