所需知识:
数组(二维),基本输入输出,函数,宏
代码:
tic-tac-toe.h
#ifndef game_h
#define game_h
#define ROW 3
#define COL 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void game(void);
void InitBoard(char board[ROW][COL],int row,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 IsWin(char board[ROW][COL],int row,int col);
#endif /* game_h */
tic-tac-toe.c
#include "game.h"
void InitBoard(char board[ROW][COL],int row,int col)
{
int i = 0,j = 0;
for(i = 0; i < ROW; i++)
{
for(j = 0; j < COL; j++)
{
board[i][j] = ' ';
}
}
}
void DisplayBoard(char board[ROW][COL],int row,int col)
{
int i = 0,j = 0;
for(i = 0; i < ROW; i++)
{
for(j = 0; j < col; j++)
{
printf(" %c ",board[i][j]);
if(j < col - 1)
printf("|");
}
printf("\n");
for(j = 0; j < col; j++)
{
if(i < row - 1)
{
printf("___");
if(j < col - 1)
printf("|");
}
}
printf("\n");
}
}
void PlayerMove(char board[ROW][COL],int row,int col)
{
int x = 0,y = 0;
printf("Player move\n");
printf("Please enter coordinates\n");
while(1)
{
scanf("%d %d",&x,&y);
if(x > row || y > col || x < 1 || y < 1)
printf("Error input\n");
else
{
if(board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("This coordinate is occupied\n");
}
}
}
}
void ComputerMove(char board[ROW][COL],int row,int col)
{
int x,y;
while(1)
{
x = rand() % ROW;
y = rand() % COL;
if(board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
char IsWin(char board[ROW][COL],int row,int col)
{
int i = 0,j = 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[2][0] == board[1][1] &&board[0][2] != ' ')
return board[0][0];
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
{
if(board[i][j] == ' ')
return '0';
}
}
return '1';
}
test.c
#include <stdio.h>
#include "game.h"
void mune(void)
{
printf("*************************\n");
printf("******** 1.Play *******\n");
printf("******** 0.Exit *******\n");
printf("*************************\n");
}
void game(void)
{
char ret = 0;
char board[ROW][COL];
InitBoard(board,ROW,COL);
DisplayBoard(board,ROW,COL);
while(1)
{
PlayerMove(board,ROW,COL);
DisplayBoard(board,ROW,COL);
ret = IsWin(board,ROW,COL);
if(ret != '0')
break;
ComputerMove(board,ROW,COL);
DisplayBoard(board,ROW,COL);
ret = IsWin(board,ROW,COL);
if(ret != '0')
break;
}
if(ret == '*')
printf("Congratulations! You win!\n");
if(ret == '#')
printf("Wasted!\n");
if(ret == '1')
printf("Leg and leg!\n");
}
void test(void)
{
int input = 1;
do
{
scanf("%d",&input);
switch(input)
{
case 1:
game();
break;
case 0:
printf("End of the game\n");
break;
default:
printf("Error input,please input again\n");
}
}while(input);
}
int main(int argc, const char * argv[]) {
// insert code here...
srand((unsigned int)time(NULL));
mune();
test();
return 0;
}