Simple three-game game

After learning to think implement a complete array of games --- three-game mini-games with the knowledge.

Three-game game game is this: When a player or a line or a diagonal on three elements, there can be connected into a straight line, the player wins.

How to Realize the three-game mini-games: Board can use an array of three rows and three columns to achieve. It generates a random number between 0 to 2 by the two computers, occupies a position. Players can enter the location you want to Lazi. Every computer and the player after Lazi determine whether one party wins, if not, it is determined whether or not the board there are vacancies. There are vacancies, then the game continues, there is no vacancy is a draw. Setting cycle to achieve each player choose whether to come back after a game comes to an end.

code show as below:

The establishment of a header file:

#ifndef GAME_H
#define GAME_H
#include<stdio.h>
#include<windows.h>
#include<time.h>
#pragma warning(disable:4996)

#define ROW 3 // definition of three rows
#define COL 3 // define three
#define p_color 'X' // player Zi is defined as X-
#define c_color 'O' // PC is defined as Zi O
#define null '' / / null is defined as (a space used when implementing initialization)

void InitBoard(char board[][COL], int row, int col);
void ShowBoard(char board[][COL], int row, int col);
void ComputerMove(char board[][COL], int row, int col);
char Judge(char board[][COL], int row, int col);

void Game();
#endif

Establish mian.c
#include "game.h"

Menu void ()
{
the printf ( "################################# \ n-");
the printf ( "# ############# three-game ############## \ the n-");
printf (" ########## 1. 2.exit ######### Play \ the n-");
printf (" ############################# #### \ n-");
the printf (" Please choose: \ n-");
}
int main ()
{
int = 0 quit;
! the while (quit)
{Menu ();
int sELECT = 0;
Scanf ("% d ", & the SELECT);
Switch (the SELECT)
{
Case 1: game (); BREAK;
Case 2: quit = 1; BREAK;
default: printf (" you entered incorrectly, 1, play games 2, exit, re enter \ n-"); BREAK;
}
}
the printf (" BYE BYE ");
System (" PAUSE ");
}

Establish game.c

#include"game.h"

void InitBoard(char board[][COL], int row, int col) // 初始化数组
{int i=0;
for(;i<ROW;i++){
int j=0;
for(;j<COL;j++)
{ board[i][j]=null;
}}
}

void ShowBoard(char board[][COL], int row, int col) //显示棋盘
{
printf(" 1 | 2 | 3\n");
printf(“\n");
int i=1;
for(;i<=ROW;i++){
printf("%d |",i);
int j = 0;
for (; j < col; j++){
printf(" %c |", board[i - 1][j]);
}
printf("\n");
printf("
\n”);
}
}

void ComputerMove(char board[][COL], int row, int col) //电脑落子
{
while(1){
int x=rand()%row;
int y=rand()%col;
if(board[x][y]==null){board[x][y]=c_color;break;}
}
}

char Judge(char board[][COL], int row, int col) //判断
{ int i=0;
int j=0;
for (;i<ROW;i++){
if (board[i][0]!=null&&board[i][0]==board[i][1]&&board[i][1]==board[i][2])
{return board[i][0];}}
for(;j<COL;j++)
{if (board[0][j]!=null&&board[0][j]==board[1][j]&&board[1][j]==board[2][j])
{return board[0][j];}}
if(board[1][1]!=null&&board[0][0]==board[1][1]&&board[1][1]==board[2][2])
{return board[1][1];}
if(board[1][1]!=null&&board[0][2]==board[1][1]&&board[1][1]==board[2][0])
{return board[1][1];}
for (i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
if (board[i][j]==null)
{return ‘N’;}
}
}
return ‘F’;
}

void Game()
{
srand((unsigned long)time(NULL)); //生成随机数
char board[ROW][COL];
InitBoard(board,ROW,COL);
char result=‘a’;
do{

Showboard (Board, the ROW, COL);
int X = 0; int Y = 0;
the printf ( "Please to zi location \ n-");
Scanf ( "% D% D", & X, & Y);
IF ( x <1 || x> 3 || y <1 || y> 3) // determine whether the player input position is incorrect
{printf ( "you mistyped, please re-enter \ n-");
Continue;}
iF ( board [x-1] [y -1] = null) {// the player determines whether the input position is occupied!
the printf ( "this position is already occupied, please re-enter: \ n-");
Continue;}
Board [X- 1] [y-1] = p_color; // player valid input position, the position occupied.
result = Judge (board, ROW, COL); // determines winning or losing the current situation
if (result = 'N'! ) // N represents continue; represents F. End
{BREAK;}
ComputerMove (Board, the ROW, COL); // computer Zi
result = Judge (board, ROW, COL); // Analyzing
IF (Result = 'N'!)
{BREAK;}
} the while (. 1);
IF (Result == p_color) {the printf ( "Congratulations, you ! won \ n ");}
else if (result == c_color) { printf ( " I'm sorry, you lose \ the n-");}
the else {printf ( "draw \ the n-");}
printf ( "You play pretty good, again a hand? \ n-");
}

Released nine original articles · won praise 1 · views 181

Guess you like

Origin blog.csdn.net/LQyh_/article/details/102986481