game.h
#ifndef _GAME_H
#define _GAME_H
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#endif
void Menu();
int Input();
int Game();
void prints();
//int* NndexInput();
//int Robot_Game();
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
static int times = 0;
static char arr[3][3] = { 0 };
void Menu(){
printf("************************\n");
printf("**** 1)开始 0)结束****\n");
printf("************************\n");
}
int Input(){
int u_input = 0;
scanf("%d", &u_input);
while (getchar() != '\n');
return u_input;
}
//int* NndexInput(){
// int u_input_row = 0;
// int u_input_column = 0;
// scanf("%d,%d", &u_input_row, &u_input_column);
//
//
//
// int arrs[] = { u_input_row, u_input_column };
//
// return arrs;
//}
//int JudgeVictory(char arr[][3]){
int JudgeVictory(){
int flag = 0; //0)机器人胜利,1)用户胜利 2)平局 3)继续游戏。
//横列
for (int i = 0; i < 3; i++)
{
//# 人类,*机器人
if (arr[i][1] == arr[i][2] && arr[i][2] == arr[i][0] && arr[i][0] != '\0'){
if (arr[i][1] == '#'){
return 1;
}
else{
return 0;
}
}
}
//纵列
for (int i = 0; i < 3; i++)
{
//# 人类,*机器人
if (arr[0][i] == arr[1][i] && arr[0][i] == arr[2][i] && arr[0][i] != '\0'){
if (arr[0][i] == '#'){
return 1;
}
else{
return 0;
}
}
}
if ((arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2]) || (arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0])){
if ( arr[1][1] != '\0')
{
if (arr[1][1] == '#' ){
return 1;
}
else{
return 0;
}
}
}
if (times == 9)
{
return 2;
}
return 3;
}
//验证该坐标是否已被输入!1) 没有,0)已被输入。
//int InsertArr(char arr[][3], int row, int left){
int InsertArr( int row, int left){
if (arr[row][left] == '\0'){
return 1;
}
return 0;
}
//void RobotInput(char arr[][3]){
void RobotInput(){
while (1){
int robotStep_row = rand() % 3;
int robotStep_column = rand() % 3;
if (InsertArr( robotStep_row, robotStep_column))
{
printf("机器人下:\n");
arr[robotStep_row][robotStep_column] = '*';
break;
}
}
times++;
prints(arr);
}
void RobotInput2(){
while (1){
int robotStep_row = rand() % 3;
int robotStep_column = rand() % 3;
if (InsertArr(robotStep_row, robotStep_column))
{
arr[robotStep_row][robotStep_column] = '#';
break;
}
}
times++;
prints(arr);
}
//void UserInput(char arr[][3]){
void UserInput(){
while (1){
//int* userStep = NndexInput();
/*int user_row = userStep[0] - 1;
int user_column = userStep[1] - 1;*/
printf("请输入您需要下的位置:\n");
Sleep(1000);
printf("请输入您的横坐标(1,2,3)。\n");
int user_row = Input() - 1;
printf("请输入您的纵坐标(1,2,3)。\n");
int user_column = Input() - 1;
if ((user_row >= 3) || user_row < 0)
{
printf("横坐标输入有误!请重新输入!");
continue;
}
if ((user_column >= 3) || user_column < 0)
{
printf("纵坐标输入有误!请重新输入!");
continue;
}
if (InsertArr( user_row, user_column)){
arr[user_row][user_column] = '#';
break;
}
else{
printf("该位置已被输入!请重新输入!");
}
}
times++;
prints();
Sleep(1000);
}
//void prints(char arr[][3]){
void prints( ){
printf("-------------\n");
for (int i = 0; i < 3; i++){
printf("| %c | %c | %c |", arr[i][0], arr[i][1], arr[i][2]);
printf("\n");
printf("-------------\n");
}
}
void Instail(){
if (times ==0)
{
return;
}
times = 0;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
arr[i][j] = '\0';
}
}
}
int Game(){
//int flag = 0; //0)机器人胜利,1)用户胜利,2)平局
Instail();
srand((size_t)time(NULL)) ;
int judgeVictory = -1;
while (1){
//机器人输入
RobotInput();
//int judgeVictory_r = JudgeVictory(arr);
judgeVictory = JudgeVictory();
if (judgeVictory != 3){
break;
}
//用户输入
UserInput();
judgeVictory = JudgeVictory();
//judgeVictory = 3 继续游戏
if (judgeVictory != 3){
break;
}
}
return judgeVictory;
}
int Robot_Game(){
//int flag = 0; //0)机器人胜利,1)用户胜利,2)平局
Instail();
srand((size_t)time(NULL));
int judgeVictory = -1;
while (1){
//机器人输入
RobotInput();
//int judgeVictory_r = JudgeVictory(arr);
judgeVictory = JudgeVictory();
if (judgeVictory != 3){
break;
}
Sleep(1000);
//用户输入
RobotInput2();
judgeVictory = JudgeVictory();
//judgeVictory = 3 继续游戏
if (judgeVictory != 3){
break;
}
Sleep(1000);
}
return judgeVictory;
}
}
//纯属本人娱乐,让机器人与机器人随机下棋。
int Robot_Game(){
//int flag = 0; //0)机器人胜利,1)用户胜利,2)平局
Instail();
srand((size_t)time(NULL));
int judgeVictory = -1;
while (1){
//机器人输入
RobotInput();
//int judgeVictory_r = JudgeVictory(arr);
judgeVictory = JudgeVictory();
if (judgeVictory != 3){
break;
}
Sleep(1000);
//用户输入
RobotInput2();
judgeVictory = JudgeVictory();
//judgeVictory = 3 继续游戏
if (judgeVictory != 3){
break;
}
Sleep(1000);
}
return judgeVictory;
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void functions(){
//0结束,1开始游戏
do{
Menu();
int u_input = Input();
if (u_input == 1){
int game_flag = Game();
if (game_flag ==1)
{
printf("恭喜您获得了胜利!\n");
Sleep(1000);
printf("请重新选择~!\n");
}
else if (game_flag == 2)
{
printf("平局,继续加油吧!\n");
}
else
{
printf("机器人胜利,继续加油吧!\n");
}
}
else if (u_input== 0)
{
printf("**** 游戏结束!****\n");
break;
}
else{
printf("**** 输入有误请重新输入!****\n");
}
} while (1);
}
//模拟机器人与机器人对战
void robot_functions(){
//0结束,1开始游戏
do{
int game_flag = Robot_Game();
if (game_flag == 1)
{
printf("Robot2获得了胜利!\n");
}
else if (game_flag == 2)
{
printf("平局!\n");
}
else
{
printf("Robot1获得了胜利!\n");
}
Sleep(1000);
} while (1);
}
int main(){
functions();
//robot_functions();
return 0;
}
运行截图: