判赢
当一行或一列都为*或·#时,游戏会结束,这里使用循环,如果某一行或某一列元素都相同,就返回这一行或这一列的任意一个元素,因为三个元素的值都相同,所以返回谁都可以
for (int 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 (int 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]; }
这里对于判赢是有限制的,只能对3*3的棋盘有效,其实还可以用循环语句修改,我这里就不改了
接下来判断是否为平局,平局的条件是棋盘满了,但没人赢,所以这里可以定义一个判满函数
int is_full(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++) { if (board[i][j] == ' ') return 0; } } return 1; }
接下来判断平局:
if (is_full(board, row, col) == 1) { return 'Q'; } //继续 //没有玩家或者电脑赢,也没有平局,游戏继续 return 'C';
接下来,在test()函数中调用刚才写的函数
void game() { char board[ROW][COL]; init_board(board, ROW, COL); print_board(board, ROW, COL); char ret = 0; while (1) { player(board, ROW, COL); print_board(board, ROW, COL); ret = judge(board, ROW, COL); if (ret != 'C') { break; } computer(board, ROW, COL); print_board(board, ROW, COL); ret = judge(board, ROW, COL); if (ret != 'C') { break; } } if (ret == '*') { printf("玩家赢\n"); } if (ret == '#') { printf("电脑赢\n"); } if (ret == 'Q') { printf("平局\n"); } }
当我们创建完一个棋盘后,调用init_board函数初始化,接下来print_board打印一遍空棋盘
接下来就是玩家下棋->判赢->电脑下棋->判赢,因为棋盘最多下九次才能下满,所以我们把这些代码放到while循环中。循环判断条件为1,如果这里的ret的值等于 C的话,跳出循环,否则继续
接下来如果跳出循环,就要判断到底是玩家赢?电脑赢?还是平局?在前面的judge函数中,如果返回的是*则玩家赢,若#则电脑赢,若返回的是Q ,则是平局
4.全部代码
void game() { char board[ROW][COL]; init_board(board, ROW, COL); print_board(board, ROW, COL); char ret = 0; while (1) { player(board, ROW, COL); print_board(board, ROW, COL); ret = judge(board, ROW, COL); if (ret != 'C') { break; } computer(board, ROW, COL); print_board(board, ROW, COL); ret = judge(board, ROW, COL); if (ret != 'C') { break; } } if (ret == '*') { printf("玩家赢\n"); } if (ret == '#') { printf("电脑赢\n"); } if (ret == 'Q') { printf("平局\n"); } }
game.h #pragma once #define ROW 3 #define COL 3 #include <stdio.h> #include <time.h> #include <stdlib.h> #include <windows.h> void init_board(char board[ROW][COL], int row, int col); void print_board(char board[ROW][COL], int row, int col); void player(char board[ROW][COL], int row, int col); void computer(char board[ROW][COL], int row, int col); char judge(char board[ROW][COL], int row, int col); int is_full(char board[ROW][COL], int row, int col);
game.c #define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void init_board(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { board[i][j] = ' '; } } } void print_board(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if(j!=col-1) printf(" %c |",board[i][j]); else printf(" %c ", board[i][j]); } printf("\n"); for (int j = 0; j < col; j++) { if (i != row - 1) { if (j != col - 1) { printf("---|"); } else { printf("---"); } } else { printf(" "); } } printf("\n"); } } void player(char board[ROW][COL], int row, int col) { while (1) { int x = 0; int y = 0; printf("输入你想要下棋的坐标值\n"); scanf("%d %d", &x, &y); printf("玩家下棋\n"); if (x >= 1 && x <= row && y>=1 && y <= col) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("输入的坐标值无效,请重新输入\n"); } } else { printf("坐标非法\n"); } } } void computer(char board[ROW][COL], int row, int col) { printf("电脑下棋\n"); Sleep(1000); int flag = 1; while (flag) { int x = rand() % 3; int y = rand() % 3; if (board[x][y] ==' ') { board[x][y] = '#'; flag = 0; } } } int is_full(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++) { if (board[i][j] == ' ') return 0; } } return 1; } char judge(char board[ROW][COL], int row, int col) { for (int 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 (int 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 (is_full(board, row, col) == 1) { return 'Q'; } //继续 //没有玩家或者电脑赢,也没有平局,游戏继续 return 'C'; }
给自己挖个坑
现在这个三子棋代能够实现基础操作了
有两个缺点:
1. 无法通过改变宏定义的值去实现N*N的棋
2. 电脑太笨,很难让电脑赢
所以我会尝试对这个程序进行优化(算是挖坑了)
————————————————