三子棋程序

头文件game.h

#ifndef _GAME_H_
#define _GAME_H_

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>//exit <process.h> or <stdlib.h>   srand <stdlib.h> 
#include <string.h>//memset <memory.h> or <string.h> 
#include <time.h>
#define ROW 3
#define COL 3
void game();
#endif

源文件game.c


#include "game.h"
static void displayBoard(char board[ROW][COL], int row)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
		if (i != 2)
		{
			printf("---|---|---\n");
		}
	}
	printf("\n");
}
static void playMove(char board[ROW][COL], int row)
{
    int x = 0;
	int y = 0;
	while (1)
	{
	printf("玩家走,please enter<x, y>:\n");
	scanf_s("%d%d", &x, &y);
	if ((x >= 0 && x <= row) && (y >= 0 && y <= COL))
	{
	if (board[x][y] == ' ')
	{
	board[x][y] = 'x';
	break;
	}
	else
	{
	printf("The place is occupied,try again\n");
	}
	}
	else
	{
	printf("Erroer,try again\n");
	}
	}
	} 
 static void computerMove(char board[ROW][COL], int row)
{
	srand((unsigned)time(NULL));//void srand(unsigned int seed);随机数
    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[ROW][COL], int row)
{
	int i = 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];
		}
	}//判断行
	for (i = 0; i < row; 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[1][1];
		}//判断右对角线
		if (board[0][2] == board[1][1] && board[1][1] == board[2][0]\
			&&board[0][2] != ' ')
		{
			return board[1][1];
		}//判断左对角线
		if (isFull(board, row))
		{
			return 'q';
		}
		return ' ';
}
void game()
{ //游戏棋盘显示
	char board[ROW][COL];
	memset(board, ' ', ROW*COL);// void *memset( void *dest, int c, size_t count );
	char ret;
	do{
		system("CLS");//清屏
		displayBoard(board, ROW);//显示面板函数
		playMove(board, ROW);//玩家移动
		//x->player,o->computer,q->equal,' '->no one win
		system("CLS");//清屏
		ret = isWin(board, ROW);
		if (ret != ' '){
			break;
		}
		computerMove(board, ROW);//电脑移动
		ret = isWin(board, ROW);//判断输赢
	} while (ret == ' ');
	if (ret == 'q')
	{
		printf("呵呵,平局!\n");
	}
	else if (ret == 'x')
	{
		printf("哈哈哈,恭喜你赢了!\n");
	}
	else if (ret == 'o')
	{
		printf("好吧,你输了!\n");
	}
	else {
		printf("debug!\n");
	}
}

源文件main.c

#include "game.h"
void menu()//屏幕菜单显示,提示玩家选择
{
	printf("##################################################\n");
	printf("######                 1.PLAY              #######\n");
	printf("######                 0.EXIT              #######\n");
	printf("######           PLEASE  SELECT  :          #######\n");

}
int main()
{
	int select = 0;
	//用一个循环游戏可以一直玩下去
	do{
		menu();//显示菜单
		printf("PLEASE CHOOSE:");
		scanf_s("%d", &select);
		switch(select)
		{
			case 1:
				game();
				break;
			case 0:
				exit(0);//结束程序,直接退出程序
			default:
				printf("ERROR,PLEASE CHOOSE AGAIN:");
				break;
		}

	} while (1);
	system("pause");
	return 0;
}

选择“1”之后的结果


接下来选择坐标<x,y>

board[0][0]之后的结果


board[1][1]


board[2][2]


在此程序中遇到这样的问题,

//下面这个程序应该是下标从0到2,可不是,而第一次的那个应该是从1到3,可也不是,刚好相反,不得其解
static void playMove(char board[ROW][COL], int row)
{
    int x = 0;
	int y = 0;
	while (1)
	{
	printf("玩家走,please enter<x, y>:\n");
	scanf_s("%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';
//应该是下标从0到2,可不是,而第一次的那个应该是从1到3,可也不是,刚好相反,不得其解
break;
}else{printf("The place is occupied,try again\n");}}else{printf("Erroer,try again\n");}
	}
	} 

board[1][1]


不明白,等明白了,再接着讨论吧!!!


猜你喜欢

转载自blog.csdn.net/pigdwh/article/details/79998080