3、玩家下棋函数的实现
这边接收的是玩家输入的坐标 一般是1 开始 到 3
但是数组下标是从0开始所以把每次玩家输入的行和列 - 1 就好了
//玩家下棋 int PlayMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("玩家下棋"); printf("请输入坐标,中间用空格隔开 ->"); scanf("%d %d",&x,&y); //检查坐标是否合法 if (x >= 1 && y >= 1 && x <= row && y <= col ) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; } else { printf("坐标被占用请重新输入!\n"); return 'P'; } } else { printf("输入错误请重新输入!\n"); return 'P'; } }
4、电脑下棋函数的实现
我们让电脑每次产生随机数坐标 产生行和列 来下棋
//电脑下棋 void ComputerMove(char board[ROW][COL], int row, int col) { //随机下棋 int x = 0; int y = 0; printf("电脑下棋:>\n"); while (1) { x = rand() % row;//产生的随机数模除我们需要的行 产生的数就在行0~3之内了 y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }
5、判断输赢函数的实现
//玩家赢返回 * //电脑赢返回 # //平局返回 Q //继续返回 C 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][1] != ' ') { return board[i][0]; } //判断列 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) == 1) { return 'Q'; } else { return 'C'; } //继续 C }
6、判断平局函数的实现
遍历一遍数组当数组里面存放的没有空格就说明棋盘满了 平局
//平局 int IsFull(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++) { if (board[i][j] == ' ') { return 0; } } } return 1; }
整体代码如下:
test.c代码
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" // 菜单 void menu() { printf("************************\n"); printf("******* 1.play *******\n"); printf("******* 0.exit *******\n"); printf("************************\n"); } //三子棋游戏 void game() { char board[ROW][COL] = {0}; //初始化数组 InitBoard(board, ROW, COL); char ret = 0; char play = 0; //打印棋盘 DisplayBoard(board, ROW, COL); while (1) { //玩家下棋 play = PlayMove(board, ROW, COL); if (play == 'P') continue; //判断输赢 ret = IsWin(board, ROW, COL); if (ret != 'C') { system("cls"); //打印棋盘 DisplayBoard(board, ROW, COL); break; } //打印棋盘 DisplayBoard(board, ROW, COL); //电脑下棋 ComputerMove(board, ROW, COL); system("cls"); //打印棋盘 DisplayBoard(board, ROW, COL); //判断输赢 ret = IsWin(board, ROW, COL); if (ret != 'C') { system("cls"); //打印棋盘 DisplayBoard(board, ROW, COL); break; } } if (ret == '*') { printf("玩家赢!\n"); } else if (ret == '#') { printf("电脑赢!\n"); } else { printf("平局!\n"); } } int main() { int input = 0; do { srand((unsigned int)time(NULL)); //打印菜单 menu(); printf("请输入你的选择 >"); scanf("%d",&input); switch (input) { //开始游戏 case 1: game(); break; //退出游戏 case 0: printf("退出游戏!"); break; default: printf("输入错误,请重新输入!"); break; } } while (input); return 0; }
game.h代码
#pragma once #include <stdio.h> #include <stdlib.h> #include <time.h> #include <stdlib.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); //玩家下棋 int PlayMove(char board[ROW][COL], int row, int col); //电脑下棋 void ComputerMove(char board[ROW][COL], int row, int col); //判断输赢 char IsWin(char board[ROW][COL], int row, int col); //平局 int IsFull(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"); for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) { printf("|"); } } printf("\n"); } } //玩家下棋 int PlayMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("玩家下棋"); printf("请输入坐标,中间用空格隔开 ->"); scanf("%d %d",&x,&y); //检查坐标是否合法 if (x >= 1 && y >= 1 && x <= row && y <= col ) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; } else { printf("坐标被占用请重新输入!\n"); return 'P'; } } else { printf("输入错误请重新输入!\n"); return 'P'; } } //电脑下棋 void ComputerMove(char board[ROW][COL], int row, int col) { //随机下棋 int x = 0; int y = 0; printf("电脑下棋:>\n"); while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } //玩家赢 * //电脑赢 # //平局 Q //继续 C 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][1] != ' ') { return board[i][0]; } //判断列 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) == 1) { return 'Q'; } else { return 'C'; } //继续 C } //平局 int IsFull(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++) { if (board[i][j] == ' ') { return 0; } } } return 1; }