首先是头文件的代码
#pragma once #define ROW 5 #define COL 5 #include<stdio.h> #include<stdlib.h> #include<time.h> void start__board(char board[ROW][COL], int row, int col); void start__qipan(char board[ROW][COL], int row, int col); //void qipan__board(char board[ROW][COL], int row, int col); void player__board(char board[ROW][COL],int row, int col); void computer__board(char board[ROW][COL], int row, int col); char win(char board[ROW][COL], int row, int col);
然后是实现函数个个功能的代码
#include"game.h" void start__board(char board[ROW][COL], int row, int col) { int i = 0; int j = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { board[i][j] = ' '; } } } void start__qipan(char board[ROW][COL], int row, int col) { int i = 0; int j = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) printf("|"); } printf("\n"); if (i < row - 1) { for (int j = 0; j < col; j++) { printf("---"); if (j < col - 1) printf("|"); } } printf("\n"); } } //void qipan__board(char board[ROW][COL], int row, int col) //{ // int i = 0; // for (i = 0; i < row; i++) // { // for (int n = 0; n < col;n++) // { // // } // } //} void player__board(char board[ROW][COL], int row, int col) { int x, y; printf("玩家下棋:\n"); while (1) { printf("请输入坐标:->\n"); scanf_s("%d %d", &x, &y); if (x > 0 && x < row+1&& y>0 && y < col+1) { if (board[x-1][y-1]==' ') { board[x-1][y-1] = '*'; break; } else printf("非法占用,请重新输入:\n"); } else printf("超出范围,请重新输入\n"); } } void computer__board(char board[ROW][COL], int row, int col) { printf("电脑下棋:\n"); while (1) { int x = rand() % 6; int y = rand() % 6; if (x > 0 && x < row + 1 && y>0 && y < col + 1) { if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } } char win(char board[ROW][COL], int row, int col) { char p='c'; int i; int j; for (int i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2]&&board[i][2] == board[i][3]&&board[i][3] == board[i][4]&&board[i][0] == '*') p = '*'; if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][2] == board[i][3] && board[i][3] == board[i][4] && board[i][0] == '#') p = '#'; } for (int j = 0; j < col; j++) { if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[2][j] == board[3][j] && board[3][j] == board[4][j] && board[j][4] == '*') p = '*'; if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[2][j] == board[3][j] && board[3][j] == board[4][j] && board[j][4] == '#') p = '#'; } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] == board[3][3] && board[3][3] == board[4][4] && board[0][0] == '*') p = '*'; if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] == board[3][3] && board[3][3] == board[4][4] && board[0][0] == '#') p = '#'; if (board[0][4] == board[1][3] && board[1][3] == board[2][2] && board[2][2] == board[3][1] && board[3][1] == board[4][0]) { if (board[0][4] == '*') p = '*'; if (board[0][4] == '#') p = '#'; } int a = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (board[i][j] != ' ') a = 1; } } if (a == 0) p = 'p'; return p; }
最后是主函数的代码啦
#include"game.h" void menu() { printf("***************************\n"); printf("********* 1.play ********\n"); printf("********* 0.exit ********\n"); printf("***************************\n"); } void game() { int ret; printf("五子棋游戏开始\n"); char board[ROW][COL] = {0}; start__board(board, ROW, COL); start__qipan(board, ROW, COL); //qipan__board(board, ROW, COL); while (1) { player__board(board, ROW, COL); ret = win(board, ROW, COL); if (ret != 'c') break; start__qipan(board, ROW, COL); computer__board(board, ROW, COL); ret =win(board, ROW, COL); if (ret != 'c') break; start__qipan(board, ROW, COL); } if (ret == '*') printf("玩家获胜\n"); if (ret == '#') printf("电脑获胜\n"); if (ret == 'p') printf("平局\n"); } int main() { srand((unsigned int)time(NULL)); int m; do { menu(); printf("请选择:->\n"); scanf_s("%d", & m); switch (m) { case 1: game(); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误,请重新选择:\n"); break; } } while (m); return 0; }
仅供参考啊!