①下面给出game.h头文件部分
#ifndef __GAME_H__#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include<Windows.h>#define ROWS 3 #define COLS 3void Meun();void Game();void InitBoard(char board[ROWS][COLS], int x, int y);void DisplayBboard(char board[ROWS][COLS], int x, int y);void PlayerMove(char board[ROWS][COLS], int x, int y);void ComputerMove(char board[ROWS][COLS], int x, int y);char Judge(char board[ROWS][COLS],玻璃折射率 int x, int y);#pragma warning(disable:4996)//#define _CRT_SECURE_NO_WARNINGS 1#endif __GAME_H__
②下面给出game.c部分代码
#include "game.h"void Meun()//打印菜单{ printf("**************************\n");printf("********* Meun *********\n");printf(" 1.Start game. \n");printf(" 0.Exit game. \n");printf("**************************\n");}void DisplayBoard(char board[ROWS][COLS], int x, int y)//打印棋盘{ int i = 0;int j = 0;for (i = 0; i<x; i++){ printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);//Sleep(1000);if (2 != i)printf(做你的红颜好吗"---|---|---\n");}}void Game()//开始游戏{ char board[ROWS][COLS];c玩游戏最好的笔记本har ret = 0;InitBoard(board, ROWS, COLS);DisplayBoard(board, ROWS, COLS);do{ PlayerMove(board, ROWS, COLS);ret = Judge(board, ROWS, COLS);if ('X' == ret){ printf("Player win!\n"); break; }if ('O' == ret){ printf("Computer win!\n"); break; }if (' ' == ret){ printf("Dogfall!\n"); break; }ComputerMove(board, ROWS, COLS);ret = Judge(board, ROWS, COLS);if ('X' == ret){ printf("Player win!\n"); break; }if ('O' == ret){ printf("Computer win!\n"); break; }if (' ' == ret){ printf("Dogfall!\n"); break; }} while (1);}void InitBoard(char board[ROWS][COLS], int x, int y)//初始化游戏{ memt(board, ' ', ROWS*COLS*sizeof(char));}void PlayerMove(char board[ROWS][COLS], int x, int y)//玩家玩{ int i = 0;int j = 0;while (1){ printf("Player input>: ");scanf("%d %d", &i, &j); //输入位置if (' ' == board[i - 1][j - 1] \&& (i - 1) >= 0 && (i - 1)<x \&& (j - 1) >= 0 && (j - 1)<y) { board[i - 1][j - 1] = 'X';DisplayBoard(board, ROWS, COLS);break;}el printf("Error input!\n");}}void ComputerMove(char board[ROWS][COLS], int x, int y)//电脑玩{ int i = 0;int j = 0;printf("Computer input>:");while (1){ i = rand() % 3;j = rand() % 3;if (' ' == board[i][j])//位置合法性检测{ printf("%d %d\n", i + 1, j + 1);board[i][j] = 'O';DisplayBoard(board, ROWS, COLS);break;}}}char Judge(char board[ROWS][COLS], int x, int y)//检测输赢{ int i = 0;int j = 0;int count = 0;for (i = 0; i<x; i++){ if (board[0][i] == board[1][i] && board[0][i] == board[2][i]){ if ('X' == board[0][i]) return 'X';if ('O' == board[0][i]) return 'O';}if (board[i][0] == board[i][1] && board[i][0] == board[i][2]){ if 基督教复活节讲章('X' == board[i][0]) return 'X';if ('O' == board[i][0]) return 'O';}}if (board[0][0] == board[1][1] && board[0][0] == board[2][2]){ if ('X' == board[0][0]) return 'X';if ('O' == board[0][0]) return 'O';}if (board[0][2] == board[1][1] && board[0][2] == board[2][0]){ if ('X' == board[1][1]) return 'X';if ('O' == board[1]家书[1]) return 'O';}for (i = 0; i<x; i++){ //判断棋盘是否有子for (j = 0; j < y; j++){ if (' ' != board[i][j]){ count++;}}}if (9 == count)return ' ';}
③下面给出main.c部分代码
#include "game.h"int main() { int input = 0;do{ Meun();printf("Plea lect >: ");scanf("%d", &input);switch (input){ ca 1:Game(); break;ca 0:printf(" Exit \n"); break;default:printf("Error! \n"); break;}} while (input);return 0;}
Gitee链接
Github链接
本文地址:https://blog.csdn.net/qq_37857219/article/details/109276387
本文发布于:2023-04-04 06:39:40,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/1d4e238a53685156bd6edbc2dcc4d6f4.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:如何用多文件编写三字棋.doc
本文 PDF 下载地址:如何用多文件编写三字棋.pdf
留言与评论(共有 0 条评论) |