栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

简易三子棋c语言

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

简易三子棋c语言

头文件game.h

#pragma once
#include
#include
#include
#include
void startgame();
void initchessboard();
void printfchessboard();
void playermove();
void computermove();
char checkresult();
static bool chessboardfull();

源文件game.c

#include"game.h"
#define row 3
#define col 3
static char chessboard[row][col];
void  initchessboard(){
	for (int i = 0; i < row; ++i){
		for (int j = 0; j < col; ++j){
			chessboard[i][j] = ' ';
		}
	}
}
void printfchessboard(){
	printf("-------------n");
	for (int i = 0; i < row; ++i){
		printf("| %c | %c | %c |n",
			chessboard[i][0],
			chessboard[i][1], 
			chessboard[i][2]);
	printf("-------------n");
	}
}
void playermove(){
	int x, y;
	while (1){
	printf("输入你所下的位置(row col):n");
	scanf("%d %d", &x, &y);
	if ((x < 0 || x >= row) || (y < 0 || y >= col)){
		printf("你输入的位置已越界,请重新输入正确的位置!!!n");
		continue;
	}
	if (chessboard[x][y] != ' '){
		printf("该位置已经被占用请重新输入n");
		continue;
	}
	chessboard[x][y] = 'x';
	break;
  }
}
 void computermove(){
	int x, y;
	srand(time(0));
	while (1){
		x = rand() % row;
		y = rand() % col;
		if (chessboard[x][y] == ' '){
			chessboard[x][y] = 'o';
			break;
		}
	}
}
 //x代表玩家赢,o代表电脑赢,h代表和棋,c代表继续下continue
char checkresult(){
	 for (int i = 0; i < row; ++i){
		 if (chessboard[i][0] != ' '&&chessboard[i][0]==chessboard[i][1]
			 &&chessboard[i][0]==chessboard[i][2]){
			 return  chessboard[i][0];
		 }
	 }
	 for (int j = 0; j < col; ++j){
		 if (chessboard[0][j] != ' '&&chessboard[0][j] == chessboard[1][j]
			 && chessboard[0][j] == chessboard[2][j]){
			 return chessboard[0][j];
		 }
	 }
	 if (chessboard [1][1]!= ' '&&chessboard[1][1] == chessboard[0][0]
		 && chessboard[1][1] == chessboard[2][2]){
		 return chessboard[1][1];
	 }
	 if (chessboard[1][1] != ' '&&chessboard[1][1] == chessboard[2][0]
		 && chessboard[1][1] == chessboard[0][2]){
		 return chessboard[1][1];
	 }
	 if (chessboardfull()){
		 return 'h';
	 }
	 return 'c';
}
static bool chessboardfull(){
	for (int i = 0; i < row; ++i){
		for (int j = 0; i < col; ++j){
			if (chessboard[i][j] == ' ')
				return false;
		}
	}
	return true;
}
void startgame()
   {
	initchessboard();
	char winner;
	while (1){
	printfchessboard();
	playermove();
	winner = checkresult();
	if (winner != 'c')
		break;
	computermove();
	if (winner != 'c')
		break;
    }
 if (winner == 'x')
	printf("玩家胜利n");
	else if (winner == 'o')
		printf("电脑获胜n");
	  else
		  printf("和棋n");
}

主函数gametest.c

#include"game.h"
int main(){
	system("color 0A");
	system("title 三子棋");
	int select = 1;
	while (select){
		printf("****************************************************************n");
		printf("*                                                              *n");
		printf("*                            简易三子棋                        *n");
		printf("*                        [1]PLAY  [0]EXIT                      *n");
		printf("*                                                              *n");
		printf("****************************************************************n");
		printf("请选择》");
		scanf("%d", &select);
		if (select == 0){
			break;
		}
		if (select != 1){
			printf("输入有误请重新输入n");
			continue;
		}
	    startgame();
	}
	printf("游戏结束n");
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/933986.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号