c language to complete the three-game game

To complete the three-game game, you need to create a header file, two source files as follows:

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"game.h"

MENU void ()
{
the printf ( "******************************************************** \ n-");
the printf ( "1.play ****** ***** \ n-" );
the printf ( "0.exit ****** ***** \ n-");
the printf ( "******************************************************** \ n-");
}
Game void ()
{
char Board [the ROW] [COL] = {0};
char RET = 0;
Initboard (Board, the ROW, COL); // initialize board
displayboard (board, ROW, COL) ; // print board

while (1) // play cycle
{
PlayerMove (Board, the ROW, COL); // players go
displayboard (board, ROW, COL) ; // print board
ret = IsWin (board, ROW, COL); // Analyzing winning or losing
IF (RET = 'C'!)
{
BREAK;
}
ComputerMove (Board, the ROW, COL); // computer go
displayboard (board, ROW, COL) ; // print board
ret = IsWin (board, ROW, COL) ;// determine the winners and losers
IF (RET = 'C'!)
{
BREAK;
}
}
IF (RET == '*')
{
the printf ( "player win \ n-");
}
the else IF (RET == '#')
{
the printf ( "PC win \ n-");
}
the else IF (RET == 'Q')
{
the printf (" draw \ n-");
}
}
void Test ()
{
int INPUT = 0;
srand ((unsigned int) Time (NULL)) ; // set a random value (time stamp)
do
{
MENU ();
the printf ( "Please choose:>");
Scanf ( "% D", & INPUT);
Switch (INPUT)
{
Case. 1:
Game ();
BREAK ;
Case 0:
printf ( "exit the game \ the n-");
BREAK;
default:
printf ( "Error select \ n");
break;
}
}
while (input);
}

int main()
{
test();
return 0;
}

 

 

game.h

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 3
#define COL 3
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);//判断输赢 Q平局 C继续 *玩家赢 #电脑赢

game.c

#include"game.h"
void Initboard(char board[ROW][COL], int row, int col)
{
int i, j;
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;
for (i = 0; i < row; i++)
{
int j;
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("---");
if (j < col - 1)
the printf ( "|");
}
the printf ( "\ n-");
}
}
}
void PlayerMove (char Board [the ROW] [COL], int Row, int COL)
{
int X, Y;
the printf ( "players walking:> \ n-");
the while (. 1) // taking process cycles
{
the printf (" Please enter the walking coordinate (l, 3):> ");
Scanf ("% D% D ", & X, & Y);
IF (X> =. 1 && X <= Y Row &&>. 1 && = Y <= COL)
{
IF (Board [. 1-X] [Y-. 1] == '')
{
Board [-X. 1] [Y- . 1] = '*';
BREAK;
}
the else
{
the printf ( "the coordinates are occupied, please re-enter \ n-!");
}
}
the else
{
the printf ( "! coordinate illegal, please re-enter \ n");
}
}
}
void ComputerMove(char board[ROW][COL],int raw,int col)
{
Int X = 0;
int Y = 0;
the printf ( "Computer walking:> \ n-");
the while (. 1)
{
X = RAND ()% RAW;
Y = RAND ()% COL;
IF (Board [X] [Y] == '')
{
Board [X] [Y] = '#';
BREAK;
}
}
}
static int IsFull (char Board [the ROW] [COL], Row int, int COL) // other undesirable We see, with this modified static function, so that he can see only their own resides in .c file, for isolation
{
int i = 0;
int J = 0;
for (i = 0; i <Row; i ++)
{
for (J = 0; J <COL; J ++)
{
IF (Board [I] [J] == '') ////////////////////// /
{
return 0; // not full
}
}
}
return. 1; // full /////////////////////////////// ////////
}
char IsWin(char board[ROW][COL], int row, int col)
{
int i;
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[1][1] != ' ')//对角线
return board[1][1];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')//对角线
return board[1][1];
if (IsFull(board, ROW, COL) == 1)
{
return 'Q';
}
return 'C';
}

 

 

Completion of three parts, namely the implementation of the game.

Guess you like

Origin www.cnblogs.com/skx123/p/12083777.html