头文件
#include<stdio.h>
#include<stdlib.h>
#define ROW 3
#define LINE 3
void init_board(char arr[ROW][LINE]);
void display_board(char arr[ROW][LINE]);
void palyer_move(char arr[ROW][LINE]);
void computer_move(char arr[ROW][LINE]);
int is_full(char arr[ROW][LINE]);
char check_win(char arr[ROW][LINE]);
void play_g();
三子棋实现函数
#define _CRT_SECURE_NO_WARNINGS 1 #pragma once
#define _CRT_SECURE_NO_WARNINGS 1 #include"sanziqi.h" void play_g() { char ret = 0; char arr[ROW][LINE]; init_board(arr); do { display_board(arr);//棋盘函数 palyer_move(arr);//玩家移动 ret = check_win(arr);//检查输赢 if (ret != ' ') break; computer_move(arr);//电脑移动 ret = check_win(arr);//检查输赢 } while (ret == ' '); system("cls"); display_board(arr); if (ret == 'X') printf("玩家赢\n"); if (ret == '0') printf("电脑赢\n"); if (ret == 'q') printf("平局\n"); } void display_board(char arr[ROW][LINE]) { int i = 0; printf(" 1 2 3 \n"); printf(" ___________\n"); for (i = 0; i < ROW; i++) { printf("%d", i+1); printf("| %c | %c | %c |\n", arr[i][0], arr[i][1], arr[i][2]); if (i != 2) printf(" |---|---|---|\n"); } printf(" ~~~~~~~~~~~~~\n"); } void init_board(char arr[ROW][LINE]) { int i = 0,j = 0; for (i = 0; i < ROW; i++) for (j = 0; j < LINE; j++) { arr[i][j] = ' '; } } void palyer_move(char arr[ROW][LINE]) { int x = 0,y=0; printf("请输入你下的坐标:"); while (1) { scanf("%d%d", &x, &y); x = x - 1; y = y - 1; if (0 <= (x)&&x < ROW && 0 <= y&&y < LINE) { if (arr[x][y] == ' ') { arr[x][y] = 'X'; break; } else printf("输入的坐标被占用"); } else printf("输入的坐标不存在"); } } int is_full(char arr[ROW][LINE]) { int i = 0, j = 0; for (i = 0; i < ROW;i++) for (j = 0; j < LINE; j++) { if (arr[i][j] == ' ') return 0; } return 1; } void computer_move(char arr[ROW][LINE]) { int x = 0, y = 0; srand((unsigned int)time(NULL)); while(1) { x = rand() % ROW; y = rand() % LINE; if (is_full == 1) break; if (arr[x][y] ==' ') { arr[x][y] = '0'; break; } } } char check_win(char arr[ROW][LINE]) { int i = 0; for (i = 0; i < ROW; i++) { if ((arr[i][0] == arr[i][1]) && (arr[i][1] == arr[i][2]) && arr[i][2] != ' ') return arr[i][0]; } for (i = 0; i < LINE; i++) { if ((arr[0][i] == arr[1][i]) && (arr[1][i] == arr[2][i]) && arr[2][i] != ' ') return arr[0][i]; } if (arr[0][2]==arr[1][1]&&arr[1][1]==arr[2][0]&&arr[1][1]!=' ') return arr[1][1]; if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2]) && arr[2][2] != ' ') return arr[1][1]; if (is_full == 1) return 'q'; return ' '; }
测试函数
#define _CRT_SECURE_NO_WARNINGS 1 #include"sanziqi.h" void manu() { printf("*********************************************\n"); printf("*********************************************\n"); printf("********* 欢迎来到三子棋游戏 ************\n"); printf("********* 1. 进入游戏 ************\n"); printf("********* 0. 退出 ************\n"); printf("*********************************************\n"); printf("*********************************************\n"); } int main() { int i = 0; while (1) { manu(); printf("请选择:>"); scanf("%d", &i); switch (i) { case 1: play_g(); break; case 0: exit(0); break; default: printf("输入错误,请重新输入!\n"); break; } } manu(); }