三子棋
text.c#include "game.h"
#define _CRT_SECURE_NO_WARNINGS
void menu()
{
printf("********三子棋游戏********\n");
printf("*****请选择->1玩0不玩*****\n");
}
void game()
{
int num = 3;
char board[ROW][COL] = { 0 };
Initia_board(board, ROW, COL);
Oput_board(board, ROW, COL);
while (1)
{
Player_move(board, ROW, COL);
Oput_board(board, ROW, COL);
num= Is_victory(board, ROW, COL);
if (num == 1)
{
printf("玩家赢\n");
break;
}
if (num ==2)
{
printf("平局\n");
break;
}
Computer_move(board, ROW, COL);
Oput_board(board, ROW, COL);
num = Is_victory(board, ROW, COL);
if (num == 1)
{
printf("电脑赢\n");
break;
}
if (num == 2)
{
printf("平局\n");
break;
}
}
printf("游戏结束\n");
}
int main()
{int input = 0;
menu();
do
{
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("请重新输入\n");
break;
}
}
while (input);
return 0;
}
game.c
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
int Is_draw(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 (board[i][j] = ' ')
return 0;
}
}
return 1;
}
void Initia_board(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++)
{
board[i][j] = ' ';
}
}
}
void Oput_board(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++)
{
printf(" %c ", board[i][j]);
if (j < COL - 1)
{
printf("|");
}
}
printf("\n");
if (i < ROW - 1)
{
for (j= 0; j < COL; j++)
{
printf("--- ");
}
}
printf("\n");
}
}
void Player_move(char board[ROW][COL], int row, int col)
{
int input1 = 0;
int input2 = 0;
while(1)
{
printf("输入下子坐标\n");
scanf("%d %d", &input1,&input2);
if ((input1 >= 1 && input1 <= row) && (input2 >= 1 && input2 <= col))
{
if (board[input1 - 1][input2 - 1] = ' ')
{
board[input1 - 1][input2 - 1] = '*';
break;
}
else
{
printf("重复下子\n");
}
}
else
{
printf("输入的坐标超出范围\n");
}
}
}
void Computer_move(char board[ROW][COL],int row,int col)
{
srand((int)time(NULL));
int x = 0;
int y = 0;
x = rand() % 3;
y = rand() % 3;
while (1)
{
x = rand() % 3;
y = rand() % 3;
if ((x >= 0 && y < row) && (y >= 0 && x < col))
{
if (board[x][y] = ' ')
{
board[x][y] = '#';
break;
}
}
}
}
int Is_victory(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int ret = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2]&& board[i][0]!=' ')
return 1;
}
for(j=0;j<col;j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j]&& board[0][j]!=' ')
return 1;
}
if (board[0][0] == board[1][1] == board[2][2] && board[0][0] != ' ')
{
return 1;
}
if (board[0][2] == board[1][1] == board[2][0] && board[2][0] != ' ')
{
return 1;
}
ret = Is_draw(board, ROW, COL);
if (ret == 1)
{
return 2;
}
return 3;
}
game.h
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <math.h>
#include <time.h>
#define ROW 3
#define COL 3
void Initia_board(char board[ROW][COL],int row,int col);
void Oput_board(char board[ROW][COL],int row,int col);
void Player_move(char board[ROW][COL], int row, int col);
void Computer_move(char board[ROW][COL], int row,int col);
int Is_victory(char board[ROW][COL],int row,int col);
为什么第一次下的时后,电脑下完后没有同步我下的呢?但后面又同步了?