c语言编程三子棋(井字棋)
发布时间:2020-12-14 02:19:01 所属栏目:Windows 来源:网络整理
导读:头文件(test.h): #ifndef _THREE_CHESS_H_//防止头文件被重复,包含[也可以用#pragma once]#define _THREE_CHESS_H_#include stdio.h#include windows.h#include time.h#pragma warning(disable:4996)//VS中解决scanf函数不能使用问题#define ROW 3#defin
头文件(test.h):
#ifndef _THREE_CHESS_H_//防止头文件被重复,包含[也可以用#pragma once] #define _THREE_CHESS_H_ #include <stdio.h> #include <windows.h> #include <time.h> #pragma warning(disable:4996)//VS中解决scanf函数不能使用问题 #define ROW 3 #define COL 3//宏定义 void ShowUI(); void Game(); void ComputerMove(char board[][COL],int row,int col); void ShowBoard(char board[][COL],int col); char Judge(char board[][COL],int col); void PlayerMove(char board[][COL],int col);//函数声明 #endif 源文件:(test.c) #include "three_chess.h" void ShowUI()//菜单 { printf("##################################n"); printf("## 1. Play 2. Exit ##n"); printf("##################################n"); printf("Please Select:> "); } void ComputerMove(char board[][COL],int col)//电脑下棋函数 { while (1){ int x = rand() % row; int y = rand() % col;//调用随机函数,确保横竖坐标小于3 if (board[x][y] == ‘ ‘){ board[x][y] = ‘O‘; break; } } } void PlayerMove(char board[][COL],int col)//客户下棋函数 { int x,y; while (1){ printf("Please Enter Your Pos(x,y):> "); 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] = ‘X‘; break; } else{ printf("Enter Pos Is Not OK,Try Again!n"); } } else{ printf("Enter Error,Try Again!n"); } } } char Judge(char board[][COL],int col)//判断函数 { int i = 0; int j = 0; //判断输赢 for (; i < row; i++){ if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ‘ ‘){//判断行 return board[i][0];返回‘X’‘O’判断谁赢 } } for (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]; } //判断是否继续游戏 for (i = 0; i < row; i++){ for (j = 0; j < col; j++){ if (board[i][j] == ‘ ‘){ return ‘N‘; } } } return ‘E‘; } void ShowBoard(char board[][COL],int col)//显示函数 { printf(" 1 2 3n"); printf("-----------------n"); int i = 0; int j = 0; for (; i < row; i++){ printf("%d |",i + 1); for (j = 0; j < col; j++){ printf(" %c |",board[i][j]); } printf("n-----------------n"); } printf("n"); } void Game()//游戏环节 { char board[ROW][COL]; memset(board,‘ ‘,sizeof(board)); char result = ‘N‘; srand((unsigned long)time(NULL)); while (1){ system("cls");//清屏 ComputerMove(board,ROW,COL); ShowBoard(board,COL); result = Judge(board,COL); if (result != ‘N‘){//‘X‘ ‘O‘ ‘E‘ ‘N‘ break; } PlayerMove(board,COL); if (result != ‘N‘){//‘X‘ ‘O‘ ‘E‘ ‘N‘ break; } } switch (result){ case ‘X‘: printf("You Win! :)n"); break; case ‘O‘: printf("You Lose,Computer Win! :(n"); break; case ‘E‘: printf("平局,恭喜!n"); break; default: break; } } 执行文件:(main.c) #include "three_chess.h" int main() { int select = 0; int quit = 0; while (!quit){ ShowUI(); scanf("%d",&select); switch (select){ case 1: Game(); break; case 2: quit = 1; printf("Bye,Bye!n"); break; default: printf("Please Enter Again!n"); break; } } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- windows-server-2008 – 在HYPER-V盒子上运行一台
- windows-server-2003 – Windows / IIS主机::多少
- windows – IDA Pro反汇编显示?而不是.data中的
- 关于 Windows 不断报 "脚本错误 当前页面的脚
- Windows运行时 – 在Windows Store应用程序上实现
- 我可以在Windows中开发.NET应用程序并为Linux和M
- 如何在Windows中锁定屏幕?
- PE基础3-资源表-重定位表-TLS表-DLL延迟加载表
- microsoft-graph – Microsoft Graph API下载文件
- microsoft-office-365 – 在Office 365上意外更改
热点阅读