今天做了一个三子棋小游戏,写了好久却只完成了一部分,先把这部分分享给大家吧!望大家给点建议和指导,最后判断输赢部分到现在还没构思出来,嘿嘿!
game.h
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __GAME_H__
#define __GAME_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define ROW 3
#define COL 3
void menu();
void game();
void Initboard(char board[ROW][COL], int rom, int col);
void displayboard(char board[ROW][COL], int row, int col);
void PlayerMove(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
char Is_Win(char board[ROW][COL], int row, int col);
#endif //__GAME_H__
game.c
#include "game.h"
void menu()
{
printf("***********************\n");
printf("**** 1.开始游戏 ****\n");
printf("**** 0.退出游戏 ****\n");
printf("***********************\n");
}
//初始化棋盘
void Initboard(char board[ROW][COL], int row, int col)
{
memset(board, ' ',row*col*sizeof(board[0][0]));
}
//打印棋盘
void displayboard(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (j < col - 1)
{
printf(" %c |", board[i][j]);
}
else
{
printf(" %c\n", board[i][j]);
}
}
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
if (j < col - 1)
{
printf("---|");
}
else
{
printf("---\n");
}
}
}
}
}
//玩家走
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while(1)
{
printf ("请输入玩家走的坐标:>");
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]='@';
break;
}
else
{
printf ("该位置已经被占用,请重新输入!\n");
}
}
else
{
printf ("输入非法,请重新输入!\n");
}
}
}
//电脑走
void ComputerMove(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
printf ("电脑走:>\n");
while(1)
{
i = rand()%ROW;
j = rand()%COL;
if (i>0 && i<row && j>0 && j<col)
{
if (board[i][j] == ' ')
{
board[i][j] = '#';
break;
}
}
}
}
test.c
#include "game.h"
int main()
{
char board[ROW][COL] = {0};
int input = 0;
char ret = ' ';
menu();
Initboard(board, ROW, COL);
srand((unsigned int )time(NULL));
printf ("请输入选项:>");
scanf ("%d",&input);
printf ("游戏开始!\n");
do
{
switch(input)
{
case 1:
displayboard(board, ROW, COL);
PlayerMove(board, ROW, COL);
/*displayboard(board, ROW, COL);*/
ComputerMove(board, ROW, COL);
break;
case 0:
printf ("退出游戏!\n");
break;
default:
printf ("输入错误,请输入0或1!\n");
break;
}
}while(input);
system ("pause");
return 0;
}