三子棋的初始版
表面结构
<span style="color:#33ccff">
#include "game.h"
void menu()
{printf(" ★★★★★★★★★★★★★★★★★★\n");printf(" ★★★ ★★★\n");printf("★★★ 1.开始游戏 0.结束游戏 ★★★\n");printf(" ★★★ ★★★\n");printf(" ★★★★★★★★★★★★★★★★★★\n");printf("请选择:");
}
void game()
{char board[ROW][LINE] = { 0 }; //创建数组(放棋子)Initboard(board, ROW, LINE); //初始化棋盘Printfboard(board, ROW, LINE); //打印棋盘while (1){Player_Runboard(board, ROW, LINE); //玩家开始下棋computer_Runboard(board, ROW, LINE); //电脑开始下棋Printfboard(board, ROW, LINE);judgeboard(board, ROW, LINE); //判断输赢if (judgeboard(board, ROW, LINE) == 'X'){printf("玩家赢!!\n");break;}else if (judgeboard(board, ROW, LINE) == '@'){printf("电脑赢!!\n");break;}}
}
void test()
{int input = 0;srand((unsigned int)time(NULL));do{menu();scanf("%d", &input);switch (input){case 1:game();break;case 0:printf("退出游戏!!\n");break;default:printf("选择错误!!\n");break;}} while (input);
}
int main()
{test();return 0;
}</span>
隐藏函数
<span style="color:#cc33cc">
#include "game.h"
void Initboard(char board[ROW][LINE], int row, int line)
{int i = 0;for (i = 0; i < row; i++){int j = 0;for (j = 0; j < line; j++){board[i][j] = ' ';}}
}
void Printfboard(char board[ROW][LINE], int row, int line)
{int i = 0;int j = 0;for (i = 0; i<row; i++){for (j = 0; j< line; j++){printf(" %c ",board[i][j]);if (j<line-1)printf("|");}printf("\n");if (i < row - 1){for (j = 0; j< line; j++){printf("---");if (j<line - 1)printf("|");}}printf("\n");}
}
void Player_Runboard(char board[ROW][LINE], int row, int line)
{int x = 0;int y = 0;printf("玩家走:\n");while (1){printf("请输入坐标:");scanf("%d%d", &x, &y);if ((x >= 1 && x <= (row + 1)) && (y >= 1 && y <= (line + 1))){if (board[x - 1][y - 1] == ' '){board[x - 1][y - 1] = 'X';break;}else{printf("坐标已被占用\n");}}else{printf("坐标非法\n");}}
}
void computer_Runboard(char board[ROW][LINE])
{int x = 0;int y = 0;printf("电脑走:\n");while (1){x = rand() % ROW;y = rand() % LINE;if (board[x][y] == ' '){board[x][y] = '@';break;}}
}
char judgeboard(char board[ROW][LINE], int row, int line)
{int i = 0;for (i = 0; i < row; i++) //横向判断{if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][1] !=' '){return board[1][1];}}for (i = 0; i < row; i++) //竖向判断{if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] != ' '){return board[1][1];}}if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != ' ') // 右斜判断{return board[1][1];}if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') // 左斜判断{return board[1][1];}
}</span>
头文件与函数的声明
<span style="color:#cc0000">
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 3
#define LINE 3
void Initboard(char board[ROW][LINE], int row, int line);
void Printfboard(char board[ROW][LINE], int row, int line);
void Player_Runboard(char board[ROW][LINE], int row, int line);
void computer_Runboard(char board[ROW][LINE], int row, int line);
char judgeboard(char board[ROW][LINE], int row, int line);#endif //__GAME_H__</span>
初步运行结果