c language straightforward three-game game

Here we created the three documents,
namely game.h test .c game .c
following is the specific code
game.h

#define _CRT_SECURE_NO_WARNINGS

#define line 3
#define rank 3

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

void meun();
void play(char arr, int a, int b);
void input(char arr, int a, int b);
void play_game(char arr, int a, int b);
int win(char arr, int a, int b);

test.c

#include “game.h”

Game void ()
{
int RET = 0;
int retu = 0;
int K = 0;
char ARR [Line] [Rank] = {0};
do
{
MeUn (); // print menu
scanf ( "% d", K &);
Switch (K)
{
Case. 1:
INPUT (ARR, Line, Rank); // initialize an array
play (arr, line, rank) ; // set the table
do
{
play_move (ARR, Line, Rank); // players take
Play (ARR, Line, Rank);
RET = win (ARR, Line, Rank);
IF (RET ==. 1)
{
the printf ( "player win \ n-");
BREAK;
}
IF (Full (ARR, Line , Rank))
{
the printf ( "draw \ n-");
BREAK;
}
the printf ( "\ n-");
computer_move (ARR, Line, Rank); // go computer
Play (ARR, Line, Rank);
RET = win (ARR, Line, Rank);
IF (RET ==. 1)
{
the printf ( "PC win \ n-");
BREAK;
}
} the while (2);
Case 0:
printf ( "game over \ the n-");
BREAK;
default:
printf ( "input errors, please re-enter \ the n-");
}
}
the while (k = 0!);
}

int main()
{
srand((unsigned int)time(NULL));
game();
system(“pause”);
return 0;
}

game.c
#include “game.h”

void meun () // Print menu
{
the printf ( " \ n-");
the printf ( "
Enter. 1: Play \ n-");
the printf ( "
Enter 0: quit \ n-");
the printf ( "
**** \ n- ");
}
void INPUT (char ARR [Line] [Rank], int A, int B)
{
int I = 0;
int J = 0;
for (I = 0; I <A; I ++) // initialize an array
{
for (J = 0; J <B; J ++)
{
ARR [I] [J] = '';
}
}
}
void Play (char ARR [Line] [Rank], int A, int B) // Print form
{
0 = I int;
int J = 0;
for (I = 0; I <A; I ++)
{
for (J = 0; J <B -. 1; J ++)
{
printf(" %c |", arr[i][j]);
}
printf(" %c “, arr[i][2]);
printf(”\n");
if (i < a - 1)
{
for (j = 0; j < b-1; j++)
{
printf("—|");
}
printf("—");
printf("\n");
}
}
}
void play_move(char arr[line][rank], int a, int b)//玩家走
{
int x = 0;
int y = 0;
while (1)
{
printf(“请输入你的的坐标\n”);
scanf("%d", &x);
scanf("%d", &y);
if (x - 1 >= 0 && x - 1 <= 2 && y - 1 >= 0 && y - 1 <= 2)
{
if (arr[x-1][y-1] == ’ ')
{
arr[x-1][y-1] = ‘*’;
break;
}
(! ARR [. 1-X] [Y-. 1] = '') IF the else
{
the printf ( "this coordinate existing element, please re-enter \ n-");
}
}
the else
{
the printf ( "input error, please re-enter \ n-");
}
}
}
void computer_move (ARR char [Line] [Rank], A int, int B) // PC go
{
the while (. 1)
{
int X = RAND ()% Line;
int Y = RAND ( )% Rank;
IF ((X> = 0 && X <= 2 && Y> = 0 && Y <= 2) && (ARR [X] [Y] == ''))
{
ARR [X] [Y] = '#';
BREAK;
}
}
}
int win (ARR char [Line] [Rank], A int, int B) determining winners and losers //
{
IF (ARR [0] [0] == ARR [0] [. 1 !] && ARR [0] [. 1] == ARR [0] [2] && ARR [0] [0] = '')
return. 1;
else if (arr[1][0] == arr[1][1] && arr[1][1] == arr[1][2] && arr[1][0] != ’ ')
return 1;
else if (arr[2][0] == arr[2][1] && arr[2][1] == arr[2][2] && arr[2][0] != ’ ')
return 1;
else if (arr[0][0] == arr[1][0] && arr[1][0] == arr[2][0] && arr[0][0] != ’ ')
return 1;
else if (arr[0][1] == arr[1][1] && arr[1][1] == arr[2][1] && arr[0][1] != ’ ')
return 1;
else if (arr[0][2] == arr[1][2] && arr[1][2] == arr[2][2] && arr[1][2] != ’ ')
return 1;
else if (arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] && arr[0][0] != ’ ')
return 1;
else if (arr[2][0] == arr[1][1] && arr[1][1] == arr[0][2] && arr[2][0] != ’ ')
return 1;
else
return 0;
}
int full(char arr[line][rank], int a, int b)
{
int i = 0;
int j = 0;
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
if (arr[i][j] == ’ ')
{
return 0;
}
}
}
return 1;
}

Guess you like

Origin blog.csdn.net/belongHWL/article/details/90347281