C语言实现三子棋
2017-12-12 17:37
435 查看
在源文件中创建test.c和game.c文件,头文件中创建game.h文件。代码如下。
实现三子棋程序,只要我们能够理清楚思路,就可以知道其实它的做法并不难,重点在于实际写代码时需要多关注细节。这里我们可以写完一块就可以立马运行程序检查是否如我们所想的效果出现,如若不是便可立即查错纠错。
实现三子棋程序,只要我们能够理清楚思路,就可以知道其实它的做法并不难,重点在于实际写代码时需要多关注细节。这里我们可以写完一块就可以立马运行程序检查是否如我们所想的效果出现,如若不是便可立即查错纠错。
test.c #include <stdio.h> #include <stdlib.h> #include <time.h> #include "game.h" void game() { char board[ROWS][COLS]; char ret = 0; init_board(board, ROWS, COLS); show_board(board, ROWS, COLS); srand((unsigned int)time(NULL)); while(1) { player_move(board, ROWS, COLS); if((ret = ISwin(board, ROWS, COLS)) != ' ') break; show_board(board, ROWS, COLS); computer_move(board, ROWS, COLS); if((ret = ISwin(board, ROWS, COLS)) != ' ') break; show_board(board, ROWS, COLS); } //判断输赢 if(ret == 'X') { printf("你赢了\n"); } else if(ret == '0') { printf("你输了\n"); } else if(ret == 'q') { printf("平局!!\n"); } show_board(board, ROWS, COLS); } void menu() { printf("*********1.play 0.exit*********\n"); } int main() { int input = 0; do { menu(); printf("请选择:"); scanf("%d", &input); switch(input) { case 1: game(); break; case 0: break; default: printf("选择错误\n"); break; } } while (input); return 0; } game.c #include "game.h" //初始化棋盘全为空格 void init_board(char board[ROWS][COLS], int row, int col) { memset(board, ' ', col*row*sizeof(char)); } //棋盘显示 void show_board(char board[ROWS][COLS], int row, int col) { int i = 0; for(i=0; i<row; i++) { printf(" %c | %c | %c \n", board[i][0],board[i][1],board[i][2]); if(i != 2) printf("---|---|---\n"); } } //玩家落子 void player_move(char board[ROWS][COLS], int row, int col) { int x = 0; int y = 0; while(1) { printf("玩家走\n"); printf("请输入坐标:如1 2\n"); scanf("%d%d", &x, &y); //数组下标从零开始,而人的习惯是从1开始,因此读入数据以后减一 x--; y--; //判断下标是否有效 if(((x>=0)&&(x<=2))&&((y>=0)&&(y<=2))) { if(board[x][y] == ' ') { board[x][y] = 'X'; break; } else { printf("下标有误, 重新输入!"); } } else { printf("下标有误, 重新输入!"); } } } //电脑落子 void computer_move(char board[ROWS][COLS], int row, int col) { printf("电脑走\n"); while(1) { int x = rand()%3; int y = rand()%3; if(board[x][y] == ' ') { board[x][y] = '0'; break; } } } //判断棋盘是否下满 static int ISfull(char board[ROWS][COLS], 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 ISwin(char board[ROWS][COLS], 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][1]; } //某列三子一样 for(i=0; i<col; i++) { if((board[0][i]==board[1][i]) &&(board[1][i]==board[2][i]) &&(board[1][i]!=' ')) return board[1][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 ' '; } game.h #ifndef __GAME_H__ #define __GAME_H__ #include <stdio.h> #include <stdlib.h> #include <string.h> #define ROWS 3 #define COLS 3 void init_board(char board[ROWS][COLS], int row, int col); void show_board(char board[ROWS][COLS], int row, int col); void player_move(char board[ROWS][COLS], int row, int col); void computer_move(char board[ROWS][COLS], int row, int col); char ISwin(char board[ROWS][COLS], int row, int col); #endif //__GAME_H__