三子棋的实现
//test.c
#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] = {0};//创建二维数组char ret = '0';InitBoard(board,ROW,COL);//初始化棋盘DisplayBoard(board,ROW,COL);//打印棋盘do{printf("-----------\n");printf("电脑走:\n");ComputerMove(board,ROW,COL);DisplayBoard(board,ROW,COL);if (ret = IsWin(board, ROW, COL) != ' '){break;}PlayerMove(board,ROW,COL);DisplayBoard(board, ROW, COL); ret = IsWin(board, ROW, COL);}while(ret == ' ');if (IsWin(board, ROW, COL) == 'q'){printf("平局!\n");}else if (IsWin(board, ROW, COL) == '#'){printf("恭喜玩家赢!\n");}else if (IsWin(board, ROW, COL) == '+'){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#ifndef __GAME_H__
#define __GAME_H__#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>#define ROW 3
#define COL 3void 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);
int IsFull(char board[ROW][COL], int row, int col);//IsWin
//'#'-玩家赢
//'+'-电脑赢
//'q'-平局
//' '-继续
char IsWin(char board[ROW][COL], int row, int col);#endif //__GAME_H__
//game.c#include "game.h"void InitBoard(char board[ROW][COL], int row, int col)
{memset(board,' ',row*col*sizeof(board[0][0]));
}
void DisplayBoard(char board[ROW][COL], int row, int col)
{int i = 0;int j = 0;for(i = 0;i < row;i++){ int j = 0;for(j=0; j<row-1; j++){printf(" %c |",board[i][j]);}printf(" %c \n",board[i][row-1]); if(i != row-1) { int j = 0;for(j=0; j<row-1; j++){printf("---|");}printf("---\n");} }
}
void PlayerMove(char board[ROW][COL], int row, int col)
{ int x = 0;int y = 0;printf("玩家走");while(1){printf("请选择>:");scanf("%d%d",&x,&y);if((x>=1)&&(x<=row)&&(y>=1)&&(y<=col)){if(board[x-1][y-1] == ' '){board[x-1][y-1] = '#';break;}elseprintf("坐标有误\n");}elseprintf("坐标有误\n");}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{int x = 0;int y = 0;while(1){x = rand() % row;y = rand() % col;if(board[x][y] == ' '){board[x][y] = '+';break;}}
}
char IsWin(char board[ROW][COL], int row, int col)
{int i = 0;int j = 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];}for(j=0; j<col; j++){if(board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')return board[1][j];}if(board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')return board[1][1];else if(board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')return board[1][1];else if(IsFull(board,ROW,COL))return 'q';elsereturn ' ';
}
int IsFull(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;
}
1.3版本