C语言实现人机模式三子棋游戏

 三子棋规则:在九宫格棋盘上,只要将自己的三个棋子走成一条线(横排、竖排、对角线),就算对方输了。

 接下来,我们就开始用C语言实现这款操作简单容易上手的小游戏。我们采用多文件方式实现:

game.h//自定义头文件

#pragma once
#ifndef _GAME_H_
#define _GAME_H_

#include <stdio.h>
#include <stdlib.h> 
#include <time.h>
#include <windows.h>



#pragma warning(disable:4996)

#define ROW 3
#define COL 3  

void game();

#endif 

main.c//测试整个程序

#include "game.h"

void menu()
{
	printf("##########################\n");
	printf("## 1. Paly       2.Exit ##\n");
	printf("##########################\n");
	printf("Please Select:\n");
}


int main()
{
	int select=0;
	do{
		menu();
		scanf("%d", &select);
		switch (select) {
		case 1:
			game();
			break;
		case 2:
			exit(0);
		default:
			 break; 
		}
	   } while (1);
	system("pause");
	return 0;
}

game.c//定义各类函数

       定义函数

	
	void game()
	{
		char board[ROW][COL];
		memset(board, ' ', COL*ROW);
		char ret;
		do{
			
			dispalyBoard(board, ROW);
			playerMove(board, ROW);
			ret = isWin(board, ROW);
			if (ret != ' ') {
				break;
			}
			computerMove(board, ROW);
			ret = isWin(board, ROW);
		} while (ret == ' ');
		if (ret == 'q') {
			printf("happy ending! \n");
		}
		else if (ret == 'x') {
			printf("you win!\n");
		}
		else if (ret == 'o') {
			printf("you lost!\n");
		}
		else {
			printf("debug\n");
		}
	}
	

       显示棋盘

static void dispalyBoard(char board[][COL], int row)

 {
	int i = 0;
	for (; i < row; i++) {
		printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
		if (i < row-1) {
			printf("---|---|---\n");
		}
	}
}
      玩家下棋子
static void playerMove(char board[][COL], int row)
{
	int x, y;
	do {
		printf("Please Enter<x,y>:\n");
		scanf("%d%d ",&x, &y);
		if (x >= 1 && x <= 3 && y >= 1 && y <= 3) {
			if (board[x-1][y-1] == ' ') 
			{
				board [x-1][y-1]= 'x';
				break;
			}
			else {
				printf("used,try again\n");
			}
		}
		else  {
			printf("Enter Error!try again\n");
		}
	} while (1);

	}
      电脑下棋子
static void computerMove(char board[][COL], int row)
	{
		srand((unsigned long)time(NULL));
		do {
			int x = rand() % row;
			int y = rand() % COL;
			if (board[x][y] == ' ') {
				board[x][y] = 'o';
				break;
			}
		} while (1);
      判断棋盘是否放满
static int isFull(char board[][COL], int row)
	{
		int i = 0;
		for (; i < row; i++) {
			int j = 0;
			for (; j < COL; j++) {
				if (board[i][j] == ' ') {
					return 0;
				}
			}
		}
		return 1;
	}

   判定输赢

 static char isWin(char board[][COL], int row)
	{
		int i = 0;
		for (i =0; i < row; i++) {
			if (board[i][0] == board[i][1] && board[i][1] == board[i][2] &&board[i][0] != ' ') {
				return board[i][0];
			}
		}
		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[0][0] != ' ') {return board[0][0];

		}
		if (board[0][2] == board[1][1] &&board[1][1] == board[2][0] &&board[1][1] != ' '){return board[1][1];
		}
		if (isFull(board,row)){
			return 'q';
		}
		return ' ';
	}
	
      代码简单,还是需要更加仔细,调试过程总是会有很多问题,不过总会一个个解决的。加油!
        




阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页