三、源代码
game.h:
#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); //玩家移动 void Playermove(char board[ROW][COL], int row, int col); //电脑移动 void Computermove(char board[ROW][COL], int row, int col); //判断输赢 //判断游戏输赢,要返回4中不同的状态 //玩家胜利--返回 "*" //电脑胜利--返回 "#" //平局------返回 "Q" //继续------返回 "C" char IsWin(char board[ROW][COL], int row, int col);
game.c:
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void InitBoard(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { board[i][j] = ' '; } } } void DisplayBoard(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; 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"); } } void Playermove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; while (1) { printf("请玩家输入:>"); scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col)//x=1 y=1在数组中表示0 0 { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("该位置已被占用,请重新输入\n"); } } else { printf("输入错误,超出范围,请重新输入\n"); } } } void Computermove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("电脑走:>\n"); while (1) { x = rand() % row;//任何一个数%row,余数一定是在0~row之间的 y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } int IsFull(char board[ROW][COL], int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (board[i][j]) { return 0;//棋盘未满 } } } return 1;//棋盘已满 } char IsWin(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[1][1] != ' ') { return board[1][1]; } if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') { return board[1][1]; } //判断平局 if (IsFull(board, row, col)) { return 'Q'; } return 'C'; }
test.c:
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include "game.h" void menu() { printf("********************************\n"); printf("*********** 三子棋 ********\n"); printf("*********** 1.Play *********\n"); printf("*********** 0.exit *********\n"); printf("********************************\n"); } void game() { //游戏过程 char board[ROW][COL]; //初始化棋盘 InitBoard(board, ROW, COL); //打印棋盘 DisplayBoard(board, ROW, COL); //判断输赢 char ret = 0 ; while (1) { Playermove(board, ROW, COL); DisplayBoard(board, ROW, COL); ret = IsWin(board, ROW, COL); if (ret != 'C') { break; } Computermove(board, ROW, COL); DisplayBoard(board, ROW, COL); ret = IsWin(board, ROW, COL); if (ret != 'C') { break; } } if (ret == '*') { printf("玩家胜利\n"); printf("是否继续游戏,请输入:\n"); } else if (ret == '#') { printf("电脑胜利\n"); printf("是否继续游戏,请输入:\n"); } else { printf("平局\n"); printf("是否继续游戏,请输入:\n"); } } int main() { int input; srand((unsigned int)time(NULL));//随机数的生成器 do { menu(); printf("请输入:>"); scanf("%d",&input); switch (input) { case 1: game(); break; case 0: printf("退出游戏\n"); break; default: printf("输入错误,请重新输入\n"); } } while (input); return 0; }
四、总结
对于三子棋游戏的实现,我们需要掌握的知识:选择语句、循环语句、函数、数组;最重要的还是自己的思路,把自己的思路理清楚,合理的规划步骤,多多考虑不同的情况以及解决办法;对于上述代码还不是最好的,后期会对三子棋进行改进创新,并分享给大家。希望能个各位博主进行探讨研究